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 } else { # Tie up loose ends
166 #die "shouldn't be reached";
171 Checks payby and payinfo.
173 For Customers (cust_main):
174 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
175 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
176 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
177 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
179 For Refunds (cust_refund):
180 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
181 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
182 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback), or 'COMP' (free)
184 For Payments (cust_pay):
185 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
186 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
187 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
188 'COMP' (free) is depricated as a payment type in cust_pay
195 FS::payby->can_payby($self->table, $self->payby)
196 or return "Illegal payby: ". $self->payby;
198 if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
199 my $payinfo = $self->payinfo;
200 if ( $ignore_masked_payinfo and $self->mask_payinfo eq $self->payinfo ) {
204 $self->payinfo($payinfo);
205 if ( $self->payinfo ) {
206 $self->payinfo =~ /^(\d{13,16}|\d{8,9})$/
207 or return "Illegal (mistyped?) credit card number (payinfo)";
209 validate($self->payinfo) or return "Illegal credit card number";
210 return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
211 && cardtype($self->payinfo) eq "Unknown";
213 $self->payinfo('N/A'); #???
217 if ( $self->is_encrypted($self->payinfo) ) {
218 #something better? all it would cause is a decryption error anyway?
219 my $error = $self->ut_anything('payinfo');
220 return $error if $error;
222 my $error = $self->ut_textn('payinfo');
223 return $error if $error;
229 =item payby_payinfo_pretty
231 Returns payment method and information (suitably masked, if applicable) as
232 a human-readable string, such as:
234 Card #54xxxxxxxxxxxx32
242 sub payby_payinfo_pretty {
244 if ( $self->payby eq 'CARD' ) {
245 'Card #'. $self->paymask;
246 } elsif ( $self->payby eq 'CHEK' ) {
247 'E-check acct#'. $self->payinfo;
248 } elsif ( $self->payby eq 'BILL' ) {
249 'Check #'. $self->payinfo;
250 } elsif ( $self->payby eq 'PREP' ) {
251 'Prepaid card #'. $self->payinfo;
252 } elsif ( $self->payby eq 'CASH' ) {
253 'Cash '. $self->payinfo;
254 } elsif ( $self->payby eq 'WEST' ) {
255 'Western Union'; #. $self->payinfo;
256 } elsif ( $self->payby eq 'MCRD' ) {
257 'Manual credit card'; #. $self->payinfo;
258 } elsif ( $self->payby eq 'PPAL' ) {
259 'PayPal transaction#' . $self->order_number;
261 $self->payby. ' '. $self->payinfo;
265 =item payinfo_used [ PAYINFO ]
267 Returns 1 if there's an existing payment using this payinfo. This can be
268 used to set the 'recurring payment' flag required by some processors.
274 my $payinfo = shift || $self->payinfo;
276 'custnum' => $self->custnum,
281 if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
282 || qsearch('cust_pay',
283 { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) } )
291 For transactions that have both 'status' and 'failure_status', shows the
292 status in a single, display-friendly string.
299 'done' => 'Approved',
300 'expired' => 'Card Expired',
301 'stolen' => 'Lost/Stolen',
302 'pickup' => 'Pick Up Card',
303 'nsf' => 'Insufficient Funds',
304 'inactive' => 'Inactive Account',
305 'blacklisted' => 'Blacklisted',
306 'declined' => 'Declined',
307 'approved' => 'Approved',
309 if ( $self->failure_status ) {
310 return $status{$self->failure_status};
312 return $status{$self->status};
322 L<FS::payby>, L<FS::Record>