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 );
10 use FS::Msgcat qw( gettext );
12 use vars qw( $conf @encrypted_fields
13 $ignore_expired_card $ignore_banned_card
17 @encrypted_fields = ('payinfo', 'paycvv');
18 sub nohistory_fields { ('payinfo', 'paycvv'); }
20 $ignore_expired_card = 0;
21 $ignore_banned_card = 0;
22 $ignore_invalid_card = 0;
24 install_callback FS::UID sub {
26 #yes, need it for stuff below (prolly should be cached)
27 $ignore_invalid_card = $conf->exists('allow_invalid_cards');
32 FS::cust_payby - Object methods for cust_payby records
38 $record = new FS::cust_payby \%hash;
39 $record = new FS::cust_payby { 'column' => 'value' };
41 $error = $record->insert;
43 $error = $new_record->replace($old_record);
45 $error = $record->delete;
47 $error = $record->check;
51 An FS::cust_payby object represents customer stored payment information.
52 FS::cust_payby inherits from FS::Record. The following fields are currently
126 Creates a new record. To add the record to the database, see L<"insert">.
128 Note that this stores the hash reference, not a distinct copy of the hash it
129 points to. You can ask the object for a copy with the I<hash> method.
133 # the new method can be inherited from FS::Record, if a table method is defined
135 sub table { 'cust_payby'; }
139 Adds this record to the database. If there is an error, returns the error,
140 otherwise returns false.
144 # the insert method can be inherited from FS::Record
148 Delete this record from the database.
152 # the delete method can be inherited from FS::Record
154 =item replace OLD_RECORD
156 Replaces the OLD_RECORD with this one in the database. If there is an error,
157 returns the error, otherwise returns false.
161 # the replace method can be inherited from FS::Record
165 Checks all fields to make sure this is a valid record. If there is
166 an error, returns the error, otherwise returns false. Called by the insert
175 $self->ut_numbern('custpaybynum')
176 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
177 || $self->ut_number('weight')
178 #encrypted #|| $self->ut_textn('payinfo')
179 #encrypted #|| $self->ut_textn('paycvv')
180 # || $self->ut_textn('paymask') #XXX something
181 #later #|| $self->ut_textn('paydate')
182 || $self->ut_numbern('paystart_month')
183 || $self->ut_numbern('paystart_year')
184 || $self->ut_numbern('payissue')
185 # || $self->ut_textn('payname') #XXX something
186 || $self->ut_alphan('paystate')
187 || $self->ut_textn('paytype')
188 || $self->ut_ipn('payip')
190 return $error if $error;
194 FS::payby->can_payby($self->table, $self->payby)
195 or return "Illegal payby: ". $self->payby;
197 # If it is encrypted and the private key is not availaible then we can't
198 # check the credit card.
199 my $check_payinfo = ! $self->is_encrypted($self->payinfo);
201 # Need some kind of global flag to accept invalid cards, for testing
203 #XXX if ( !$import && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
204 if ( !$ignore_invalid_card &&
205 $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
207 my $payinfo = $self->payinfo;
209 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
210 or return gettext('invalid_card'); # . ": ". $self->payinfo;
212 $self->payinfo($payinfo);
214 or return gettext('invalid_card'); # . ": ". $self->payinfo;
216 return gettext('unknown_card_type')
217 if $self->payinfo !~ /^99\d{14}$/ #token
218 && cardtype($self->payinfo) eq "Unknown";
220 unless ( $ignore_banned_card ) {
221 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
223 if ( $ban->bantype eq 'warn' ) {
224 #or others depending on value of $ban->reason ?
225 return '_duplicate_card'.
226 ': disabled from'. time2str('%a %h %o at %r', $ban->_date).
227 ' until '. time2str('%a %h %o at %r', $ban->_end_date).
228 ' (ban# '. $ban->bannum. ')'
229 unless $self->override_ban_warn;
231 return 'Banned credit card: banned on '.
232 time2str('%a %h %o at %r', $ban->_date).
233 ' by '. $ban->otaker.
234 ' (ban# '. $ban->bannum. ')';
239 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
240 if ( cardtype($self->payinfo) eq 'American Express card' ) {
241 $self->paycvv =~ /^(\d{4})$/
242 or return "CVV2 (CID) for American Express cards is four digits.";
245 $self->paycvv =~ /^(\d{3})$/
246 or return "CVV2 (CVC2/CID) is three digits.";
253 my $cardtype = cardtype($payinfo);
254 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
256 return "Start date or issue number is required for $cardtype cards"
257 unless $self->paystart_month && $self->paystart_year or $self->payissue;
259 return "Start month must be between 1 and 12"
260 if $self->paystart_month
261 and $self->paystart_month < 1 || $self->paystart_month > 12;
263 return "Start year must be 1990 or later"
264 if $self->paystart_year
265 and $self->paystart_year < 1990;
267 return "Issue number must be beween 1 and 99"
269 and $self->payissue < 1 || $self->payissue > 99;
272 $self->paystart_month('');
273 $self->paystart_year('');
277 } elsif ( !$ignore_invalid_card &&
278 $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
280 my $payinfo = $self->payinfo;
281 $payinfo =~ s/[^\d\@\.]//g;
282 if ( $conf->config('echeck-country') eq 'CA' ) {
283 $payinfo =~ /^(\d+)\@(\d{5})\.(\d{3})$/
284 or return 'invalid echeck account@branch.bank';
285 $payinfo = "$1\@$2.$3";
286 } elsif ( $conf->config('echeck-country') eq 'US' ) {
287 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
290 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@routing';
293 $self->payinfo($payinfo);
296 unless ( $ignore_banned_card ) {
297 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
299 if ( $ban->bantype eq 'warn' ) {
300 #or others depending on value of $ban->reason ?
301 return '_duplicate_ach' unless $self->override_ban_warn;
303 return 'Banned ACH account: banned on '.
304 time2str('%a %h %o at %r', $ban->_date).
305 ' by '. $ban->otaker.
306 ' (ban# '. $ban->bannum. ')';
311 } elsif ( $self->payby eq 'LECB' ) {
313 my $payinfo = $self->payinfo;
315 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
317 $self->payinfo($payinfo);
320 } elsif ( $self->payby eq 'BILL' ) {
322 $error = $self->ut_textn('payinfo');
323 return "Illegal P.O. number: ". $self->payinfo if $error;
326 } elsif ( $self->payby eq 'COMP' ) {
328 my $curuser = $FS::CurrentUser::CurrentUser;
329 if ( ! $self->custnum
330 && ! $curuser->access_right('Complimentary customer')
333 return "You are not permitted to create complimentary accounts."
336 $error = $self->ut_textn('payinfo');
337 return "Illegal comp account issuer: ". $self->payinfo if $error;
340 } elsif ( $self->payby eq 'PREPAY' ) {
342 my $payinfo = $self->payinfo;
343 $payinfo =~ s/\W//g; #anything else would just confuse things
344 $self->payinfo($payinfo);
345 $error = $self->ut_alpha('payinfo');
346 return "Illegal prepayment identifier: ". $self->payinfo if $error;
347 return "Unknown prepayment identifier"
348 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
353 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
354 return "Expiration date required"
355 # shouldn't payinfo_check do this?
356 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD|PPAL)$/;
360 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
361 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
362 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
363 ( $m, $y ) = ( $2, "19$1" );
364 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
365 ( $m, $y ) = ( $3, "20$2" );
367 return "Illegal expiration date: ". $self->paydate;
369 $m = sprintf('%02d',$m);
370 $self->paydate("$y-$m-01");
371 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
372 return gettext('expired_card')
375 !$ignore_expired_card
376 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
379 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
380 ( ! $conf->exists('require_cardname')
381 || $self->payby !~ /^(CARD|DCRD)$/ )
383 $self->payname( $self->first. " ". $self->getfield('last') );
385 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
386 or return gettext('illegal_name'). " payname: ". $self->payname;
401 L<FS::Record>, schema.html from the base documentation.