communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[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 Pretty printing
26
27 =head1 FIELDS
28
29 =over 4
30
31 =item payby
32
33 The following payment types (payby) are supported:
34
35 For Customers (cust_main):
36 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
37 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
38 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
39 'PREPAY' (special billing type: applies a credit and sets billing type to I<BILL> - see L<FS::prepay_credit>)
40
41 For Refunds (cust_refund):
42 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
43 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
44 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' Chargeback, or 'COMP' (free)
45
46
47 For Payments (cust_pay):
48 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
49 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
50 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
51 'COMP' (free) is depricated as a payment type in cust_pay
52
53 =cut 
54
55 # was this supposed to do something?
56  
57 #sub payby {
58 #  my($self,$payby) = @_;
59 #  if ( defined($payby) ) {
60 #    $self->setfield('payby', $payby);
61 #  } 
62 #  return $self->getfield('payby')
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
106 =item paymask
107
108 =cut
109
110 sub paymask {
111   my($self, $paymask) = @_;
112
113   if ( defined($paymask) && $paymask ne '' ) {
114     # I hate this little bit of magic...  I don't expect it to cause a problem,
115     # but who knows...  If the payinfo is passed in masked then ignore it and
116     # set it based on the payinfo.  The only guy that should call this in this
117     # way is... $self->payinfo
118     $self->setfield('paymask', $self->mask_payinfo());
119
120   } else {
121
122     $paymask=$self->getfield('paymask');
123     if (!defined($paymask) || $paymask eq '') {
124       # Generate it if it's blank - Note that we're not going to set it - just
125       # generate
126       $paymask = $self->mask_payinfo();
127     }
128
129   }
130
131   return $paymask;
132 }
133
134 =back
135
136 =head1 METHODS
137
138 =over 4
139
140 =item mask_payinfo [ PAYBY, PAYINFO ]
141
142 This method converts the payment info (credit card, bank account, etc.) into a
143 masked string.
144
145 Optionally, an arbitrary payby and payinfo can be passed.
146
147 =cut
148
149 sub mask_payinfo {
150   my $self = shift;
151   my $payby   = scalar(@_) ? shift : $self->payby;
152   my $payinfo = scalar(@_) ? shift : $self->payinfo;
153
154   # Check to see if it's encrypted...
155   my $paymask;
156   if ( $self->is_encrypted($payinfo) ) {
157     $paymask = 'N/A';
158   } else {
159     # if not, mask it...
160     if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
161       # Credit Cards
162       my $conf = new FS::Conf;
163       my $mask_method = $conf->config('card_masking_method') || 'first6last4';
164       $mask_method =~ /^first(\d+)last(\d+)$/
165         or die "can't parse card_masking_method $mask_method";
166       my($first, $last) = ($1, $2);
167
168       $paymask = substr($payinfo,0,$first).
169                  'x'x(length($payinfo)-$first-$last).
170                  substr($payinfo,(length($payinfo)-$last));
171     } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
172       # Checks (Show last 2 @ bank)
173       my( $account, $aba ) = split('@', $payinfo );
174       $paymask = 'x'x(length($account)-2).
175                  substr($account,(length($account)-2))."@".$aba;
176     } else { # Tie up loose ends
177       $paymask = $payinfo;
178     }
179   }
180   return $paymask;
181 }
182
183 =item payinfo_check
184
185 Checks payby and payinfo.
186
187 For Customers (cust_main):
188 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
189 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
190 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
191 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
192
193 For Refunds (cust_refund):
194 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
195 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
196 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback),  or 'COMP' (free)
197
198 For Payments (cust_pay):
199 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
200 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
201 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
202 'COMP' (free) is depricated as a payment type in cust_pay
203
204 =cut
205
206 sub payinfo_check {
207   my $self = shift;
208
209   FS::payby->can_payby($self->table, $self->payby)
210     or return "Illegal payby: ". $self->payby;
211
212   if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
213     my $payinfo = $self->payinfo;
214     $payinfo =~ s/\D//g;
215     $self->payinfo($payinfo);
216     if ( $self->payinfo ) {
217       $self->payinfo =~ /^(\d{13,16})$/
218         or return "Illegal (mistyped?) credit card number (payinfo)";
219       $self->payinfo($1);
220       validate($self->payinfo) or return "Illegal credit card number";
221       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
222     } else {
223       $self->payinfo('N/A'); #???
224     }
225   } else {
226     if ( $self->is_encrypted($self->payinfo) ) {
227       #something better?  all it would cause is a decryption error anyway?
228       my $error = $self->ut_anything('payinfo');
229       return $error if $error;
230     } else {
231       my $error = $self->ut_textn('payinfo');
232       return $error if $error;
233     }
234   }
235
236   '';
237
238 }
239
240 =item payby_payinfo_pretty
241
242 Returns payment method and information (suitably masked, if applicable) as
243 a human-readable string, such as:
244
245   Card #54xxxxxxxxxxxx32
246
247 or
248
249   Check #119006
250
251 =cut
252
253 sub payby_payinfo_pretty {
254   my $self = shift;
255   if ( $self->payby eq 'CARD' ) {
256     'Card #'. $self->paymask;
257   } elsif ( $self->payby eq 'CHEK' ) {
258     'E-check acct#'. $self->payinfo;
259   } elsif ( $self->payby eq 'BILL' ) {
260     'Check #'. $self->payinfo;
261   } elsif ( $self->payby eq 'PREP' ) {
262     'Prepaid card #'. $self->payinfo;
263   } elsif ( $self->payby eq 'CASH' ) {
264     'Cash '. $self->payinfo;
265   } elsif ( $self->payby eq 'WEST' ) {
266     'Western Union'; #. $self->payinfo;
267   } elsif ( $self->payby eq 'MCRD' ) {
268     'Manual credit card'; #. $self->payinfo;
269   } else {
270     $self->payby. ' '. $self->payinfo;
271   }
272 }
273
274 =back
275
276 =head1 BUGS
277
278 Future items?
279   Encryption - In the Future (Pull from Record.pm)
280   Bad Card Stuff - In the Future (Integrate Banned Pay)
281   Currency - In the Future
282
283 =head1 SEE ALSO
284
285 L<FS::payby>, L<FS::Record>
286
287 =cut
288
289 1;
290