X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcust_payby.pm;h=e4a1d193c1b83db1e7b6d83dfd6785c327fc06dd;hp=9111fdff1833ab4f1f6ca08ccc0f4e3bbdbfb3c4;hb=8c72aca69588468b2e5b35397e4d6fb3d543155e;hpb=c4e788ef6f21b5a8582a1005512a15ba2f022945 diff --git a/FS/FS/cust_payby.pm b/FS/FS/cust_payby.pm index 9111fdff1..e4a1d193c 100644 --- a/FS/FS/cust_payby.pm +++ b/FS/FS/cust_payby.pm @@ -19,6 +19,7 @@ sub nohistory_fields { ('payinfo', 'paycvv'); } our $ignore_expired_card = 0; our $ignore_banned_card = 0; our $ignore_invalid_card = 0; +our $ignore_cardtype = 0; our $conf; install_callback FS::UID sub { @@ -114,6 +115,9 @@ paytype payip +=item paycardtype + +The credit card type (deduced from the card number). =back @@ -195,10 +199,6 @@ sub replace { ? shift : $self->replace_old; - if ( length($old->paycvv) && $self->paycvv =~ /^\s*[\*x]*\s*$/ ) { - $self->paycvv($old->paycvv); - } - if ( $self->payby =~ /^(CARD|DCRD)$/ && ( $self->payinfo =~ /xx/ || $self->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/ @@ -220,6 +220,17 @@ sub replace { $self->payinfo($new_account.'@'.$new_aba); } + # only unmask paycvv if payinfo stayed the same + if ( $self->payby =~ /^(CARD|DCRD)$/ and $self->paycvv =~ /^\s*[\*x]+\s*$/ ) { + if ( $old->payinfo eq $self->payinfo + && $old->paymask eq $self->paymask + ) { + $self->paycvv($old->paycvv); + } else { + $self->paycvv(''); + } + } + local($ignore_expired_card) = 1 if $old->payby =~ /^(CARD|DCRD)$/ && $self->payby =~ /^(CARD|DCRD)$/ @@ -236,6 +247,11 @@ sub replace { { my $error = $self->check_payinfo_cardtype; return $error if $error; + + if ( $conf->exists('business-onlinepayment-verification') ) { + $error = $self->verify; + return $error if $error; + } } local $SIG{HUP} = 'IGNORE'; @@ -318,6 +334,13 @@ sub check { # Need some kind of global flag to accept invalid cards, for testing # on scrubbed data. #XXX if ( !$import && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) { + + # In this block: detect card type; reject credit card / account numbers that + # are impossible or banned; reject other payment features (date, CVV length) + # that are inappropriate for the card type. + # However, if the payinfo is encrypted then just detect card type and assume + # the other checks were already done. + if ( !$ignore_invalid_card && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) { @@ -330,9 +353,12 @@ sub check { validate($payinfo) or return gettext('invalid_card'); # . ": ". $self->payinfo; - return gettext('unknown_card_type') - if $self->payinfo !~ /^99\d{14}$/ #token - && cardtype($self->payinfo) eq "Unknown"; + my $cardtype = cardtype($payinfo); + $cardtype = 'Tokenized' if $self->payinfo =~ /^99\d{14}$/; #token + + return gettext('unknown_card_type') if $cardtype eq "Unknown"; + + $self->set('paycardtype', $cardtype); unless ( $ignore_banned_card ) { my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } ); @@ -354,7 +380,7 @@ sub check { } if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) { - if ( cardtype($self->payinfo) eq 'American Express card' ) { + if ( $cardtype eq 'American Express card' ) { $self->paycvv =~ /^(\d{4})$/ or return "CVV2 (CID) for American Express cards is four digits."; $self->paycvv($1); @@ -367,7 +393,6 @@ sub check { $self->paycvv(''); } - my $cardtype = cardtype($payinfo); if ( $cardtype =~ /^(Switch|Solo)$/i ) { return "Start date or issue number is required for $cardtype cards" @@ -425,6 +450,15 @@ sub check { } } + } elsif ( $self->payby =~ /^CARD|DCRD$/ and $self->paymask ) { + # either ignoring invalid cards, or we can't decrypt the payinfo, but + # try to detect the card type anyway. this never returns failure, so + # the contract of $ignore_invalid_cards is maintained. + $self->set('paycardtype', cardtype($self->paymask)); + } else { + $self->set('paycardtype', ''); + } + # } elsif ( $self->payby eq 'PREPAY' ) { # # my $payinfo = $self->payinfo; @@ -436,8 +470,6 @@ sub check { # unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } ); # $self->paycvv(''); - } - if ( $self->payby =~ /^(CHEK|DCHK)$/ ) { $self->paydate(''); @@ -445,6 +477,7 @@ sub check { } elsif ( $self->payby =~ /^(CARD|DCRD)$/ ) { # shouldn't payinfo_check do this? + # (except we don't ever call payinfo_check from here) return "Expiration date required" if $self->paydate eq '' || $self->paydate eq '-'; @@ -488,7 +521,11 @@ sub check { } - ### + if ( ! $self->custpaybynum + && $conf->exists('business-onlinepayment-verification') ) { + $error = $self->verify; + return $error if $error; + } $self->SUPER::check; } @@ -496,15 +533,21 @@ sub check { sub check_payinfo_cardtype { my $self = shift; + return '' if $ignore_cardtype; + return '' unless $self->payby =~ /^(CARD|CHEK)$/; my $payinfo = $self->payinfo; $payinfo =~ s/\D//g; - return '' if $payinfo =~ /^99\d{14}$/; #token + if ( $payinfo =~ /^99\d{14}$/ ) { + $self->set('paycardtype', 'Tokenized'); + return ''; + } my %bop_card_types = map { $_=>1 } values %{ card_types() }; my $cardtype = cardtype($payinfo); + $self->set('paycardtype', $cardtype); return "$cardtype not accepted" unless $bop_card_types{$cardtype}; @@ -560,6 +603,39 @@ sub paydate_mon_year { } +=item label + +Returns a one line text label for this payment type. + +=cut + +my %weight = ( + 1 => 'Primary', + 2 => 'Secondary', + 3 => 'Tertiary', + 4 => 'Fourth', + 5 => 'Fifth', + 6 => 'Sixth', + 7 => 'Seventh', +); + +sub label { + my $self = shift; + + my $name = $self->payby =~ /^(CARD|DCRD)$/ + && $self->paycardtype || FS::payby->shortname($self->payby); + + ( $self->payby =~ /^(CARD|CHEK)$/ ? $weight{$self->weight}. ' automatic ' + : 'Manual ' + ). + "$name: ". $self->paymask. + ( $self->payby =~ /^(CARD|DCRD)$/ + ? ' Exp '. join('/', $self->paydate_mon_year) + : '' + ); + +} + =item realtime_bop =cut @@ -581,6 +657,44 @@ sub realtime_bop { } +=item verify + +=cut + +sub verify { + my $self = shift; + return '' unless $self->payby =~ /^(CARD|DCRD)$/; + + my %opt = (); + + # false laziness with check + my( $m, $y ); + if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) { + ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" ); + } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) { + ( $m, $y ) = ( $2, "19$1" ); + } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) { + ( $m, $y ) = ( $3, "20$2" ); + } else { + return "Illegal expiration date: ". $self->paydate; + } + $m = sprintf('%02d',$m); + $opt{paydate} = "$y-$m-01"; + + $opt{$_} = $self->$_() for qw( payinfo payname paycvv ); + + if ( $self->locationnum ) { + my $cust_location = $self->cust_location; + $opt{$_} = $cust_location->$_() for qw( address1 address2 city state zip ); + } + + $self->cust_main->realtime_verify_bop({ + 'method' => FS::payby->payby2bop( $self->payby ), + %opt, + }); + +} + =item paytypes Returns a list of valid values for the paytype field (bank account type for @@ -625,6 +739,9 @@ sub cgi_hash_callback { 'CARD' => 'DCRD', 'CHEK' => 'DCHK', ); + # the payby selector gives the choice of CARD or CHEK (or others, but + # those are the ones with auto and on-demand versions). if the user didn't + # choose a weight, then they mean DCRD/DCHK. $hashref->{payby} = $noauto{$hashref->{payby}} if ! $hashref->{weight} && exists $noauto{$hashref->{payby}}; @@ -748,6 +865,9 @@ sub search_sql { ' LEFT JOIN cust_location AS '.$pre.'location '. 'ON (cust_main.'.$pre.'locationnum = '.$pre.'location.locationnum) '; } + # always make referral available in results + # (maybe we should be using FS::UI::Web::join_cust_main instead?) + $addl_from .= ' LEFT JOIN (select refnum, referral from part_referral) AS part_referral_x ON (cust_main.refnum = part_referral_x.refnum) '; my $count_query = "SELECT COUNT(*) FROM cust_payby $addl_from $extra_sql"; @@ -776,6 +896,18 @@ sub search_sql { =back +=cut + +sub _upgrade_data { + + my $class = shift; + local $ignore_banned_card = 1; + local $ignore_expired_card = 1; + local $ignore_invalid_card = 1; + $class->upgrade_set_cardtype; + +} + =head1 BUGS =head1 SEE ALSO