All tests passing, able to talk to USPS and get rates for multiple package configurations.

This commit is contained in:
Colin Kuskie 2009-11-06 19:43:12 -08:00
parent c2b41f9884
commit 6543ed5027
3 changed files with 228 additions and 430 deletions

View file

@ -48,15 +48,12 @@ result by the quantity, rather than doing several identical checks.
sub buildXML { sub buildXML {
my ($self, $cart, @packages) = @_; my ($self, $cart, @packages) = @_;
tie my %xmlHash, 'Tie::IxHash'; tie my %xmlHash, 'Tie::IxHash';
%xmlHash = ( RateV3Request => {}, ); %xmlHash = ( IntlRateRequest => {}, );
my $xmlTop = $xmlHash{RateV3Request}; my $xmlTop = $xmlHash{IntlRateRequest};
$xmlTop->{USERID} = $self->get('userId'); $xmlTop->{USERID} = $self->get('userId');
$xmlTop->{Package} = []; $xmlTop->{Package} = [];
##Do a request for each package. ##Do a request for each package.
my $packageIndex; my $packageIndex;
my $shipType = $self->get('shipType');
my $service = $shipType eq 'PRIORITY VARIABLE' ? 'PRIORITY'
: $shipType;
PACKAGE: for(my $packageIndex = 0; $packageIndex < scalar @packages; $packageIndex++) { PACKAGE: for(my $packageIndex = 0; $packageIndex < scalar @packages; $packageIndex++) {
my $package = $packages[$packageIndex]; my $package = $packages[$packageIndex];
next PACKAGE unless scalar @{ $package }; next PACKAGE unless scalar @{ $package };
@ -78,21 +75,13 @@ sub buildXML {
$ounces = 0.1; $ounces = 0.1;
} }
my $destination = $package->[0]->getShippingAddress; my $destination = $package->[0]->getShippingAddress;
my $destZipCode = $destination->get('code'); my $country = $destination->get('country');
$packageData{ID} = $packageIndex; $packageData{ID} = $packageIndex;
$packageData{Service} = [ $service ]; $packageData{Pounds} = [ $pounds ];
$packageData{ZipOrigination} = [ $self->get('sourceZip') ]; $packageData{Ounces} = [ $ounces ];
$packageData{ZipDestination} = [ $destZipCode ]; $packageData{Machinable} = [ 'true' ];
$packageData{Pounds} = [ $pounds ]; $packageData{MailType} = [ 'Package' ];
$packageData{Ounces} = [ $ounces ]; $packageData{Country} = [ $country ];
if ($shipType eq 'PRIORITY') {
$packageData{Container} = [ 'FLAT RATE BOX' ];
}
elsif ($shipType eq 'PRIORITY VARIABLE') {
#$packageData{Container} = [ 'VARIABLE' ];
}
$packageData{Size} = [ 'REGULAR' ];
$packageData{Machinable} = [ 'true' ];
push @{ $xmlTop->{Package} }, \%packageData; push @{ $xmlTop->{Package} }, \%packageData;
} }
my $xml = XMLout(\%xmlHash, my $xml = XMLout(\%xmlHash,
@ -124,9 +113,6 @@ costs are assessed.
sub calculate { sub calculate {
my ($self, $cart) = @_; my ($self, $cart) = @_;
if (! $self->get('sourceZip')) {
WebGUI::Error::InvalidParam->throw(error => q{Driver configured without a source zipcode.});
}
if (! $self->get('userId')) { if (! $self->get('userId')) {
WebGUI::Error::InvalidParam->throw(error => q{Driver configured without a USPS userId.}); WebGUI::Error::InvalidParam->throw(error => q{Driver configured without a USPS userId.});
} }
@ -173,7 +159,7 @@ Processed XML data from an XML rate request, processed in perl data structure.
have this structure: have this structure:
{ {
RateV3Response => { IntlRateResponse => {
Package => [ Package => [
{ {
ID => 0, ID => 0,
@ -194,17 +180,24 @@ The set of shippable units, which are required to do quantity lookups.
sub _calculateFromXML { sub _calculateFromXML {
my ($self, $xmlData, @shippableUnits) = @_; my ($self, $xmlData, @shippableUnits) = @_;
my $cost = 0; my $cost = 0;
foreach my $package (@{ $xmlData->{RateV3Response}->{Package} }) { foreach my $package (@{ $xmlData->{IntlRateResponse}->{Package} }) {
my $id = $package->{ID}; my $id = $package->{ID};
my $rate = $package->{Postage}->{Rate};
##Error check for invalid index ##Error check for invalid index
if ($id < 0 || $id > $#shippableUnits) { if ($id < 0 || $id > $#shippableUnits) {
WebGUI::Error::Shop::RemoteShippingRate->throw(error => "Illegal package index returned by USPS: $id"); WebGUI::Error::Shop::RemoteShippingRate->throw(error => "Illegal package index returned by USPS: $id");
} }
if (exists $package->{Error}) { if (exists $package->{Error}) {
WebGUI::Error::Shop::RemoteShippingRate->throw(error => $package->{Description}); WebGUI::Error::Shop::RemoteShippingRate->throw(error => $package->{Error}->{Description});
} }
my $unit = $shippableUnits[$id]; my $unit = $shippableUnits[$id];
my $rate;
SERVICE: foreach my $service (@{ $package->{Service} }) {
next SERVICE unless $service->{ID} eq $self->get('shipType');
$rate = $service->{Postage};
}
if (!$rate) {
WebGUI::Error::Shop::RemoteShippingRate->throw(error => 'Selected shipping service not available');
}
if ($unit->[0]->getSku->shipsSeparately) { if ($unit->[0]->getSku->shipsSeparately) {
##This is a single item due to ships separately. Since in reality there will be ##This is a single item due to ships separately. Since in reality there will be
## N things being shipped, multiply the rate by the quantity. ## N things being shipped, multiply the rate by the quantity.
@ -238,10 +231,14 @@ sub definition {
my $i18n2 = WebGUI::International->new($session, 'ShipDriver_USPSInternational'); my $i18n2 = WebGUI::International->new($session, 'ShipDriver_USPSInternational');
tie my %shippingTypes, 'Tie::IxHash'; tie my %shippingTypes, 'Tie::IxHash';
##Note, these keys are used by buildXML ##Note, these keys are used by buildXML
$shippingTypes{'PRIORITY VARIABLE'} = $i18n->get('priority variable'); $shippingTypes{1} = $i18n2->get('express mail international');
$shippingTypes{'PRIORITY'} = $i18n->get('priority'); $shippingTypes{2} = $i18n2->get('priority mail international');
$shippingTypes{'EXPRESS' } = $i18n->get('express'); $shippingTypes{6} = $i18n2->get('global express guaranteed rectangular');
$shippingTypes{'PARCEL' } = $i18n->get('parcel post'); $shippingTypes{7} = $i18n2->get('global express guaranteed non-rectangular');
$shippingTypes{9} = $i18n2->get('priority mail flat rate box');
$shippingTypes{11} = $i18n2->get('priority mail large flat rate box');
$shippingTypes{15} = $i18n2->get('first class mail international parcels');
$shippingTypes{16} = $i18n2->get('priority mail small flat rate box');
tie my %fields, 'Tie::IxHash'; tie my %fields, 'Tie::IxHash';
%fields = ( %fields = (
instructions => { instructions => {

View file

@ -1,4 +1,4 @@
package WebGUI::i18n::English::ShipDriver_USPS; package WebGUI::i18n::English::ShipDriver_USPSInternational;
use strict; use strict;
@ -8,7 +8,55 @@ our $I18N = {
message => q|U.S. Postal Service, International|, message => q|U.S. Postal Service, International|,
lastUpdated => 1203569535, lastUpdated => 1203569535,
context => q|Name of the shipping driver|, context => q|Name of the shipping driver|,
} },
'express mail international' => {
message => q|Express Mail International|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
'priority mail international' => {
message => q|Priority Mail International|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
'global express guaranteed rectangular' => {
message => q|Global Express Guaranteed Non-Document Rectangular|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
'global express guaranteed non-rectangular' => {
message => q|Global Express Guaranteed Non-Document Non-Rectangular|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
'priority mail flat rate box' => {
message => q|Priority Mail Flat Rate Box|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
'priority mail large flat rate box' => {
message => q|Priority Mail Large Flat Rate Box|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
'priority mail small flat rate box' => {
message => q|Priority Mail Small Flat Rate Box|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
'first class mail international parcels' => {
message => q|First Class Mail International Parcels|,
lastUpdated => 1203569535,
context => q|Name of a shipping option|,
},
}; };

View file

@ -25,7 +25,7 @@ use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session; use WebGUI::Session;
use WebGUI::Shop::ShipDriver::USPSInternational; use WebGUI::Shop::ShipDriver::USPSInternational;
plan tests => 53; plan tests => 34;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Init # Init
@ -124,7 +124,7 @@ cmp_deeply(
isa_ok( isa_ok(
$definition = WebGUI::Shop::ShipDriver::USPS->definition($session), $definition = WebGUI::Shop::ShipDriver::USPSInternational->definition($session),
'ARRAY' 'ARRAY'
); );
@ -136,14 +136,14 @@ isa_ok(
####################################################################### #######################################################################
my $options = { my $options = {
label => 'USPS Driver', label => 'Intl USPS Driver',
enabled => 1, enabled => 1,
}; };
$driver2 = WebGUI::Shop::ShipDriver::USPS->create($session, $options); $driver2 = WebGUI::Shop::ShipDriver::USPSInternational->create($session, $options);
addToCleanup($driver2); addToCleanup($driver2);
isa_ok($driver2, 'WebGUI::Shop::ShipDriver::USPS'); isa_ok($driver2, 'WebGUI::Shop::ShipDriver::USPSInternational');
isa_ok($driver2, 'WebGUI::Shop::ShipDriver'); isa_ok($driver2, 'WebGUI::Shop::ShipDriver');
####################################################################### #######################################################################
@ -152,7 +152,7 @@ isa_ok($driver2, 'WebGUI::Shop::ShipDriver');
# #
####################################################################### #######################################################################
is (WebGUI::Shop::ShipDriver::USPS->getName($session), 'U.S. Postal Service', 'getName returns the human readable name of this driver'); is (WebGUI::Shop::ShipDriver::USPSInternational->getName($session), 'U.S. Postal Service, International', 'getName returns the human readable name of this driver');
####################################################################### #######################################################################
# #
@ -174,28 +174,12 @@ undef $driver2;
# #
####################################################################### #######################################################################
my $driver = WebGUI::Shop::ShipDriver::USPS->create($session, { my $driver = WebGUI::Shop::ShipDriver::USPSInternational->create($session, {
label => 'Shipping from Shawshank', label => 'Shipping from Shawshank',
enabled => 1, enabled => 1,
shipType => 'PARCEL',
}); });
addToCleanup($driver); addToCleanup($driver);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no zipcode has been set');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a source zipcode.',
),
'... checking error message',
);
my $properties = $driver->get();
$properties->{sourceZip} = '97123';
$driver->update($properties);
eval { $driver->calculate() }; eval { $driver->calculate() };
$e = Exception::Class->caught(); $e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no userId'); isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no userId');
@ -212,17 +196,15 @@ addToCleanup($cart);
my $addressBook = $cart->getAddressBook; my $addressBook = $cart->getAddressBook;
my $workAddress = $addressBook->addAddress({ my $workAddress = $addressBook->addAddress({
label => 'work', label => 'work',
organization => 'Plain Black Corporation', organization => 'ProcoliX',
address1 => '1360 Regent St. #145', address1 => 'Rotterdamseweg 183C',
city => 'Madison', state => 'WI', code => '53715', city => 'Delft', code => '2629HD',
country => 'United States', country => 'Netherlands',
}); });
my $wucAddress = $addressBook->addAddress({ my $sdhAddress = $addressBook->addAddress({
label => 'wuc', label => 'other side of planet',
organization => 'Madison Concourse Hotel', organization => 'SDH',
address1 => '1 W Dayton St', country => 'Australia',
city => 'Madison', state => 'WI', code => '53703',
country => 'United States',
}); });
$cart->update({shippingAddressId => $workAddress->getId}); $cart->update({shippingAddressId => $workAddress->getId});
@ -262,7 +244,7 @@ cmp_bag(
); );
my $rockHammer2 = $bible->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer)); my $rockHammer2 = $bible->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
$rockHammer2->update({shippingAddressId => $wucAddress->getId}); $rockHammer2->update({shippingAddressId => $sdhAddress->getId});
cmp_bag( cmp_bag(
[$driver->_getShippableUnits($cart)], [$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], [ ignore() ], [ ignore() ], [ ignore() ], ], [[ ignore(), ignore() ], [ ignore() ], [ ignore() ], [ ignore() ], ],
@ -285,31 +267,16 @@ if (! $userId) {
$hasRealUserId = 0; $hasRealUserId = 0;
$userId = "blahBlahBlah"; $userId = "blahBlahBlah";
} }
$properties = $driver->get(); my $properties = $driver->get();
$properties->{userId} = $userId; $properties->{userId} = $userId;
$properties->{sourceZip} = '97123'; $properties->{shipType} = '9';
$driver->update($properties); $driver->update($properties);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer)); $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
my @shippableUnits = $driver->_getShippableUnits($cart); my @shippableUnits = $driver->_getShippableUnits($cart);
$properties = $driver->get();
$properties->{addInsurance} = 1;
$driver->update($properties);
is($driver->_calculateInsurance(@shippableUnits), 2, '_calculateInsurance: one item in cart with quantity=1, calculates insurance');
$properties->{addInsurance} = 0;
$driver->update($properties);
is($driver->_calculateInsurance(@shippableUnits), 0, '_calculateInsurance: returns 0 if insurance is not enabled');
$properties->{addInsurance} = 1;
$properties->{insuranceRates} = '';
$driver->update($properties);
is($driver->_calculateInsurance(@shippableUnits), 0, '_calculateInsurance: returns 0 if rates are not set');
my $xml = $driver->buildXML($cart, @shippableUnits); my $xml = $driver->buildXML($cart, @shippableUnits);
like($xml, qr/<RateV3Request USERID="[^"]+"/, 'buildXML: checking userId is an attribute of the RateV3Request tag'); like($xml, qr/<IntlRateRequest USERID="[^"]+"/, 'buildXML: checking userId is an attribute of the IntlRateRequest tag');
like($xml, qr/<Package ID="0"/, 'buildXML: checking ID is an attribute of the Package tag'); like($xml, qr/<Package ID="0"/, 'buildXML: checking ID is an attribute of the Package tag');
my $xmlData = XMLin($xml, my $xmlData = XMLin($xml,
@ -319,23 +286,22 @@ my $xmlData = XMLin($xml,
cmp_deeply( cmp_deeply(
$xmlData, $xmlData,
{ {
RateV3Request => { IntlRateRequest => {
USERID => $userId, USERID => $userId,
Package => [ Package => [
{ {
ID => 0, ID => 0,
ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0',
Pounds => '1', Ounces => '8.0', Machinable => 'true', Country => 'Netherlands',
Size => 'REGULAR', Service => 'PARCEL', MailType => 'Package',
Machinable => 'true',
}, },
], ],
} }
}, },
'buildXML: PARCEL service, 1 item in cart' 'buildXML: 1 item in cart'
); );
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order'); like($xml, qr/IntlRateRequest USERID.+?Package ID=.+?Pounds.+?Ounces.+?Machinable.+?MailType.+?Country.+?/, '... and tag order');
SKIP: { SKIP: {
@ -350,15 +316,25 @@ SKIP: {
Package => [ Package => [
{ {
ID => 0, ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(), AreasServed => ignore(), Prohibitions => ignore(),
Machinable => ignore(), Ounces => ignore(), ExpressMail => ignore(), CustomsForms => ignore(),
Pounds => ignore(), Size => ignore(), Observations => ignore(), Restrictions => ignore(),
Zone => ignore(), Service => [
Postage => { {
CLASSID => ignore(), ID => ignore(),
MailService => ignore(), MaxWeight => ignore(),
Rate => num(10,10), ##A number around 10... MaxDimensions => ignore(),
} MailType => 'Package',
Ounces => '8',
Pounds => '1',
Country => 'NETHERLANDS',
Machinable => 'true',
Postage => num(100,99),
SvcCommitments => ignore(),
SvcDescription => ignore(),
},
(ignore())x12,
],
}, },
], ],
}, },
@ -369,13 +345,22 @@ SKIP: {
my $cost = $driver->_calculateFromXML( my $cost = $driver->_calculateFromXML(
{ {
RateV3Response => { IntlRateResponse => {
Package => [ Package => [
{ {
ID => 0, ID => 0,
Postage => { Service => [
Rate => 5.25, {
}, ID => '9',
Postage => '5.25',
MaxWeight => '70'
},
{
ID => '11',
Postage => '7.25',
MaxWeight => '70'
},
],
}, },
], ],
}, },
@ -388,8 +373,6 @@ is($cost, 5.25, '_calculateFromXML calculates shipping cost correctly for 1 item
$bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible)); $bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
@shippableUnits = $driver->_getShippableUnits($cart); @shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 7, '_calculateInsurance: two items in cart with quantity=1, calculates insurance');
$xml = $driver->buildXML($cart, @shippableUnits); $xml = $driver->buildXML($cart, @shippableUnits);
$xmlData = XMLin( $xml, $xmlData = XMLin( $xml,
KeepRoot => 1, KeepRoot => 1,
@ -399,22 +382,20 @@ $xmlData = XMLin( $xml,
cmp_deeply( cmp_deeply(
$xmlData, $xmlData,
{ {
RateV3Request => { IntlRateRequest => {
USERID => $userId, USERID => $userId,
Package => [ Package => [
{ {
ID => 0, ID => 0,
ZipDestination => '53715', ZipOrigination => '97123', Pounds => '2', Ounces => '0.0',
Pounds => '2', Ounces => '0.0', Machinable => 'true', Country => 'Netherlands',
Size => 'REGULAR', Service => 'PARCEL', MailType => 'Package',
Machinable => 'true',
}, },
{ {
ID => 1, ID => 1,
ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0',
Pounds => '1', Ounces => '8.0', Machinable => 'true', Country => 'Netherlands',
Size => 'REGULAR', Service => 'PARCEL', MailType => 'Package',
Machinable => 'true',
}, },
], ],
} }
@ -428,57 +409,41 @@ SKIP: {
my $response = $driver->_doXmlRequest($xml); my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful for 2 items in cart'); ok($response->is_success, '_doXmlRequest to USPS successful for 2 items in cart');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '0.0',
Pounds => 2, Size => ignore(),
Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
{
ID => 1,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '8.0',
Pounds => 1, Size => ignore(),
Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format for 2 items in cart. If this test fails, the driver may need to be updated'
);
} }
$cost = $driver->_calculateFromXML( $cost = $driver->_calculateFromXML(
{ {
RateV3Response => { IntlRateResponse => {
Package => [ Package => [
{ {
ID => 0, ID => 0,
Postage => { Service => [
Rate => 7.00, {
}, ID => '9',
Postage => '7.00',
MaxWeight => '70'
},
{
ID => '11',
Postage => '9.00',
MaxWeight => '70'
},
],
}, },
{ {
ID => 1, ID => 1,
Postage => { Service => [
Rate => 5.25, {
}, ID => '9',
Postage => '5.25',
MaxWeight => '70'
},
{
ID => '11',
Postage => '7.25',
MaxWeight => '70'
},
],
}, },
], ],
}, },
@ -491,23 +456,39 @@ is($cost, 12.25, '_calculateFromXML calculates shipping cost correctly for 2 ite
$bibleItem->setQuantity(2); $bibleItem->setQuantity(2);
@shippableUnits = $driver->_getShippableUnits($cart); @shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 8, '_calculateInsurance: two items in cart with quantity=2, calculates insurance');
$cost = $driver->_calculateFromXML( $cost = $driver->_calculateFromXML(
{ {
RateV3Response => { IntlRateResponse => {
Package => [ Package => [
{ {
ID => 0, ID => 0,
Postage => { Service => [
Rate => 7.00, {
}, ID => '9',
Postage => '7.00',
MaxWeight => '70'
},
{
ID => '11',
Postage => '9.00',
MaxWeight => '70'
},
],
}, },
{ {
ID => 1, ID => 1,
Postage => { Service => [
Rate => 5.25, {
}, ID => '9',
Postage => '5.25',
MaxWeight => '70'
},
{
ID => '11',
Postage => '7.25',
MaxWeight => '70'
},
],
}, },
], ],
}, },
@ -517,9 +498,8 @@ $cost = $driver->_calculateFromXML(
is($cost, 19.25, '_calculateFromXML calculates shipping cost correctly for 2 items in the cart, with quantity of 2'); is($cost, 19.25, '_calculateFromXML calculates shipping cost correctly for 2 items in the cart, with quantity of 2');
$rockHammer2 = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer)); $rockHammer2 = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
$rockHammer2->update({shippingAddressId => $wucAddress->getId}); $rockHammer2->update({shippingAddressId => $sdhAddress->getId});
@shippableUnits = $driver->_getShippableUnits($cart); @shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 12, '_calculateInsurance: calculates insurance');
$xml = $driver->buildXML($cart, @shippableUnits); $xml = $driver->buildXML($cart, @shippableUnits);
$xmlData = XMLin( $xml, $xmlData = XMLin( $xml,
@ -530,29 +510,26 @@ $xmlData = XMLin( $xml,
cmp_deeply( cmp_deeply(
$xmlData, $xmlData,
{ {
RateV3Request => { IntlRateRequest => {
USERID => $userId, USERID => $userId,
Package => [ Package => [
{ {
ID => 0, ID => 0,
ZipDestination => '53715', ZipOrigination => '97123', Pounds => '2', Ounces => '0.0',
Pounds => '2', Ounces => '0.0', Machinable => 'true', Country => 'Netherlands',
Size => 'REGULAR', Service => 'PARCEL', MailType => 'Package',
Machinable => 'true',
}, },
{ {
ID => 1, ID => 1,
ZipDestination => '53715', ZipOrigination => '97123', Pounds => '12', Ounces => '0.0',
Pounds => '1', Ounces => '8.0', Machinable => 'true', Country => 'Australia',
Size => 'REGULAR', Service => 'PARCEL', MailType => 'Package',
Machinable => 'true',
}, },
{ {
ID => 2, ID => 2,
ZipDestination => '53703', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0',
Pounds => '12', Ounces => '0.0', Machinable => 'true', Country => 'Netherlands',
Size => 'REGULAR', Service => 'PARCEL', MailType => 'Package',
Machinable => 'true',
}, },
], ],
} }
@ -561,252 +538,44 @@ cmp_deeply(
); );
SKIP: { SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId; skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml); my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful for 3 items in cart'); ok($response->is_success, '_doXmlRequest to USPS successful for 3 items in cart');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '0.0',
Pounds => 2, Size => ignore(),
Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
{
ID => 1,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '8.0',
Pounds => 1, Size => ignore(),
Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
{
ID => 2,
ZipOrigination => ignore(), ZipDestination => 53703,
Machinable => ignore(), Ounces => '0.0',
Pounds => 12, Size => ignore(),
Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(20,20), ##A number around 20...
}
},
],
},
'... returned data from USPS in correct format for 3 items in cart. If this test fails, the driver may need to be updated'
);
} }
####################################################################### #######################################################################
# #
# Test Priority shipping setup # Check too heavy for my shipping type
# #
####################################################################### #######################################################################
$cart->empty; $cart->empty;
$properties = $driver->get(); $properties = $driver->get();
$properties->{shipType} = 'PRIORITY'; $properties->{shipType} = '9';
$driver->update($properties); $driver->update($properties);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
@shippableUnits = $driver->_getShippableUnits($cart);
$xml = $driver->buildXML($cart, @shippableUnits);
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PRIORITY',
Machinable => 'true', Container => 'FLAT RATE BOX',
},
],
}
},
'buildXML: PRIORITY service, 1 item in cart'
);
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Container.+?Size.+?Machinable/, '... and tag order');
SKIP: { SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId; skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml); my $heavyHammer = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
ok($response->is_success, '_doXmlRequest to USPS successful'); $heavyHammer->setQuantity(2);
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],); $cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', "USPS returns error when package is too heavy for the selected service");
cmp_deeply( cmp_deeply(
$xmlData, $e,
{ methods(
Package => [ error => 'Selected shipping service not available',
{ ),
ID => 0, '... checking error message',
ZipOrigination => ignore(), ZipDestination => ignore(),
Container => ignore(), Ounces => ignore(), ##Machinable missing, added Container
Pounds => ignore(), Size => ignore(),
Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
); );
} $heavyHammer->setQuantity(20);
$cost = eval { $driver->calculate($cart); };
####################################################################### $e = Exception::Class->caught();
# isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', "USPS returns error when package is too heavy for any service");
# Test EXPRESS shipping setup
#
#######################################################################
$properties = $driver->get();
$properties->{shipType} = 'EXPRESS';
$driver->update($properties);
$xml = $driver->buildXML($cart, @shippableUnits);
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'EXPRESS',
Machinable => 'true',
},
],
}
},
'buildXML: EXPRESS service, 1 item in cart'
);
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order');
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '... _doXmlRequest to USPS successful');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Ounces => ignore(), Pounds => ignore(),
Size => ignore(), Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(30,30), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
);
}
#######################################################################
#
# Test PRIORITY VARIABLE shipping setup
#
#######################################################################
$properties = $driver->get();
$properties->{shipType} = 'PRIORITY VARIABLE';
$driver->update($properties);
$xml = $driver->buildXML($cart, @shippableUnits);
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PRIORITY',
Machinable => 'true',# Container => 'VARIABLE',
},
],
}
},
'buildXML: PRIORITY, VARIABLE service, 1 item in cart'
);
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order');
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '... _doXmlRequest to USPS successful');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Ounces => ignore(), Pounds => ignore(),
Size => ignore(), Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(8,8), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
);
} }
@ -818,7 +587,7 @@ SKIP: {
my $userId = $driver->get('userId'); my $userId = $driver->get('userId');
$properties = $driver->get(); $properties = $driver->get();
$properties->{userId} = '__NEVER_GOING_TO_HAPPEN__'; $properties->{userId} = '_NO_NO_NO_NO';
$driver->update($properties); $driver->update($properties);
$cost = eval { $driver->calculate($cart); }; $cost = eval { $driver->calculate($cart); };
@ -829,33 +598,17 @@ $properties->{userId} = $userId;
$driver->update($properties); $driver->update($properties);
my $dutchAddress = $addressBook->addAddress({ my $dutchAddress = $addressBook->addAddress({
label => 'dutch', label => 'american',
address1 => 'Rotterdamseweg 183C', organization => 'Plain Black Corporation',
city => 'Delft', code => '2629HD', address1 => '1360 Regent St. #145',
country => 'Netherlands', city => 'Madison', state => 'WI', code => '53715',
country => 'United States',
}); });
$cart->update({shippingAddressId => $dutchAddress->getId}); $cart->update({shippingAddressId => $dutchAddress->getId});
$cost = eval { $driver->calculate($cart); }; $cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught(); $e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', "calculate won't calculate for foreign countries"); isa_ok($e, 'WebGUI::Error::InvalidParam', "calculate won't calculate for domestic countries");
$cart->update({shippingAddressId => $workAddress->getId}); $cart->update({shippingAddressId => $workAddress->getId});
#<?xml version="1.0"?>
#<RateV3Response><Package ID="0"><Error><Number>-2147219500</Number>
#<Source>DomesticRatesV3;clsRateV3.ValidateWeight;RateEngineV3.ProcessRequest</Source>
#<Description>Please enter the package weight. </Description>
#<HelpFile></HelpFile><HelpContext>1000440</HelpContext></Error></Package></RateV3Response>
#######################################################################
#
# _calculateInsurance edge case
#
#######################################################################
$cart->empty;
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $gospels));
@shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 1, '_calculateInsurance: calculates insurance using the first bin');