b030bd73493b75a9a92a06e4f4d08e7562f69bae
[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 sub paycvv {
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'
85     } else {
86       $paycvv = $self->getfield('paycvv'); # This is okay since we are the 'getter'
87       return $paycvv;
88     }
89   } else {
90 #    warn "This doesn't work for other tables besides cust_main
91     '';
92   } 
93 }
94
95 =item paymask
96
97 =cut
98
99 sub paymask {
100   my($self, $paymask) = @_;
101
102   if ( defined($paymask) ) {
103     $self->setfield('paymask', $paymask);
104   } else {
105     $self->getfield('paymask') || $self->mask_payinfo;
106   }
107 }
108
109 =back
110
111 =head1 METHODS
112
113 =over 4
114
115 =item mask_payinfo [ PAYBY, PAYINFO ]
116
117 This method converts the payment info (credit card, bank account, etc.) into a
118 masked string.
119
120 Optionally, an arbitrary payby and payinfo can be passed.
121
122 =cut
123
124 sub mask_payinfo {
125   my $self = shift;
126   my $payby   = scalar(@_) ? shift : $self->payby;
127   my $payinfo = scalar(@_) ? shift : $self->payinfo;
128
129   # Check to see if it's encrypted...
130   if ( ref($self) && $self->is_encrypted($payinfo) ) {
131     return 'N/A';
132   } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
133     return 'N/A (tokenized)'; #?
134   } else { # if not, mask it...
135
136     if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
137
138       # Credit Cards
139
140       # special handling for Local Isracards: always show last 4 
141       if ( $payinfo =~ /^(\d{8,9})$/ ) {
142
143         return 'x'x(length($payinfo)-4).
144                substr($payinfo,(length($payinfo)-4));
145
146       }
147
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);
153
154       return substr($payinfo,0,$first).
155              'x'x(length($payinfo)-$first-$last).
156              substr($payinfo,(length($payinfo)-$last));
157
158     } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
159
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 : '');
165
166     } elsif ($payby eq 'EDI') {
167       # EDI.
168       # These numbers have been seen anywhere from 8 to 30 digits, and 
169       # possibly more.  Lacking any better idea I'm going to mask all but
170       # the last 4 digits.
171       return 'x' x (length($payinfo) - 4) . substr($payinfo, -4);
172
173     } else { # Tie up loose ends
174       return $payinfo;
175     }
176   }
177   #die "shouldn't be reached";
178 }
179
180 =item payinfo_check
181
182 Checks payby and payinfo.
183
184 For Customers (cust_main):
185 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
186 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
187 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
188 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
189
190 For Refunds (cust_refund):
191 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
192 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
193 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback),  or 'COMP' (free)
194
195 For Payments (cust_pay):
196 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
197 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
198 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
199 'COMP' (free) is depricated as a payment type in cust_pay
200
201 =cut
202
203 sub payinfo_check {
204   my $self = shift;
205
206   FS::payby->can_payby($self->table, $self->payby)
207     or return "Illegal payby: ". $self->payby;
208
209   if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
210     my $payinfo = $self->payinfo;
211     if ( $ignore_masked_payinfo and $self->mask_payinfo eq $self->payinfo ) {
212       # allow it
213     } else {
214       $payinfo =~ s/\D//g;
215       $self->payinfo($payinfo);
216       if ( $self->payinfo ) {
217         $self->payinfo =~ /^(\d{13,16}|\d{8,9})$/
218           or return "Illegal (mistyped?) credit card number (payinfo)";
219         $self->payinfo($1);
220         validate($self->payinfo) or return "Illegal credit card number";
221         return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
222                                    && cardtype($self->payinfo) eq "Unknown";
223       } else {
224         $self->payinfo('N/A'); #???
225       }
226     }
227   } else {
228     if ( $self->is_encrypted($self->payinfo) ) {
229       #something better?  all it would cause is a decryption error anyway?
230       my $error = $self->ut_anything('payinfo');
231       return $error if $error;
232     } else {
233       my $error = $self->ut_textn('payinfo');
234       return $error if $error;
235     }
236   }
237
238 }
239
240 =item payby_payinfo_pretty [ LOCALE ]
241
242 Returns payment method and information (suitably masked, if applicable) as
243 a human-readable string, such as:
244
245   Card #54xxxxxxxxxxxx32
246
247 or
248
249   Check #119006
250
251 =cut
252
253 sub payby_payinfo_pretty {
254   my $self = shift;
255   my $locale = shift;
256   my $lh = FS::L10N->get_handle($locale);
257   if ( $self->payby eq 'CARD' ) {
258     $lh->maketext('Card #') . $self->paymask;
259   } elsif ( $self->payby eq 'CHEK' ) {
260
261     #false laziness w/view/cust_main/payment_history.html::translate_payinfo
262     my( $account, $aba ) = split('@', $self->paymask );
263
264     if ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #blame canada
265       my($branch, $routing) = ($1, $2);
266       $lh->maketext("Routing [_1], Branch [_2], Acct [_3]",
267                      $routing, $branch, $account);
268     } else {
269       $lh->maketext("Routing [_1], Acct [_2]", $aba, $account);
270     }
271
272   } elsif ( $self->payby eq 'BILL' ) {
273     $lh->maketext('Check #') . $self->payinfo;
274   } elsif ( $self->payby eq 'PREP' ) {
275     $lh->maketext('Prepaid card #') . $self->payinfo;
276   } elsif ( $self->payby eq 'CASH' ) {
277     $lh->maketext('Cash') . ' ' . $self->payinfo;
278   } elsif ( $self->payby eq 'WEST' ) {
279     # does Western Union localize their name?
280     $lh->maketext('Western Union');
281   } elsif ( $self->payby eq 'MCRD' ) {
282     $lh->maketext('Manual credit card');
283   } elsif ( $self->payby eq 'EDI' ) {
284     $lh->maketext('EDI') . ' ' . $self->paymask;
285   } elsif ( $self->payby eq 'PPAL' ) {
286     $lh->maketext('PayPal transaction#') . $self->order_number;
287   } else {
288     $self->payby. ' '. $self->payinfo;
289   }
290 }
291
292 =item payinfo_used [ PAYINFO ]
293
294 Returns 1 if there's an existing payment using this payinfo.  This can be 
295 used to set the 'recurring payment' flag required by some processors.
296
297 =cut
298
299 sub payinfo_used {
300   my $self = shift;
301   my $payinfo = shift || $self->payinfo;
302   my %hash = (
303     'custnum' => $self->custnum,
304     'payby'   => 'CARD',
305   );
306
307   return 1
308   if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
309   || qsearch('cust_pay', 
310     { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) }  )
311   ;
312
313   return 0;
314 }
315
316 =back
317
318 =head1 BUGS
319
320 =head1 SEE ALSO
321
322 L<FS::payby>, L<FS::Record>
323
324 =cut
325
326 1;
327