1 package FS::cust_payby;
4 use base qw( FS::payinfo_Mixin FS::Record );
6 use FS::Record qw( qsearchs ); #qsearch;
10 use vars qw( $conf @encrypted_fields
11 $ignore_expired_card $ignore_banned_card
14 @encrypted_fields = ('payinfo', 'paycvv');
15 sub nohistory_fields { ('payinfo', 'paycvv'); }
17 $ignore_expired_card = 0;
18 $ignore_banned_card = 0;
20 install_callback FS::UID sub {
22 #yes, need it for stuff below (prolly should be cached)
27 FS::cust_payby - Object methods for cust_payby records
33 $record = new FS::cust_payby \%hash;
34 $record = new FS::cust_payby { 'column' => 'value' };
36 $error = $record->insert;
38 $error = $new_record->replace($old_record);
40 $error = $record->delete;
42 $error = $record->check;
46 An FS::cust_payby object represents customer stored payment information.
47 FS::cust_payby inherits from FS::Record. The following fields are currently
121 Creates a new record. To add the record to the database, see L<"insert">.
123 Note that this stores the hash reference, not a distinct copy of the hash it
124 points to. You can ask the object for a copy with the I<hash> method.
128 # the new method can be inherited from FS::Record, if a table method is defined
130 sub table { 'cust_payby'; }
134 Adds this record to the database. If there is an error, returns the error,
135 otherwise returns false.
139 # the insert method can be inherited from FS::Record
143 Delete this record from the database.
147 # the delete method can be inherited from FS::Record
149 =item replace OLD_RECORD
151 Replaces the OLD_RECORD with this one in the database. If there is an error,
152 returns the error, otherwise returns false.
156 # the replace method can be inherited from FS::Record
160 Checks all fields to make sure this is a valid record. If there is
161 an error, returns the error, otherwise returns false. Called by the insert
170 $self->ut_numbern('custpaybynum')
171 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
172 || $self->ut_number('weight')
173 #encrypted #|| $self->ut_textn('payinfo')
174 #encrypted #|| $self->ut_textn('paycvv')
175 # || $self->ut_textn('paymask') #XXX something
176 #later #|| $self->ut_textn('paydate')
177 || $self->ut_numbern('paystart_month')
178 || $self->ut_numbern('paystart_year')
179 || $self->ut_numbern('payissue')
180 # || $self->ut_textn('payname') #XXX something
181 || $self->ut_alphan('paystate')
182 || $self->ut_textn('paytype')
183 || $self->ut_ipn('payip')
185 return $error if $error;
189 FS::payby->can_payby($self->table, $self->payby)
190 or return "Illegal payby: ". $self->payby;
192 # If it is encrypted and the private key is not availaible then we can't
193 # check the credit card.
194 my $check_payinfo = ! $self->is_encrypted($self->payinfo);
196 # Need some kind of global flag to accept invalid cards, for testing
198 #XXX if ( !$import && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
199 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
201 my $payinfo = $self->payinfo;
203 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
204 or return gettext('invalid_card'); # . ": ". $self->payinfo;
206 $self->payinfo($payinfo);
208 or return gettext('invalid_card'); # . ": ". $self->payinfo;
210 return gettext('unknown_card_type')
211 if $self->payinfo !~ /^99\d{14}$/ #token
212 && cardtype($self->payinfo) eq "Unknown";
214 unless ( $ignore_banned_card ) {
215 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
217 if ( $ban->bantype eq 'warn' ) {
218 #or others depending on value of $ban->reason ?
219 return '_duplicate_card'.
220 ': disabled from'. time2str('%a %h %o at %r', $ban->_date).
221 ' until '. time2str('%a %h %o at %r', $ban->_end_date).
222 ' (ban# '. $ban->bannum. ')'
223 unless $self->override_ban_warn;
225 return 'Banned credit card: banned on '.
226 time2str('%a %h %o at %r', $ban->_date).
227 ' by '. $ban->otaker.
228 ' (ban# '. $ban->bannum. ')';
233 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
234 if ( cardtype($self->payinfo) eq 'American Express card' ) {
235 $self->paycvv =~ /^(\d{4})$/
236 or return "CVV2 (CID) for American Express cards is four digits.";
239 $self->paycvv =~ /^(\d{3})$/
240 or return "CVV2 (CVC2/CID) is three digits.";
247 my $cardtype = cardtype($payinfo);
248 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
250 return "Start date or issue number is required for $cardtype cards"
251 unless $self->paystart_month && $self->paystart_year or $self->payissue;
253 return "Start month must be between 1 and 12"
254 if $self->paystart_month
255 and $self->paystart_month < 1 || $self->paystart_month > 12;
257 return "Start year must be 1990 or later"
258 if $self->paystart_year
259 and $self->paystart_year < 1990;
261 return "Issue number must be beween 1 and 99"
263 and $self->payissue < 1 || $self->payissue > 99;
266 $self->paystart_month('');
267 $self->paystart_year('');
271 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
273 my $payinfo = $self->payinfo;
274 $payinfo =~ s/[^\d\@\.]//g;
275 if ( $conf->config('echeck-country') eq 'CA' ) {
276 $payinfo =~ /^(\d+)\@(\d{5})\.(\d{3})$/
277 or return 'invalid echeck account@branch.bank';
278 $payinfo = "$1\@$2.$3";
279 } elsif ( $conf->config('echeck-country') eq 'US' ) {
280 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
283 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@routing';
286 $self->payinfo($payinfo);
289 unless ( $ignore_banned_card ) {
290 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
292 if ( $ban->bantype eq 'warn' ) {
293 #or others depending on value of $ban->reason ?
294 return '_duplicate_ach' unless $self->override_ban_warn;
296 return 'Banned ACH account: banned on '.
297 time2str('%a %h %o at %r', $ban->_date).
298 ' by '. $ban->otaker.
299 ' (ban# '. $ban->bannum. ')';
304 } elsif ( $self->payby eq 'LECB' ) {
306 my $payinfo = $self->payinfo;
308 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
310 $self->payinfo($payinfo);
313 } elsif ( $self->payby eq 'BILL' ) {
315 $error = $self->ut_textn('payinfo');
316 return "Illegal P.O. number: ". $self->payinfo if $error;
319 } elsif ( $self->payby eq 'COMP' ) {
321 my $curuser = $FS::CurrentUser::CurrentUser;
322 if ( ! $self->custnum
323 && ! $curuser->access_right('Complimentary customer')
326 return "You are not permitted to create complimentary accounts."
329 $error = $self->ut_textn('payinfo');
330 return "Illegal comp account issuer: ". $self->payinfo if $error;
333 } elsif ( $self->payby eq 'PREPAY' ) {
335 my $payinfo = $self->payinfo;
336 $payinfo =~ s/\W//g; #anything else would just confuse things
337 $self->payinfo($payinfo);
338 $error = $self->ut_alpha('payinfo');
339 return "Illegal prepayment identifier: ". $self->payinfo if $error;
340 return "Unknown prepayment identifier"
341 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
346 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
347 return "Expiration date required"
348 # shouldn't payinfo_check do this?
349 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD|PPAL)$/;
353 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
354 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
355 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
356 ( $m, $y ) = ( $2, "19$1" );
357 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
358 ( $m, $y ) = ( $3, "20$2" );
360 return "Illegal expiration date: ". $self->paydate;
362 $m = sprintf('%02d',$m);
363 $self->paydate("$y-$m-01");
364 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
365 return gettext('expired_card')
368 !$ignore_expired_card
369 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
372 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
373 ( ! $conf->exists('require_cardname')
374 || $self->payby !~ /^(CARD|DCRD)$/ )
376 $self->payname( $self->first. " ". $self->getfield('last') );
378 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
379 or return gettext('illegal_name'). " payname: ". $self->payname;
394 L<FS::Record>, schema.html from the base documentation.