From a5c6520a56103786a4cd5c3383176eaa5ae69fd4 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 29 Oct 2014 18:53:47 -0400 Subject: [PATCH] Update USPS ShipDriver to V4. Fixes bug #12455 --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Shop/ShipDriver/USPS.pm | 21 ++-- t/Shop/ShipDriver/USPS.t | 154 ++++++++++++++++++++--------- 3 files changed, 121 insertions(+), 55 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index d5faf9c74..f05257cb6 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -1,5 +1,6 @@ 7.10.29 - fixed #12454: Email to Group ignores the From field + - fixed #12455: USPS Ship driver fails 7.10.28 - fixed: Clearing the caches for groups that don't exist, but have created group objects. diff --git a/lib/WebGUI/Shop/ShipDriver/USPS.pm b/lib/WebGUI/Shop/ShipDriver/USPS.pm index 7335c2d4a..698bee70e 100644 --- a/lib/WebGUI/Shop/ShipDriver/USPS.pm +++ b/lib/WebGUI/Shop/ShipDriver/USPS.pm @@ -48,16 +48,17 @@ result by the quantity, rather than doing several identical checks. sub buildXML { my ($self, $cart, @packages) = @_; tie my %xmlHash, 'Tie::IxHash'; - %xmlHash = ( RateV3Request => {}, ); - my $xmlTop = $xmlHash{RateV3Request}; + %xmlHash = ( RateV4Request => {}, ); + my $xmlTop = $xmlHash{RateV4Request}; $xmlTop->{USERID} = $self->get('userId'); $xmlTop->{Package} = []; ##Do a request for each package. my $packageIndex; my $shipType = $self->get('shipType'); - my $service = $shipType eq 'PRIORITY VARIABLE' - ? 'PRIORITY' - : $shipType; + my $service = $shipType eq 'PRIORITY VARIABLE' ? 'PRIORITY' + : $shipType eq 'PARCEL' ? 'STANDARD POST' + : $shipType + ; my $sourceZip = $self->get('sourceZip'); $sourceZip =~ s/^(\d{5}).*$/$1/; PACKAGE: for(my $packageIndex = 0; $packageIndex < scalar @packages; $packageIndex++) { @@ -92,8 +93,8 @@ sub buildXML { if ($shipType eq 'PRIORITY') { $packageData{Container} = [ 'FLAT RATE BOX' ]; } - elsif ($shipType eq 'PRIORITY VARIABLE') { - #$packageData{Container} = [ 'VARIABLE' ]; + else { + $packageData{Container} = [ 'VARIABLE' ]; } $packageData{Size} = [ 'REGULAR' ]; $packageData{Machinable} = [ 'true' ]; @@ -178,7 +179,7 @@ Processed XML data from an XML rate request, processed in perl data structure. have this structure: { - RateV3Response => { + RateV4Response => { Package => [ { ID => 0, @@ -199,7 +200,7 @@ The set of shippable units, which are required to do quantity lookups. sub _calculateFromXML { my ($self, $xmlData, @shippableUnits) = @_; my $cost = 0; - foreach my $package (@{ $xmlData->{RateV3Response}->{Package} }) { + foreach my $package (@{ $xmlData->{RateV4Response}->{Package} }) { my $id = $package->{ID}; my $rate = $package->{Postage}->{Rate}; ##Error check for invalid index @@ -397,7 +398,7 @@ sub _doXmlRequest { $userAgent->env_proxy; $userAgent->agent('WebGUI'); $userAgent->timeout('45'); - my $url = 'http://production.shippingapis.com/ShippingAPI.dll?API=RateV3&XML='; + my $url = 'http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML='; $url .= $xml; my $request = HTTP::Request->new(GET => $url); my $response = $userAgent->request($request); diff --git a/t/Shop/ShipDriver/USPS.t b/t/Shop/ShipDriver/USPS.t index cbc1034ba..061a15a51 100644 --- a/t/Shop/ShipDriver/USPS.t +++ b/t/Shop/ShipDriver/USPS.t @@ -25,7 +25,7 @@ use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; use WebGUI::Shop::ShipDriver::USPS; -plan tests => 69; +plan tests => 73; #---------------------------------------------------------------------------- # Init @@ -325,7 +325,7 @@ $driver->update($properties); is($driver->_calculateInsurance(@shippableUnits), 0, '_calculateInsurance: returns 0 if rates are not set'); my $xml = $driver->buildXML($cart, @shippableUnits); -like($xml, qr/ { + RateV4Request => { USERID => $userId, Package => [ { ID => 0, ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0', - Size => 'REGULAR', Service => 'PARCEL', - Machinable => 'true', + Size => 'REGULAR', Service => 'STANDARD POST', + Machinable => 'true', Container => 'VARIABLE', }, ], } @@ -351,7 +351,7 @@ cmp_deeply( 'buildXML: PARCEL service, 1 item in cart' ); -like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order'); +like($xml, qr/RateV4Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order'); SKIP: { @@ -369,7 +369,7 @@ SKIP: { ZipOrigination => ignore(), ZipDestination => ignore(), Machinable => ignore(), Ounces => ignore(), Pounds => ignore(), Size => ignore(), - Zone => ignore(), Container => {}, + Zone => ignore(), Container => 'VARIABLE', Postage => { CLASSID => ignore(), MailService => ignore(), @@ -379,13 +379,13 @@ SKIP: { ], }, '... returned data from USPS in correct format. If this test fails, the driver may need to be updated' - ); + ) or diag $xml."\n".$response->content; } my $cost = $driver->_calculateFromXML( { - RateV3Response => { + RateV4Response => { Package => [ { ID => 0, @@ -415,22 +415,22 @@ $xmlData = XMLin( $xml, cmp_deeply( $xmlData, { - RateV3Request => { + RateV4Request => { USERID => $userId, Package => [ { ID => 0, ZipDestination => '53715', ZipOrigination => '97123', Pounds => '2', Ounces => '0.0', - Size => 'REGULAR', Service => 'PARCEL', - Machinable => 'true', + Size => 'REGULAR', Service => 'STANDARD POST', + Machinable => 'true', Container => 'VARIABLE', }, { ID => 1, ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0', - Size => 'REGULAR', Service => 'PARCEL', - Machinable => 'true', + Size => 'REGULAR', Service => 'STANDARD POST', + Machinable => 'true', Container => 'VARIABLE', }, ], } @@ -454,7 +454,7 @@ SKIP: { ZipOrigination => ignore(), ZipDestination => ignore(), Machinable => ignore(), Ounces => '0.0', Pounds => 2, Size => ignore(), - Zone => ignore(), Container => {}, + Zone => ignore(), Container => ignore(), Postage => { CLASSID => ignore(), MailService => ignore(), @@ -466,7 +466,7 @@ SKIP: { ZipOrigination => ignore(), ZipDestination => ignore(), Machinable => ignore(), Ounces => '8.0', Pounds => 1, Size => ignore(), - Zone => ignore(), Container => {}, + Zone => ignore(), Container => ignore(), Postage => { CLASSID => ignore(), MailService => ignore(), @@ -482,7 +482,7 @@ SKIP: { $cost = $driver->_calculateFromXML( { - RateV3Response => { + RateV4Response => { Package => [ { ID => 0, @@ -511,7 +511,7 @@ is(calculateInsurance($driver), 8, '_calculateInsurance: two items in cart with $cost = $driver->_calculateFromXML( { - RateV3Response => { + RateV4Response => { Package => [ { ID => 0, @@ -546,29 +546,29 @@ $xmlData = XMLin( $xml, cmp_deeply( $xmlData, { - RateV3Request => { + RateV4Request => { USERID => $userId, Package => [ { ID => 0, ZipDestination => '53715', ZipOrigination => '97123', Pounds => '2', Ounces => '0.0', - Size => 'REGULAR', Service => 'PARCEL', - Machinable => 'true', + Size => 'REGULAR', Service => 'STANDARD POST', + Machinable => 'true', Container => 'VARIABLE', }, { ID => 1, ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0', - Size => 'REGULAR', Service => 'PARCEL', - Machinable => 'true', + Size => 'REGULAR', Service => 'STANDARD POST', + Machinable => 'true', Container => 'VARIABLE', }, { ID => 2, ZipDestination => '53703', ZipOrigination => '97123', Pounds => '12', Ounces => '0.0', - Size => 'REGULAR', Service => 'PARCEL', - Machinable => 'true', + Size => 'REGULAR', Service => 'STANDARD POST', + Machinable => 'true', Container => 'VARIABLE', }, ], } @@ -592,7 +592,7 @@ SKIP: { ZipOrigination => ignore(), ZipDestination => ignore(), Machinable => ignore(), Ounces => '0.0', Pounds => 2, Size => ignore(), - Zone => ignore(), Container => {}, + Zone => ignore(), Container => ignore(), Postage => { CLASSID => ignore(), MailService => ignore(), @@ -604,7 +604,7 @@ SKIP: { ZipOrigination => ignore(), ZipDestination => ignore(), Machinable => ignore(), Ounces => '8.0', Pounds => 1, Size => ignore(), - Zone => ignore(), Container => {}, + Zone => ignore(), Container => ignore(), Postage => { CLASSID => ignore(), MailService => ignore(), @@ -616,7 +616,7 @@ SKIP: { ZipOrigination => ignore(), ZipDestination => 53703, Machinable => ignore(), Ounces => '0.0', Pounds => 12, Size => ignore(), - Zone => ignore(), Container => {}, + Zone => ignore(), Container => ignore(), Postage => { CLASSID => ignore(), MailService => ignore(), @@ -631,7 +631,7 @@ SKIP: { } my $xmlData = XMLin(q{ -971235371520.0REGULARTRUE7Parcel Post7.62971235371518.0REGULARTRUE7Parcel Post7.629712353703120.0REGULARTRUE7Parcel Post16.67 +971235371520.0REGULARTRUE7Parcel Post7.62971235371518.0REGULARTRUE7Parcel Post7.629712353703120.0REGULARTRUE7Parcel Post16.67 }, KeepRoot => 1, ForceArray => [qw/Package/],); my $cost = $driver->_calculateFromXML($xmlData, @shippableUnits); @@ -658,7 +658,7 @@ my $xmlData = XMLin($xml, cmp_deeply( $xmlData, { - RateV3Request => { + RateV4Request => { USERID => $userId, Package => [ { @@ -673,7 +673,7 @@ cmp_deeply( }, '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'); +like($xml, qr/RateV4Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Container.+?Size.+?Machinable/, '... and tag order'); SKIP: { @@ -723,7 +723,7 @@ my $xmlData = XMLin($xml, cmp_deeply( $xmlData, { - RateV3Request => { + RateV4Request => { USERID => $userId, Package => [ { @@ -731,14 +731,14 @@ cmp_deeply( ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0', Size => 'REGULAR', Service => 'EXPRESS', - Machinable => 'true', + Machinable => 'true', Container => 'VARIABLE', }, ], } }, 'buildXML: EXPRESS service, 1 item in cart' ); -like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order'); +like($xml, qr/RateV4Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order'); SKIP: { @@ -752,7 +752,7 @@ SKIP: { { Package => [ { - ID => 0, Container => {}, + ID => 0, Container => ignore(), ZipOrigination => ignore(), ZipDestination => ignore(), Ounces => ignore(), Pounds => ignore(), Size => ignore(), Zone => ignore(), @@ -787,7 +787,7 @@ my $xmlData = XMLin($xml, cmp_deeply( $xmlData, { - RateV3Request => { + RateV4Request => { USERID => $userId, Package => [ { @@ -795,14 +795,14 @@ cmp_deeply( ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0', Size => 'REGULAR', Service => 'PRIORITY', - Machinable => 'true',# Container => 'VARIABLE', + 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'); +like($xml, qr/RateV4Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order'); SKIP: { @@ -816,7 +816,7 @@ SKIP: { { Package => [ { - ID => 0, Container => {}, + ID => 0, Container => 'VARIABLE', ZipOrigination => ignore(), ZipDestination => ignore(), Ounces => ignore(), Pounds => ignore(), Size => ignore(), Zone => ignore(), @@ -829,7 +829,71 @@ SKIP: { ], }, '... returned data from USPS in correct format. If this test fails, the driver may need to be updated' - ); + ) or diag $xml."\n".$response->content; + +} + +####################################################################### +# +# Test PRIORITY shipping setup +# +####################################################################### + +$properties = $driver->get(); +$properties->{shipType} = 'PRIORITY'; +$driver->update($properties); + +$xml = $driver->buildXML($cart, @shippableUnits); +my $xmlData = XMLin($xml, + KeepRoot => 1, + ForceArray => ['Package'], +); +cmp_deeply( + $xmlData, + { + RateV4Request => { + 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, VARIABLE service, 1 item in cart' +); +like($xml, qr/RateV4Request 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, Container => 'FLAT RATE BOX', + 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' + ) or diag $xml."\n".$response->content; } @@ -847,7 +911,7 @@ my $xmlData = XMLin($driver->buildXML($cart, @shippableUnits), cmp_deeply( $xmlData, { - RateV3Request => { + RateV4Request => { USERID => $userId, Package => [ { @@ -855,7 +919,7 @@ cmp_deeply( ZipDestination => '53715', ZipOrigination => '97123', Pounds => '1', Ounces => '8.0', Size => 'REGULAR', Service => 'PRIORITY', - Machinable => 'true',# Container => 'VARIABLE', + Machinable => 'true', Container => 'FLAT RATE BOX', }, ], } @@ -908,10 +972,10 @@ isa_ok($e, 'WebGUI::Error::InvalidParam', "calculate won't calculate for foreign $cart->update({shippingAddressId => $workAddress->getId}); # -#-2147219500 -#DomesticRatesV3;clsRateV3.ValidateWeight;RateEngineV3.ProcessRequest +#-2147219500 +#DomesticRatesV4;clsRateV4.ValidateWeight;RateEngineV4.ProcessRequest #Please enter the package weight. -#1000440 +#1000440 ####################################################################### #