force country to ISO-3166-alpha-3
[Business-OnlinePayment-IPPay.git] / IPPay.pm
index 03eaefa..9c35919 100644 (file)
--- a/IPPay.pm
+++ b/IPPay.pm
@@ -5,16 +5,13 @@ 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.02';
+$VERSION = '0.04';
 $DEBUG = 0;
 $me = 'Business::OnlinePayment::IPPay';
 
@@ -191,9 +188,20 @@ sub submit {
 
   $self->required_fields(@required_fields);
 
+  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);
+    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 +223,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,7 +262,7 @@ sub submit {
                           Address             => 'ship_address',
                           City                => 'ship_city',
                           StateProv           => 'ship_state',
-                          Country             => 'ship_country',
+                          Country             => \$ship_country,
                           Phone               => 'ship_phone',
                         );
 
@@ -243,7 +272,7 @@ sub submit {
                             Address             => 'address',
                             City                => 'city',
                             StateProv           => 'state',
-                            Country             => 'country',
+                            Country             => \$country,
                             Phone               => 'phone',
                           );
   }
@@ -288,7 +317,7 @@ sub submit {
                           BillingCity         => 'city',
                           BillingStateProv    => 'state',
                           BillingPostalCode   => 'zip',
-                          BillingCountry      => 'country',
+                          BillingCountry      => \$country,
                           BillingPhone        => 'phone',
                           Email               => 'email',
                           UserIPAddr          => 'customer_ip',
@@ -315,12 +344,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);
@@ -492,7 +515,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',
@@ -511,7 +534,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