Added taxes()
[Business-OnlinePayment-InternetSecure.git] / InternetSecure.pm
index e3a21e5..a7b1d0f 100755 (executable)
@@ -15,6 +15,8 @@ use base qw(Business::OnlinePayment Exporter);
 our $VERSION = '0.01';
 
 
+use constant SUCCESS_CODES => qw(2000 90000 900P1);
+
 use constant CARD_TYPES => {
                                VI => 'Visa',
                                MC => 'MasterCard',
@@ -40,9 +42,12 @@ sub set_defaults {
                                receipt_number  sales_number    uuid    guid
                                date
                                card_type       cardholder
-                               total_amount
+                               total_amount    taxes
                                avs_response    cvv2_response
                        ));
+       
+       # Just in case someone tries to call taxes() *before* submit()
+       $self->taxes( {} );
 }
 
 # OnlinePayment's get_fields now filters out undefs in 3.x. :(
@@ -221,6 +226,27 @@ sub infuse {
        }
 }
 
+sub extract_taxes {
+       my ($self, $response) = @_;
+
+       my %taxes;
+
+       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))
+               {
+                       $taxes{ $node->{code} } = $node->{subtotal};
+               }
+       }
+
+       return %taxes;
+}
+
 # Parse the server's response and set various fields
 #
 sub parse_response {
@@ -237,9 +263,6 @@ sub parse_response {
                        SuppressEmpty => undef,
                );
        
-       my $code = $self->result_code($response->{Page});
-       $self->is_success($code eq '2000' || $code eq '90000' || $code eq '900P1');
-
        $self->infuse($response,
                        result_code     => 'Page',
                        error_message   => 'Verbiage',
@@ -258,12 +281,17 @@ sub parse_response {
                        total_amount    => 'TotalAmount',
                        );
        
+       $self->is_success(scalar grep $self->result_code eq $_, SUCCESS_CODES);
+
        # Completely undocumented field that sometimes override <Verbiage>
        $self->error_message($response->{Error}) if $response->{Error};
+
+       # Delete error_message if transaction was successful
+       $self->error_message(undef) if $self->is_success;
        
        $self->card_type(CARD_TYPES->{$self->card_type});
        
-       $self->{products_raw} = $response->{Products};
+       $self->taxes( { $self->extract_taxes($response) } );
 
        return $self;
 }
@@ -469,9 +497,9 @@ Response code returned by InternetSecure.
 
 =item error_message()
 
-Text description of the response code.  (Do not rely on this to check for
-errors, as a description will also be returned for successful transactions.
-Use B<is_success>() instead.)
+Error message if the transaction was unsuccessful; C<undef> otherwise.  (You
+should not rely on this to test whether a transaction was successful; use
+B<is_success>() instead.)
 
 =item receipt_number()
 
@@ -509,6 +537,11 @@ Date and time of the transaction.  Format is C<YYYY/MM/DD hh:mm:ss>.
 
 Total amount billed for this order, including taxes.
 
+=item taxes()
+
+Returns a I<reference> to a hash that maps tax names (such as C<GST>) to the
+amount that was billed for each.
+
 =item cardholder()
 
 Cardholder's name.  This is currently a mere copy of the B<name> field passed
@@ -531,10 +564,6 @@ following:
 
 =back
 
-=item products_raw()
-
-...
-
 
 =back