X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcust_pay_batch.pm;h=8059f1ca2e0be297d297ae2fbcd064bb87d5c984;hp=97c168159632349ddfaaf34968fae36ecadadc4a;hb=d20581bcbf2809d5c2969d773b16a0c8714a6dec;hpb=c8f80bd5aaeb0f3844a7cece4bfe250d4f89f745 diff --git a/FS/FS/cust_pay_batch.pm b/FS/FS/cust_pay_batch.pm index 97c168159..8059f1ca2 100644 --- a/FS/FS/cust_pay_batch.pm +++ b/FS/FS/cust_pay_batch.pm @@ -188,6 +188,18 @@ sub check { $self->SUPER::check; } +=item cust_main + +Returns the customer (see L) for this batched credit card +payment. + +=cut + +sub cust_main { + my $self = shift; + qsearchs( 'cust_main', { 'custnum' => $self->custnum } ); +} + =back =head1 SUBROUTINES @@ -210,8 +222,11 @@ sub import_results { my $paybatch = $param->{'paybatch'}; my @fields; - my $condition; + my $end_condition; + my $end_hook; my $hook; + my $approved_condition; + my $declined_condition; if ( $format eq 'csv-td_canada_trust-merchant_pc_batch' ) { @@ -236,9 +251,18 @@ sub import_results { '', # Terminal ID: Terminal ID used to process the transaction ); - $condition = sub { + $end_condition = sub { my $hash = shift; - $hash->{'result'} == 3 && $hash->{'type'} == 0; + $hash->{'type'} eq '0BC'; + }; + + $end_hook = sub { + my( $hash, $total) = @_; + $total = sprintf("%.2f", $total); + my $batch_total = sprintf("%.2f", $hash->{'paybatchnum'} / 100 ); + return "Our total $total does not match bank total $batch_total!" + if $total != $batch_total; + ''; }; $hook = sub { @@ -252,6 +276,18 @@ sub import_results { substr($hash->{'_date'}, 0, 4)-1900, ); }; + $approved_condition = sub { + my $hash = shift; + $hash->{'type'} eq '0' && $hash->{'result'} == 3; + }; + + $declined_condition = sub { + my $hash = shift; + $hash->{'type'} eq '0' && ( $hash->{'result'} == 4 + || $hash->{'result'} == 5 ); + }; + + } else { return "Unknown format $format"; } @@ -269,9 +305,12 @@ sub import_results { local $FS::UID::AutoCommit = 0; my $dbh = dbh; + my $total = 0; my $line; while ( defined($line=<$fh>) ) { + next if $line =~ /^\s*$/; #skip blank lines + $csv->parse($line) or do { $dbh->rollback if $oldAutoCommit; return "can't parse: ". $csv->error_input(); @@ -285,6 +324,15 @@ sub import_results { $hash{$field} = $value; } + if ( &{$end_condition}(\%hash) ) { + my $error = &{$end_hook}(\%hash, $total); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + last; + } + my $cust_pay_batch = qsearchs('cust_pay_batch', { 'paybatchnum' => $hash{'paybatchnum'} } ); unless ( $cust_pay_batch ) { @@ -299,23 +347,31 @@ sub import_results { return "error removing paybatchnum $hash{'paybatchnum'}: $error\n"; } - next unless &{$condition}(\%hash); - &{$hook}(\%hash); - my $cust_pay = new FS::cust_pay ( { - 'custnum' => $custnum, - 'payby' => 'CARD', - 'paybatch' => $paybatch, - map { $_ => $hash{$_} } (qw( paid _date payinfo )), - } ); - $error = $cust_pay->insert; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return "error adding payment paybatchnum $hash{'paybatchnum'}: $error\n"; - } + if ( &{$approved_condition}(\%hash) ) { + + my $cust_pay = new FS::cust_pay ( { + 'custnum' => $custnum, + 'payby' => 'CARD', + 'paybatch' => $paybatch, + map { $_ => $hash{$_} } (qw( paid _date payinfo )), + } ); + $error = $cust_pay->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "error adding payment paybatchnum $hash{'paybatchnum'}: $error\n"; + } + $total += $hash{'paid'}; + + $cust_pay->cust_main->apply_payments; - $cust_pay->cust_main->apply_payments; + } elsif ( &{$declined_condition}(\%hash) ) { + + #this should be configurable... if anybody else ever uses batches + $cust_pay_batch->cust_main->suspend; + + } }