fix zip parsing for batch results - don't want to abort processing because of an...
[freeside.git] / FS / FS / cust_pay_batch.pm
index 515bb9a..6e261c6 100644 (file)
@@ -3,10 +3,11 @@ package FS::cust_pay_batch;
 use strict;
 use vars qw( @ISA $DEBUG );
 use FS::Record qw(dbh qsearch qsearchs);
+use FS::payinfo_Mixin;
 use FS::part_bill_event qw(due_events);
 use Business::CreditCard 0.28;
 
-@ISA = qw( FS::Record );
+@ISA = qw( FS::Record FS::payinfo_Mixin );
 
 # 1 is mostly method/subroutine entry and options
 # 2 traces progress of some operations
@@ -115,7 +116,7 @@ returns the error, otherwise returns false.
 
 Checks all fields to make sure this is a valid transaction.  If there is
 an error, returns the error, otherwise returns false.  Called by the insert
-and repalce methods.
+and replace methods.
 
 =cut
 
@@ -124,7 +125,7 @@ sub check {
 
   my $error = 
       $self->ut_numbern('paybatchnum')
-    || $self->ut_numbern('trancode') #depriciated
+    || $self->ut_numbern('trancode') #deprecated
     || $self->ut_money('amount')
     || $self->ut_number('invnum')
     || $self->ut_number('custnum')
@@ -142,11 +143,7 @@ sub check {
   $self->first =~ /^([\w \,\.\-\']+)$/ or return "Illegal first name";
   $self->first($1);
 
-  $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP|CASH|WEST|MCRD)$/
-    or return "Illegal payby";
-  $self->payby($1);
-
-  $error = FS::payby::payinfo_check($self->payby, \$self->payinfo);
+  $error = $self->payinfo_check();
   return $error if $error;
 
   if ( $self->exp eq '' ) {
@@ -177,15 +174,16 @@ sub check {
     $self->payname($1);
   }
 
-  #$self->zip =~ /^\s*(\w[\w\-\s]{3,8}\w)\s*$/
-  #  or return "Illegal zip: ". $self->zip;
-  #$self->zip($1);
+  #we have lots of old zips in there... don't hork up batch results cause of em
+  $self->zip =~ /^\s*(\w[\w\-\s]{3,8}\w)\s*$/
+    or return "Illegal zip: ". $self->zip;
+  $self->zip($1);
 
   $self->country =~ /^(\w\w)$/ or return "Illegal country: ". $self->country;
   $self->country($1);
 
-  $error = $self->ut_zip('zip', $self->country);
-  return $error if $error;
+  #$error = $self->ut_zip('zip', $self->country);
+  #return $error if $error;
 
   #check invnum, custnum, ?
 
@@ -346,6 +344,59 @@ sub import_results {
     };
 
 
+  }elsif ( $format eq 'csv-chase_canada-E-xactBatch' ) {
+
+    $filetype = "CSV";
+
+    @fields = (
+      '',            # Internal(bank) id of the transaction
+      '',            # Transaction Type:  00 - purchase,      01 - preauth,
+                     #                    02 - completion,    03 - forcepost,
+                     #                    04 - refund,        05 - auth,
+                     #                    06 - purchase corr, 07 - refund corr,
+                     #                    08 - void           09 - void return
+      '',            # gateway used to process this transaction
+      'paid',        # Amount:  Amount of the transaction.  Dollars and cents
+                     #          with decimal entered.
+      'auth',        # Auth#:  Authorization number (if approved)
+      'payinfo',     # Card Number:  Card number for the transaction
+      '',            # Expiry Date:  Expiry date of the card
+      '',            # Cardholder Name
+      'bankcode',    # Bank response code (3 alphanumeric)
+      'bankmess',    # Bank response message
+      'etgcode',     # ETG response code (2 alphanumeric)
+      'etgmess',     # ETG response message
+      '',            # Returned customer number for the transaction
+      'paybatchnum', # Reference#:  paybatch number of the transaction
+      '',            # Reference#:  Invoice number of the transaction
+      'result',      # Processing Result: Approved of Declined
+    );
+
+    $end_condition = sub {
+      '';
+    };
+
+    $hook = sub {
+      my $hash = shift;
+      my $cpb = shift;
+      $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'}); #hmmmm
+      $hash->{'_date'} = time;  # got a better one?
+      $hash->{'payinfo'} = $cpb->{'payinfo'}
+        if( substr($hash->{'payinfo'}, -4) eq substr($cpb->{'payinfo'}, -4) );
+    };
+
+    $approved_condition = sub {
+      my $hash = shift;
+      $hash->{'etgcode'} eq '00' && $hash->{'result'} eq "Approved";
+    };
+
+    $declined_condition = sub {
+      my $hash = shift;
+      $hash->{'etgcode'} ne '00' # internal processing error
+        || ( $hash->{'result'} eq "Declined" );
+    };
+
+
   }elsif ( $format eq 'PAP' ) {
 
     $filetype = "Fixed264";
@@ -480,7 +531,7 @@ sub import_results {
 
     my $new_cust_pay_batch = new FS::cust_pay_batch { $cust_pay_batch->hash };
 
-    &{$hook}(\%hash);
+    &{$hook}(\%hash, $cust_pay_batch->hashref);
 
     if ( &{$approved_condition}(\%hash) ) {