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.
This commit is contained in:
Colin Kuskie 2009-01-15 00:34:34 +00:00
parent 3348df94da
commit 1b6dd7be63
6 changed files with 47 additions and 71 deletions

View file

@ -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

View file

@ -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 --------------------------------

View file

@ -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<definition> for details.
@ -55,17 +51,14 @@ A list of properties to assign to this PayDriver. See C<definition> 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;
}

View file

@ -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<new>.
@ -146,10 +144,6 @@ To access driver objects that have already been configured, use C<new>.
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<definition> 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);
}

View file

@ -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',
);

View file

@ -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