This commit was generated by cvs2svn to compensate for changes in r6255,
[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 This class handles the following functions for payinfo...
22
23 Payment Mask (Generation and Storage)
24 Data Validation (parent checks need to be sure to call this)
25 Encryption - In the Future (Pull from Record.pm)
26 Bad Card Stuff - In the Future (Integrate Banned Pay)
27 Currency - In the Future
28
29 =head1 FIELDS
30
31 =over 4
32
33 =item payby
34
35 The following payment types (payby) are supported:
36
37 For Customers (cust_main):
38 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
39 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
40 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
41 'PREPAY' (special billing type: applies a credit and sets billing type to I<BILL> - see L<FS::prepay_credit>)
42
43 For Refunds (cust_refund):
44 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
45 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
46 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' Chargeback, or 'COMP' (free)
47
48
49 For Payments (cust_pay):
50 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
51 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
52 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
53 'COMP' (free) is depricated as a payment type in cust_pay
54
55 =cut 
56
57 # was this supposed to do something?
58  
59 #sub payby {
60 #  my($self,$payby) = @_;
61 #  if ( defined($payby) ) {
62 #    $self->setfield('payby', $payby);
63 #  } 
64 #  return $self->getfield('payby')
65 #}
66
67 =item payinfo
68
69 Payment information (payinfo) can be one of the following types:
70
71 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
72
73 =cut
74
75 sub payinfo {
76   my($self,$payinfo) = @_;
77   if ( defined($payinfo) ) {
78     $self->setfield('payinfo', $payinfo); # This is okay since we are the 'setter'
79     $self->paymask($self->mask_payinfo());
80   } else {
81     $payinfo = $self->getfield('payinfo'); # This is okay since we are the 'getter'
82     return $payinfo;
83   }
84 }
85
86 =item paycvv
87
88 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
89
90 =cut
91
92 sub paycvv {
93   my($self,$paycvv) = @_;
94   # This is only allowed in cust_main... Even then it really shouldn't be stored...
95   if ($self->table eq 'cust_main') {
96     if ( defined($paycvv) ) {
97       $self->setfield('paycvv', $paycvv); # This is okay since we are the 'setter'
98     } else {
99       $paycvv = $self->getfield('paycvv'); # This is okay since we are the 'getter'
100       return $paycvv;
101     }
102   } else {
103 #    warn "This doesn't work for other tables besides cust_main
104     '';
105   } 
106 }
107
108 =item paymask
109
110 =cut
111
112 sub paymask {
113   my($self, $paymask) = @_;
114
115   if ( defined($paymask) && $paymask ne '' ) {
116     # I hate this little bit of magic...  I don't expect it to cause a problem,
117     # but who knows...  If the payinfo is passed in masked then ignore it and
118     # set it based on the payinfo.  The only guy that should call this in this
119     # way is... $self->payinfo
120     $self->setfield('paymask', $self->mask_payinfo());
121
122   } else {
123
124     $paymask=$self->getfield('paymask');
125     if (!defined($paymask) || $paymask eq '') {
126       # Generate it if it's blank - Note that we're not going to set it - just
127       # generate
128       $paymask = $self->mask_payinfo();
129     }
130
131   }
132
133   return $paymask;
134 }
135
136 =back
137
138 =head1 METHODS
139
140 =over 4
141
142 =item mask_payinfo [ PAYBY, PAYINFO ]
143
144 This method converts the payment info (credit card, bank account, etc.) into a
145 masked string.
146
147 Optionally, an arbitrary payby and payinfo can be passed.
148
149 =cut
150
151 sub mask_payinfo {
152   my $self = shift;
153   my $payby   = scalar(@_) ? shift : $self->payby;
154   my $payinfo = scalar(@_) ? shift : $self->payinfo;
155
156   # Check to see if it's encrypted...
157   my $paymask;
158   if ( $self->is_encrypted($payinfo) ) {
159     $paymask = 'N/A';
160   } else {
161     # if not, mask it...
162     if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
163       # Credit Cards
164       my $conf = new FS::Conf;
165       my $mask_method = $conf->config('card_masking_method') || 'first6last4';
166       $mask_method =~ /^first(\d+)last(\d+)$/
167         or die "can't parse card_masking_method $mask_method";
168       my($first, $last) = ($1, $2);
169
170       $paymask = substr($payinfo,0,$first).
171                  'x'x(length($payinfo)-$first-$last).
172                  substr($payinfo,(length($payinfo)-$last));
173     } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
174       # Checks (Show last 2 @ bank)
175       my( $account, $aba ) = split('@', $payinfo );
176       $paymask = 'x'x(length($account)-2).
177                  substr($account,(length($account)-2))."@".$aba;
178     } else { # Tie up loose ends
179       $paymask = $payinfo;
180     }
181   }
182   return $paymask;
183 }
184
185 =cut
186
187 sub _mask_payinfo {
188   my $self = shift;
189
190 =item payinfo_check
191
192 Checks payby and payinfo.
193
194 For Customers (cust_main):
195 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
196 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
197 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
198 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
199
200 For Refunds (cust_refund):
201 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
202 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
203 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback),  or 'COMP' (free)
204
205 For Payments (cust_pay):
206 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
207 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
208 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
209 'COMP' (free) is depricated as a payment type in cust_pay
210
211 =cut
212
213 sub payinfo_check {
214   my $self = shift;
215
216   FS::payby->can_payby($self->table, $self->payby)
217     or return "Illegal payby: ". $self->payby;
218
219   if ( $self->payby eq 'CARD' ) {
220     my $payinfo = $self->payinfo;
221     $payinfo =~ s/\D//g;
222     $self->payinfo($payinfo);
223     if ( $self->payinfo ) {
224       $self->payinfo =~ /^(\d{13,16})$/
225         or return "Illegal (mistyped?) credit card number (payinfo)";
226       $self->payinfo($1);
227       validate($self->payinfo) or return "Illegal credit card number";
228       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
229     } else {
230       $self->payinfo('N/A');
231     }
232   } else {
233     my $error = $self->ut_textn('payinfo');
234     return $error if $error;
235   }
236 }
237
238 =head1 BUGS
239
240 Have to add the future items...
241
242 =head1 SEE ALSO
243
244 L<FS::payby>, L<FS::Record>
245
246 =cut
247
248 1;
249