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