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
79 #this prevents encrypting empty values on insert?
81 my($self,$paycvv) = @_;
82 # This is only allowed in cust_payby (formerly cust_main)
83 # It shouldn't be stored longer than necessary to run the first transaction
84 if ( defined($paycvv) ) {
85 $self->setfield('paycvv', $paycvv);
87 $self->getfield('paycvv');
96 my($self, $paymask) = @_;
98 if ( defined($paymask) ) {
99 $self->setfield('paymask', $paymask);
101 $self->getfield('paymask') || $self->mask_payinfo;
111 =item mask_payinfo [ PAYBY, PAYINFO ]
113 This method converts the payment info (credit card, bank account, etc.) into a
116 Optionally, an arbitrary payby and payinfo can be passed.
122 my $payby = scalar(@_) ? shift : $self->payby;
123 my $payinfo = scalar(@_) ? shift : $self->payinfo;
125 # Check to see if it's encrypted...
126 if ( ref($self) && $self->is_encrypted($payinfo) ) {
128 } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
129 return 'N/A (tokenized)'; #?
130 } else { # if not, mask it...
132 if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
136 # special handling for Local Isracards: always show last 4
137 if ( $payinfo =~ /^(\d{8,9})$/ ) {
139 return 'x'x(length($payinfo)-4).
140 substr($payinfo,(length($payinfo)-4));
144 my $conf = new FS::Conf;
145 my $mask_method = $conf->config('card_masking_method') || 'first6last4';
146 $mask_method =~ /^first(\d+)last(\d+)$/
147 or die "can't parse card_masking_method $mask_method";
148 my($first, $last) = ($1, $2);
150 return substr($payinfo,0,$first).
151 'x'x(length($payinfo)-$first-$last).
152 substr($payinfo,(length($payinfo)-$last));
154 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
156 # Checks (Show last 2 @ bank)
157 my( $account, $aba ) = split('@', $payinfo );
158 return 'x'x(length($account)-2).
159 substr($account,(length($account)-2)).
160 ( length($aba) ? "@".$aba : '');
162 } elsif ($payby eq 'EDI') {
164 # These numbers have been seen anywhere from 8 to 30 digits, and
165 # possibly more. Lacking any better idea I'm going to mask all but
167 return 'x' x (length($payinfo) - 4) . substr($payinfo, -4);
169 } else { # Tie up loose ends
173 #die "shouldn't be reached";
178 Checks payby and payinfo.
180 For Customers (cust_main):
181 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
182 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
183 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
184 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
186 For Refunds (cust_refund):
187 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
188 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
189 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback), or 'COMP' (free)
191 For Payments (cust_pay):
192 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
193 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
194 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
195 'COMP' (free) is depricated as a payment type in cust_pay
202 FS::payby->can_payby($self->table, $self->payby)
203 or return "Illegal payby: ". $self->payby;
205 if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
206 my $payinfo = $self->payinfo;
207 if ( $ignore_masked_payinfo and $self->mask_payinfo eq $self->payinfo ) {
211 $self->payinfo($payinfo);
212 if ( $self->payinfo ) {
213 $self->payinfo =~ /^(\d{13,16}|\d{8,9})$/
214 or return "Illegal (mistyped?) credit card number (payinfo)";
216 validate($self->payinfo) or return "Illegal credit card number";
217 return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
218 && cardtype($self->payinfo) eq "Unknown";
220 $self->payinfo('N/A'); #???
224 if ( $self->is_encrypted($self->payinfo) ) {
225 #something better? all it would cause is a decryption error anyway?
226 my $error = $self->ut_anything('payinfo');
227 return $error if $error;
229 my $error = $self->ut_textn('payinfo');
230 return $error if $error;
236 =item payby_payinfo_pretty [ LOCALE ]
238 Returns payment method and information (suitably masked, if applicable) as
239 a human-readable string, such as:
241 Card #54xxxxxxxxxxxx32
249 sub payby_payinfo_pretty {
252 my $lh = FS::L10N->get_handle($locale);
253 if ( $self->payby eq 'CARD' ) {
254 $lh->maketext('Card #') . $self->paymask;
255 } elsif ( $self->payby eq 'CHEK' ) {
257 #false laziness w/view/cust_main/payment_history.html::translate_payinfo
258 my( $account, $aba ) = split('@', $self->paymask );
260 if ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #blame canada
261 my($branch, $routing) = ($1, $2);
262 $lh->maketext("Routing [_1], Branch [_2], Acct [_3]",
263 $routing, $branch, $account);
265 $lh->maketext("Routing [_1], Acct [_2]", $aba, $account);
268 } elsif ( $self->payby eq 'BILL' ) {
269 $lh->maketext('Check #') . $self->payinfo;
270 } elsif ( $self->payby eq 'PREP' ) {
271 $lh->maketext('Prepaid card #') . $self->payinfo;
272 } elsif ( $self->payby eq 'CASH' ) {
273 $lh->maketext('Cash') . ' ' . $self->payinfo;
274 } elsif ( $self->payby eq 'WEST' ) {
275 # does Western Union localize their name?
276 $lh->maketext('Western Union');
277 } elsif ( $self->payby eq 'MCRD' ) {
278 $lh->maketext('Manual credit card');
279 } elsif ( $self->payby eq 'EDI' ) {
280 $lh->maketext('EDI') . ' ' . $self->paymask;
281 } elsif ( $self->payby eq 'PPAL' ) {
282 $lh->maketext('PayPal transaction#') . $self->order_number;
284 $self->payby. ' '. $self->payinfo;
288 =item payinfo_used [ PAYINFO ]
290 Returns 1 if there's an existing payment using this payinfo. This can be
291 used to set the 'recurring payment' flag required by some processors.
297 my $payinfo = shift || $self->payinfo;
299 'custnum' => $self->custnum,
304 if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
305 || qsearch('cust_pay',
306 { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) } )
314 For transactions that have both 'status' and 'failure_status', shows the
315 status in a single, display-friendly string.
322 'done' => 'Approved',
323 'expired' => 'Card Expired',
324 'stolen' => 'Lost/Stolen',
325 'pickup' => 'Pick Up Card',
326 'nsf' => 'Insufficient Funds',
327 'inactive' => 'Inactive Account',
328 'blacklisted' => 'Blacklisted',
329 'declined' => 'Declined',
330 'approved' => 'Approved',
332 if ( $self->failure_status ) {
333 return $status{$self->failure_status};
335 return $status{$self->status};
345 L<FS::payby>, L<FS::Record>