s/JetPay/ippay/ per new spec
[Business-OnlinePayment-IPPay.git] / IPPay.pm
index ab063cb..4d9dbd5 100644 (file)
--- a/IPPay.pm
+++ b/IPPay.pm
@@ -11,7 +11,7 @@ use Business::OnlinePayment::HTTPS;
 use vars qw($VERSION $DEBUG @ISA $me);
 
 @ISA = qw(Business::OnlinePayment::HTTPS);
-$VERSION = '0.05_02';
+$VERSION = '0.11_01';
 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
 
 $DEBUG = 0;
@@ -19,7 +19,7 @@ $me = 'Business::OnlinePayment::IPPay';
 
 sub _info {
   {
-    'info_version'          => '0.01',
+    'info_compat'           => '0.01',
     'module_version'        => $VERSION,
     'supported_types'       => [ qw( CC ECHECK ) ],
     'supported_actions'     => { 'CC' => [
@@ -28,6 +28,7 @@ sub _info {
                                      'Post Authorization',
                                      'Void',
                                      'Credit',
+                                     'Reverse Authorization',
                                    ],
                                    'ECHECK' => [
                                      'Normal Authorization',
@@ -45,20 +46,17 @@ sub set_defaults {
     my %opts = @_;
 
     # standard B::OP methods/data
-    $self->server('gateway17.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
                      ));
 
-    # module specific data
-    if ( $opts{debug} ) {
-        $self->debug( $opts{debug} );
-        delete $opts{debug};
-    }
+    $DEBUG = exists($opts{debug}) ? $opts{debug} : 0;
 
+    # module specific data
     my %_defaults = ();
     foreach my $key (keys %opts) {
       $key =~ /^default_(\w*)$/ or next;
@@ -89,6 +87,7 @@ sub map_fields {
       ( 'normal authorization'            => 'SALE',
         'authorization only'              => 'AUTHONLY',
         'post authorization'              => 'CAPT',
+        'reverse authorization'           => 'REVERSEAUTH',
         'void'                            => 'VOID',
         'credit'                          => 'CREDIT',
       );
@@ -97,20 +96,25 @@ 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'   => 'CHECKING',
+                           'business savings'    => 'SAVINGS',
+                           #not technically B:OP valid i guess?
+                           'checking'            => 'CHECKING',
+                           'savings'             => 'SAVINGS',
+                          );
+      $content{'account_type'} = $account_types{lc($content{'account_type'})}
+                                 || $content{'account_type'};
+    }
 
     $content{Origin} = 'RECURRING' 
       if ($content{recurring_billing} &&$content{recurring_billing} eq 'YES' );
@@ -170,7 +174,7 @@ sub submit {
   $self->is_success(0);
   $self->map_fields();
 
-  my @required_fields = qw(action login type);
+  my @required_fields = qw(action login password type);
 
   my $action = lc($self->{_content}->{action});
   my $type = $self->transaction_type();
@@ -189,6 +193,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 );
 
@@ -209,14 +215,20 @@ sub submit {
   foreach ( keys ( %{($self->{_defaults})} ) ) {
     $content{$_} = $self->{_defaults}->{$_} unless exists($content{$_});
   }
+  if ($self->test_transaction()) {
+    $content{'login'} = 'TESTTERMINAL';
+    $self->server('testgtwy.ippay.com') if $self->server eq 'gtwy.ippay.com';
+  }
   $self->content(%content);
 
   $self->required_fields(@required_fields);
 
-  if ($self->test_transaction()) {
-    $self->server('test1.jetpay.com');
-    $self->port('443');
-    $self->path('/jetpay');
+  #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'};
@@ -226,7 +238,7 @@ sub submit {
          "(HTTPS headers: ".
          join(", ", map { "$_ => ". $headers{$_} } keys %headers ). ") ".
          "(Raw HTTPS content: $page)"
-      if $DEBUG;
+      if $DEBUG > 1;
     return unless $server_response=~ /^200/;
     $transaction_id = $page;
   }
@@ -271,6 +283,8 @@ sub submit {
 
   tie my %ach, 'Tie::IxHash',
     $self->revmap_fields(
+                          #wtf, this is a "Type"" attribute of the ACH element,
+                          # not a child element like the others
                           #AccountType         => 'account_type',
                           AccountNumber       => 'account_number',
                           ABA                 => 'routing_code',
@@ -301,6 +315,7 @@ sub submit {
                             Phone               => 'phone',
                           );
   }
+  delete $shippingaddr{Country} unless $shippingaddr{Country};
 
   tie my %shippinginfo, 'Tie::IxHash',
     $self->revmap_fields(
@@ -345,15 +360,16 @@ sub submit {
                           BillingCountry      => \$country,
                           BillingPhone        => 'phone',
                           Email               => 'email',
-                          UserIPAddr          => 'customer_ip',
+                          UserIPAddress       => 'customer_ip',
                           UserHost            => 'UserHost',
                           UDField1            => 'UDField1',
                           UDField2            => 'UDField2',
-                          UDField3            => 'UDField3',
+                          UDField3            => \"$me $VERSION", #'UDField3',
                           ActionCode          => 'ActionCode',
                           IndustryInfo        => \%industryinfo,
                           ShippingInfo        => \%shippinginfo,
                         );
+  delete $req{BillingCountry} unless $req{BillingCountry};
 
   my $post_data;
   my $writer = new XML::Writer( OUTPUT      => \$post_data,
@@ -362,18 +378,18 @@ sub submit {
                                 ENCODING    => 'us-ascii',
                               );
   $writer->xmlDecl();
-  $writer->startTag('JetPay');
+  $writer->startTag('ippay');
   foreach ( keys ( %req ) ) {
     $self->_xmlwrite($writer, $_, $req{$_});
   }
-  $writer->endTag('JetPay');
+  $writer->endTag('ippay');
   $writer->end();
 
-  warn "$post_data\n" if $DEBUG;
+  warn "$post_data\n" if $DEBUG > 1;
 
   my ($page,$server_response,%headers) = $self->https_post($post_data);
 
-  warn "$page\n" if $DEBUG;
+  warn "$page\n" if $DEBUG > 1;
 
   my $response = {};
   if ($server_response =~ /^200/){
@@ -381,7 +397,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");
@@ -396,21 +412,49 @@ sub submit {
   $self->is_success($self->result_code() eq '000' ? 1 : 0);
 
   unless ($self->is_success()) {
-    unless ( $self->error_message() ) { #additional logging information
-      $self->error_message(
-        "(HTTPS response: $server_response) ".
-        "(HTTPS headers: ".
-          join(", ", map { "$_ => ". $headers{$_} } keys %headers ). ") ".
-        "(Raw HTTPS content: $page)"
-      );
+    unless ( $self->error_message() ) {
+      if ( $DEBUG ) {
+        #additional logging information, possibly too sensitive for an error msg
+        # (IPPay seems to have a failure mode where they return the full
+        #  original request including card number)
+        $self->error_message(
+          "(HTTPS response: $server_response) ".
+          "(HTTPS headers: ".
+            join(", ", map { "$_ => ". $headers{$_} } keys %headers ). ") ".
+          "(Raw HTTPS content: $page)"
+        );
+      } else {
+        $self->error_message('No ResponseText or ErrMsg was returned by IPPay (enable debugging for raw HTTPS response)');
+      }
     }
   }
 
 }
 
+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);
+
+  my %att = ();
+  if ( $item eq 'ACH' ) {
+    $att{'Type'} = $self->{_content}->{'account_type'}
+      if $self->{_content}->{'account_type'}; #necessary so we don't pass empty?
+    $att{'SEC'}  = $self->{_content}->{'nacha_sec_code'}
+                 || ( $att{'Type'} =~ /business/i ? 'CCD' : 'PPD' );
+  }
+
+  $writer->startTag($item, %att);
+
   if ( ref( $value ) eq 'HASH' ) {
     foreach ( keys ( %$value ) ) {
       $self->_xmlwrite($writer, $_, $value->{$_});
@@ -418,6 +462,7 @@ sub _xmlwrite {
   }else{
     $writer->characters($value);
   }
+
   $writer->endTag($item);
 }
 
@@ -500,6 +545,7 @@ The following actions are valid
 
   normal authorization
   authorization only
+  reverse authorization
   post authorization
   credit
   void
@@ -544,11 +590,10 @@ from content(%content):
       BillingCountry      => 'country',           # forced to ISO-3166-alpha-3
       BillingPhone        => 'phone',
       Email               => 'email',
-      UserIPAddr          => 'customer_ip',
+      UserIPAddress        => 'customer_ip',
       UserHost            => 'UserHost',
       UDField1            => 'UDField1',
       UDField2            => 'UDField2',
-      UDField3            => 'UDField3',
       ActionCode          => 'ActionCode',
       IndustryInfo
         Type                => 'IndustryInfo',
@@ -567,14 +612,38 @@ from content(%content):
 
 =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>
+
+Reverse Authorization patch from dougforpres
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (c) 1999 Jason Kohles
+Copyright (c) 2002-2003 Ivan Kohler
+Copyright (c) 2008-2021 Freeside Internet Services, Inc.
+
+All rights reserved. This program is free software; you can redistribute it
+and/or modify it under the same terms as Perl itself.
+
+=head1 ADVERTISEMENT
+
+Need a complete, open-source back-office and customer self-service solution?
+The Freeside software includes support for credit card and electronic check
+processing with IPPay and over 50 other gateways, invoicing, integrated
+trouble ticketing, and customer signup and self-service web interfaces.
 
-Jeff Finucane, ippay@weasellips.com
+http://freeside.biz/freeside/
 
 =head1 SEE ALSO