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