Merge remote-tracking branch 'upstream/master'
[freeside.git] / FS / FS / payinfo_Mixin.pm
1 package FS::payinfo_Mixin;
2
3 use strict;
4 use Business::CreditCard;
5 use FS::payby;
6 use FS::Record qw(qsearch);
7
8 use vars qw($ignore_masked_payinfo);
9
10 =head1 NAME
11
12 FS::payinfo_Mixin - Mixin class for records in tables that contain payinfo.  
13
14 =head1 SYNOPSIS
15
16 package FS::some_table;
17 use vars qw(@ISA);
18 @ISA = qw( FS::payinfo_Mixin FS::Record );
19
20 =head1 DESCRIPTION
21
22 This is a mixin class for records that contain payinfo. 
23
24 =head1 FIELDS
25
26 =over 4
27
28 =item payby
29
30 The following payment types (payby) are supported:
31
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>)
37
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)
42
43
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),
48 'PPAL' (PayPal)
49 'COMP' (free) is depricated as a payment type in cust_pay
50
51 =cut 
52
53 =item payinfo
54
55 Payment information (payinfo) can be one of the following types:
56
57 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) 
58 prepayment identifier (see L<FS::prepay_credit>), PayPal transaction ID
59
60 =cut
61
62 sub payinfo {
63   my($self,$payinfo) = @_;
64
65   if ( defined($payinfo) ) {
66     $self->setfield('payinfo', $payinfo);
67     $self->paymask($self->mask_payinfo) unless $payinfo =~ /^99\d{14}$/; #token
68   } else {
69     $self->getfield('payinfo');
70   }
71 }
72
73 =item paycvv
74
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
76
77 =cut
78
79 #this prevents encrypting empty values on insert?
80 sub paycvv {
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);
86   } else {
87     $self->getfield('paycvv');
88   }
89 }
90
91 =item paymask
92
93 =cut
94
95 sub paymask {
96   my($self, $paymask) = @_;
97
98   if ( defined($paymask) ) {
99     $self->setfield('paymask', $paymask);
100   } else {
101     $self->getfield('paymask') || $self->mask_payinfo;
102   }
103 }
104
105 =back
106
107 =head1 METHODS
108
109 =over 4
110
111 =item mask_payinfo [ PAYBY, PAYINFO ]
112
113 This method converts the payment info (credit card, bank account, etc.) into a
114 masked string.
115
116 Optionally, an arbitrary payby and payinfo can be passed.
117
118 =cut
119
120 sub mask_payinfo {
121   my $self = shift;
122   my $payby   = scalar(@_) ? shift : $self->payby;
123   my $payinfo = scalar(@_) ? shift : $self->payinfo;
124
125   # Check to see if it's encrypted...
126   if ( ref($self) && $self->is_encrypted($payinfo) ) {
127     return 'N/A';
128   } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
129     return 'N/A (tokenized)'; #?
130   } else { # if not, mask it...
131
132     if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
133
134       # Credit Cards
135
136       # special handling for Local Isracards: always show last 4 
137       if ( $payinfo =~ /^(\d{8,9})$/ ) {
138
139         return 'x'x(length($payinfo)-4).
140                substr($payinfo,(length($payinfo)-4));
141
142       }
143
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);
149
150       return substr($payinfo,0,$first).
151              'x'x(length($payinfo)-$first-$last).
152              substr($payinfo,(length($payinfo)-$last));
153
154     } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
155
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 : '');
161
162     } else { # Tie up loose ends
163       return $payinfo;
164     }
165   }
166   #die "shouldn't be reached";
167 }
168
169 =item payinfo_check
170
171 Checks payby and payinfo.
172
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>)
178
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)
183
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
189
190 =cut
191
192 sub payinfo_check {
193   my $self = shift;
194
195   FS::payby->can_payby($self->table, $self->payby)
196     or return "Illegal payby: ". $self->payby;
197
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 ) {
201       # allow it
202     } else {
203       $payinfo =~ s/\D//g;
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)";
208         $self->payinfo($1);
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";
212       } else {
213         $self->payinfo('N/A'); #???
214       }
215     }
216   } else {
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;
221     } else {
222       my $error = $self->ut_textn('payinfo');
223       return $error if $error;
224     }
225   }
226
227 }
228
229 =item payby_payinfo_pretty [ LOCALE ]
230
231 Returns payment method and information (suitably masked, if applicable) as
232 a human-readable string, such as:
233
234   Card #54xxxxxxxxxxxx32
235
236 or
237
238   Check #119006
239
240 =cut
241
242 sub payby_payinfo_pretty {
243   my $self = shift;
244   my $locale = shift;
245   my $lh = FS::L10N->get_handle($locale);
246   if ( $self->payby eq 'CARD' ) {
247     $lh->maketext('Card #') . $self->paymask;
248   } elsif ( $self->payby eq 'CHEK' ) {
249     $lh->maketext('E-check acct#') . $self->payinfo;
250   } elsif ( $self->payby eq 'BILL' ) {
251     $lh->maketext('Check #') . $self->payinfo;
252   } elsif ( $self->payby eq 'PREP' ) {
253     $lh->maketext('Prepaid card #') . $self->payinfo;
254   } elsif ( $self->payby eq 'CASH' ) {
255     $lh->maketext('Cash') . ' ' . $self->payinfo;
256   } elsif ( $self->payby eq 'WEST' ) {
257     # does Western Union localize their name?
258     $lh->maketext('Western Union');
259   } elsif ( $self->payby eq 'MCRD' ) {
260     $lh->maketext('Manual credit card');
261   } elsif ( $self->payby eq 'PPAL' ) {
262     $lh->maketext('PayPal transaction#') . $self->order_number;
263   } else {
264     $self->payby. ' '. $self->payinfo;
265   }
266 }
267
268 =item payinfo_used [ PAYINFO ]
269
270 Returns 1 if there's an existing payment using this payinfo.  This can be 
271 used to set the 'recurring payment' flag required by some processors.
272
273 =cut
274
275 sub payinfo_used {
276   my $self = shift;
277   my $payinfo = shift || $self->payinfo;
278   my %hash = (
279     'custnum' => $self->custnum,
280     'payby'   => 'CARD',
281   );
282
283   return 1
284   if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
285   || qsearch('cust_pay', 
286     { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) }  )
287   ;
288
289   return 0;
290 }
291
292 =item display_status
293
294 For transactions that have both 'status' and 'failure_status', shows the
295 status in a single, display-friendly string.
296
297 =cut
298
299 sub display_status {
300   my $self = shift;
301   my %status = (
302     'done'        => 'Approved',
303     'expired'     => 'Card Expired',
304     'stolen'      => 'Lost/Stolen',
305     'pickup'      => 'Pick Up Card',
306     'nsf'         => 'Insufficient Funds',
307     'inactive'    => 'Inactive Account',
308     'blacklisted' => 'Blacklisted',
309     'declined'    => 'Declined',
310     'approved'    => 'Approved',
311   );
312   if ( $self->failure_status ) {
313     return $status{$self->failure_status};
314   } else {
315     return $status{$self->status};
316   }
317 }
318
319 =back
320
321 =head1 BUGS
322
323 =head1 SEE ALSO
324
325 L<FS::payby>, L<FS::Record>
326
327 =cut
328
329 1;
330