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