failure_status support; better handling of error messages
[Business-BatchPayment-Paymentech.git] / Paymentech.pm
index 027bbee..19277cc 100644 (file)
@@ -94,7 +94,6 @@ sub default_transport {
   Business::BatchPayment::Paymentech::Transport->new(
     login     => $self->login,
     password  => $self->password,
-    put_path  => $self->fileDateTime,
     debug     => $self->debug,
     test_mode => $self->test_mode,
   );
@@ -132,7 +131,8 @@ sub format_header {
   $xml->startTag('batchFileID');
   $xml->dataElement(userID => $self->login);
   $xml->dataElement(fileDateTime => $self->fileDateTime);
-  $xml->dataElement(fileID => $self->fileDateTime);
+  $xml->dataElement(fileID => sprintf('%06d-', $batch->batch_id) . 
+                              $self->fileDateTime);
   $xml->endTag('batchFileID');
 }
 
@@ -240,13 +240,47 @@ sub parse_item {
     second  => $sec,
   );
 
+  my %failure_status = (
+    # API version 2.6, April 2013
+    '00'  => undef,       # Approved
+    '04'  => 'pickup',
+    '33'  => 'expired',
+    '41'  => 'stolen',
+    '42'  => 'inactive',
+    '43'  => 'stolen',
+    '44'  => 'inactive',
+    '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
+  ); # all others are "decline"
+
+  my $failure_status = undef;
+  my $error_message;
+
+  if ( $resp->{procStatus} ) {
+    $error_message = $resp->{procStatusMessage};
+  } elsif ( $resp->{respCode} ) {
+    $error_message = $resp->{respCodeMessage};
+    $failure_status = $failure_status{ $resp->{respCode} } || 'decline';
+  } else {
+    $error_message = '';
+  }
+
   my $item = Business::BatchPayment->create(Item =>
     tid           => $resp->{orderID},
     process_date  => $dt,
     authorization => $resp->{authorizationCode},
     order_number  => $resp->{txRefNum},
     approved      => ($resp->{approvalStatus} == 1),
-    error_message => $resp->{procStatusMessage},
+    error_message => $error_message,
+    failure_status  => $failure_status,
   );
   $item;
 }
@@ -286,7 +320,8 @@ sub upload {
   my $self = shift;
   my $content = shift;
   my $tmpdir = tempdir( CLEANUP => 1 );
-  my $filename = $self->put_path; # also the value of the fileId tag
+  $content =~ /<fileID>(.*)<\/fileID>/;
+  my $filename = $1;
   my $archive_dir = $self->archive_to;
 
   warn "Writing temp file to $tmpdir/$filename.xml.\n" if $self->debug;