1 package FS::cust_pay_batch;
4 use vars qw( @ISA $DEBUG );
5 use Carp qw( confess );
6 use Business::CreditCard 0.28;
7 use FS::Record qw(dbh qsearch qsearchs);
12 @ISA = qw( FS::payinfo_Mixin FS::cust_main_Mixin FS::Record );
14 # 1 is mostly method/subroutine entry and options
15 # 2 traces progress of some operations
16 # 3 is even more information including possibly sensitive data
21 FS::cust_pay_batch - Object methods for batch cards
25 use FS::cust_pay_batch;
27 $record = new FS::cust_pay_batch \%hash;
28 $record = new FS::cust_pay_batch { 'column' => 'value' };
30 $error = $record->insert;
32 $error = $new_record->replace($old_record);
34 $error = $record->delete;
36 $error = $record->check;
38 #deprecated# $error = $record->retriable;
42 An FS::cust_pay_batch object represents a credit card transaction ready to be
43 batched (sent to a processor). FS::cust_pay_batch inherits from FS::Record.
44 Typically called by the collect method of an FS::cust_main object. The
45 following fields are currently supported:
49 =item paybatchnum - primary key (automatically assigned)
51 =item batchnum - indentifies group in batch
53 =item payby - CARD/CHEK/LECB/BILL/COMP
57 =item exp - card expiration
61 =item invnum - invoice
63 =item custnum - customer
65 =item payname - name on card
83 =item status - 'Approved' or 'Declined'
85 =item error_message - the error returned by the gateway if any
95 Creates a new record. To add the record to the database, see L<"insert">.
97 Note that this stores the hash reference, not a distinct copy of the hash it
98 points to. You can ask the object for a copy with the I<hash> method.
102 sub table { 'cust_pay_batch'; }
106 Adds this record to the database. If there is an error, returns the error,
107 otherwise returns false.
111 Delete this record from the database. If there is an error, returns the error,
112 otherwise returns false.
114 =item replace OLD_RECORD
116 Replaces the OLD_RECORD with this one in the database. If there is an error,
117 returns the error, otherwise returns false.
121 Checks all fields to make sure this is a valid transaction. If there is
122 an error, returns the error, otherwise returns false. Called by the insert
131 $self->ut_numbern('paybatchnum')
132 || $self->ut_numbern('trancode') #deprecated
133 || $self->ut_money('amount')
134 || $self->ut_number('invnum')
135 || $self->ut_number('custnum')
136 || $self->ut_text('address1')
137 || $self->ut_textn('address2')
138 || $self->ut_text('city')
139 || $self->ut_textn('state')
142 return $error if $error;
144 $self->getfield('last') =~ /^([\w \,\.\-\']+)$/ or return "Illegal last name";
145 $self->setfield('last',$1);
147 $self->first =~ /^([\w \,\.\-\']+)$/ or return "Illegal first name";
150 $error = $self->payinfo_check();
151 return $error if $error;
153 if ( $self->exp eq '' ) {
154 return "Expiration date required"
155 unless $self->payby =~ /^(CHEK|DCHK|LECB|WEST)$/;
158 if ( $self->exp =~ /^(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})$/ ) {
159 $self->exp("$1-$2-$3");
160 } elsif ( $self->exp =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
161 if ( length($2) == 4 ) {
162 $self->exp("$2-$1-01");
163 } elsif ( $2 > 98 ) { #should pry change to check for "this year"
164 $self->exp("19$2-$1-01");
166 $self->exp("20$2-$1-01");
169 return "Illegal expiration date";
173 if ( $self->payname eq '' ) {
174 $self->payname( $self->first. " ". $self->getfield('last') );
176 $self->payname =~ /^([\w \,\.\-\']+)$/
177 or return "Illegal billing name";
181 #we have lots of old zips in there... don't hork up batch results cause of em
182 $self->zip =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
183 or return "Illegal zip: ". $self->zip;
186 $self->country =~ /^(\w\w)$/ or return "Illegal country: ". $self->country;
189 #$error = $self->ut_zip('zip', $self->country);
190 #return $error if $error;
192 #check invnum, custnum, ?
199 Returns the customer (see L<FS::cust_main>) for this batched credit card
206 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
211 Returns the payment batch this payment belongs to (L<FS::pay_batch).
217 FS::pay_batch->by_key($self->batchnum);
220 #you know what, screw this in the new world of events. we should be able to
221 #get the event defs to retry (remove once.pm condition, add every.pm) without
222 #mucking about with statuses of previous cust_event records. right?
226 #Marks the corresponding event (see L<FS::cust_bill_event>) for this batched
227 #credit card payment as retriable. Useful if the corresponding financial
228 #institution account was declined for temporary reasons and/or a manual
231 #Implementation details: For the named customer's invoice, changes the
232 #statustext of the 'done' (without statustext) event to 'retriable.'
238 confess "deprecated method cust_pay_batch->retriable called; try removing ".
239 "the once condition and adding an every condition?";
243 local $SIG{HUP} = 'IGNORE'; #Hmm
244 local $SIG{INT} = 'IGNORE';
245 local $SIG{QUIT} = 'IGNORE';
246 local $SIG{TERM} = 'IGNORE';
247 local $SIG{TSTP} = 'IGNORE';
248 local $SIG{PIPE} = 'IGNORE';
250 my $oldAutoCommit = $FS::UID::AutoCommit;
251 local $FS::UID::AutoCommit = 0;
254 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
255 or return "event $self->eventnum references nonexistant invoice $self->invnum";
257 warn "cust_pay_batch->retriable working with self of " . $self->paybatchnum . " and invnum of " . $self->invnum;
258 my @cust_bill_event =
259 sort { $a->part_bill_event->seconds <=> $b->part_bill_event->seconds }
261 $_->part_bill_event->eventcode =~ /\$cust_bill->batch_card/
262 && $_->status eq 'done'
265 $cust_bill->cust_bill_event;
266 # complain loudly if scalar(@cust_bill_event) > 1 ?
267 my $error = $cust_bill_event[0]->retriable;
269 # gah, even with transactions.
270 $dbh->commit if $oldAutoCommit; #well.
271 return "error marking invoice event retriable: $error";
276 =item approve PAYBATCH
278 Approve this payment. This will replace the existing record with the
279 same paybatchnum, set its status to 'Approved', and generate a payment
280 record (L<FS::cust_pay>). This should only be called from the batch
286 # to break up the Big Wall of Code that is import_results
288 my $paybatch = shift;
289 my $paybatchnum = $new->paybatchnum;
290 my $old = qsearchs('cust_pay_batch', { paybatchnum => $paybatchnum })
291 or return "paybatchnum $paybatchnum not found";
292 return "paybatchnum $paybatchnum already resolved ('".$old->status."')"
294 $new->status('Approved');
295 my $error = $new->replace($old);
297 return "error updating status of paybatchnum $paybatchnum: $error\n";
299 my $cust_pay = new FS::cust_pay ( {
300 'custnum' => $new->custnum,
301 'payby' => $new->payby,
302 'paybatch' => $paybatch,
303 'payinfo' => $new->payinfo || $old->payinfo,
304 'paid' => $new->paid,
305 '_date' => $new->_date,
306 'usernum' => $new->usernum,
307 'batchnum' => $new->batchnum,
309 $error = $cust_pay->insert;
311 return "error inserting payment for paybatchnum $paybatchnum: $error\n";
313 $cust_pay->cust_main->apply_payments;
317 =item decline [ REASON ]
319 Decline this payment. This will replace the existing record with the
320 same paybatchnum, set its status to 'Declined', and run collection events
321 as appropriate. This should only be called from the batch import process.
323 REASON is a string description of the decline reason, defaulting to
330 my $reason = shift || 'Returned payment';
331 #my $conf = new FS::Conf;
333 my $paybatchnum = $new->paybatchnum;
334 my $old = qsearchs('cust_pay_batch', { paybatchnum => $paybatchnum })
335 or return "paybatchnum $paybatchnum not found";
336 if ( $old->status ) {
337 # Handle the case where payments are rejected after the batch has been
338 # approved. FS::pay_batch::import_results won't allow results to be
339 # imported to a closed batch unless batch-manual_approval is enabled,
340 # so we don't check it here.
341 # if ( $conf->exists('batch-manual_approval') and
342 if ( lc($old->status) eq 'approved' ) {
344 my $cust_pay = qsearchs('cust_pay', {
345 custnum => $new->custnum,
346 batchnum => $new->batchnum
349 $cust_pay ||= qsearchs('cust_pay', {
350 custnum => $new->custnum,
351 paybatch => $new->batchnum
354 # should never happen...
355 return "failed to revoke paybatchnum $paybatchnum, payment not found";
357 $cust_pay->void($reason);
360 # normal case: refuse to do anything
361 return "paybatchnum $paybatchnum already resolved ('".$old->status."')";
364 $new->status('Declined');
365 $new->error_message($reason);
366 my $error = $new->replace($old);
368 return "error updating status of paybatchnum $paybatchnum: $error\n";
370 my $due_cust_event = $new->cust_main->due_cust_event(
371 'eventtable' => 'cust_pay_batch',
372 'objects' => [ $new ],
374 if ( !ref($due_cust_event) ) {
375 return $due_cust_event;
377 # XXX breaks transaction integrity
378 foreach my $cust_event (@$due_cust_event) {
379 next unless $cust_event->test_conditions;
380 if ( my $error = $cust_event->do_event() ) {
391 There should probably be a configuration file with a list of allowed credit
396 L<FS::cust_main>, L<FS::Record>