From cd548fd6710c06289fbc8d58f126dde962c68058 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Fri, 8 Oct 2010 11:38:52 -0700 Subject: [PATCH] PayPal drivers converted to Moose. --- .../Shop/PayDriver/PayPal/ExpressCheckout.pm | 150 +++++++++-------- lib/WebGUI/Shop/PayDriver/PayPal/PayPalStd.pm | 157 ++++++++---------- t/Shop/PayDriver/PayPalStd.t | 22 +-- 3 files changed, 158 insertions(+), 171 deletions(-) diff --git a/lib/WebGUI/Shop/PayDriver/PayPal/ExpressCheckout.pm b/lib/WebGUI/Shop/PayDriver/PayPal/ExpressCheckout.pm index 4826fe4fb..8fbd84f74 100644 --- a/lib/WebGUI/Shop/PayDriver/PayPal/ExpressCheckout.pm +++ b/lib/WebGUI/Shop/PayDriver/PayPal/ExpressCheckout.pm @@ -15,7 +15,6 @@ package WebGUI::Shop::PayDriver::PayPal::ExpressCheckout; =cut use strict; -use base qw/WebGUI::Shop::PayDriver/; use LWP::UserAgent; use Tie::IxHash; @@ -27,6 +26,80 @@ use URI; use Readonly; use Data::Dumper; +Readonly my $I18N => 'PayDriver_ExpressCheckout'; +use Moose; +use WebGUI::Definition::Shop; +extends qw/WebGUI::Shop::PayDriver PayDriver_ExpressCheckout/; + +define pluginName => []; + +property paypal => ( + fieldType => 'text', + label => ['paypal', 'PayDriver_ExpressCheckout'], + hoverHelp => ['paypal help', 'PayDriver_ExpressCheckout'], + default => 'https://www.paypal.com/webscr', + ); + +property sandbox => ( + fieldType => 'text', + label => ['sandbox', 'PayDriver_ExpressCheckout'], + hoverHelp => ['sandbox help', 'PayDriver_ExpressCheckout'], + default => 'https://www.sandbox.paypal.com/webscr', + ); + +property api => ( + fieldType => 'text', + label => ['api', 'PayDriver_ExpressCheckout'], + hoverHelp => ['api help', 'PayDriver_ExpressCheckout'], + default => 'https://api-3t.payPal.com/nvp', + ); + +property apiSandbox => ( + fieldType => 'text', + label => ['apiSandbox', 'PayDriver_ExpressCheckout'], + hoverHelp => ['apiSandbox help', 'PayDriver_ExpressCheckout'], + default => 'https://api-3t.sandbox.payPal.com/nvp', + ); + +property user => ( + fieldType => 'text', + label => ['user', 'PayDriver_ExpressCheckout'], + hoverHelp => ['user help', 'PayDriver_ExpressCheckout'], + ); + +property password => ( + fieldType => 'text', + label => ['password', 'PayDriver_ExpressCheckout'], + hoverHelp => ['password help', 'PayDriver_ExpressCheckout'], + ); + +property currency => ( + fieldType => 'text', + label => ['currency', 'PayDriver_ExpressCheckout'], + hoverHelp => ['currency help', 'PayDriver_ExpressCheckout'], + default => 'USD', + ); + +property testMode => ( + fieldType => 'yesNo', + label => ['testMode', 'PayDriver_ExpressCheckout'], + hoverHelp => ['testMode help', 'PayDriver_ExpressCheckout'], + ); + +property signature => ( + fieldType => 'text', + label => ['signature', 'PayDriver_ExpressCheckout'], + hoverHelp => ['signature help', 'PayDriver_ExpressCheckout'], + ); + +property summaryTemplateId => ( + fieldType => 'template', + label => ['summary template', 'PayDriver_ExpressCheckout'], + hoverHelp => ['summary template help', 'PayDriver_ExpressCheckout'], + namespace => 'Shop/Credentials', + default => 'GqnZPB0gLoZmqQzYFaq7bg', + ); + =head1 NAME WebGUI::Shop::PayDriver::PayPal @@ -51,8 +124,6 @@ The following methods are available from this class: =cut -Readonly my $I18N => 'PayDriver_ExpressCheckout'; - #------------------------------------------------------------------- =head2 apiUrl @@ -64,66 +135,11 @@ use the sandbox) sub apiUrl { my $self = shift; - return $self->get( $self->get('testMode') ? 'apiSandbox' : 'api' ); + return $self->get( $self->testMode ? 'apiSandbox' : 'api' ); } #------------------------------------------------------------------- -=head2 definition - -Standard definition method. - -=cut - -sub definition { - my ( $class, $session, $definition ) = @_; - my $i18n = WebGUI::International->new( $session, $I18N ); - - tie my %fields, 'Tie::IxHash'; - my @fieldNames = qw( - paypal sandbox - api apiSandbox - user password - currency testMode - signature - ); - - foreach my $f (@fieldNames) { - $fields{$f} = { - fieldType => 'text', - label => $i18n->get($f), - hoverHelp => $i18n->get("$f help"), - }; - } - - $fields{currency}{defaultValue} = 'USD'; - - $fields{testMode}{fieldType} = 'YesNo'; - - $fields{sandbox}{defaultValue} = 'https://www.sandbox.paypal.com/webscr'; - $fields{apiSandbox}{defaultValue} = 'https://api-3t.sandbox.payPal.com/nvp'; - - $fields{paypal}{defaultValue} = 'https://www.paypal.com/webscr'; - $fields{api}{defaultValue} = 'https://api-3t.payPal.com/nvp'; - - $fields{summaryTemplateId} = { - fieldType => 'template', - label => $i18n->get('summary template'), - hoverHelp => $i18n->get('summary template help'), - namespace => 'Shop/Credentials', - defaultValue => 'GqnZPB0gLoZmqQzYFaq7bg', - }, - - push @{$definition}, { - name => $i18n->get('name'), - properties => \%fields, - }; - - return $class->SUPER::definition( $session, $definition ); -} ## end sub definition - -#------------------------------------------------------------------- - =head2 getButton Overridden, submits to www_sendToPaypal with the proper parameters. @@ -137,7 +153,7 @@ sub getButton { my $payForm = WebGUI::Form::formHeader($session) . $self->getDoFormTags('sendToPayPal') - . WebGUI::Form::submit( $session, { value => $self->get('name') } ) + . WebGUI::Form::submit( $session, { value => $self->name } ) . WebGUI::Form::formFooter($session); return $payForm; @@ -157,9 +173,9 @@ sub payPalForm { my $self = shift; my $args = ref $_[0] eq 'HASH' ? shift : {@_}; $args->{VERSION} = '58.0'; - $args->{USER} = $self->get('user'); - $args->{PWD} = $self->get('password'); - $args->{SIGNATURE} = $self->get('signature'); + $args->{USER} = $self->user; + $args->{PWD} = $self->password; + $args->{SIGNATURE} = $self->signature; return $args; } @@ -175,7 +191,7 @@ use the sandbox) sub payPalUrl { my $self = shift; - return $self->get( $self->get('testMode') ? 'sandbox' : 'paypal' ); + return $self->get( $self->testMode ? 'sandbox' : 'paypal' ); } #------------------------------------------------------------------- @@ -197,7 +213,7 @@ sub processPayment { PAYERID => $self->session->form->process('PayerId'), TOKEN => $self->session->form->process('token'), AMT => $self->getCart->calculateTotal, - CURRENCYCODE => $self->get('currency'), + CURRENCYCODE => $self->currency, PAYMENTACTION => 'SALE', ); my $response = LWP::UserAgent->new->post( $self->apiUrl, $form ); @@ -286,13 +302,13 @@ sub www_sendToPayPal { my $form = $self->payPalForm( METHOD => 'SetExpressCheckout', AMT => $self->getCart->calculateTotal, - CURRENCYCODE => $self->get('currency'), + CURRENCYCODE => $self->currency, RETURNURL => $returnUrl->as_string, CANCELURL => $cancelUrl->as_string, PAYMENTACTION => 'SALE', ); - my $testMode = $self->get('testMode'); + my $testMode = $self->testMode; my $response = LWP::UserAgent->new->post( $self->apiUrl, $form ); my $params = $self->responseHash($response); my $i18n = WebGUI::International->new( $self->session, $I18N ); diff --git a/lib/WebGUI/Shop/PayDriver/PayPal/PayPalStd.pm b/lib/WebGUI/Shop/PayDriver/PayPal/PayPalStd.pm index 9478b9d90..15f855eb0 100644 --- a/lib/WebGUI/Shop/PayDriver/PayPal/PayPalStd.pm +++ b/lib/WebGUI/Shop/PayDriver/PayPal/PayPalStd.pm @@ -16,16 +16,75 @@ package WebGUI::Shop::PayDriver::PayPal::PayPalStd; use strict; -use base qw/WebGUI::Shop::PayDriver::PayPal/; - use URI; use URI::Escape; use LWP::UserAgent; use Readonly; -use Tie::IxHash; - Readonly my $I18N => 'PayDriver_PayPalStd'; +use Moose; +use WebGUI::Definition::Shop; +extends qw/WebGUI::Shop::PayDriver::PayPal/; + +define pluginName => [qw/PayPal PayDriver_PayPalStd/]; +property vendorId => ( + fieldType => 'text', + label => ['vendorId', 'PayDriver_PayPalStd'], + hoverHelp => ['vendorId help', 'PayDriver_PayPalStd'], + ); +property signature => ( + fieldType => 'textarea', + label => ['signature', 'PayDriver_PayPalStd'], + hoverHelp => ['signature help', 'PayDriver_PayPalStd'], + ); +property identityToken => ( + fieldType => 'text', + label => ['identity token', 'PayDriver_PayPalStd'], + hoverHelp => ['identity token help', 'PayDriver_PayPalStd'], + ); +property currency => ( + fieldType => 'selectBox', + label => ['currency', 'PayDriver_PayPalStd'], + hoverHelp => ['currency help', 'PayDriver_PayPalStd'], + default => 'USD', + options => \&_currency_options, + ); +sub _currency_options { + my $self = shift; + return $self->getPaymentCurrencies(); +} +property useSandbox => ( + fieldType => 'yesNo', + label => ['use sandbox', 'PayDriver_PayPalStd'], + hoverHelp => ['use sandbox help', 'PayDriver_PayPalStd'], + default => 1, + ); +property sandboxUrl => ( + fieldType => 'text', + label => ['sandbox url', 'PayDriver_PayPalStd'], + hoverHelp => ['sandbox url help', 'PayDriver_PayPalStd'], + default => 'https://www.sandbox.paypal.com/cgi-bin/webscr', + ); +property liveUrl => ( + fieldType => 'text', + label => ['live url', 'PayDriver_PayPalStd'], + hoverHelp => ['live url help', 'PayDriver_PayPalStd'], + default => 'https://www.paypal.com/cgi-bin/webscr', + ); +property buttonImage => ( + fieldType => 'text', + label => ['button image', 'PayDriver_PayPalStd'], + hoverHelp => ['button image help', 'PayDriver_PayPalStd'], + default => '', + ); +property summaryTemplateId => ( + fieldType => 'template', + label => ['summary template', 'PayDriver_PayPalStd'], + hoverHelp => ['summary template help', 'PayDriver_PayPalStd'], + namespace => 'Shop/Credentials', + default => '', + ); + =head1 NAME PayPal Website payments standard @@ -65,82 +124,6 @@ sub handlesRecurring { 0 } # Recurring TX stuff removed, for now. -#------------------------------------------------------------------- -sub definition { - my $class = shift; - my $session = shift; - WebGUI::Error::InvalidParam->throw( error => q{Must provide a session variable} ) - unless ref $session eq 'WebGUI::Session'; - my $definition = shift; - - my $i18n = WebGUI::International->new( $session, $I18N ); - - tie my %fields, 'Tie::IxHash'; - %fields = ( - vendorId => { - fieldType => 'text', - label => $i18n->get('vendorId'), - hoverHelp => $i18n->get('vendorId help'), - }, - signature => { - fieldType => 'textarea', - label => $i18n->get('signature'), - hoverHelp => $i18n->get('signature help'), - }, - identityToken => { - fieldType => 'text', - label => $i18n->get('identity token'), - hoverHelp => $i18n->get('identity token help'), - }, - currency => { - fieldType => 'selectBox', - label => $i18n->get('currency'), - hoverHelp => $i18n->get('currency help'), - defaultValue => 'USD', - options => $class->getPaymentCurrencies(), - }, - useSandbox => { - fieldType => 'yesNo', - label => $i18n->get('use sandbox'), - hoverHelp => $i18n->get('use sandbox help'), - defaultValue => 1, - }, - sandboxUrl => { - fieldType => 'text', - label => $i18n->get('sandbox url'), - hoverHelp => $i18n->get('sandbox url help'), - defaultValue => 'https://www.sandbox.paypal.com/cgi-bin/webscr', - }, - liveUrl => { - fieldType => 'text', - label => $i18n->get('live url'), - hoverHelp => $i18n->get('live url help'), - defaultValue => 'https://www.paypal.com/cgi-bin/webscr', - }, - buttonImage => { - fieldType => 'text', - label => $i18n->get('button image'), - hoverHelp => $i18n->get('button image help'), - defaultValue => '', - }, - summaryTemplateId => { - fieldType => 'template', - label => $i18n->get('summary template'), - hoverHelp => $i18n->get('summary template help'), - namespace => 'Shop/Credentials', - defaultValue => '', - }, - ); - - push @{$definition}, - { - name => $i18n->get('PayPal'), - properties => \%fields, - }; - - return $class->SUPER::definition( $session, $definition ); -} - #------------------------------------------------------------------- =head2 getButton @@ -173,8 +156,8 @@ sub getButton { my $button; my $i18n = WebGUI::International->new( $session, 'PayDriver_PayPalStd' ); my $text = $i18n->get('PayPal'); - if ( $self->get('buttonImage') ) { - my $raw = $self->get('buttonImage'); + if ( $self->buttonImage ) { + my $raw = $self->buttonImage; WebGUI::Macro::process( $session, \$raw ); $button = qq{ '_cart', upload => 1, - business => $self->get('vendorId'), - currency_code => $self->get('currency'), + business => $self->vendorId, + currency_code => $self->currency, no_shipping => 1, return => $return->as_string, @@ -260,8 +243,8 @@ Returns the url of the paypal gateway, taking into account useSandbox. sub payPalUrl { my $self = shift; - my $field = $self->get('useSandbox') ? 'sandboxUrl' : 'liveUrl'; - return $self->get($field); + my $field = $self->useSandbox ? 'sandboxUrl' : 'liveUrl'; + return $self->$field; } #------------------------------------------------------------------- @@ -285,7 +268,7 @@ sub processPayment { my %form = ( cmd => '_notify-synch', tx => $tx, - at => $self->get('identityToken'), + at => $self->identityToken, ); my $response = LWP::UserAgent->new->post($self->payPalUrl, \%form); my ($status, @lines) = split("\n", uri_unescape($response->content)); diff --git a/t/Shop/PayDriver/PayPalStd.t b/t/Shop/PayDriver/PayPalStd.t index d2162b6a6..e70192de5 100644 --- a/t/Shop/PayDriver/PayPalStd.t +++ b/t/Shop/PayDriver/PayPalStd.t @@ -21,6 +21,7 @@ use HTML::Form; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; +use WebGUI::Shop::PayDriver::PayPal::PayPalStd; #---------------------------------------------------------------------------- # Init @@ -29,16 +30,13 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 3; -plan tests => 1 + $tests; +plan tests => 3; #---------------------------------------------------------------------------- # figure out if the test can actually run my $e; -my $loaded = use_ok('WebGUI::Shop::PayDriver::PayPal::PayPalStd'); - ####################################################################### # # getName @@ -48,25 +46,15 @@ my $driver; my $options = { label => 'PayPal', enabled => 1, - group => 3, - receiptMessage => 'Pannenkoeken zijn nog lekkerder met spek', + groupToUse => 3, }; -$driver = WebGUI::Shop::PayDriver::PayPal::PayPalStd->create( $session, $options ); +$driver = WebGUI::Shop::PayDriver::PayPal::PayPalStd->new( $session, $options ); +WebGUI::Test->addToCleanup($driver); isa_ok ($driver, 'WebGUI::Shop::PayDriver'); isa_ok ($driver, 'WebGUI::Shop::PayDriver::PayPal::PayPalStd'); is($driver->getName($session), 'PayPal', 'getName returns the human readable name of this driver'); -####################################################################### -# -# delete -# -####################################################################### - -$driver->delete; - -undef $driver; - #vim:ft=perl