Rename taxes() to tax_amounts()
[Business-OnlinePayment-InternetSecure.git] / InternetSecure.pm
index 10d7d6e..cb6d00e 100755 (executable)
@@ -42,9 +42,12 @@ sub set_defaults {
                                receipt_number  sales_number    uuid    guid
                                date
                                card_type       cardholder
-                               total_amount
+                               total_amount    tax_amounts
                                avs_response    cvv2_response
                        ));
+       
+       # Just in case someone tries to call tax_amounts() *before* submit()
+       $self->tax_amounts( {} );
 }
 
 # OnlinePayment's get_fields now filters out undefs in 3.x. :(
@@ -105,9 +108,16 @@ sub prod_string {
 
        my @flags = ($currency);
 
-       foreach (split ' ' => uc($data{taxes} || '')) {
-               croak "Unknown tax code $_" unless /^(GST|PST|HST)$/;
-               push @flags, $_;
+       my @taxes;
+       if (ref $data{taxes}) {
+               @taxes = @{ $data{taxes} };
+       } elsif ($data{taxes}) {
+               @taxes = split ' ' => $data{taxes};
+       }
+
+       foreach (@taxes) {
+               croak "Unknown tax code $_" unless /^(GST|PST|HST)$/i;
+               push @flags, uc $_;
        }
 
        if ($self->test_transaction) {
@@ -143,8 +153,6 @@ sub to_xml {
        croak "Unknown currency code ", $content{currency}
                unless $content{currency} =~ /^(CAD|USD)$/;
        
-       $content{taxes} = uc($content{taxes} || '');
-
        my %data = $self->get_remap_fields(qw(
                        xxxCard_Number          card_number
 
@@ -223,6 +231,27 @@ sub infuse {
        }
 }
 
+sub extract_tax_amounts {
+       my ($self, $response) = @_;
+
+       my %tax_amounts;
+
+       my $products = $response->{Products};
+       return unless $products;
+
+       foreach my $node (@$products) {
+               my $flags = $node->{flags};
+               if ($flags &&
+                       grep($_ eq '{TAX}', @$flags) &&
+                       grep($_ eq '{CALCULATED}', @$flags))
+               {
+                       $tax_amounts{ $node->{code} } = $node->{subtotal};
+               }
+       }
+
+       return %tax_amounts;
+}
+
 # Parse the server's response and set various fields
 #
 sub parse_response {
@@ -267,7 +296,7 @@ sub parse_response {
        
        $self->card_type(CARD_TYPES->{$self->card_type});
        
-       $self->{products_raw} = $response->{Products};
+       $self->tax_amounts( { $self->extract_tax_amounts($response) } );
 
        return $self;
 }
@@ -441,10 +470,12 @@ C<CAD> (default) or C<USD>.
 
 =item taxes
 
-Taxes to be added automatically to B<amount> by InternetSecure.
+Taxes to be added automatically to B<amount> by InternetSecure.  Available
+taxes are C<GST>, C<PST> and C<HST>.
 
-Available taxes are C<GST>, C<PST> and C<HST>.  Multiple taxes can specified
-by concatenating them with spaces, such as C<GST HST>.
+This argument can either be a single string of taxes concatenated with spaces
+(such as C<GST PST>), or a reference to an array of taxes (such as C<[ "GST",
+"PST" ]>).
 
 =item name / company / address / city / state / zip / country / phone / email
 
@@ -513,6 +544,12 @@ Date and time of the transaction.  Format is C<YYYY/MM/DD hh:mm:ss>.
 
 Total amount billed for this order, including taxes.
 
+=item tax_amounts()
+
+Returns a I<reference> to a hash that maps taxes, which were listed under the
+B<taxes> argument to B<submit>(), to the amount that was calculated by
+InternetSecure.
+
 =item cardholder()
 
 Cardholder's name.  This is currently a mere copy of the B<name> field passed
@@ -535,10 +572,6 @@ following:
 
 =back
 
-=item products_raw()
-
-...
-
 
 =back