diff --git a/lib/WebGUI/Shop/ShipDriver/USPS.pm b/lib/WebGUI/Shop/ShipDriver/USPS.pm
index a717d8daa..2ed5fd888 100644
--- a/lib/WebGUI/Shop/ShipDriver/USPS.pm
+++ b/lib/WebGUI/Shop/ShipDriver/USPS.pm
@@ -93,9 +93,6 @@ sub buildXML {
}
$packageData{Size} = [ 'REGULAR' ];
$packageData{Machinable} = [ 'true' ];
- if ($self->get('addInsurance')) {
- $packageData{ValueOfContents} = $cost;
- }
push @{ $xmlTop->{Package} }, \%packageData;
}
my $xml = XMLout(\%xmlHash,
@@ -158,6 +155,7 @@ sub calculate {
}
##Summarize costs from returned data
$cost = $self->_calculateFromXML($xmlData, @shippableUnits);
+ $cost += $self->_calculateInsurance(@shippableUnits);
return $cost;
}
@@ -215,6 +213,29 @@ sub _calculateFromXML {
#-------------------------------------------------------------------
+=head2 _calculateInsurance ( @shippableUnits )
+
+Takes data from the USPS and returns the calculated shipping price.
+
+=head3 @shippableUnits
+
+The set of shippable units, which are required to do quantity and cost lookups.
+
+=cut
+
+sub _calculateInsurance {
+ my ($self, @shippableUnits) = @_;
+ my $cost = 0;
+ return $cost unless $self-get('addInsurance') && $self->get('insuranceRates');
+ my @insuranceTable = map { my ($cost,$value) = split /:/, $_; [$cost, $value]; }
+ split /\r?\n/, $self->get('insuranceRates');
+ ##Sort by increasing value for easy post processing
+ my @insuranceTable = map { $_->[1] } sort { $a <=> $b } map { [ $_->[0], $_ ] } @insuranceTable;
+ return $cost;
+}
+
+#-------------------------------------------------------------------
+
=head2 definition ( $session )
This subroutine returns an arrayref of hashrefs, used to validate data put into
@@ -275,6 +296,12 @@ sub definition {
hoverHelp => $i18n->get('add insurance help'),
defaultValue => 0,
},
+ insuranceRates => {
+ fieldType => 'textarea',
+ label => $i18n->get('insurance rates'),
+ hoverHelp => $i18n->get('insurance rates help'),
+ defaultValue => "50:1.75\n100:2.25",
+ },
##Note, if a flat fee is added to this driver, then according to the license
##terms the website must display a note to the user (shop customer) that additional
##fees have been added.
diff --git a/lib/WebGUI/i18n/English/ShipDriver_USPS.pm b/lib/WebGUI/i18n/English/ShipDriver_USPS.pm
index e5d0fc6f6..b31b228bf 100644
--- a/lib/WebGUI/i18n/English/ShipDriver_USPS.pm
+++ b/lib/WebGUI/i18n/English/ShipDriver_USPS.pm
@@ -97,7 +97,7 @@ our $I18N = {
'add insurance' => {
message => q|Ship with insurance?|,
lastUpdated => 1253988886,
- context => q|Label for a type of shipping from the USPS.|,
+ context => q|Label for the edit screen.|,
},
'add insurance help' => {
@@ -106,6 +106,18 @@ our $I18N = {
context => q|Label for a type of shipping from the USPS.|,
},
+ 'insurance rates' => {
+ message => q|Insurance Rate Table|,
+ lastUpdated => 1253988886,
+ context => q|Label for the edit screen.|,
+ },
+
+ 'insurance rates help' => {
+ message => q|Enter in one field per line with the format, value:cost.
value is the value of the contents.
cost is the cost of insurance at that value.|,
+ lastUpdated => 1253988884,
+ context => q|Help for the insurance rate field.|,
+ },
+
};
1;
diff --git a/t/Shop/ShipDriver/USPS.t b/t/Shop/ShipDriver/USPS.t
index 1c8f5637f..6f8ad6cf7 100644
--- a/t/Shop/ShipDriver/USPS.t
+++ b/t/Shop/ShipDriver/USPS.t
@@ -757,63 +757,15 @@ SKIP: {
$properties = $driver->get();
$properties->{addInsurance} = 1;
+$properties->{insuranceTable} = <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',
- Size => 'REGULAR', Service => 'PRIORITY',
- Machinable => 'true', ValueOfContents => 7.50,
- },
- ],
- }
- },
- 'buildXML: contains ValueOfContents when insurance is requested'
-);
-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/],);
- diag $response->content;
-# 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'
-# );
-
-}
-
#----------------------------------------------------------------------------
# Cleanup