recurring indicator for Paymentech batches, #19571
[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 ( $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     } else { # Tie up loose ends
173       return $payinfo;
174     }
175   }
176   #die "shouldn't be reached";
177 }
178
179 =item payinfo_check
180
181 Checks payby and payinfo.
182
183 For Customers (cust_main):
184 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
185 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
186 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
187 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
188
189 For Refunds (cust_refund):
190 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
191 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
192 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback),  or 'COMP' (free)
193
194 For Payments (cust_pay):
195 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
196 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
197 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
198 'COMP' (free) is depricated as a payment type in cust_pay
199
200 =cut
201
202 sub payinfo_check {
203   my $self = shift;
204
205   FS::payby->can_payby($self->table, $self->payby)
206     or return "Illegal payby: ". $self->payby;
207
208   if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
209     my $payinfo = $self->payinfo;
210     $payinfo =~ s/\D//g;
211     $self->payinfo($payinfo);
212     if ( $self->payinfo ) {
213       $self->payinfo =~ /^(\d{13,16}|\d{8,9})$/
214         or return "Illegal (mistyped?) credit card number (payinfo)";
215       $self->payinfo($1);
216       validate($self->payinfo) or return "Illegal credit card number";
217       return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
218                                  && cardtype($self->payinfo) eq "Unknown";
219     } else {
220       $self->payinfo('N/A'); #???
221     }
222   } else {
223     if ( $self->is_encrypted($self->payinfo) ) {
224       #something better?  all it would cause is a decryption error anyway?
225       my $error = $self->ut_anything('payinfo');
226       return $error if $error;
227     } else {
228       my $error = $self->ut_textn('payinfo');
229       return $error if $error;
230     }
231   }
232
233   '';
234
235 }
236
237 =item payby_payinfo_pretty
238
239 Returns payment method and information (suitably masked, if applicable) as
240 a human-readable string, such as:
241
242   Card #54xxxxxxxxxxxx32
243
244 or
245
246   Check #119006
247
248 =cut
249
250 sub payby_payinfo_pretty {
251   my $self = shift;
252   if ( $self->payby eq 'CARD' ) {
253     'Card #'. $self->paymask;
254   } elsif ( $self->payby eq 'CHEK' ) {
255     'E-check acct#'. $self->payinfo;
256   } elsif ( $self->payby eq 'BILL' ) {
257     'Check #'. $self->payinfo;
258   } elsif ( $self->payby eq 'PREP' ) {
259     'Prepaid card #'. $self->payinfo;
260   } elsif ( $self->payby eq 'CASH' ) {
261     'Cash '. $self->payinfo;
262   } elsif ( $self->payby eq 'WEST' ) {
263     'Western Union'; #. $self->payinfo;
264   } elsif ( $self->payby eq 'MCRD' ) {
265     'Manual credit card'; #. $self->payinfo;
266   } else {
267     $self->payby. ' '. $self->payinfo;
268   }
269 }
270
271 =item payinfo_used [ PAYINFO ]
272
273 Returns 1 if there's an existing payment using this payinfo.  This can be 
274 used to set the 'recurring payment' flag required by some processors.
275
276 =cut
277
278 sub payinfo_used {
279   my $self = shift;
280   my $payinfo = shift || $self->payinfo;
281   my %hash = (
282     'custnum' => $self->custnum,
283     'payby'   => 'CARD',
284   );
285
286   return 1
287   if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
288   || qsearch('cust_pay', 
289     { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) }  )
290   ;
291
292   return 0;
293 }
294
295 =back
296
297 =head1 BUGS
298
299 =head1 SEE ALSO
300
301 L<FS::payby>, L<FS::Record>
302
303 =cut
304
305 1;
306