From 1b6dd7be63fd1b185e7801067f31a2c77dcf5370 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 15 Jan 2009 00:34:34 +0000 Subject: [PATCH] Forward port the PayDriver bug fixes from 7.5 branch. Labels are always taken consistently from the JSON blob, and not a mixture of the two. --- docs/changelog/7.x.x.txt | 1 + docs/upgrades/upgrade_7.6.8-7.6.9.pl | 24 +++++++++++++++++++ lib/WebGUI/Shop/Pay.pm | 9 +------ lib/WebGUI/Shop/PayDriver.pm | 26 +++++++-------------- t/Shop/Pay.t | 23 ++++++------------ t/Shop/PayDriver.t | 35 ++++------------------------ 6 files changed, 47 insertions(+), 71 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 9ae6d0fc1..15cddc68c 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -5,6 +5,7 @@ - fixed #9492: Problem in passing form variables to Macro SQL inside a snippet - fixed #9404: Head tags for admin user with admin mode off - fixed #9507: Navigation: relDepth is calculated with starting point, instead of the first displayed page + - fixed a bug where no Payment Gateway labels show up when checking out. 7.6.8 - added #!/usr/bin/env perl to all utility scripts diff --git a/docs/upgrades/upgrade_7.6.8-7.6.9.pl b/docs/upgrades/upgrade_7.6.8-7.6.9.pl index fde3aea21..3da43274c 100644 --- a/docs/upgrades/upgrade_7.6.8-7.6.9.pl +++ b/docs/upgrades/upgrade_7.6.8-7.6.9.pl @@ -22,6 +22,8 @@ use Getopt::Long; use WebGUI::Session; use WebGUI::Storage; use WebGUI::Asset; +use WebGUI::Shop::Pay; +use WebGUI::Shop::PayDriver; my $toVersion = '7.6.9'; @@ -31,6 +33,7 @@ my $quiet; # this line required my $session = start(); # this line required # upgrade functions go here +fixPayDriverLabels($session); finish($session); # this line required @@ -44,6 +47,27 @@ finish($session); # this line required # print "DONE!\n" unless $quiet; #} +#---------------------------------------------------------------------------- +# Get rid of the duplicate label properties in the PayDrivers. +sub fixPayDriverLabels { + my $session = shift; + print "\tGet rid of the duplicate label properties in the PayDrivers... " unless $quiet; + my $pay = WebGUI::Shop::Pay->new($session); + my $gateways = $pay->getPaymentGateways; + foreach my $gateway (@{ $gateways }) { + my $gatewayId = $gateway->getId; + my $jsonLabel = $gateway->get('label'); + next if $jsonLabel; + my $dbLabel = $session->db->quickScalar('select label from paymentGateway where paymentGatewayId=?', [$gatewayId]); + my $properties = $gateway->get(); + $properties->{label} = $dbLabel; + $gateway->update($properties); + } + $session->db->write('alter table paymentGateway drop column label'); + print "DONE!\n" unless $quiet; +} + + # -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/lib/WebGUI/Shop/Pay.pm b/lib/WebGUI/Shop/Pay.pm index 751c94f39..a190bf2d9 100644 --- a/lib/WebGUI/Shop/Pay.pm +++ b/lib/WebGUI/Shop/Pay.pm @@ -42,10 +42,6 @@ back up to the top. The class of the new PayDriver object to create. -=head4 $label - -The label for this instance. - =head4 $options A list of properties to assign to this PayDriver. See C for details. @@ -55,17 +51,14 @@ A list of properties to assign to this PayDriver. See C for details sub addPaymentGateway { my $self = shift; my $requestedClass = shift; - my $label = shift; my $options = shift; WebGUI::Error::InvalidParam->throw(error => q{Must provide a class to create an object}) unless defined $requestedClass; WebGUI::Error::InvalidParam->throw(error => q{The requested class is not enabled in your WebGUI configuration file}, param => $requestedClass) unless isIn($requestedClass, (keys %{$self->getDrivers}) ); - WebGUI::Error::InvalidParam->throw(error => q{Must provide a label to create an object}) - unless $label; WebGUI::Error::InvalidParam->throw(error => q{You must pass a hashref of options to create a new PayDriver object}) unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options }; - my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'create', [ $self->session, $label, $options ]) }; + my $driver = eval { WebGUI::Pluggable::instanciate($requestedClass, 'create', [ $self->session, $options ]) }; return $driver; } diff --git a/lib/WebGUI/Shop/PayDriver.pm b/lib/WebGUI/Shop/PayDriver.pm index cf931fb09..1e5cfc57e 100644 --- a/lib/WebGUI/Shop/PayDriver.pm +++ b/lib/WebGUI/Shop/PayDriver.pm @@ -38,7 +38,6 @@ readonly session => my %session; readonly className => my %className; readonly paymentGatewayId => my %paymentGatewayId; readonly options => my %options; -readonly label => my %label; #------------------------------------------------------------------- @@ -49,7 +48,7 @@ Private method used to build objects, shared by new and create. =cut sub _buildObj { - my ($class, $session, $requestedClass, $paymentGatewayId, $label, $options) = @_; + my ($class, $session, $requestedClass, $paymentGatewayId, $options) = @_; my $self = {}; bless $self, $requestedClass; register $self; @@ -57,10 +56,9 @@ sub _buildObj { my $id = id $self; $session{ $id } = $session; - $paymentGatewayId{ $id } = $paymentGatewayId; - $label{ $id } = $label; $options{ $id } = $options; $className{ $id } = $requestedClass; + $paymentGatewayId{ $id } = $paymentGatewayId; return $self; } @@ -137,7 +135,7 @@ to do calculations. #------------------------------------------------------------------- -=head2 create ( $session, $label, $options ) +=head2 create ( $session, $options ) Constructor for new WebGUI::Shop::PayDriver objects. Returns a WebGUI::Shop::PayDriver object. To access driver objects that have already been configured, use C. @@ -146,10 +144,6 @@ To access driver objects that have already been configured, use C. A WebGUI::Session object. -=head4 $label - -A human readable label for this payment. - =head4 $options A list of properties to assign to this PayDriver. See C for details. @@ -161,23 +155,21 @@ sub create { my $session = shift; WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) unless ref $session eq 'WebGUI::Session'; - my $label = shift; - WebGUI::Error::InvalidParam->throw(error => q{Must provide a human readable label in the hashref of options}) - unless $label; my $options = shift; WebGUI::Error::InvalidParam->throw(error => q{Must provide a hashref of options}) unless ref $options eq 'HASH' and scalar keys %{ $options }; + WebGUI::Error::InvalidParam->throw(error => q{Must provide a human readable label in the hashref of options}) + unless exists $options->{label} && $options->{label}; # Generate a unique id for this payment my $paymentGatewayId = $session->id->generate; # Build object - my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $label, $options); + my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $options); # and persist this instance in the db - $session->db->write('insert into paymentGateway (paymentGatewayId, label, className) VALUES (?,?,?)', [ + $session->db->write('insert into paymentGateway (paymentGatewayId, className) VALUES (?,?)', [ $paymentGatewayId, - $label, $class, ]); @@ -550,7 +542,7 @@ sub new { my $options = from_json($properties->{options}); - my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $properties->{ label }, $options); + my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $options); return $self; } @@ -600,7 +592,7 @@ sub processPropertiesFromFormPost { ); } } - $properties{title} = $fullDefinition->[0]{name} if ($properties{title} eq "" || lc($properties{title}) eq "untitled"); + $properties{label} = $fullDefinition->[0]{name} if ($properties{label} eq "" || lc($properties{label}) eq "untitled"); $self->update(\%properties); } diff --git a/t/Shop/Pay.t b/t/Shop/Pay.t index 2f6d6332e..3d4d97df4 100644 --- a/t/Shop/Pay.t +++ b/t/Shop/Pay.t @@ -33,7 +33,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 19; +my $tests = 18; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -106,15 +106,6 @@ throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDrive 'addPaymentGateway croaks without a configured class', ); -throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash'); }, - 'WebGUI::Error::InvalidParam', - { - error => 'Must provide a label to create an object', - }, - 'addPaymentGateway requires a label', -); - - throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL'); }, 'WebGUI::Error::InvalidParam', { @@ -123,7 +114,7 @@ throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDrive 'addPaymentGateway croaks without options to build a object with', ); -throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL', {}); }, +throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', {}); }, 'WebGUI::Error::InvalidParam', { error => 'You must pass a hashref of options to create a new PayDriver object', @@ -135,9 +126,9 @@ my $options = { enabled => 1, label => 'Cold, stone hard cash', }; -$newDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL', $options); +$newDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', $options); isa_ok($newDriver, 'WebGUI::Shop::PayDriver::Cash', 'added a new, configured Cash driver'); -is($newDriver->label, 'JAL', 'label passed correctly to paydriver'); +is($newDriver->get('label'), 'Cold, stone hard cash', 'label passed correctly to paydriver'); #TODO: check if options are stored. @@ -212,14 +203,14 @@ my $otherOptions = { enabled => 1, label => 'Even harder cash', }; -$anotherDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'Pomade', $otherOptions); +$anotherDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', $otherOptions); my $gateways = $pay->getPaymentGateways; -my @returnedIds = map {$_->label} @{ $gateways }; +my @returnedIds = map {$_->get('label')} @{ $gateways }; cmp_bag( \@returnedIds, [ - qw/Cash ITransact Pomade JAL/ + qw/Cash ITransact/, 'Even harder cash', 'Cold, stone hard cash', ], 'getPaymentGateways returns all create payment drivers', ); diff --git a/t/Shop/PayDriver.t b/t/Shop/PayDriver.t index bd3fcda2a..b5cd72b33 100644 --- a/t/Shop/PayDriver.t +++ b/t/Shop/PayDriver.t @@ -31,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 49; +my $tests = 46; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -150,31 +150,9 @@ cmp_deeply ( 'create takes exception to not giving it a session object', ); -eval { $driver = WebGUI::Shop::PayDriver->create($session); }; +eval { $driver = WebGUI::Shop::PayDriver->create($session, {}); }; $e = Exception::Class->caught(); -isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a label'); -cmp_deeply ( - $e, - methods( - error => 'Must provide a human readable label in the hashref of options', - ), - 'create takes exception to not giving it a hashref of options', -); - -eval { $driver = WebGUI::Shop::PayDriver->create($session, 'Very human readable label'); }; -$e = Exception::Class->caught(); -isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a hashref of options'); -cmp_deeply ( - $e, - methods( - error => 'Must provide a hashref of options', - ), - 'create takes exception to not giving it a hashref of options', -); - -eval { $driver = WebGUI::Shop::PayDriver->create($session, 'Very human readable label', {}); }; -$e = Exception::Class->caught(); -isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it an empty hashref of options'); +isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to giving it an empty hashref of options'); cmp_deeply ( $e, methods( @@ -185,7 +163,6 @@ cmp_deeply ( # Test functionality -my $label = 'Human Readable Label'; my $options = { label => 'Fast and harmless', enabled => 1, @@ -193,19 +170,18 @@ my $options = { receiptMessage => 'Pannenkoeken zijn nog lekkerder met spek', }; -$driver = WebGUI::Shop::PayDriver->create( $session, $label, $options ); +$driver = WebGUI::Shop::PayDriver->create( $session, $options ); isa_ok ($driver, 'WebGUI::Shop::PayDriver', 'create creates WebGUI::Shop::PayDriver object'); +like($driver->getId, $session->id->getValidator, 'driver id is a valid GUID'); my $dbData = $session->db->quickHashRef('select * from paymentGateway where paymentGatewayId=?', [ $driver->getId ]); -#diag ($driver->getId); cmp_deeply ( $dbData, { paymentGatewayId => $driver->getId, className => ref $driver, - label => $driver->label, options => q|{"group":3,"receiptMessage":"Pannenkoeken zijn nog lekkerder met spek","label":"Fast and harmless","enabled":1}|, }, 'Correct data written to the db', @@ -213,7 +189,6 @@ cmp_deeply ( - ####################################################################### # # session