1 package FS::payinfo_Mixin;
4 use Business::CreditCard;
6 use FS::Record qw(qsearch);
8 use vars qw($ignore_masked_payinfo);
12 FS::payinfo_Mixin - Mixin class for records in tables that contain payinfo.
16 package FS::some_table;
18 @ISA = qw( FS::payinfo_Mixin FS::Record );
22 This is a mixin class for records that contain payinfo.
30 The following payment types (payby) are supported:
32 For Customers (cust_main):
33 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
34 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
35 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
36 'PREPAY' (special billing type: applies a credit and sets billing type to I<BILL> - see L<FS::prepay_credit>)
38 For Refunds (cust_refund):
39 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
40 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
41 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' Chargeback, or 'COMP' (free)
44 For Payments (cust_pay):
45 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
46 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
47 'CASH' (cash), 'WEST' (Western Union), 'MCRD' (Manual credit card),
49 'COMP' (free) is depricated as a payment type in cust_pay
55 Payment information (payinfo) can be one of the following types:
57 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username)
58 prepayment identifier (see L<FS::prepay_credit>), PayPal transaction ID
63 my($self,$payinfo) = @_;
65 if ( defined($payinfo) ) {
66 $self->setfield('payinfo', $payinfo);
67 $self->paymask($self->mask_payinfo) unless $payinfo =~ /^99\d{14}$/; #token
69 $self->getfield('payinfo');
75 Card Verification Value, "CVV2" (also known as CVC2 or CID), the 3 or 4 digit number on the back (or front, for American Express) of the credit card
80 my($self,$paycvv) = @_;
81 # This is only allowed in cust_main... Even then it really shouldn't be stored...
82 if ($self->table eq 'cust_main') {
83 if ( defined($paycvv) ) {
84 $self->setfield('paycvv', $paycvv); # This is okay since we are the 'setter'
86 $paycvv = $self->getfield('paycvv'); # This is okay since we are the 'getter'
90 # warn "This doesn't work for other tables besides cust_main
100 my($self, $paymask) = @_;
102 if ( defined($paymask) ) {
103 $self->setfield('paymask', $paymask);
105 $self->getfield('paymask') || $self->mask_payinfo;
115 =item mask_payinfo [ PAYBY, PAYINFO ]
117 This method converts the payment info (credit card, bank account, etc.) into a
120 Optionally, an arbitrary payby and payinfo can be passed.
126 my $payby = scalar(@_) ? shift : $self->payby;
127 my $payinfo = scalar(@_) ? shift : $self->payinfo;
129 # Check to see if it's encrypted...
130 if ( $self->is_encrypted($payinfo) ) {
132 } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
133 return 'N/A (tokenized)'; #?
134 } else { # if not, mask it...
136 if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
140 # special handling for Local Isracards: always show last 4
141 if ( $payinfo =~ /^(\d{8,9})$/ ) {
143 return 'x'x(length($payinfo)-4).
144 substr($payinfo,(length($payinfo)-4));
148 my $conf = new FS::Conf;
149 my $mask_method = $conf->config('card_masking_method') || 'first6last4';
150 $mask_method =~ /^first(\d+)last(\d+)$/
151 or die "can't parse card_masking_method $mask_method";
152 my($first, $last) = ($1, $2);
154 return substr($payinfo,0,$first).
155 'x'x(length($payinfo)-$first-$last).
156 substr($payinfo,(length($payinfo)-$last));
158 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
160 # Checks (Show last 2 @ bank)
161 my( $account, $aba ) = split('@', $payinfo );
162 return 'x'x(length($account)-2).
163 substr($account,(length($account)-2)).
164 ( length($aba) ? "@".$aba : '');
166 } else { # Tie up loose ends
170 #die "shouldn't be reached";
175 Checks payby and payinfo.
177 For Customers (cust_main):
178 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
179 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
180 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
181 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
183 For Refunds (cust_refund):
184 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
185 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
186 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback), or 'COMP' (free)
188 For Payments (cust_pay):
189 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
190 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
191 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
192 'COMP' (free) is depricated as a payment type in cust_pay
199 FS::payby->can_payby($self->table, $self->payby)
200 or return "Illegal payby: ". $self->payby;
202 if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
203 my $payinfo = $self->payinfo;
204 if ( $ignore_masked_payinfo and $self->mask_payinfo eq $self->payinfo ) {
208 $self->payinfo($payinfo);
209 if ( $self->payinfo ) {
210 $self->payinfo =~ /^(\d{13,16}|\d{8,9})$/
211 or return "Illegal (mistyped?) credit card number (payinfo)";
213 validate($self->payinfo) or return "Illegal credit card number";
214 return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
215 && cardtype($self->payinfo) eq "Unknown";
217 $self->payinfo('N/A'); #???
221 if ( $self->is_encrypted($self->payinfo) ) {
222 #something better? all it would cause is a decryption error anyway?
223 my $error = $self->ut_anything('payinfo');
224 return $error if $error;
226 my $error = $self->ut_textn('payinfo');
227 return $error if $error;
233 =item payby_payinfo_pretty
235 Returns payment method and information (suitably masked, if applicable) as
236 a human-readable string, such as:
238 Card #54xxxxxxxxxxxx32
246 sub payby_payinfo_pretty {
248 if ( $self->payby eq 'CARD' ) {
249 'Card #'. $self->paymask;
250 } elsif ( $self->payby eq 'CHEK' ) {
251 'E-check acct#'. $self->payinfo;
252 } elsif ( $self->payby eq 'BILL' ) {
253 'Check #'. $self->payinfo;
254 } elsif ( $self->payby eq 'PREP' ) {
255 'Prepaid card #'. $self->payinfo;
256 } elsif ( $self->payby eq 'CASH' ) {
257 'Cash '. $self->payinfo;
258 } elsif ( $self->payby eq 'WEST' ) {
259 'Western Union'; #. $self->payinfo;
260 } elsif ( $self->payby eq 'MCRD' ) {
261 'Manual credit card'; #. $self->payinfo;
262 } elsif ( $self->payby eq 'PPAL' ) {
263 'PayPal transaction#' . $self->order_number;
265 $self->payby. ' '. $self->payinfo;
269 =item payinfo_used [ PAYINFO ]
271 Returns 1 if there's an existing payment using this payinfo. This can be
272 used to set the 'recurring payment' flag required by some processors.
278 my $payinfo = shift || $self->payinfo;
280 'custnum' => $self->custnum,
285 if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
286 || qsearch('cust_pay',
287 { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) } )
299 L<FS::payby>, L<FS::Record>