1 package FS::payinfo_Mixin;
4 use Business::CreditCard;
9 FS::payinfo_Mixin - Mixin class for records in tables that contain payinfo.
13 package FS::some_table;
15 @ISA = qw( FS::payinfo_Mixin FS::Record );
19 This is a mixin class for records that contain payinfo.
27 The following payment types (payby) are supported:
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>)
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)
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
49 # was this supposed to do something?
52 # my($self,$payby) = @_;
53 # if ( defined($payby) ) {
54 # $self->setfield('payby', $payby);
56 # return $self->getfield('payby')
61 Payment information (payinfo) can be one of the following types:
63 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
68 my($self,$payinfo) = @_;
70 if ( defined($payinfo) ) {
71 $self->setfield('payinfo', $payinfo);
72 $self->paymask($self->mask_payinfo) unless $payinfo =~ /^99\d{14}$/; #token
74 $self->getfield('payinfo');
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
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'
91 $paycvv = $self->getfield('paycvv'); # This is okay since we are the 'getter'
95 # warn "This doesn't work for other tables besides cust_main
105 my($self, $paymask) = @_;
107 if ( defined($paymask) ) {
108 $self->setfield('paymask', $paymask);
110 $self->getfield('paymask') || $self->mask_payinfo;
120 =item mask_payinfo [ PAYBY, PAYINFO ]
122 This method converts the payment info (credit card, bank account, etc.) into a
125 Optionally, an arbitrary payby and payinfo can be passed.
131 my $payby = scalar(@_) ? shift : $self->payby;
132 my $payinfo = scalar(@_) ? shift : $self->payinfo;
134 # Check to see if it's encrypted...
135 if ( $self->is_encrypted($payinfo) ) {
137 } elsif ( $payinfo =~ /^99\d{14}$/ || $payinfo eq 'N/A' ) { #token
138 return 'N/A (tokenized)'; #?
139 } else { # if not, mask it...
141 if ($payby eq 'CARD' || $payby eq 'DCRD' || $payby eq 'MCRD') {
145 # special handling for Local Isracards: always show last 4
146 if ( $payinfo =~ /^(\d{8,9})$/ ) {
148 return 'x'x(length($payinfo)-4).
149 substr($payinfo,(length($payinfo)-4));
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);
159 return substr($payinfo,0,$first).
160 'x'x(length($payinfo)-$first-$last).
161 substr($payinfo,(length($payinfo)-$last));
163 } elsif ($payby eq 'CHEK' || $payby eq 'DCHK' ) {
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 : '');
171 } else { # Tie up loose ends
175 #die "shouldn't be reached";
180 Checks payby and payinfo.
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>)
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)
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
204 FS::payby->can_payby($self->table, $self->payby)
205 or return "Illegal payby: ". $self->payby;
207 if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
208 my $payinfo = $self->payinfo;
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)";
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";
219 $self->payinfo('N/A'); #???
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;
227 my $error = $self->ut_textn('payinfo');
228 return $error if $error;
236 =item payby_payinfo_pretty
238 Returns payment method and information (suitably masked, if applicable) as
239 a human-readable string, such as:
241 Card #54xxxxxxxxxxxx32
249 sub payby_payinfo_pretty {
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;
266 $self->payby. ' '. $self->payinfo;
276 L<FS::payby>, L<FS::Record>