0.06: fix january bug in tests
[Business-OnlinePayment-IPPay.git] / IPPay.pm
index f544718..59c3e78 100644 (file)
--- a/IPPay.pm
+++ b/IPPay.pm
@@ -5,15 +5,41 @@ use Carp;
 use Tie::IxHash;
 use XML::Simple;
 use XML::Writer;
+use Locale::Country;
 use Business::OnlinePayment;
 use Business::OnlinePayment::HTTPS;
 use vars qw($VERSION $DEBUG @ISA $me);
 
 @ISA = qw(Business::OnlinePayment::HTTPS);
-$VERSION = '0.02';
-$DEBUG = 1;
+$VERSION = '0.06';
+$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',
+                                   ],
+                                   'ECHECK' => [
+                                     'Normal Authorization',
+                                     'Void',
+                                     'Credit',
+                                   ],
+                                 },
+    'CC_void_requires_card' => 1,
+    'ECHECK_void_requires_account' => 1,
+  };
+}
+
 sub set_defaults {
     my $self = shift;
     my %opts = @_;
@@ -187,6 +213,20 @@ sub submit {
 
   $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;
+    }
+  }
+
+  if ($self->test_transaction()) {
+    $self->server('test1.jetpay.com');
+    $self->port('443');
+    $self->path('/jetpay');
+  }
+
   my $transaction_id = $content{'order_number'};
   unless ($transaction_id) {
     my ($page, $server_response, %headers) = $self->https_get('dummy' => 1);
@@ -216,6 +256,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',
@@ -234,7 +295,7 @@ sub submit {
                           Address             => 'ship_address',
                           City                => 'ship_city',
                           StateProv           => 'ship_state',
-                          Country             => 'ship_country',
+                          Country             => \$ship_country,
                           Phone               => 'ship_phone',
                         );
 
@@ -244,10 +305,11 @@ sub submit {
                             Address             => 'address',
                             City                => 'city',
                             StateProv           => 'state',
-                            Country             => 'country',
+                            Country             => \$country,
                             Phone               => 'phone',
                           );
   }
+  delete $shippingaddr{Country} unless $shippingaddr{Country};
 
   tie my %shippinginfo, 'Tie::IxHash',
     $self->revmap_fields(
@@ -289,7 +351,7 @@ sub submit {
                           BillingCity         => 'city',
                           BillingStateProv    => 'state',
                           BillingPostalCode   => 'zip',
-                          BillingCountry      => 'country',
+                          BillingCountry      => \$country,
                           BillingPhone        => 'phone',
                           Email               => 'email',
                           UserIPAddr          => 'customer_ip',
@@ -301,6 +363,7 @@ sub submit {
                           IndustryInfo        => \%industryinfo,
                           ShippingInfo        => \%shippinginfo,
                         );
+  delete $req{BillingCountry} unless $req{BillingCountry};
 
   my $post_data;
   my $writer = new XML::Writer( OUTPUT      => \$post_data,
@@ -316,12 +379,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);
@@ -361,6 +418,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);
@@ -375,6 +443,7 @@ sub _xmlwrite {
 }
 
 1;
+
 __END__
 
 =head1 NAME
@@ -493,7 +562,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',
@@ -512,7 +581,7 @@ 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