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