update URLs from paymentech.net to chasepaymentech.com
[Business-OnlinePayment-PaymenTech.git] / lib / Business / OnlinePayment / PaymenTech.pm
index 8ce9a1e..30d9824 100644 (file)
@@ -8,7 +8,10 @@ use Tie::IxHash;
 use vars qw($VERSION $DEBUG @ISA $me);
 
 @ISA = qw(Business::OnlinePayment::HTTPS);
-$VERSION = '2.03_01';
+
+$VERSION = '2.07';
+$VERSION = eval $VERSION; # modperlstyle: convert the string into a number
+
 $DEBUG = 0;
 $me='Business::OnlinePayment::PaymenTech';
 
@@ -21,52 +24,53 @@ my %request_header = (
 ); # Content-Type has to be passed separately
 
 tie my %new_order, 'Tie::IxHash', (
-  OrbitalConnectionUsername => ':login',
-  OrbitalConnectionPassword => ':password',
-  IndustryType              => 'EC', # Assume industry = Ecommerce
-  MessageType               => ':message_type',
-  BIN                       => ':bin',
-  MerchantID                => ':merchant_id',
-  TerminalID                => ':terminal_id',
-  CardBrand                 => '',
-  AccountNum                => ':card_number',
-  Exp                       => ':expiration',
-  CurrencyCode              => ':currency_code',
-  CurrencyExponent          => ':currency_exp',
-  CardSecValInd             => ':cvvind',
-  CardSecVal                => ':cvv2',
-  AVSzip                    => ':zip',
-  AVSaddress1               => ':address',
-  AVScity                   => ':city',
-  AVSstate                  => ':state',
-  OrderID                   => ':invoice_number',
-  Amount                    => ':amount',
-  Comments                  => ':email', # as per B:OP:WesternACH
-  TxRefNum                  => ':order_number', # used only for Refund
+  OrbitalConnectionUsername => [ ':login', 32 ],
+  OrbitalConnectionPassword => [ ':password', 32 ],
+  IndustryType              => [ 'EC', 2 ],
+  MessageType               => [ ':message_type', 2 ],
+  BIN                       => [ ':bin', 6 ],
+  MerchantID                => [ ':merchant_id', 12 ],
+  TerminalID                => [ ':terminal_id', 3 ],
+  CardBrand                 => [ '', 2 ], 
+  AccountNum                => [ ':card_number', 19 ],
+  Exp                       => [ ':expiration', 4 ],
+  CurrencyCode              => [ ':currency_code', 3 ],
+  CurrencyExponent          => [ ':currency_exp', 6 ],
+  CardSecValInd             => [ ':cvvind', 1 ],
+  CardSecVal                => [ ':cvv2', 4 ],
+  AVSzip                    => [ ':zip', 10 ],
+  AVSaddress1               => [ ':address', 30 ],
+  AVScity                   => [ ':city', 20 ],
+  AVSstate                  => [ ':state', 2 ],
+  AVScountryCode            => [ ':country', 2 ],
+  OrderID                   => [ ':invoice_number', 22 ], 
+  Amount                    => [ ':amount', 12 ],
+  Comments                  => [ ':email', 64 ],
+  TxRefNum                  => [ ':order_number', 40 ],# used only for Refund
 );
 
 tie my %mark_for_capture, 'Tie::IxHash', (
-  OrbitalConnectionUsername => ':login',
-  OrbitalConnectionPassword => ':password',
-  OrderID                   => ':invoice_number',
-  Amount                    => ':amount',
-  BIN                       => ':bin',
-  MerchantID                => ':merchant_id',
-  TerminalID                => ':terminal_id',
-  TxRefNum                  => ':order_number',
+  OrbitalConnectionUsername => [ ':login', 32 ],
+  OrbitalConnectionPassword => [ ':password', 32 ],
+  OrderID                   => [ ':invoice_number', 22 ],
+  Amount                    => [ ':amount', 12 ],
+  BIN                       => [ ':bin', 6 ],
+  MerchantID                => [ ':merchant_id', 12 ],
+  TerminalID                => [ ':terminal_id', 3 ],
+  TxRefNum                  => [ ':order_number', 40 ],
 );
 
 tie my %reversal, 'Tie::IxHash', (
-  OrbitalConnectionUsername => ':login',
-  OrbitalConnectionPassword => ':password',
-  TxRefNum                  => ':order_number',
-  TxRefIdx                  => 0,
-  OrderID                   => ':invoice_number',
-  BIN                       => ':bin',
-  MerchantID                => ':merchant_id',
-  TerminalID                => ':terminal_id',
+  OrbitalConnectionUsername => [ ':login', 32 ],
+  OrbitalConnectionPassword => [ ':password', 32 ],
+  TxRefNum                  => [ ':order_number', 40 ],
+  TxRefIdx                  => [ '0', 4 ],
+  OrderID                   => [ ':invoice_number', 22 ],
+  BIN                       => [ ':bin', 6 ],
+  MerchantID                => [ ':merchant_id', 12 ],
+  TerminalID                => [ ':terminal_id', 3 ],
+  OnlineReversalInd         => [ 'Y', 1 ],
 # Always attempt to reverse authorization.
-  OnlineReversalInd         => 'Y',
 );
 
 my %defaults = (
@@ -93,25 +97,54 @@ my %currency_code = (
   MXN => [484, 2],
 );
 
+my %paymentech_countries = map { $_ => 1 } qw( US CA GB UK );
+my %failure_status = (
+  # values of the RespCode element
+  # in theory RespMsg should be set to a descriptive message, but it looks
+  # like that's not reliable
+  # XXX we should have a way to indicate other actions required by the 
+  # processor, such as "honor with identification", "call for instructions",
+  # etc.
+  '00'  => undef,         # Approved
+  '04'  => 'pickup',      # Pickup
+  '33'  => 'expired',     # Card is Expired
+  '41'  => 'stolen',      # Lost/Stolen
+  '42'  => 'inactive',    # Account Not Active
+  '43'  => 'stolen',      # Lost/Stolen Card
+  '44'  => 'inactive',    # Account Not Active
+  #'45' duplicate transaction, should also have its own status
+  'B7'  => 'blacklisted', # Fraud
+  'B9'  => 'blacklisted', # On Negative File
+  'BB'  => 'stolen',      # Possible Compromise
+  'BG'  => 'blacklisted', # Blocked Account
+  'BQ'  => 'blacklisted', # Issuer has Flagged Account as Suspected Fraud
+  'C4'  => 'nsf',         # Over Credit Limit
+  'D5'  => 'blacklisted', # On Negative File
+  'D7'  => 'nsf',         # Insufficient Funds
+  'F3'  => 'inactive',    # Account Closed
+  'K6'  => 'nsf',         # NSF
+);
+
 sub set_defaults {
     my $self = shift;
 
-    $self->server('orbitalvar1.paymentech.net') unless $self->server; # this is the test server.
+    $self->server('orbitalvar1.chasepaymentech.com') unless $self->server; # this is the test server.
     $self->port('443') unless $self->port;
     $self->path('/authorize') unless $self->path;
 
     $self->build_subs(qw( 
       order_number
-      ProcStatus 
-      ApprovalStatus 
-      StatusMsg 
-      Response
-      RespCode
-      AuthCode
-      AVSRespCode
-      CVV2RespCode
-     ));
-
+    ));
+
+    #leaking gateway-specific anmes?  need to be mapped to B:OP standards :)
+    # ProcStatus 
+    # ApprovalStatus 
+    # StatusMsg 
+    # RespCode
+    # AuthCode
+    # AVSRespCode
+    # CVV2RespCode
+    # Response
 }
 
 sub build {
@@ -122,7 +155,8 @@ sub build {
   ref($skel) eq 'HASH' or die 'Tried to build non-hash';
   foreach my $k (keys(%$skel)) {
     my $v = $skel->{$k};
-    # Not recursive like B:OP:WesternACH; Paymentech requests are only one layer deep.
+    my $l;
+    ($v, $l) = @$v if(ref $v eq 'ARRAY');
     if($v =~ /^:(.*)/) {
       # Get the content field with that name.
       $data{$k} = $content{$1};
@@ -130,6 +164,8 @@ sub build {
     else {
       $data{$k} = $v;
     }
+    # Ruthlessly enforce field length.
+    $data{$k} = substr($data{$k}, 0, $l) if($data{$k} and $l);
   }
   return \%data;
 }
@@ -186,7 +222,15 @@ sub map_fields {
       $content{invoice_number} ||= sprintf("%04x%04x",time % 2**16,int(rand() * 2**16));
     }
 
-    $content{expiration} =~ s/\D//g; # Because Freeside sends it as mm/yy, not mmyy.
+    # Always send as MMYY
+    $content{expiration} =~ s/\D//g; 
+    $content{expiration} = sprintf('%04d',$content{expiration});
+
+    $content{country} ||= 'US';
+    $content{country} = ( $paymentech_countries{ $content{country} }
+                            ? $content{country}
+                            : ''
+                        ),
 
     $self->content(%content);
     return;
@@ -226,7 +270,7 @@ sub submit {
   my $post_data = XMLout({ Request => $request }, KeepRoot => 1, NoAttr => 1, NoSort => 1);
 
   if (!$self->test_transaction()) {
-    $self->server('orbital1.paymentech.net');
+    $self->server('orbital1.chasepaymentech.com');
   }
 
   warn $post_data if $DEBUG;
@@ -238,42 +282,69 @@ sub submit {
 
   warn $page if $DEBUG;
 
-  my $response;
-  my $error = '';
-  if ($server_response =~ /200/){
-    $response = XMLin($page, KeepRoot => 0);
-    $self->Response($response);
-    my ($r) = values(%$response);
-    foreach(qw(ProcStatus RespCode AuthCode AVSRespCode CVV2RespCode)) {
-      if(exists($r->{$_}) and
-         !ref($r->{$_})) {
-        $self->$_($r->{$_});
-      }
-    }
-    if(!exists($r->{'ProcStatus'})) {
-      $error = "Malformed response: '$page'";
-      $self->is_success(0);
-    }
-    elsif( $r->{'ProcStatus'} != 0 or 
-          # NewOrders get ApprovalStatus, Reversals don't.
-          ( exists($r->{'ApprovalStatus'}) ?
-            $r->{'ApprovalStatus'} != 1 :
-            $r->{'StatusMsg'} ne 'Approved' )
-          ) {
-      $error = "Transaction error: '". ($r->{'ProcStatusMsg'} || $r->{'StatusMsg'}) . "'";
+  my $response = XMLin($page, KeepRoot => 0);
+  #$self->Response($response);
+
+  #use Data::Dumper;
+  #warn Dumper($response) if $DEBUG;
+
+  my ($r) = values(%$response);
+  #foreach(qw(ProcStatus RespCode AuthCode AVSRespCode CVV2RespCode)) {
+  #  if(exists($r->{$_}) and
+  #     !ref($r->{$_})) {
+  #    $self->$_($r->{$_});
+  #  }
+  #}
+
+  foreach (keys %$r) {
+
+    #turn empty hashrefs into the empty string
+    $r->{$_} = '' if ref($r->{$_}) && ! keys %{ $r->{$_} };
+
+    #turn hashrefs with content into scalars
+    $r->{$_} = $r->{$_}{'content'}
+      if ref($r->{$_}) && exists($r->{$_}{'content'});
+  }
+
+  if ($server_response !~ /^200/) {
+
+    #$self->is_success(0);
+    my $error = "Server error: '$server_response'";
+    $error .= " / Transaction error: '".
+              ($r->{'ProcStatusMsg'} || $r->{'StatusMsg'}) . "'"
+      if $r->{'ProcStatus'} != 0;
+    #$self->error_message($error);
+    #overzealous?  are there "normal decline" transaction errors being returned?
+    die "$error\n";
+
+  } else {
+
+    die "Unable to parse response_page\n" if !exists($r->{'ProcStatus'});
+
+    if ( $r->{'ProcStatus'} != 0 or 
+              # NewOrders get ApprovalStatus, Reversals don't.
+              ( exists($r->{'ApprovalStatus'}) ?
+                $r->{'ApprovalStatus'} != 1 :
+                $r->{'StatusMsg'} ne 'Approved' )
+            )
+    {
+
+      $self->failure_status( $failure_status{ $r->{RespCode} } || 'decline' );
       $self->is_success(0);
-    }
-    else {
-      # success!
+      $self->error_message( "Transaction error: '".
+                            ($r->{'ProcStatusMsg'} || $r->{'StatusMsg'}) . "'"
+                          );
+
+    } else { # success!
+
       $self->is_success(1);
       # For credits, AuthCode is empty and gets converted to a hashref.
       $self->authorization($r->{'AuthCode'}) if !ref($r->{'AuthCode'});
       $self->order_number($r->{'TxRefNum'});
     }
-  } else {
-    $error = "Server error: '$server_response'";
+
   }
-  $self->error_message($error);
+
 }
 
 1;
@@ -313,11 +384,7 @@ Business::OnlinePayment::PaymenTech - Chase Paymentech backend for Business::Onl
 
 =head1 NOTES
 
-The only supported transaction types are Normal Authorization and Credit.  Paymentech 
-supports separate Authorize and Capture actions as well as recurring billing, but 
-those are not yet implemented.
-
-Electronic check processing is not yet supported.
+Electronic check processing and recurring billing are not yet supported.
 
 =head1 AUTHOR