also mask EDI acct# on invoices, #26859
[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 =head1 NAME
9
10 FS::payinfo_Mixin - Mixin class for records in tables that contain payinfo.  
11
12 =head1 SYNOPSIS
13
14 package FS::some_table;
15 use vars qw(@ISA);
16 @ISA = qw( FS::payinfo_Mixin FS::Record );
17
18 =head1 DESCRIPTION
19
20 This is a mixin class for records that contain payinfo. 
21
22 =head1 FIELDS
23
24 =over 4
25
26 =item payby
27
28 The following payment types (payby) are supported:
29
30 For Customers (cust_main):
31 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
32 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
33 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
34 'PREPAY' (special billing type: applies a credit and sets billing type to I<BILL> - see L<FS::prepay_credit>)
35
36 For Refunds (cust_refund):
37 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
38 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
39 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' Chargeback, or 'COMP' (free)
40
41
42 For Payments (cust_pay):
43 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
44 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
45 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
46 'COMP' (free) is depricated as a payment type in cust_pay
47
48 =cut 
49
50 # was this supposed to do something?
51  
52 #sub payby {
53 #  my($self,$payby) = @_;
54 #  if ( defined($payby) ) {
55 #    $self->setfield('payby', $payby);
56 #  } 
57 #  return $self->getfield('payby')
58 #}
59
60 =item payinfo
61
62 Payment information (payinfo) can be one of the following types:
63
64 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
65
66 =cut
67
68 sub payinfo {
69   my($self,$payinfo) = @_;
70
71   if ( defined($payinfo) ) {
72     $self->setfield('payinfo', $payinfo);
73     $self->paymask($self->mask_payinfo) unless $payinfo =~ /^99\d{14}$/; #token
74   } else {
75     $self->getfield('payinfo');
76   }
77 }
78
79 =item paycvv
80
81 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
82
83 =cut
84
85 sub paycvv {
86   my($self,$paycvv) = @_;
87   # This is only allowed in cust_main... Even then it really shouldn't be stored...
88   if ($self->table eq 'cust_main') {
89     if ( defined($paycvv) ) {
90       $self->setfield('paycvv', $paycvv); # This is okay since we are the 'setter'
91     } else {
92       $paycvv = $self->getfield('paycvv'); # This is okay since we are the 'getter'
93       return $paycvv;
94     }
95   } else {
96 #    warn "This doesn't work for other tables besides cust_main
97     '';
98   } 
99 }
100
101 =item paymask
102
103 =cut
104
105 sub paymask {
106   my($self, $paymask) = @_;
107
108   if ( defined($paymask) ) {
109     $self->setfield('paymask', $paymask);
110   } else {
111     $self->getfield('paymask') || $self->mask_payinfo;
112   }
113 }
114
115 =back
116
117 =head1 METHODS
118
119 =over 4
120
121 =item mask_payinfo [ PAYBY, PAYINFO ]
122
123 This method converts the payment info (credit card, bank account, etc.) into a
124 masked string.
125
126 Optionally, an arbitrary payby and payinfo can be passed.
127
128 =cut
129
130 sub mask_payinfo {
131   my $self = shift;
132   my $payby   = scalar(@_) ? shift : $self->payby;
133   my $payinfo = scalar(@_) ? shift : $self->payinfo;
134
135   # Check to see if it's encrypted...
136   if ( ref($self) && $self->is_encrypted($payinfo) ) {
137     return 'N/A';
138   } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
139     return 'N/A (tokenized)'; #?
140   } else { # if not, mask it...
141
142     if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
143
144       # Credit Cards
145
146       # special handling for Local Isracards: always show last 4 
147       if ( $payinfo =~ /^(\d{8,9})$/ ) {
148
149         return 'x'x(length($payinfo)-4).
150                substr($payinfo,(length($payinfo)-4));
151
152       }
153
154       my $conf = new FS::Conf;
155       my $mask_method = $conf->config('card_masking_method') || 'first6last4';
156       $mask_method =~ /^first(\d+)last(\d+)$/
157         or die "can't parse card_masking_method $mask_method";
158       my($first, $last) = ($1, $2);
159
160       return substr($payinfo,0,$first).
161              'x'x(length($payinfo)-$first-$last).
162              substr($payinfo,(length($payinfo)-$last));
163
164     } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
165
166       # Checks (Show last 2 @ bank)
167       my( $account, $aba ) = split('@', $payinfo );
168       return 'x'x(length($account)-2).
169              substr($account,(length($account)-2)).
170              ( length($aba) ? "@".$aba : '');
171
172     } elsif ($payby eq 'EDI') {
173       # EDI.
174       # These numbers have been seen anywhere from 8 to 30 digits, and 
175       # possibly more.  Lacking any better idea I'm going to mask all but
176       # the last 4 digits.
177       return 'x' x (length($payinfo) - 4) . substr($payinfo, -4);
178
179     } else { # Tie up loose ends
180       return $payinfo;
181     }
182   }
183   #die "shouldn't be reached";
184 }
185
186 =item payinfo_check
187
188 Checks payby and payinfo.
189
190 For Customers (cust_main):
191 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
192 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
193 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
194 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
195
196 For Refunds (cust_refund):
197 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
198 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
199 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback),  or 'COMP' (free)
200
201 For Payments (cust_pay):
202 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
203 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
204 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
205 'COMP' (free) is depricated as a payment type in cust_pay
206
207 =cut
208
209 sub payinfo_check {
210   my $self = shift;
211
212   FS::payby->can_payby($self->table, $self->payby)
213     or return "Illegal payby: ". $self->payby;
214
215   if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
216     my $payinfo = $self->payinfo;
217     $payinfo =~ s/\D//g;
218     $self->payinfo($payinfo);
219     if ( $self->payinfo ) {
220       $self->payinfo =~ /^(\d{13,16}|\d{8,9})$/
221         or return "Illegal (mistyped?) credit card number (payinfo)";
222       $self->payinfo($1);
223       validate($self->payinfo) or return "Illegal credit card number";
224       return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
225                                  && cardtype($self->payinfo) eq "Unknown";
226     } else {
227       $self->payinfo('N/A'); #???
228     }
229   } else {
230     if ( $self->is_encrypted($self->payinfo) ) {
231       #something better?  all it would cause is a decryption error anyway?
232       my $error = $self->ut_anything('payinfo');
233       return $error if $error;
234     } else {
235       my $error = $self->ut_textn('payinfo');
236       return $error if $error;
237     }
238   }
239
240   '';
241
242 }
243
244 =item payby_payinfo_pretty [ LOCALE ]
245
246 Returns payment method and information (suitably masked, if applicable) as
247 a human-readable string, such as:
248
249   Card #54xxxxxxxxxxxx32
250
251 or
252
253   Check #119006
254
255 =cut
256
257 sub payby_payinfo_pretty {
258   my $self = shift;
259   my $locale = shift;
260   my $lh = FS::L10N->get_handle($locale);
261   if ( $self->payby eq 'CARD' ) {
262     $lh->maketext('Card #') . $self->paymask;
263   } elsif ( $self->payby eq 'CHEK' ) {
264
265     #false laziness w/view/cust_main/payment_history.html::translate_payinfo
266     my( $account, $aba ) = split('@', $self->paymask );
267
268     if ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #blame canada
269       my($branch, $routing) = ($1, $2);
270       $lh->maketext("Routing [_1], Branch [_2], Acct [_3]",
271                      $routing, $branch, $account);
272     } else {
273       $lh->maketext("Routing [_1], Acct [_2]", $aba, $account);
274     }
275
276   } elsif ( $self->payby eq 'BILL' ) {
277     $lh->maketext('Check #') . $self->payinfo;
278   } elsif ( $self->payby eq 'PREP' ) {
279     $lh->maketext('Prepaid card #') . $self->payinfo;
280   } elsif ( $self->payby eq 'CASH' ) {
281     $lh->maketext('Cash') . ' ' . $self->payinfo;
282   } elsif ( $self->payby eq 'WEST' ) {
283     # does Western Union localize their name?
284     $lh->maketext('Western Union');
285   } elsif ( $self->payby eq 'MCRD' ) {
286     $lh->maketext('Manual credit card');
287   } elsif ( $self->payby eq 'EDI' ) {
288     $lh->maketext('EDI') . ' ' . $self->paymask;
289   } else {
290     $self->payby. ' '. $self->payinfo;
291   }
292 }
293
294 =item payinfo_used [ PAYINFO ]
295
296 Returns 1 if there's an existing payment using this payinfo.  This can be 
297 used to set the 'recurring payment' flag required by some processors.
298
299 =cut
300
301 sub payinfo_used {
302   my $self = shift;
303   my $payinfo = shift || $self->payinfo;
304   my %hash = (
305     'custnum' => $self->custnum,
306     'payby'   => 'CARD',
307   );
308
309   return 1
310   if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
311   || qsearch('cust_pay', 
312     { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) }  )
313   ;
314
315   return 0;
316 }
317
318 =back
319
320 =head1 BUGS
321
322 =head1 SEE ALSO
323
324 L<FS::payby>, L<FS::Record>
325
326 =cut
327
328 1;
329