1 package FS::cust_payby;
2 use base qw( FS::payinfo_Mixin FS::cust_main_Mixin FS::Record );
5 use Business::CreditCard qw( validate cardtype );
7 use FS::Msgcat qw( gettext );
8 use FS::Record; #qw( qsearch qsearchs );
13 our @encrypted_fields = ('payinfo', 'paycvv');
14 sub nohistory_fields { ('payinfo', 'paycvv'); }
16 our $ignore_expired_card = 0;
17 our $ignore_banned_card = 0;
18 our $ignore_invalid_card = 0;
21 install_callback FS::UID sub {
23 #yes, need it for stuff below (prolly should be cached)
24 $ignore_invalid_card = $conf->exists('allow_invalid_cards');
29 FS::cust_payby - Object methods for cust_payby records
35 $record = new FS::cust_payby \%hash;
36 $record = new FS::cust_payby { 'column' => 'value' };
38 $error = $record->insert;
40 $error = $new_record->replace($old_record);
42 $error = $record->delete;
44 $error = $record->check;
48 An FS::cust_payby object represents customer stored payment information.
49 FS::cust_payby inherits from FS::Record. The following fields are currently
123 Creates a new record. To add the record to the database, see L<"insert">.
125 Note that this stores the hash reference, not a distinct copy of the hash it
126 points to. You can ask the object for a copy with the I<hash> method.
130 # the new method can be inherited from FS::Record, if a table method is defined
132 sub table { 'cust_payby'; }
136 Adds this record to the database. If there is an error, returns the error,
137 otherwise returns false.
144 local $SIG{HUP} = 'IGNORE';
145 local $SIG{INT} = 'IGNORE';
146 local $SIG{QUIT} = 'IGNORE';
147 local $SIG{TERM} = 'IGNORE';
148 local $SIG{TSTP} = 'IGNORE';
149 local $SIG{PIPE} = 'IGNORE';
151 my $oldAutoCommit = $FS::UID::AutoCommit;
152 local $FS::UID::AutoCommit = 0;
155 my $error = $self->SUPER::insert;
157 $dbh->rollback if $oldAutoCommit;
161 if ( $self->payby =~ /^(CARD|CHEK)$/ ) {
162 # new auto card/check info, want to retry realtime_ invoice events
163 # (new customer? that's okay, they won't have any)
164 my $error = $self->cust_main->retry_realtime;
166 $dbh->rollback if $oldAutoCommit;
171 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
178 Delete this record from the database.
180 =item replace OLD_RECORD
182 Replaces the OLD_RECORD with this one in the database. If there is an error,
183 returns the error, otherwise returns false.
190 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
192 : $self->replace_old;
194 if ( length($old->paycvv) && $self->paycvv =~ /^\s*[\*x]*\s*$/ ) {
195 $self->paycvv($old->paycvv);
198 if ( $self->payby =~ /^(CARD|DCRD)$/
199 && ( $self->payinfo =~ /xx/
200 || $self->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
206 $self->payinfo($old->payinfo);
208 } elsif ( $self->payby =~ /^(CHEK|DCHK)$/ && $self->payinfo =~ /xx/ ) {
209 #fix for #3085 "edit of customer's routing code only surprisingly causes
210 #nothing to happen...
211 # this probably won't do the right thing when we don't have the
212 # public key (can't actually get the real $old->payinfo)
213 my($new_account, $new_aba) = split('@', $self->payinfo);
214 my($old_account, $old_aba) = split('@', $old->payinfo);
215 $new_account = $old_account if $new_account =~ /xx/;
216 $new_aba = $old_aba if $new_aba =~ /xx/;
217 $self->payinfo($new_account.'@'.$new_aba);
220 local($ignore_expired_card) = 1
221 if $old->payby =~ /^(CARD|DCRD)$/
222 && $self->payby =~ /^(CARD|DCRD)$/
223 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
225 local($ignore_banned_card) = 1
226 if ( $old->payby =~ /^(CARD|DCRD)$/ && $self->payby =~ /^(CARD|DCRD)$/
227 || $old->payby =~ /^(CHEK|DCHK)$/ && $self->payby =~ /^(CHEK|DCHK)$/ )
228 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
230 local $SIG{HUP} = 'IGNORE';
231 local $SIG{INT} = 'IGNORE';
232 local $SIG{QUIT} = 'IGNORE';
233 local $SIG{TERM} = 'IGNORE';
234 local $SIG{TSTP} = 'IGNORE';
235 local $SIG{PIPE} = 'IGNORE';
237 my $oldAutoCommit = $FS::UID::AutoCommit;
238 local $FS::UID::AutoCommit = 0;
241 my $error = $self->SUPER::replace($old);
243 $dbh->rollback if $oldAutoCommit;
247 if ( $self->payby =~ /^(CARD|CHEK)$/
248 && ( ( $self->get('payinfo') ne $old->get('payinfo')
249 && $self->get('payinfo') !~ /^99\d{14}$/
251 || grep { $self->get($_) ne $old->get($_) } qw(paydate payname)
256 # card/check/lec info has changed, want to retry realtime_ invoice events
257 my $error = $self->cust_main->retry_realtime;
259 $dbh->rollback if $oldAutoCommit;
264 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
271 Checks all fields to make sure this is a valid record. If there is
272 an error, returns the error, otherwise returns false. Called by the insert
281 $self->ut_numbern('custpaybynum')
282 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
283 || $self->ut_numbern('weight')
284 #encrypted #|| $self->ut_textn('payinfo')
285 #encrypted #|| $self->ut_textn('paycvv')
286 # || $self->ut_textn('paymask') #XXX something
287 #later #|| $self->ut_textn('paydate')
288 || $self->ut_numbern('paystart_month')
289 || $self->ut_numbern('paystart_year')
290 || $self->ut_numbern('payissue')
291 # || $self->ut_textn('payname') #XXX something
292 || $self->ut_alphan('paystate')
293 || $self->ut_textn('paytype')
294 || $self->ut_ipn('payip')
296 return $error if $error;
300 FS::payby->can_payby($self->table, $self->payby)
301 or return "Illegal payby: ". $self->payby;
303 # If it is encrypted and the private key is not availaible then we can't
304 # check the credit card.
305 my $check_payinfo = ! $self->is_encrypted($self->payinfo);
307 # Need some kind of global flag to accept invalid cards, for testing
309 #XXX if ( !$import && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
310 if ( !$ignore_invalid_card &&
311 $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
313 my $payinfo = $self->payinfo;
315 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
316 or return gettext('invalid_card'); #. ": ". $self->payinfo;
318 $self->payinfo($payinfo);
320 or return gettext('invalid_card'); # . ": ". $self->payinfo;
322 return gettext('unknown_card_type')
323 if $self->payinfo !~ /^99\d{14}$/ #token
324 && cardtype($self->payinfo) eq "Unknown";
326 unless ( $ignore_banned_card ) {
327 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
329 if ( $ban->bantype eq 'warn' ) {
330 #or others depending on value of $ban->reason ?
331 return '_duplicate_card'.
332 ': disabled from'. time2str('%a %h %o at %r', $ban->_date).
333 ' until '. time2str('%a %h %o at %r', $ban->_end_date).
334 ' (ban# '. $ban->bannum. ')'
335 unless $self->override_ban_warn;
337 return 'Banned credit card: banned on '.
338 time2str('%a %h %o at %r', $ban->_date).
339 ' by '. $ban->otaker.
340 ' (ban# '. $ban->bannum. ')';
345 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
346 if ( cardtype($self->payinfo) eq 'American Express card' ) {
347 $self->paycvv =~ /^(\d{4})$/
348 or return "CVV2 (CID) for American Express cards is four digits.";
351 $self->paycvv =~ /^(\d{3})$/
352 or return "CVV2 (CVC2/CID) is three digits.";
359 my $cardtype = cardtype($payinfo);
360 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
362 return "Start date or issue number is required for $cardtype cards"
363 unless $self->paystart_month && $self->paystart_year or $self->payissue;
365 return "Start month must be between 1 and 12"
366 if $self->paystart_month
367 and $self->paystart_month < 1 || $self->paystart_month > 12;
369 return "Start year must be 1990 or later"
370 if $self->paystart_year
371 and $self->paystart_year < 1990;
373 return "Issue number must be beween 1 and 99"
375 and $self->payissue < 1 || $self->payissue > 99;
378 $self->paystart_month('');
379 $self->paystart_year('');
383 } elsif ( !$ignore_invalid_card &&
384 $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
386 my $payinfo = $self->payinfo;
387 $payinfo =~ s/[^\d\@\.]//g;
388 if ( $conf->config('echeck-country') eq 'CA' ) {
389 $payinfo =~ /^(\d+)\@(\d{5})\.(\d{3})$/
390 or return 'invalid echeck account@branch.bank';
391 $payinfo = "$1\@$2.$3";
392 } elsif ( $conf->config('echeck-country') eq 'US' ) {
393 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
396 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@routing';
399 $self->payinfo($payinfo);
402 unless ( $ignore_banned_card ) {
403 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
405 if ( $ban->bantype eq 'warn' ) {
406 #or others depending on value of $ban->reason ?
407 return '_duplicate_ach' unless $self->override_ban_warn;
409 return 'Banned ACH account: banned on '.
410 time2str('%a %h %o at %r', $ban->_date).
411 ' by '. $ban->otaker.
412 ' (ban# '. $ban->bannum. ')';
417 # } elsif ( $self->payby eq 'PREPAY' ) {
419 # my $payinfo = $self->payinfo;
420 # $payinfo =~ s/\W//g; #anything else would just confuse things
421 # $self->payinfo($payinfo);
422 # $error = $self->ut_alpha('payinfo');
423 # return "Illegal prepayment identifier: ". $self->payinfo if $error;
424 # return "Unknown prepayment identifier"
425 # unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
430 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
431 return "Expiration date required"
432 # shouldn't payinfo_check do this?
433 unless $self->payby =~ /^(CHEK|DCHK)$/;
437 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
438 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
439 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
440 ( $m, $y ) = ( $2, "19$1" );
441 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
442 ( $m, $y ) = ( $3, "20$2" );
444 return "Illegal expiration date: ". $self->paydate;
446 $m = sprintf('%02d',$m);
447 $self->paydate("$y-$m-01");
448 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
449 return gettext('expired_card')
452 !$ignore_expired_card
453 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
456 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
457 ( ! $conf->exists('require_cardname')
458 || $self->payby !~ /^(CARD|DCRD)$/ )
460 $self->payname( $self->first. " ". $self->getfield('last') );
463 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
464 $self->payname =~ /^([\w \,\.\-\']*)$/
465 or return gettext('illegal_name'). " payname: ". $self->payname;
468 $self->payname =~ /^([\w \,\.\-\'\&]*)$/
469 or return gettext('illegal_name'). " payname: ". $self->payname;
480 sub _banned_pay_hashref {
491 'payby' => $payby2ban{$self->payby},
492 'payinfo' => $self->payinfo,
493 #don't ever *search* on reason! #'reason' =>
497 =item paydate_mon_year
499 Returns a two element list consisting of the paydate month and year.
503 sub paydate_mon_year {
506 my $date = $self->paydate; # || '12-2037';
508 #false laziness w/elements/select-month_year.html
509 if ( $date =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
511 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
514 warn "unrecognized expiration date format: $date";
525 my( $self, %opt ) = @_;
527 $opt{$_} = $self->$_() for qw( payinfo payname paydate );
529 if ( $self->locationnum ) {
530 my $cust_location = $self->cust_location;
531 $opt{$_} = $cust_location->$_() for qw( address1 address2 city state zip );
534 $self->cust_main->realtime_bop({
535 'method' => FS::payby->payby2bop( $self->payby ),
543 Returns a list of valid values for the paytype field (bank account type for
544 electronic check payment).
551 ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
554 =item cgi_cust_payby_fields
556 Returns the field names used in the web interface (including some pseudo-fields).
560 sub cgi_cust_payby_fields {
562 [qw( payby payinfo paydate_month paydate_year paycvv payname weight
563 payinfo1 payinfo2 payinfo3 paytype paystate )];
566 =item cgi_hash_callback HASHREF
568 Subroutine (not a class or object method). Processes a hash reference
569 of web interface contet (transfers the data from pseudo-fields to real fields).
573 sub cgi_hash_callback {
580 $hashref->{payby} = $noauto{$hashref->{payby}}
581 if ! $hashref->{weight} && exists $noauto{$hashref->{payby}};
583 if ( $hashref->{payby} =~ /^(CHEK|DCHK)$/ ) {
585 unless ( grep $hashref->{$_}, qw( payinfo1 payinfo2 payinfo3 payname ) ) {
590 $hashref->{payinfo} = $hashref->{payinfo1}. '@';
591 $hashref->{payinfo} .= $hashref->{payinfo3}.'.'
592 if $conf->config('echeck-country') eq 'CA';
593 $hashref->{payinfo} .= $hashref->{'payinfo2'};
595 $hashref->{payname} .= $hashref->{'payname_CHEK'};
597 } elsif ( $hashref->{payby} =~ /^(CARD|DCRD)$/ ) {
599 unless ( grep $hashref->{$_}, qw( payinfo paycvv payname ) ) {
606 $hashref->{paydate}= $hashref->{paydate_month}. '-'. $hashref->{paydate_year};
614 Returns a qsearch hash expression to search for parameters specified in HASHREF.
633 my ($class, $params) = @_;
638 # initialize these to prevent warnings
640 'paydate_year' => '',
648 if ( $params->{'payby'} ) {
650 my @payby = ref( $params->{'payby'} )
651 ? @{ $params->{'payby'} }
652 : ( $params->{'payby'} );
654 @payby = grep /^([A-Z]{4})$/, @payby;
655 my $in_payby = 'IN(' . join(',', map {"'$_'"} @payby) . ')';
656 push @where, "cust_payby.payby $in_payby"
661 # paydate_year / paydate_month
664 if ( $params->{'paydate_year'} =~ /^(\d{4})$/ ) {
666 $params->{'paydate_month'} =~ /^(\d\d?)$/
667 or die "paydate_year without paydate_month?";
671 'cust_payby.paydate IS NOT NULL',
672 "cust_payby.paydate != ''",
673 "CAST(cust_payby.paydate AS timestamp) < CAST('$year-$month-01' AS timestamp )"
677 # setup queries, subs, etc. for the search
680 $orderby ||= 'ORDER BY custnum';
682 # here is the agent virtualization
684 $FS::CurrentUser::CurrentUser->agentnums_sql(table => 'cust_main');
686 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
688 my $addl_from = ' LEFT JOIN cust_main USING ( custnum ) ';
689 # always make address fields available in results
690 for my $pre ('bill_', 'ship_') {
692 ' LEFT JOIN cust_location AS '.$pre.'location '.
693 'ON (cust_main.'.$pre.'locationnum = '.$pre.'location.locationnum) ';
696 my $count_query = "SELECT COUNT(*) FROM cust_payby $addl_from $extra_sql";
698 my @select = ( 'cust_payby.*',
699 #'cust_main.custnum',
700 # there's a good chance that we'll need these
701 'cust_main.bill_locationnum',
702 'cust_main.ship_locationnum',
703 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
706 my $select = join(', ', @select);
709 'table' => 'cust_payby',
711 'addl_from' => $addl_from,
713 'extra_sql' => $extra_sql,
714 'order_by' => $orderby,
715 'count_query' => $count_query,
727 L<FS::Record>, schema.html from the base documentation.