1 package FS::payinfo_Mixin;
4 use Business::CreditCard;
6 use FS::Record qw(qsearch);
10 FS::payinfo_Mixin - Mixin class for records in tables that contain payinfo.
14 package FS::some_table;
16 @ISA = qw( FS::payinfo_Mixin FS::Record );
20 This is a mixin class for records that contain payinfo.
28 The following payment types (payby) are supported:
30 For Customers (cust_main):
31 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
32 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
33 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
34 'PREPAY' (special billing type: applies a credit and sets billing type to I<BILL> - see L<FS::prepay_credit>)
36 For Refunds (cust_refund):
37 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
38 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
39 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' Chargeback, or 'COMP' (free)
42 For Payments (cust_pay):
43 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
44 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
45 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
46 'COMP' (free) is depricated as a payment type in cust_pay
50 # was this supposed to do something?
53 # my($self,$payby) = @_;
54 # if ( defined($payby) ) {
55 # $self->setfield('payby', $payby);
57 # return $self->getfield('payby')
62 Payment information (payinfo) can be one of the following types:
64 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
69 my($self,$payinfo) = @_;
71 if ( defined($payinfo) ) {
72 $self->setfield('payinfo', $payinfo);
73 $self->paymask($self->mask_payinfo) unless $payinfo =~ /^99\d{14}$/; #token
75 $self->getfield('payinfo');
81 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
86 my($self,$paycvv) = @_;
87 # This is only allowed in cust_main... Even then it really shouldn't be stored...
88 if ($self->table eq 'cust_main') {
89 if ( defined($paycvv) ) {
90 $self->setfield('paycvv', $paycvv); # This is okay since we are the 'setter'
92 $paycvv = $self->getfield('paycvv'); # This is okay since we are the 'getter'
96 # warn "This doesn't work for other tables besides cust_main
106 my($self, $paymask) = @_;
108 if ( defined($paymask) ) {
109 $self->setfield('paymask', $paymask);
111 $self->getfield('paymask') || $self->mask_payinfo;
121 =item mask_payinfo [ PAYBY, PAYINFO ]
123 This method converts the payment info (credit card, bank account, etc.) into a
126 Optionally, an arbitrary payby and payinfo can be passed.
132 my $payby = scalar(@_) ? shift : $self->payby;
133 my $payinfo = scalar(@_) ? shift : $self->payinfo;
135 # Check to see if it's encrypted...
136 if ( $self->is_encrypted($payinfo) ) {
138 } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
139 return 'N/A (tokenized)'; #?
140 } else { # if not, mask it...
142 if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
146 # special handling for Local Isracards: always show last 4
147 if ( $payinfo =~ /^(\d{8,9})$/ ) {
149 return 'x'x(length($payinfo)-4).
150 substr($payinfo,(length($payinfo)-4));
154 my $conf = new FS::Conf;
155 my $mask_method = $conf->config('card_masking_method') || 'first6last4';
156 $mask_method =~ /^first(\d+)last(\d+)$/
157 or die "can't parse card_masking_method $mask_method";
158 my($first, $last) = ($1, $2);
160 return substr($payinfo,0,$first).
161 'x'x(length($payinfo)-$first-$last).
162 substr($payinfo,(length($payinfo)-$last));
164 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
166 # Checks (Show last 2 @ bank)
167 my( $account, $aba ) = split('@', $payinfo );
168 return 'x'x(length($account)-2).
169 substr($account,(length($account)-2)).
170 ( length($aba) ? "@".$aba : '');
172 } else { # Tie up loose ends
176 #die "shouldn't be reached";
181 Checks payby and payinfo.
183 For Customers (cust_main):
184 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
185 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
186 'LECB' (Phone bill billing), 'BILL' (billing), 'COMP' (free), or
187 'PREPAY' (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
189 For Refunds (cust_refund):
190 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
191 'LECB' (Phone bill billing), 'BILL' (billing), 'CASH' (cash),
192 'WEST' (Western Union), 'MCRD' (Manual credit card), 'CBAK' (Chargeback), or 'COMP' (free)
194 For Payments (cust_pay):
195 'CARD' (credit cards), 'CHEK' (electronic check/ACH),
196 'LECB' (phone bill billing), 'BILL' (billing), 'PREP' (prepaid card),
197 'CASH' (cash), 'WEST' (Western Union), or 'MCRD' (Manual credit card)
198 'COMP' (free) is depricated as a payment type in cust_pay
205 FS::payby->can_payby($self->table, $self->payby)
206 or return "Illegal payby: ". $self->payby;
208 if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
209 my $payinfo = $self->payinfo;
211 $self->payinfo($payinfo);
212 if ( $self->payinfo ) {
213 $self->payinfo =~ /^(\d{13,16}|\d{8,9})$/
214 or return "Illegal (mistyped?) credit card number (payinfo)";
216 validate($self->payinfo) or return "Illegal credit card number";
217 return "Unknown card type" if $self->payinfo !~ /^99\d{14}$/ #token
218 && cardtype($self->payinfo) eq "Unknown";
220 $self->payinfo('N/A'); #???
223 if ( $self->is_encrypted($self->payinfo) ) {
224 #something better? all it would cause is a decryption error anyway?
225 my $error = $self->ut_anything('payinfo');
226 return $error if $error;
228 my $error = $self->ut_textn('payinfo');
229 return $error if $error;
237 =item payby_payinfo_pretty
239 Returns payment method and information (suitably masked, if applicable) as
240 a human-readable string, such as:
242 Card #54xxxxxxxxxxxx32
250 sub payby_payinfo_pretty {
252 if ( $self->payby eq 'CARD' ) {
253 'Card #'. $self->paymask;
254 } elsif ( $self->payby eq 'CHEK' ) {
255 'E-check acct#'. $self->payinfo;
256 } elsif ( $self->payby eq 'BILL' ) {
257 'Check #'. $self->payinfo;
258 } elsif ( $self->payby eq 'PREP' ) {
259 'Prepaid card #'. $self->payinfo;
260 } elsif ( $self->payby eq 'CASH' ) {
261 'Cash '. $self->payinfo;
262 } elsif ( $self->payby eq 'WEST' ) {
263 'Western Union'; #. $self->payinfo;
264 } elsif ( $self->payby eq 'MCRD' ) {
265 'Manual credit card'; #. $self->payinfo;
267 $self->payby. ' '. $self->payinfo;
271 =item payinfo_used [ PAYINFO ]
273 Returns 1 if there's an existing payment using this payinfo. This can be
274 used to set the 'recurring payment' flag required by some processors.
280 my $payinfo = shift || $self->payinfo;
282 'custnum' => $self->custnum,
287 if qsearch('cust_pay', { %hash, 'payinfo' => $payinfo } )
288 || qsearch('cust_pay',
289 { %hash, 'paymask' => $self->mask_payinfo('CARD', $payinfo) } )
301 L<FS::payby>, L<FS::Record>