1 package FS::cust_payby;
4 use base qw( FS::payinfo_Mixin FS::Record );
6 use FS::Record qw( qsearchs ); #qsearch;
9 use Business::CreditCard qw( validate cardtype );
11 use vars qw( $conf @encrypted_fields
12 $ignore_expired_card $ignore_banned_card
15 @encrypted_fields = ('payinfo', 'paycvv');
16 sub nohistory_fields { ('payinfo', 'paycvv'); }
18 $ignore_expired_card = 0;
19 $ignore_banned_card = 0;
21 install_callback FS::UID sub {
23 #yes, need it for stuff below (prolly should be cached)
28 FS::cust_payby - Object methods for cust_payby records
34 $record = new FS::cust_payby \%hash;
35 $record = new FS::cust_payby { 'column' => 'value' };
37 $error = $record->insert;
39 $error = $new_record->replace($old_record);
41 $error = $record->delete;
43 $error = $record->check;
47 An FS::cust_payby object represents customer stored payment information.
48 FS::cust_payby inherits from FS::Record. The following fields are currently
122 Creates a new record. To add the record to the database, see L<"insert">.
124 Note that this stores the hash reference, not a distinct copy of the hash it
125 points to. You can ask the object for a copy with the I<hash> method.
129 # the new method can be inherited from FS::Record, if a table method is defined
131 sub table { 'cust_payby'; }
135 Adds this record to the database. If there is an error, returns the error,
136 otherwise returns false.
140 # the insert method can be inherited from FS::Record
144 Delete this record from the database.
148 # the delete method can be inherited from FS::Record
150 =item replace OLD_RECORD
152 Replaces the OLD_RECORD with this one in the database. If there is an error,
153 returns the error, otherwise returns false.
157 # the replace method can be inherited from FS::Record
161 Checks all fields to make sure this is a valid record. If there is
162 an error, returns the error, otherwise returns false. Called by the insert
171 $self->ut_numbern('custpaybynum')
172 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
173 || $self->ut_number('weight')
174 #encrypted #|| $self->ut_textn('payinfo')
175 #encrypted #|| $self->ut_textn('paycvv')
176 # || $self->ut_textn('paymask') #XXX something
177 #later #|| $self->ut_textn('paydate')
178 || $self->ut_numbern('paystart_month')
179 || $self->ut_numbern('paystart_year')
180 || $self->ut_numbern('payissue')
181 # || $self->ut_textn('payname') #XXX something
182 || $self->ut_alphan('paystate')
183 || $self->ut_textn('paytype')
184 || $self->ut_ipn('payip')
186 return $error if $error;
190 FS::payby->can_payby($self->table, $self->payby)
191 or return "Illegal payby: ". $self->payby;
193 # If it is encrypted and the private key is not availaible then we can't
194 # check the credit card.
195 my $check_payinfo = ! $self->is_encrypted($self->payinfo);
197 # Need some kind of global flag to accept invalid cards, for testing
199 #XXX if ( !$import && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
200 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
202 my $payinfo = $self->payinfo;
204 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
205 or return gettext('invalid_card'); # . ": ". $self->payinfo;
207 $self->payinfo($payinfo);
209 or return gettext('invalid_card'); # . ": ". $self->payinfo;
211 return gettext('unknown_card_type')
212 if $self->payinfo !~ /^99\d{14}$/ #token
213 && cardtype($self->payinfo) eq "Unknown";
215 unless ( $ignore_banned_card ) {
216 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
218 if ( $ban->bantype eq 'warn' ) {
219 #or others depending on value of $ban->reason ?
220 return '_duplicate_card'.
221 ': disabled from'. time2str('%a %h %o at %r', $ban->_date).
222 ' until '. time2str('%a %h %o at %r', $ban->_end_date).
223 ' (ban# '. $ban->bannum. ')'
224 unless $self->override_ban_warn;
226 return 'Banned credit card: banned on '.
227 time2str('%a %h %o at %r', $ban->_date).
228 ' by '. $ban->otaker.
229 ' (ban# '. $ban->bannum. ')';
234 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
235 if ( cardtype($self->payinfo) eq 'American Express card' ) {
236 $self->paycvv =~ /^(\d{4})$/
237 or return "CVV2 (CID) for American Express cards is four digits.";
240 $self->paycvv =~ /^(\d{3})$/
241 or return "CVV2 (CVC2/CID) is three digits.";
248 my $cardtype = cardtype($payinfo);
249 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
251 return "Start date or issue number is required for $cardtype cards"
252 unless $self->paystart_month && $self->paystart_year or $self->payissue;
254 return "Start month must be between 1 and 12"
255 if $self->paystart_month
256 and $self->paystart_month < 1 || $self->paystart_month > 12;
258 return "Start year must be 1990 or later"
259 if $self->paystart_year
260 and $self->paystart_year < 1990;
262 return "Issue number must be beween 1 and 99"
264 and $self->payissue < 1 || $self->payissue > 99;
267 $self->paystart_month('');
268 $self->paystart_year('');
272 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
274 my $payinfo = $self->payinfo;
275 $payinfo =~ s/[^\d\@\.]//g;
276 if ( $conf->config('echeck-country') eq 'CA' ) {
277 $payinfo =~ /^(\d+)\@(\d{5})\.(\d{3})$/
278 or return 'invalid echeck account@branch.bank';
279 $payinfo = "$1\@$2.$3";
280 } elsif ( $conf->config('echeck-country') eq 'US' ) {
281 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
284 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@routing';
287 $self->payinfo($payinfo);
290 unless ( $ignore_banned_card ) {
291 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
293 if ( $ban->bantype eq 'warn' ) {
294 #or others depending on value of $ban->reason ?
295 return '_duplicate_ach' unless $self->override_ban_warn;
297 return 'Banned ACH account: banned on '.
298 time2str('%a %h %o at %r', $ban->_date).
299 ' by '. $ban->otaker.
300 ' (ban# '. $ban->bannum. ')';
305 } elsif ( $self->payby eq 'LECB' ) {
307 my $payinfo = $self->payinfo;
309 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
311 $self->payinfo($payinfo);
314 } elsif ( $self->payby eq 'BILL' ) {
316 $error = $self->ut_textn('payinfo');
317 return "Illegal P.O. number: ". $self->payinfo if $error;
320 } elsif ( $self->payby eq 'COMP' ) {
322 my $curuser = $FS::CurrentUser::CurrentUser;
323 if ( ! $self->custnum
324 && ! $curuser->access_right('Complimentary customer')
327 return "You are not permitted to create complimentary accounts."
330 $error = $self->ut_textn('payinfo');
331 return "Illegal comp account issuer: ". $self->payinfo if $error;
334 } elsif ( $self->payby eq 'PREPAY' ) {
336 my $payinfo = $self->payinfo;
337 $payinfo =~ s/\W//g; #anything else would just confuse things
338 $self->payinfo($payinfo);
339 $error = $self->ut_alpha('payinfo');
340 return "Illegal prepayment identifier: ". $self->payinfo if $error;
341 return "Unknown prepayment identifier"
342 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
347 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
348 return "Expiration date required"
349 # shouldn't payinfo_check do this?
350 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD|PPAL)$/;
354 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
355 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
356 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
357 ( $m, $y ) = ( $2, "19$1" );
358 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
359 ( $m, $y ) = ( $3, "20$2" );
361 return "Illegal expiration date: ". $self->paydate;
363 $m = sprintf('%02d',$m);
364 $self->paydate("$y-$m-01");
365 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
366 return gettext('expired_card')
369 !$ignore_expired_card
370 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
373 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
374 ( ! $conf->exists('require_cardname')
375 || $self->payby !~ /^(CARD|DCRD)$/ )
377 $self->payname( $self->first. " ". $self->getfield('last') );
379 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
380 or return gettext('illegal_name'). " payname: ". $self->payname;
395 L<FS::Record>, schema.html from the base documentation.