diff --git a/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm b/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm index e614bddb7..1a37d8b05 100644 --- a/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm +++ b/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm @@ -59,21 +59,26 @@ sub buildXML { next PACKAGE unless scalar @{ $package }; tie my %packageData, 'Tie::IxHash'; my $weight = 0; + my $value = 0; foreach my $item (@{ $package }) { my $sku = $item->getSku; my $itemWeight = $sku->getWeight(); + my $itemValue = $sku->getPrice(); ##Items that ship separately with a quantity > 1 are rate estimated as 1 item and then the ##shipping cost is multiplied by the quantity. if (! $sku->shipsSeparately ) { $itemWeight *= $item->get('quantity'); + $itemValue *= $item->get('quantity'); } $weight += $itemWeight; + $value += $itemValue; } my $pounds = int($weight); my $ounces = sprintf '%3.1f', (16 * ($weight - $pounds)); if ($pounds == 0 && $ounces eq '0.0' ) { $ounces = 0.1; } + $value = sprintf '%.2f', $value; my $destination = $package->[0]->getShippingAddress; my $country = $destination->get('country'); $packageData{ID} = $packageIndex; @@ -81,6 +86,9 @@ sub buildXML { $packageData{Ounces} = [ $ounces ]; $packageData{Machinable} = [ 'true' ]; $packageData{MailType} = [ 'Package' ]; + if ($self->get('addInsurance')) { + $packageData{ValueOfContents} = [ $value ]; + } $packageData{Country} = [ $country ]; push @{ $xmlTop->{Package} }, \%packageData; } @@ -138,6 +146,7 @@ sub calculate { WebGUI::Error::Shop::RemoteShippingRate->throw(error => 'Problem connecting to USPS Web Tools: '. $response->status_line); } my $returnedXML = $response->content; + #warn $returnedXML; my $xmlData = XMLin($returnedXML, KeepRoot => 1, ForceArray => [qw/Package/]); if (exists $xmlData->{Error}) { WebGUI::Error::Shop::RemoteShippingRate->throw(error => 'Problem with USPS Web Tools XML: '. $xmlData->{Error}->{Description}); @@ -194,6 +203,12 @@ sub _calculateFromXML { SERVICE: foreach my $service (@{ $package->{Service} }) { next SERVICE unless $service->{ID} eq $self->get('shipType'); $rate = $service->{Postage}; + if ($self->get('addInsurance')) { + if (exists $service->{InsComment}) { + WebGUI::Error::Shop::RemoteShippingRate->throw(error => "No insurance because of: ".$service->{InsComment}); + } + $rate += $service->{Insurance}; + } } if (!$rate) { WebGUI::Error::Shop::RemoteShippingRate->throw(error => 'Selected shipping service not available'); diff --git a/t/Shop/ShipDriver/USPSInternational.t b/t/Shop/ShipDriver/USPSInternational.t index 163c7aeee..8d24e5f17 100644 --- a/t/Shop/ShipDriver/USPSInternational.t +++ b/t/Shop/ShipDriver/USPSInternational.t @@ -25,7 +25,7 @@ use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; use WebGUI::Shop::ShipDriver::USPSInternational; -plan tests => 34; +plan tests => 37; #---------------------------------------------------------------------------- # Init @@ -340,7 +340,6 @@ SKIP: { }, '... returned data from USPS in correct format. If this test fails, the driver may need to be updated' ); - } my $cost = $driver->_calculateFromXML( @@ -579,6 +578,62 @@ SKIP: { } +####################################################################### +# +# Insurance +# +####################################################################### + +SKIP: { + + skip 'No userId for testing', 3 unless $hasRealUserId; + + + $cart->empty; + $properties = $driver->get(); + $properties->{shipType} = '9'; + $driver->update($properties); + $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer)); + + my $noInsuranceCost = $driver->calculate($cart); + + $properties->{addInsurance} = 1; + $driver->update($properties); + + @shippableUnits = $driver->_getShippableUnits($cart); + my $xml = $driver->buildXML($cart, @shippableUnits); + my $xmlData = XMLin($xml, + KeepRoot => 1, + ForceArray => ['Package'], + ); + + cmp_deeply( + $xmlData, + { + IntlRateRequest => { + USERID => $userId, + Package => [ + { + ID => 0, + Pounds => '12', Ounces => '0.0', + Machinable => 'true', Country => 'Netherlands', + MailType => 'Package', ValueOfContents => '19.99', + }, + ], + } + }, + 'buildXML: 1 item in cart' + ); + like($xml, qr/IntlRateRequest USERID.+?Package ID=.+?Pounds.+?Ounces.+?Machinable.+?MailType.+?ValueOfContents.+?Country.+?/, '... and tag order'); + + my $insuredCost = $driver->calculate($cart); + cmp_ok $noInsuranceCost, '<', $insuredCost, 'insured cost is higher than uninsured cost'; + + $properties->{addInsurance} = 0; + $driver->update($properties); + +} + ####################################################################### # # Check for throwing an exception