7 use FS::Record qw( dbh qsearch qsearchs );
9 use FS::part_bill_event qw(due_events);
11 @ISA = qw(FS::Record);
15 FS::pay_batch - Object methods for pay_batch records
21 $record = new FS::pay_batch \%hash;
22 $record = new FS::pay_batch { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::pay_batch object represents an example. FS::pay_batch inherits from
35 FS::Record. The following fields are currently supported:
39 =item batchnum - primary key
41 =item payby - CARD or CHEK
43 =item status - O (Open), I (In-transit), or R (Resolved)
58 Creates a new example. To add the example to the database, see L<"insert">.
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to. You can ask the object for a copy with the I<hash> method.
65 # the new method can be inherited from FS::Record, if a table method is defined
67 sub table { 'pay_batch'; }
71 Adds this record to the database. If there is an error, returns the error,
72 otherwise returns false.
76 # the insert method can be inherited from FS::Record
80 Delete this record from the database.
84 # the delete method can be inherited from FS::Record
86 =item replace OLD_RECORD
88 Replaces the OLD_RECORD with this one in the database. If there is an error,
89 returns the error, otherwise returns false.
93 # the replace method can be inherited from FS::Record
97 Checks all fields to make sure this is a valid example. If there is
98 an error, returns the error, otherwise returns false. Called by the insert
103 # the check method should currently be supplied - FS::Record contains some
104 # data checking routines
110 $self->ut_numbern('batchnum')
111 || $self->ut_enum('payby', [ 'CARD', 'CHEK' ])
112 || $self->ut_enum('status', [ 'O', 'I', 'R' ])
114 return $error if $error;
133 $self->status(shift);
134 $self->download(time)
135 if $self->status eq 'I' && ! $self->download;
137 if $self->status eq 'R' && ! $self->upload;
141 =item import_results OPTION => VALUE, ...
143 Import batch results.
147 I<filehandle> - open filehandle of results file.
149 I<format> - "csv-td_canada_trust-merchant_pc_batch", "csv-chase_canada-E-xactBatch", "ach-spiritone", or "PAP"
156 my $param = ref($_[0]) ? shift : { @_ };
157 my $fh = $param->{'filehandle'};
158 my $format = $param->{'format'};
160 my $filetype; # CSV, Fixed80, Fixed264
162 my $formatre; # for Fixed.+
168 my $approved_condition;
169 my $declined_condition;
171 if ( $format eq 'csv-td_canada_trust-merchant_pc_batch' ) {
176 'paybatchnum', # Reference#: Invoice number of the transaction
177 'paid', # Amount: Amount of the transaction. Dollars and cents
178 # with no decimal entered.
179 '', # Card Type: 0 - MCrd, 1 - Visa, 2 - AMEX, 3 - Discover,
180 # 4 - Insignia, 5 - Diners/EnRoute, 6 - JCB
181 '_date', # Transaction Date: Date the Transaction was processed
182 'time', # Transaction Time: Time the transaction was processed
183 'payinfo', # Card Number: Card number for the transaction
184 '', # Expiry Date: Expiry date of the card
185 '', # Auth#: Authorization number entered for force post
187 'type', # Transaction Type: 0 - purchase, 40 - refund,
189 'result', # Processing Result: 3 - Approval,
190 # 4 - Declined/Amount over limit,
191 # 5 - Invalid/Expired/stolen card,
193 '', # Terminal ID: Terminal ID used to process the transaction
196 $end_condition = sub {
198 $hash->{'type'} eq '0BC';
202 my( $hash, $total) = @_;
203 $total = sprintf("%.2f", $total);
204 my $batch_total = sprintf("%.2f", $hash->{'paybatchnum'} / 100 );
205 return "Our total $total does not match bank total $batch_total!"
206 if $total != $batch_total;
212 $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
213 $hash->{'_date'} = timelocal( substr($hash->{'time'}, 4, 2),
214 substr($hash->{'time'}, 2, 2),
215 substr($hash->{'time'}, 0, 2),
216 substr($hash->{'_date'}, 6, 2),
217 substr($hash->{'_date'}, 4, 2)-1,
218 substr($hash->{'_date'}, 0, 4)-1900, );
221 $approved_condition = sub {
223 $hash->{'type'} eq '0' && $hash->{'result'} == 3;
226 $declined_condition = sub {
228 $hash->{'type'} eq '0' && ( $hash->{'result'} == 4
229 || $hash->{'result'} == 5 );
233 }elsif ( $format eq 'csv-chase_canada-E-xactBatch' ) {
238 '', # Internal(bank) id of the transaction
239 '', # Transaction Type: 00 - purchase, 01 - preauth,
240 # 02 - completion, 03 - forcepost,
241 # 04 - refund, 05 - auth,
242 # 06 - purchase corr, 07 - refund corr,
243 # 08 - void 09 - void return
244 '', # gateway used to process this transaction
245 'paid', # Amount: Amount of the transaction. Dollars and cents
246 # with decimal entered.
247 'auth', # Auth#: Authorization number (if approved)
248 'payinfo', # Card Number: Card number for the transaction
249 '', # Expiry Date: Expiry date of the card
250 '', # Cardholder Name
251 'bankcode', # Bank response code (3 alphanumeric)
252 'bankmess', # Bank response message
253 'etgcode', # ETG response code (2 alphanumeric)
254 'etgmess', # ETG response message
255 '', # Returned customer number for the transaction
256 'paybatchnum', # Reference#: paybatch number of the transaction
257 '', # Reference#: Invoice number of the transaction
258 'result', # Processing Result: Approved of Declined
261 $end_condition = sub {
268 $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'}); #hmmmm
269 $hash->{'_date'} = time; # got a better one?
270 $hash->{'payinfo'} = $cpb->{'payinfo'}
271 if( substr($hash->{'payinfo'}, -4) eq substr($cpb->{'payinfo'}, -4) );
274 $approved_condition = sub {
276 $hash->{'etgcode'} eq '00' && $hash->{'result'} eq "Approved";
279 $declined_condition = sub {
281 $hash->{'etgcode'} ne '00' # internal processing error
282 || ( $hash->{'result'} eq "Declined" );
286 }elsif ( $format eq 'PAP' ) {
288 $filetype = "Fixed264";
291 'recordtype', # We are interested in the 'D' or debit records
292 'batchnum', # Record#: batch number we used when sending the file
293 'datacenter', # Where in the bowels of the bank the data was processed
294 'paid', # Amount: Amount of the transaction. Dollars and cents
295 # with no decimal entered.
296 '_date', # Transaction Date: Date the Transaction was processed
297 'bank', # Routing information
298 'payinfo', # Account number for the transaction
299 'paybatchnum', # Reference#: Invoice number of the transaction
302 $formatre = '^(.).{19}(.{4})(.{3})(.{10})(.{6})(.{9})(.{12}).{110}(.{19}).{71}$';
304 $end_condition = sub {
306 $hash->{'recordtype'} eq 'W';
310 my( $hash, $total) = @_;
311 $total = sprintf("%.2f", $total);
312 my $batch_total = $hash->{'datacenter'}.$hash->{'paid'}.
313 substr($hash->{'_date'},0,1); # YUCK!
314 $batch_total = sprintf("%.2f", $batch_total / 100 );
315 return "Our total $total does not match bank total $batch_total!"
316 if $total != $batch_total;
322 $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
323 my $tmpdate = timelocal( 0,0,1,1,0,substr($hash->{'_date'}, 0, 3)+2000);
324 $tmpdate += 86400*(substr($hash->{'_date'}, 3, 3)-1) ;
325 $hash->{'_date'} = $tmpdate;
326 $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
329 $approved_condition = sub {
333 $declined_condition = sub {
337 }elsif ( $format eq 'ach-spiritone' ) {
343 'paybatchnum', # ID: Number of the transaction
344 'aba', # ABA Number for the transaction
345 'payinfo', # Bank Account Number for the transaction
346 '', # Transaction Type: 27 - debit
347 'paid', # Amount: Amount of the transaction. Dollars and cents
348 # with decimal entered.
349 '', # Default Transaction Type
350 '', # Default Amount: Dollars and cents with decimal entered.
353 $end_condition = sub {
359 $hash->{'_date'} = time; # got a better one?
360 $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'aba'};
363 $approved_condition = sub {
367 $declined_condition = sub {
373 return "Unknown format $format";
376 my $csv = new Text::CSV_XS;
378 local $SIG{HUP} = 'IGNORE';
379 local $SIG{INT} = 'IGNORE';
380 local $SIG{QUIT} = 'IGNORE';
381 local $SIG{TERM} = 'IGNORE';
382 local $SIG{TSTP} = 'IGNORE';
383 local $SIG{PIPE} = 'IGNORE';
385 my $oldAutoCommit = $FS::UID::AutoCommit;
386 local $FS::UID::AutoCommit = 0;
389 my $reself = $self->select_for_update;
391 unless ( $reself->status eq 'I' ) {
392 $dbh->rollback if $oldAutoCommit;
393 return "batchnum ". $self->batchnum. "no longer in transit";
396 my $error = $self->set_status('R');
398 $dbh->rollback if $oldAutoCommit;
404 while ( defined($line=<$fh>) ) {
406 next if $line =~ /^\s*$/; #skip blank lines
408 if ($filetype eq "CSV") {
409 $csv->parse($line) or do {
410 $dbh->rollback if $oldAutoCommit;
411 return "can't parse: ". $csv->error_input();
413 @values = $csv->fields();
414 }elsif ($filetype eq "Fixed80" || $filetype eq "Fixed264"){
415 @values = $line =~ /$formatre/;
417 $dbh->rollback if $oldAutoCommit;
418 return "can't parse: ". $line;
421 $dbh->rollback if $oldAutoCommit;
422 return "Unknown file type $filetype";
426 foreach my $field ( @fields ) {
427 my $value = shift @values;
429 $hash{$field} = $value;
432 if ( &{$end_condition}(\%hash) ) {
433 my $error = &{$end_hook}(\%hash, $total);
435 $dbh->rollback if $oldAutoCommit;
442 qsearchs('cust_pay_batch', { 'paybatchnum' => $hash{'paybatchnum'}+0 } );
443 unless ( $cust_pay_batch ) {
444 return "unknown paybatchnum $hash{'paybatchnum'}\n";
446 my $custnum = $cust_pay_batch->custnum,
447 my $payby = $cust_pay_batch->payby,
449 my $new_cust_pay_batch = new FS::cust_pay_batch { $cust_pay_batch->hash };
451 &{$hook}(\%hash, $cust_pay_batch->hashref);
453 if ( &{$approved_condition}(\%hash) ) {
455 $new_cust_pay_batch->status('Approved');
457 my $cust_pay = new FS::cust_pay ( {
458 'custnum' => $custnum,
460 'paybatch' => $self->batchnum,
461 map { $_ => $hash{$_} } (qw( paid _date payinfo )),
463 $error = $cust_pay->insert;
465 $dbh->rollback if $oldAutoCommit;
466 return "error adding payment paybatchnum $hash{'paybatchnum'}: $error\n";
468 $total += $hash{'paid'};
470 $cust_pay->cust_main->apply_payments;
472 } elsif ( &{$declined_condition}(\%hash) ) {
474 $new_cust_pay_batch->status('Declined');
476 foreach my $part_bill_event ( due_events ( $new_cust_pay_batch,
481 # don't run subsequent events if balance<=0
482 last if $cust_pay_batch->cust_main->balance <= 0;
484 if (my $error = $part_bill_event->do_event($new_cust_pay_batch)) {
485 # gah, even with transactions.
486 $dbh->commit if $oldAutoCommit; #well.
494 my $error = $new_cust_pay_batch->replace($cust_pay_batch);
496 $dbh->rollback if $oldAutoCommit;
497 return "error updating status of paybatchnum $hash{'paybatchnum'}: $error\n";
502 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
511 status is somewhat redundant now that download and upload exist
515 L<FS::Record>, schema.html from the base documentation.