gateway URL change
[Business-OnlinePayment-IPPay.git] / IPPay.pm
index d9a0624..5d97655 100644 (file)
--- a/IPPay.pm
+++ b/IPPay.pm
@@ -5,27 +5,50 @@ use Carp;
 use Tie::IxHash;
 use XML::Simple;
 use XML::Writer;
-use LWP::UserAgent;
-use HTTP::Request;
-use HTTP::Request::Common qw (POST);
-use Date::Calc qw(Add_Delta_YM Add_Delta_Days);
+use Locale::Country;
 use Business::OnlinePayment;
 use Business::OnlinePayment::HTTPS;
 use vars qw($VERSION $DEBUG @ISA $me);
 
 @ISA = qw(Business::OnlinePayment::HTTPS);
-$VERSION = '0.01';
-$DEBUG = 1;
+$VERSION = '0.07';
+$VERSION = eval $VERSION; # modperlstyle: convert the string into a number
+
+$DEBUG = 0;
 $me = 'Business::OnlinePayment::IPPay';
 
+sub _info {
+  {
+    'info_version'          => '0.01',
+    'module_version'        => $VERSION,
+    'supported_types'       => [ qw( CC ECHECK ) ],
+    'supported_actions'     => { 'CC' => [
+                                     'Normal Authorization',
+                                     'Authorization Only',
+                                     'Post Authorization',
+                                     'Void',
+                                     'Credit',
+                                     'Reverse Authorization',
+                                   ],
+                                   'ECHECK' => [
+                                     'Normal Authorization',
+                                     'Void',
+                                     'Credit',
+                                   ],
+                                 },
+    'CC_void_requires_card' => 1,
+    'ECHECK_void_requires_account' => 1,
+  };
+}
+
 sub set_defaults {
     my $self = shift;
     my %opts = @_;
 
     # standard B::OP methods/data
-    $self->server('test1.jetpay.com') unless $self->server;
+    $self->server('gtwy.ippay.com') unless $self->server;
     $self->port('443') unless $self->port;
-    $self->path('/jetpay') unless $self->path;
+    $self->path('/ippay') unless $self->path;
 
     $self->build_subs(qw( order_number avs_code cvv2_response
                           response_page response_code response_headers
@@ -67,6 +90,7 @@ sub map_fields {
       ( 'normal authorization'            => 'SALE',
         'authorization only'              => 'AUTHONLY',
         'post authorization'              => 'CAPT',
+        'reverse authorization'           => 'REVERSEAUTH',
         'void'                            => 'VOID',
         'credit'                          => 'CREDIT',
       );
@@ -75,20 +99,21 @@ sub map_fields {
         'void'                            => 'VOIDACH',
         'credit'                          => 'REVERSAL',
       );
+
     if ($self->transaction_type eq 'CC') {
       $content{'TransactionType'} = $actions{$action} || $action;
-    }elsif ($self->transaction_type eq 'ECHECK') {
-      $content{'TransactionType'} = $check_actions{$action} || $action;
-    }
+    } elsif ($self->transaction_type eq 'ECHECK') {
 
+      $content{'TransactionType'} = $check_actions{$action} || $action;
 
-    # ACCOUNT TYPE MAP
-    my %account_types = ('personal checking'   => 'Checking',
-                         'personal savings'    => 'Savings',
-                         'business checking'   => 'BusinessCk',
-                        );
-    $content{'account_type'} = $account_types{lc($content{'account_type'})}
-                               || $content{'account_type'};
+      # ACCOUNT TYPE MAP
+      my %account_types = ('personal checking'   => 'Checking',
+                           'personal savings'    => 'Savings',
+                           'business checking'   => 'BusinessCk',
+                          );
+      $content{'account_type'} = $account_types{lc($content{'account_type'})}
+                                 || $content{'account_type'};
+    }
 
     $content{Origin} = 'RECURRING' 
       if ($content{recurring_billing} &&$content{recurring_billing} eq 'YES' );
@@ -167,6 +192,8 @@ sub submit {
         
   }elsif ( $action eq 'post authorization' && $type eq 'CC') {
     push @required_fields, qw( order_number );
+  }elsif ( $action eq 'reverse authorization' && $type eq 'CC') {
+    push @required_fields, qw( order_number card_number expiration amount );
   }elsif ( $action eq 'void') {
     push @required_fields, qw( order_number amount );
 
@@ -187,13 +214,29 @@ sub submit {
   foreach ( keys ( %{($self->{_defaults})} ) ) {
     $content{$_} = $self->{_defaults}->{$_} unless exists($content{$_});
   }
+  if ($self->test_transaction()) {
+    $content{'login'} = 'TESTTERMINAL';
+  }
   $self->content(%content);
 
   $self->required_fields(@required_fields);
 
+  #quick validation because ippay dumps an error indecipherable to the end user
+  if (grep { /^routing_code$/ } @required_fields) {
+    unless( $content{routing_code} =~ /^\d{9}$/ ) {
+      $self->_error_response('Invalid routing code');
+      return;
+    }
+  }
+
   my $transaction_id = $content{'order_number'};
   unless ($transaction_id) {
     my ($page, $server_response, %headers) = $self->https_get('dummy' => 1);
+    warn "fetched transaction id: (HTTPS response: $server_response) ".
+         "(HTTPS headers: ".
+         join(", ", map { "$_ => ". $headers{$_} } keys %headers ). ") ".
+         "(Raw HTTPS content: $page)"
+      if $DEBUG;
     return unless $server_response=~ /^200/;
     $transaction_id = $page;
   }
@@ -215,6 +258,27 @@ sub submit {
   my $terminalid = $content{login} if $type eq 'CC';
   my $merchantid = $content{login} if $type eq 'ECHECK';
 
+  my $country = country2code( $content{country}, LOCALE_CODE_ALPHA_3 );
+  $country  = country_code2code( $content{country},
+                                 LOCALE_CODE_ALPHA_2,
+                                 LOCALE_CODE_ALPHA_3
+                               )
+    unless $country;
+  $country = $content{country}
+    unless $country;
+  $country = uc($country) if $country;
+
+  my $ship_country =
+    country2code( $content{ship_country}, LOCALE_CODE_ALPHA_3 );
+  $ship_country  = country_code2code( $content{ship_country},
+                                 LOCALE_CODE_ALPHA_2,
+                                 LOCALE_CODE_ALPHA_3
+                               )
+    unless $ship_country;
+  $ship_country = $content{ship_country}
+    unless $ship_country;
+  $ship_country = uc($ship_country) if $ship_country;
+
   tie my %ach, 'Tie::IxHash',
     $self->revmap_fields(
                           #AccountType         => 'account_type',
@@ -233,10 +297,22 @@ sub submit {
                           Address             => 'ship_address',
                           City                => 'ship_city',
                           StateProv           => 'ship_state',
-                          Country             => 'ship_country',
+                          Country             => \$ship_country,
                           Phone               => 'ship_phone',
                         );
 
+  unless ( $type ne 'CC' || keys %shippingaddr ) {
+    tie %shippingaddr, 'Tie::IxHash',
+      $self->revmap_fields(
+                            Address             => 'address',
+                            City                => 'city',
+                            StateProv           => 'state',
+                            Country             => \$country,
+                            Phone               => 'phone',
+                          );
+  }
+  delete $shippingaddr{Country} unless $shippingaddr{Country};
+
   tie my %shippinginfo, 'Tie::IxHash',
     $self->revmap_fields(
                           CustomerPO          => 'CustomerPO',
@@ -277,7 +353,7 @@ sub submit {
                           BillingCity         => 'city',
                           BillingStateProv    => 'state',
                           BillingPostalCode   => 'zip',
-                          BillingCountry      => 'country',
+                          BillingCountry      => \$country,
                           BillingPhone        => 'phone',
                           Email               => 'email',
                           UserIPAddr          => 'customer_ip',
@@ -289,6 +365,7 @@ sub submit {
                           IndustryInfo        => \%industryinfo,
                           ShippingInfo        => \%shippinginfo,
                         );
+  delete $req{BillingCountry} unless $req{BillingCountry};
 
   my $post_data;
   my $writer = new XML::Writer( OUTPUT      => \$post_data,
@@ -304,12 +381,6 @@ sub submit {
   $writer->endTag('JetPay');
   $writer->end();
 
-  if ($self->test_transaction()) {
-    $self->server('test1.jetpay.com');
-    $self->port('443');
-    $self->path('/jetpay');
-  }
-
   warn "$post_data\n" if $DEBUG;
 
   my ($page,$server_response,%headers) = $self->https_post($post_data);
@@ -322,7 +393,7 @@ sub submit {
     if (  exists($response->{ActionCode}) && !exists($response->{ErrMsg})) {
       $self->error_message($response->{ResponseText});
     }else{
-      $self->error_message($response->{Errmsg});
+      $self->error_message($response->{ErrMsg});
     }
 #  }else{
 #    $self->error_message("Server Failed");
@@ -349,6 +420,17 @@ sub submit {
 
 }
 
+sub _error_response {
+  my ($self, $error_message) = (shift, shift);
+  $self->result_code('');
+  $self->order_number('');
+  $self->authorization('');
+  $self->cvv2_response('');
+  $self->avs_code('');
+  $self->is_success( 0);
+  $self->error_message($error_message);
+}
+
 sub _xmlwrite {
   my ($self, $writer, $item, $value) = @_;
   $writer->startTag($item);
@@ -363,6 +445,7 @@ sub _xmlwrite {
 }
 
 1;
+
 __END__
 
 =head1 NAME
@@ -440,6 +523,7 @@ The following actions are valid
 
   normal authorization
   authorization only
+  reverse authorization
   post authorization
   credit
   void
@@ -481,7 +565,7 @@ from content(%content):
       BillingCity         => 'city',
       BillingStateProv    => 'state',
       BillingPostalCode   => 'zip',
-      BillingCountry      => 'country',
+      BillingCountry      => 'country',           # forced to ISO-3166-alpha-3
       BillingPhone        => 'phone',
       Email               => 'email',
       UserIPAddr          => 'customer_ip',
@@ -500,21 +584,27 @@ from content(%content):
           Address             => 'ship_address',
           City                => 'ship_city',
           StateProv           => 'ship_state',
-          Country             => 'ship_country',
+          Country             => 'ship_country',  # forced to ISO-3166-alpha-3
           Phone               => 'ship_phone',
 
 =head1 NOTE
 
 =head1 COMPATIBILITY
 
+Version 0.07 changes the server name and path for IPPay's late 2012 update.
+
 Business::OnlinePayment::IPPay uses IPPay XML Product Specifications version
 1.1.2.
 
 See http://www.ippay.com/ for more information.
 
-=head1 AUTHOR
+=head1 AUTHORS
+
+Original author: Jeff Finucane
+
+Current maintainer: Ivan Kohler <ivan-ippay@freeside.biz>
 
-Jeff Finucane, ippay@weasellips.com
+Reverse Authorization patch from dougforpres
 
 =head1 SEE ALSO