import torrus 1.0.9
[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   my $paymask;
136   if ( $self->is_encrypted($payinfo) ) {
137     $paymask = 'N/A';
138   } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
139     $paymask = 'N/A (tokenized)'; #?
140   } else {
141     # if not, mask it...
142     if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
143       # Credit Cards
144       my $conf = new FS::Conf;
145       my $mask_method = $conf->config('card_masking_method') || 'first6last4';
146       $mask_method =~ /^first(\d+)last(\d+)$/
147         or die "can't parse card_masking_method $mask_method";
148       my($first, $last) = ($1, $2);
149
150       $paymask = substr($payinfo,0,$first).
151                  'x'x(length($payinfo)-$first-$last).
152                  substr($payinfo,(length($payinfo)-$last));
153     } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
154       # Checks (Show last 2 @ bank)
155       my( $account, $aba ) = split('@', $payinfo );
156       $paymask = 'x'x(length($account)-2).
157                  substr($account,(length($account)-2))."@".$aba;
158     } else { # Tie up loose ends
159       $paymask = $payinfo;
160     }
161   }
162   $paymask;
163 }
164
165 =item payinfo_check
166
167 Checks payby and payinfo.
168
169 For Customers (cust_main):
170 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
171 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
172 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
173 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
174
175 For Refunds (cust_refund):
176 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
177 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
178 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback),  or 'COMP' (free)
179
180 For Payments (cust_pay):
181 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
182 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
183 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
184 'COMP' (free) is depricated as a payment type in cust_pay
185
186 =cut
187
188 sub payinfo_check {
189   my $self = shift;
190
191   FS::payby->can_payby($self->table, $self->payby)
192     or return "Illegal payby: ". $self->payby;
193
194   if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
195     my $payinfo = $self->payinfo;
196     $payinfo =~ s/\D//g;
197     $self->payinfo($payinfo);
198     if ( $self->payinfo ) {
199       $self->payinfo =~ /^(\d{13,16})$/
200         or return "Illegal (mistyped?) credit card number (payinfo)";
201       $self->payinfo($1);
202       validate($self->payinfo) or return "Illegal credit card number";
203       return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
204                                  && cardtype($self->payinfo) eq "Unknown";
205     } else {
206       $self->payinfo('N/A'); #???
207     }
208   } else {
209     if ( $self->is_encrypted($self->payinfo) ) {
210       #something better?  all it would cause is a decryption error anyway?
211       my $error = $self->ut_anything('payinfo');
212       return $error if $error;
213     } else {
214       my $error = $self->ut_textn('payinfo');
215       return $error if $error;
216     }
217   }
218
219   '';
220
221 }
222
223 =item payby_payinfo_pretty
224
225 Returns payment method and information (suitably masked, if applicable) as
226 a human-readable string, such as:
227
228   Card #54xxxxxxxxxxxx32
229
230 or
231
232   Check #119006
233
234 =cut
235
236 sub payby_payinfo_pretty {
237   my $self = shift;
238   if ( $self->payby eq 'CARD' ) {
239     'Card #'. $self->paymask;
240   } elsif ( $self->payby eq 'CHEK' ) {
241     'E-check acct#'. $self->payinfo;
242   } elsif ( $self->payby eq 'BILL' ) {
243     'Check #'. $self->payinfo;
244   } elsif ( $self->payby eq 'PREP' ) {
245     'Prepaid card #'. $self->payinfo;
246   } elsif ( $self->payby eq 'CASH' ) {
247     'Cash '. $self->payinfo;
248   } elsif ( $self->payby eq 'WEST' ) {
249     'Western Union'; #. $self->payinfo;
250   } elsif ( $self->payby eq 'MCRD' ) {
251     'Manual credit card'; #. $self->payinfo;
252   } else {
253     $self->payby. ' '. $self->payinfo;
254   }
255 }
256
257 =back
258
259 =head1 BUGS
260
261 =head1 SEE ALSO
262
263 L<FS::payby>, L<FS::Record>
264
265 =cut
266
267 1;
268