1 package FS::cust_main::Billing_Realtime;
4 use vars qw( $conf $DEBUG $me );
5 use vars qw( $realtime_bop_decline_quiet ); #ugh
7 use Business::CreditCard 0.35;
8 use Business::OnlinePayment;
9 use FS::UID qw( dbh myconnect );
10 use FS::Record qw( qsearch qsearchs );
11 use FS::Misc qw( send_email );
14 use FS::cust_pay_pending;
15 use FS::cust_bill_pay;
19 $realtime_bop_decline_quiet = 0;
21 # 1 is mostly method/subroutine entry and options
22 # 2 traces progress of some operations
23 # 3 is even more information including possibly sensitive data
25 $me = '[FS::cust_main::Billing_Realtime]';
28 our $BOP_TESTING_SUCCESS = 1;
30 install_callback FS::UID sub {
32 #yes, need it for stuff below (prolly should be cached)
37 FS::cust_main::Billing_Realtime - Realtime billing mixin for cust_main
43 These methods are available on FS::cust_main objects.
49 =item realtime_collect [ OPTION => VALUE ... ]
51 Attempt to collect the customer's current balance with a realtime credit
52 card or electronic check transaction (see realtime_bop() below).
54 Returns the result of realtime_bop(): nothing, an error message, or a
55 hashref of state information for a third-party transaction.
57 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
59 I<method> is one of: I<CC> or I<ECHECK>. If none is specified
60 then it is deduced from the customer record.
62 If no I<amount> is specified, then the customer balance is used.
64 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
65 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
66 if set, will override the value from the customer record.
68 I<description> is a free-text field passed to the gateway. It defaults to
69 the value defined by the business-onlinepayment-description configuration
70 option, or "Internet services" if that is unset.
72 If an I<invnum> is specified, this payment (if successful) is applied to the
75 I<apply> will automatically apply a resulting payment.
77 I<quiet> can be set true to suppress email decline notices.
79 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
80 resulting paynum, if any.
82 I<payunique> is a unique identifier for this payment.
84 I<session_id> is a session identifier associated with this payment.
86 I<depend_jobnum> allows payment capture to unlock export jobs
90 sub realtime_collect {
91 my( $self, %options ) = @_;
93 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
96 warn "$me realtime_collect:\n";
97 warn " $_ => $options{$_}\n" foreach keys %options;
100 $options{amount} = $self->balance unless exists( $options{amount} );
101 return '' unless $options{amount} > 0;
103 $options{method} = FS::payby->payby2bop($self->payby)
104 unless exists( $options{method} );
106 return $self->realtime_bop({%options});
110 =item realtime_bop { [ ARG => VALUE ... ] }
112 Runs a realtime credit card or ACH (electronic check) transaction
113 via a Business::OnlinePayment realtime gateway. See
114 L<http://420.am/business-onlinepayment> for supported gateways.
116 Required arguments in the hashref are I<method>, and I<amount>
118 Available methods are: I<CC>, I<ECHECK>, or I<PAYPAL>
120 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
122 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
123 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
124 if set, will override the value from the customer record.
126 I<description> is a free-text field passed to the gateway. It defaults to
127 the value defined by the business-onlinepayment-description configuration
128 option, or "Internet services" if that is unset.
130 If an I<invnum> is specified, this payment (if successful) is applied to the
131 specified invoice. If the customer has exactly one open invoice, that
132 invoice number will be assumed. If you don't specify an I<invnum> you might
133 want to call the B<apply_payments> method or set the I<apply> option.
135 I<no_invnum> can be set to true to prevent that default invnum from being set.
137 I<apply> can be set to true to run B<apply_payments_and_credits> on success.
139 I<no_auto_apply> can be set to true to set that flag on the resulting payment
140 (prevents payment from being applied by B<apply_payments> or B<apply_payments_and_credits>,
141 but will still be applied if I<invnum> exists...use with I<no_invnum> for intended effect.)
143 I<quiet> can be set true to surpress email decline notices.
145 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
146 resulting paynum, if any.
148 I<payunique> is a unique identifier for this payment.
150 I<session_id> is a session identifier associated with this payment.
152 I<depend_jobnum> allows payment capture to unlock export jobs
154 I<discount_term> attempts to take a discount by prepaying for discount_term.
155 The payment will fail if I<amount> is incorrect for this discount term.
157 A direct (Business::OnlinePayment) transaction will return nothing on success,
158 or an error message on failure.
160 A third-party transaction will return a hashref containing:
162 - popup_url: the URL to which a browser should be redirected to complete
164 - collectitems: an arrayref of name-value pairs to be posted to popup_url.
165 - reference: a reference ID for the transaction, to show the customer.
167 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
171 # some helper routines
172 sub _bop_recurring_billing {
173 my( $self, %opt ) = @_;
175 my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
177 if ( defined($method) && $method eq 'transaction_is_recur' ) {
179 return 1 if $opt{'trans_is_recur'};
183 # return 1 if the payinfo has been used for another payment
184 return $self->payinfo_used($opt{'payinfo'}); # in payinfo_Mixin
192 sub _payment_gateway {
193 my ($self, $options) = @_;
195 if ( $options->{'selfservice'} ) {
196 my $gatewaynum = FS::Conf->new->config('selfservice-payment_gateway');
198 return $options->{payment_gateway} ||=
199 qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
203 if ( $options->{'fake_gatewaynum'} ) {
204 $options->{payment_gateway} =
205 qsearchs('payment_gateway',
206 { 'gatewaynum' => $options->{'fake_gatewaynum'}, }
210 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
211 unless exists($options->{payment_gateway});
213 $options->{payment_gateway};
217 my ($self, $options) = @_;
220 'login' => $options->{payment_gateway}->gateway_username,
221 'password' => $options->{payment_gateway}->gateway_password,
226 my ($self, $options) = @_;
228 $options->{payment_gateway}->gatewaynum
229 ? $options->{payment_gateway}->options
230 : @{ $options->{payment_gateway}->get('options') };
235 my ($self, $options) = @_;
237 unless ( $options->{'description'} ) {
238 if ( $conf->exists('business-onlinepayment-description') ) {
239 my $dtempl = $conf->config('business-onlinepayment-description');
241 my $agent = $self->agent->agent;
243 $options->{'description'} = eval qq("$dtempl");
245 $options->{'description'} = 'Internet services';
249 unless ( exists( $options->{'payinfo'} ) ) {
250 $options->{'payinfo'} = $self->payinfo;
251 $options->{'paymask'} = $self->paymask;
254 # Default invoice number if the customer has exactly one open invoice.
255 unless ( $options->{'invnum'} || $options->{'no_invnum'} ) {
256 $options->{'invnum'} = '';
257 my @open = $self->open_cust_bill;
258 $options->{'invnum'} = $open[0]->invnum if scalar(@open) == 1;
261 $options->{payname} = $self->payname unless exists( $options->{payname} );
265 my ($self, $options) = @_;
268 my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
269 $content{customer_ip} = $payip if length($payip);
271 $content{invoice_number} = $options->{'invnum'}
272 if exists($options->{'invnum'}) && length($options->{'invnum'});
274 $content{email_customer} =
275 ( $conf->exists('business-onlinepayment-email_customer')
276 || $conf->exists('business-onlinepayment-email-override') );
278 my ($payname, $payfirst, $paylast);
279 if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
280 ($payname = $options->{payname}) =~
281 /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
282 or return "Illegal payname $payname";
283 ($payfirst, $paylast) = ($1, $2);
285 $payfirst = $self->getfield('first');
286 $paylast = $self->getfield('last');
287 $payname = "$payfirst $paylast";
290 $content{last_name} = $paylast;
291 $content{first_name} = $payfirst;
293 $content{name} = $payname;
295 $content{address} = exists($options->{'address1'})
296 ? $options->{'address1'}
298 my $address2 = exists($options->{'address2'})
299 ? $options->{'address2'}
301 $content{address} .= ", ". $address2 if length($address2);
303 $content{city} = exists($options->{city})
306 $content{state} = exists($options->{state})
309 $content{zip} = exists($options->{zip})
312 $content{country} = exists($options->{country})
313 ? $options->{country}
316 #3.0 is a good a time as any to get rid of this... add a config to pass it
317 # if anyone still needs it
318 #$content{referer} = 'http://cleanwhisker.420.am/';
320 $content{phone} = $self->daytime || $self->night;
322 my $currency = $conf->exists('business-onlinepayment-currency')
323 && $conf->config('business-onlinepayment-currency');
324 $content{currency} = $currency if $currency;
329 my %bop_method2payby = (
338 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
341 if (ref($_[0]) eq 'HASH') {
344 my ( $method, $amount ) = ( shift, shift );
346 $options{method} = $method;
347 $options{amount} = $amount;
352 # optional credit card surcharge
355 my $cc_surcharge = 0;
356 my $cc_surcharge_pct = 0;
357 $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage', $self->agentnum)
358 if $conf->config('credit-card-surcharge-percentage', $self->agentnum)
359 && $options{method} eq 'CC';
361 my $cc_surcharge_flat = 0;
362 $cc_surcharge_flat = $conf->config('credit-card-surcharge-flatfee', $self->agentnum)
363 if $conf->config('credit-card-surcharge-flatfee', $self->agentnum)
364 && $options{method} eq 'CC';
366 # always add cc surcharge if called from event
367 if($options{'cc_surcharge_from_event'} && ($cc_surcharge_pct > 0 || $cc_surcharge_flat > 0)) {
368 if ($options{'amount'} > 0) {
369 $cc_surcharge = ($options{'amount'} * ($cc_surcharge_pct / 100)) + $cc_surcharge_flat;
370 $options{'amount'} += $cc_surcharge;
371 $options{'amount'} = sprintf("%.2f", $options{'amount'}); # round (again)?
374 elsif($cc_surcharge_pct > 0 || $cc_surcharge_flat > 0) {
375 # we're called not from event (i.e. from a
376 # payment screen), so consider the given
377 # amount as post-surcharge-processing_fee
378 $cc_surcharge = $options{'amount'} - $options{'processing-fee'} - (($options{'amount'} - ($cc_surcharge_flat + $options{'processing-fee'})) / ( 1 + $cc_surcharge_pct/100 )) if $options{'amount'} > 0;
381 $cc_surcharge = sprintf("%.2f",$cc_surcharge) if $cc_surcharge > 0;
382 $options{'cc_surcharge'} = $cc_surcharge;
386 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
387 warn " cc_surcharge = $cc_surcharge\n";
390 warn " $_ => $options{$_}\n" foreach keys %options;
393 return $self->fake_bop(\%options) if $options{'fake'};
395 $self->_bop_defaults(\%options);
398 # set trans_is_recur based on invnum if there is one
401 my $trans_is_recur = 0;
402 if ( $options{'invnum'} ) {
404 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
405 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
411 $cust_bill->cust_bill_pkg;
414 if grep { $_->freq ne '0' } @part_pkg;
422 my $payment_gateway = $self->_payment_gateway( \%options );
423 my $namespace = $payment_gateway->gateway_namespace;
425 eval "use $namespace";
429 # check for banned credit card/ACH
432 my $ban = FS::banned_pay->ban_search(
433 'payby' => $bop_method2payby{$options{method}},
434 'payinfo' => $options{payinfo},
436 return "Banned credit card" if $ban && $ban->bantype ne 'warn';
439 # check for term discount validity
442 my $discount_term = $options{discount_term};
443 if ( $discount_term ) {
444 my $bill = ($self->cust_bill)[-1]
445 or return "Can't apply a term discount to an unbilled customer";
446 my $plan = FS::discount_plan->new(
448 months => $discount_term
449 ) or return "No discount available for term '$discount_term'";
451 if ( $plan->discounted_total != $options{amount} ) {
452 return "Incorrect term prepayment amount (term $discount_term, amount $options{amount}, requires ".$plan->discounted_total.")";
460 my $bop_content = $self->_bop_content(\%options);
461 return $bop_content unless ref($bop_content);
463 my @invoicing_list = $self->invoicing_list_emailonly;
464 if ( $conf->exists('emailinvoiceautoalways')
465 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
466 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
467 push @invoicing_list, $self->all_emails;
470 my $email = ($conf->exists('business-onlinepayment-email-override'))
471 ? $conf->config('business-onlinepayment-email-override')
472 : $invoicing_list[0];
477 if ( $namespace eq 'Business::OnlinePayment' ) {
479 if ( $options{method} eq 'CC' ) {
481 $content{card_number} = $options{payinfo};
482 $paydate = exists($options{'paydate'})
483 ? $options{'paydate'}
485 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
486 $content{expiration} = "$2/$1";
488 my $paycvv = exists($options{'paycvv'})
491 $content{cvv2} = $paycvv
494 my $paystart_month = exists($options{'paystart_month'})
495 ? $options{'paystart_month'}
496 : $self->paystart_month;
498 my $paystart_year = exists($options{'paystart_year'})
499 ? $options{'paystart_year'}
500 : $self->paystart_year;
502 $content{card_start} = "$paystart_month/$paystart_year"
503 if $paystart_month && $paystart_year;
505 my $payissue = exists($options{'payissue'})
506 ? $options{'payissue'}
508 $content{issue_number} = $payissue if $payissue;
510 if ( $self->_bop_recurring_billing(
511 'payinfo' => $options{'payinfo'},
512 'trans_is_recur' => $trans_is_recur,
516 $content{recurring_billing} = 'YES';
517 $content{acct_code} = 'rebill'
518 if $conf->exists('credit_card-recurring_billing_acct_code');
521 } elsif ( $options{method} eq 'ECHECK' ){
523 ( $content{account_number}, $content{routing_code} ) =
524 split('@', $options{payinfo});
525 $content{bank_name} = $options{payname};
526 $content{bank_state} = exists($options{'paystate'})
527 ? $options{'paystate'}
528 : $self->getfield('paystate');
529 $content{account_type}=
530 (exists($options{'paytype'}) && $options{'paytype'})
531 ? uc($options{'paytype'})
532 : uc($self->getfield('paytype')) || 'PERSONAL CHECKING';
534 $content{company} = $self->company if $self->company;
536 if ( $content{account_type} =~ /BUSINESS/i && $self->company ) {
537 $content{account_name} = $self->company;
539 $content{account_name} = $self->getfield('first'). ' '.
540 $self->getfield('last');
543 $content{customer_org} = $self->company ? 'B' : 'I';
544 $content{state_id} = exists($options{'stateid'})
545 ? $options{'stateid'}
546 : $self->getfield('stateid');
547 $content{state_id_state} = exists($options{'stateid_state'})
548 ? $options{'stateid_state'}
549 : $self->getfield('stateid_state');
550 $content{customer_ssn} = exists($options{'ss'})
555 die "unknown method ". $options{method};
558 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
561 die "unknown namespace $namespace";
568 my $balance = exists( $options{'balance'} )
569 ? $options{'balance'}
572 warn "claiming mutex on customer ". $self->custnum. "\n" if $DEBUG > 1;
573 $self->select_for_update; #mutex ... just until we get our pending record in
574 warn "obtained mutex on customer ". $self->custnum. "\n" if $DEBUG > 1;
576 #the checks here are intended to catch concurrent payments
577 #double-form-submission prevention is taken care of in cust_pay_pending::check
580 return "The customer's balance has changed; $options{method} transaction aborted."
581 if $self->balance < $balance;
583 #also check and make sure there aren't *other* pending payments for this cust
585 my @pending = qsearch('cust_pay_pending', {
586 'custnum' => $self->custnum,
587 'status' => { op=>'!=', value=>'done' }
590 #for third-party payments only, remove pending payments if they're in the
591 #'thirdparty' (waiting for customer action) state.
592 if ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
593 foreach ( grep { $_->status eq 'thirdparty' } @pending ) {
594 my $error = $_->delete;
595 warn "error deleting unfinished third-party payment ".
596 $_->paypendingnum . ": $error\n"
599 @pending = grep { $_->status ne 'thirdparty' } @pending;
602 return "A payment is already being processed for this customer (".
603 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
604 "); $options{method} transaction aborted."
607 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
609 my $cust_pay_pending = new FS::cust_pay_pending {
610 'custnum' => $self->custnum,
611 'paid' => $options{amount},
613 'payby' => $bop_method2payby{$options{method}},
614 'payinfo' => $options{payinfo},
615 'paymask' => $options{paymask},
616 'paydate' => $paydate,
617 'recurring_billing' => $content{recurring_billing},
618 'pkgnum' => $options{'pkgnum'},
620 'gatewaynum' => $payment_gateway->gatewaynum || '',
621 'session_id' => $options{session_id} || '',
622 'jobnum' => $options{depend_jobnum} || '',
624 $cust_pay_pending->payunique( $options{payunique} )
625 if defined($options{payunique}) && length($options{payunique});
627 warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n"
629 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
630 return $cpp_new_err if $cpp_new_err;
632 warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n"
634 warn Dumper($cust_pay_pending) if $DEBUG > 2;
636 my( $action1, $action2 ) =
637 split( /\s*\,\s*/, $payment_gateway->gateway_action );
639 my $transaction = new $namespace( $payment_gateway->gateway_module,
640 $self->_bop_options(\%options),
643 $transaction->content(
644 'type' => $options{method},
645 $self->_bop_auth(\%options),
646 'action' => $action1,
647 'description' => $options{'description'},
648 'amount' => $options{amount},
649 #'invoice_number' => $options{'invnum'},
650 'customer_id' => $self->custnum,
652 'reference' => $cust_pay_pending->paypendingnum, #for now
653 'callback_url' => $payment_gateway->gateway_callback_url,
654 'cancel_url' => $payment_gateway->gateway_cancel_url,
659 $cust_pay_pending->status('pending');
660 my $cpp_pending_err = $cust_pay_pending->replace;
661 return $cpp_pending_err if $cpp_pending_err;
663 warn Dumper($transaction) if $DEBUG > 2;
665 unless ( $BOP_TESTING ) {
666 $transaction->test_transaction(1)
667 if $conf->exists('business-onlinepayment-test_transaction');
668 $transaction->submit();
670 if ( $BOP_TESTING_SUCCESS ) {
671 $transaction->is_success(1);
672 $transaction->authorization('fake auth');
674 $transaction->is_success(0);
675 $transaction->error_message('fake failure');
679 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
681 $cust_pay_pending->status('thirdparty');
682 my $cpp_err = $cust_pay_pending->replace;
683 return { error => $cpp_err } if $cpp_err;
684 return { reference => $cust_pay_pending->paypendingnum,
685 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
687 } elsif ( $transaction->is_success() && $action2 ) {
689 $cust_pay_pending->status('authorized');
690 my $cpp_authorized_err = $cust_pay_pending->replace;
691 return $cpp_authorized_err if $cpp_authorized_err;
693 my $auth = $transaction->authorization;
694 my $ordernum = $transaction->can('order_number')
695 ? $transaction->order_number
699 new Business::OnlinePayment( $payment_gateway->gateway_module,
700 $self->_bop_options(\%options),
705 type => $options{method},
707 $self->_bop_auth(\%options),
708 order_number => $ordernum,
709 amount => $options{amount},
710 authorization => $auth,
711 description => $options{'description'},
714 foreach my $field (qw( authorization_source_code returned_ACI
715 transaction_identifier validation_code
716 transaction_sequence_num local_transaction_date
717 local_transaction_time AVS_result_code )) {
718 $capture{$field} = $transaction->$field() if $transaction->can($field);
721 $capture->content( %capture );
723 $capture->test_transaction(1)
724 if $conf->exists('business-onlinepayment-test_transaction');
727 unless ( $capture->is_success ) {
728 my $e = "Authorization successful but capture failed, custnum #".
729 $self->custnum. ': '. $capture->result_code.
730 ": ". $capture->error_message;
738 # remove paycvv after initial transaction
741 #false laziness w/misc/process/payment.cgi - check both to make sure working
743 if ( length($self->paycvv)
744 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
746 my $error = $self->remove_cvv;
748 warn "WARNING: error removing cvv: $error\n";
757 if ( $transaction->can('card_token') && $transaction->card_token ) {
759 if ( $options{'payinfo'} eq $self->payinfo ) {
760 $self->payinfo($transaction->card_token);
761 my $error = $self->replace;
763 warn "WARNING: error storing token: $error, but proceeding anyway\n";
773 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
785 if (ref($_[0]) eq 'HASH') {
788 my ( $method, $amount ) = ( shift, shift );
790 $options{method} = $method;
791 $options{amount} = $amount;
794 if ( $options{'fake_failure'} ) {
795 return "Error: No error; test failure requested with fake_failure";
798 my $cust_pay = new FS::cust_pay ( {
799 'custnum' => $self->custnum,
800 'invnum' => $options{'invnum'},
801 'paid' => $options{amount},
803 'payby' => $bop_method2payby{$options{method}},
804 #'payinfo' => $payinfo,
805 'payinfo' => '4111111111111111',
806 #'paydate' => $paydate,
807 'paydate' => '2012-05-01',
808 'processor' => 'FakeProcessor',
810 'order_number' => '32',
812 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
815 warn "fake_bop\n cust_pay: ". Dumper($cust_pay) . "\n options: ";
816 warn " $_ => $options{$_}\n" foreach keys %options;
819 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
822 $cust_pay->invnum(''); #try again with no specific invnum
823 my $error2 = $cust_pay->insert( $options{'manual'} ?
824 ( 'manual' => 1 ) : ()
827 # gah, even with transactions.
828 my $e = 'WARNING: Card/ACH debited but database not updated - '.
829 "error inserting (fake!) payment: $error2".
830 " (previously tried insert with invnum #$options{'invnum'}" .
837 if ( $options{'paynum_ref'} ) {
838 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
846 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
848 # Wraps up processing of a realtime credit card or ACH (electronic check)
851 sub _realtime_bop_result {
852 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
854 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
857 warn "$me _realtime_bop_result: pending transaction ".
858 $cust_pay_pending->paypendingnum. "\n";
859 warn " $_ => $options{$_}\n" foreach keys %options;
862 my $payment_gateway = $options{payment_gateway}
863 or return "no payment gateway in arguments to _realtime_bop_result";
865 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
866 my $cpp_captured_err = $cust_pay_pending->replace;
867 return $cpp_captured_err if $cpp_captured_err;
869 if ( $transaction->is_success() ) {
871 my $order_number = $transaction->order_number
872 if $transaction->can('order_number');
874 my $cust_pay = new FS::cust_pay ( {
875 'custnum' => $self->custnum,
876 'invnum' => $options{'invnum'},
877 'paid' => $cust_pay_pending->paid,
879 'payby' => $cust_pay_pending->payby,
880 'payinfo' => $options{'payinfo'},
881 'paymask' => $options{'paymask'} || $cust_pay_pending->paymask,
882 'paydate' => $cust_pay_pending->paydate,
883 'pkgnum' => $cust_pay_pending->pkgnum,
884 'discount_term' => $options{'discount_term'},
885 'gatewaynum' => ($payment_gateway->gatewaynum || ''),
886 'processor' => $payment_gateway->gateway_module,
887 'auth' => $transaction->authorization,
888 'order_number' => $order_number || '',
889 'no_auto_apply' => $options{'no_auto_apply'} ? 'Y' : '',
891 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
892 $cust_pay->payunique( $options{payunique} )
893 if defined($options{payunique}) && length($options{payunique});
895 my $oldAutoCommit = $FS::UID::AutoCommit;
896 local $FS::UID::AutoCommit = 0;
899 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
901 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
904 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
905 $cust_pay->invnum(''); #try again with no specific invnum
906 $cust_pay->paynum('');
907 my $error2 = $cust_pay->insert( $options{'manual'} ?
908 ( 'manual' => 1 ) : ()
911 # gah. but at least we have a record of the state we had to abort in
912 # from cust_pay_pending now.
913 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
914 my $e = "WARNING: $options{method} captured but payment not recorded -".
915 " error inserting payment (". $payment_gateway->gateway_module.
917 " (previously tried insert with invnum #$options{'invnum'}" .
918 ": $error ) - pending payment saved as paypendingnum ".
919 $cust_pay_pending->paypendingnum. "\n";
925 my $jobnum = $cust_pay_pending->jobnum;
927 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
929 unless ( $placeholder ) {
930 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
931 my $e = "WARNING: $options{method} captured but job $jobnum not ".
932 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
937 $error = $placeholder->delete;
940 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
941 my $e = "WARNING: $options{method} captured but could not delete ".
942 "job $jobnum for paypendingnum ".
943 $cust_pay_pending->paypendingnum. ": $error\n";
950 if ( $options{'paynum_ref'} ) {
951 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
954 $cust_pay_pending->status('done');
955 $cust_pay_pending->statustext('captured');
956 $cust_pay_pending->paynum($cust_pay->paynum);
957 my $cpp_done_err = $cust_pay_pending->replace;
959 if ( $cpp_done_err ) {
961 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
962 my $e = "WARNING: $options{method} captured but payment not recorded - ".
963 "error updating status for paypendingnum ".
964 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
970 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
972 if ( $options{'apply'} ) {
973 my $apply_error = $self->apply_payments_and_credits;
974 if ( $apply_error ) {
975 warn "WARNING: error applying payment: $apply_error\n";
976 #but we still should return no error cause the payment otherwise went
981 # have a CC surcharge portion --> one-time charge
982 if ( $options{'cc_surcharge'} > 0 || $options{'processing-fee'} > 0) {
983 # XXX: this whole block needs to be in a transaction?
986 $invnum = $options{'invnum'} if $options{'invnum'};
987 unless ( $invnum ) { # probably from a payment screen
988 # do we have any open invoices? pick earliest
989 # uses the fact that cust_main->cust_bill sorts by date ascending
990 my @open = $self->open_cust_bill;
991 $invnum = $open[0]->invnum if scalar(@open);
994 unless ( $invnum ) { # still nothing? pick last closed invoice
995 # again uses fact that cust_main->cust_bill sorts by date ascending
996 my @closed = $self->cust_bill;
997 $invnum = $closed[$#closed]->invnum if scalar(@closed);
1000 unless ( $invnum ) {
1001 # XXX: unlikely case - pre-paying before any invoices generated
1002 # what it should do is create a new invoice and pick it
1003 warn 'CC SURCHARGE OR PROCESS FEE AND NO INVOICES PICKED TO APPLY IT!';
1007 if ($options{'cc_surcharge'} > 0) {
1009 my $cc_surcharge_text = 'Credit Card Surcharge';
1010 $cc_surcharge_text = $conf->config('credit-card-surcharge-text', $self->agentnum) if $conf->exists('credit-card-surcharge-text', $self->agentnum);
1011 my $charge_error = $self->charge({
1012 'amount' => $options{'cc_surcharge'},
1013 'pkg' => $cc_surcharge_text,
1015 'cust_pkg_ref' => \$cust_pkg,
1019 warn 'Unable to add CC surcharge cust_pkg';
1023 $cust_pkg->setup(time);
1024 my $cp_error = $cust_pkg->replace;
1026 warn 'Unable to set setup time on cust_pkg for cc surcharge';
1030 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
1031 unless ( $cust_bill ) {
1032 warn "race condition + invoice deletion just happened";
1037 $cust_bill->add_cc_surcharge($cust_pkg->pkgnum,$options{'cc_surcharge'});
1039 warn "cannot add CC surcharge to invoice #$invnum: $grand_error"
1041 } # end if $options{'cc_surcharge'}
1043 if ($options{'processing-fee'} > 0) {
1045 my $processing_fee_text = 'Payment Processing Fee';
1046 my $pf_change_error = $self->charge({
1047 'amount' => $options{'processing-fee'},
1048 'pkg' => $processing_fee_text,
1050 'cust_pkg_ref' => \$pf_cust_pkg,
1053 if($pf_change_error) {
1054 warn 'Unable to add payment processing fee';
1058 $pf_cust_pkg->setup(time);
1059 my $pf_error = $pf_cust_pkg->replace;
1061 warn 'Unable to set setup time on cust_pkg for processing fee';
1065 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
1066 unless ( $cust_bill ) {
1067 warn "race condition + invoice deletion just happened";
1071 my $grand_pf_error =
1072 $cust_bill->add_cc_surcharge($pf_cust_pkg->pkgnum,$options{'processing-fee'});
1074 warn "cannot add Processing fee to invoice #$invnum: $grand_pf_error"
1076 } #end if $options{'processing-fee'}
1078 } #end if ( $options{'cc_surcharge'} > 0 || $options{'processing-fee'} > 0)
1080 return ''; #no error
1086 my $perror = $payment_gateway->gateway_module. " error: ".
1087 $transaction->error_message;
1089 my $jobnum = $cust_pay_pending->jobnum;
1091 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
1093 if ( $placeholder ) {
1094 my $error = $placeholder->depended_delete;
1095 $error ||= $placeholder->delete;
1096 warn "error removing provisioning jobs after declined paypendingnum ".
1097 $cust_pay_pending->paypendingnum. ": $error\n";
1099 my $e = "error finding job $jobnum for declined paypendingnum ".
1100 $cust_pay_pending->paypendingnum. "\n";
1106 unless ( $transaction->error_message ) {
1109 if ( $transaction->can('response_page') ) {
1111 'page' => ( $transaction->can('response_page')
1112 ? $transaction->response_page
1115 'code' => ( $transaction->can('response_code')
1116 ? $transaction->response_code
1119 'headers' => ( $transaction->can('response_headers')
1120 ? $transaction->response_headers
1126 "No additional debugging information available for ".
1127 $payment_gateway->gateway_module;
1130 $perror .= "No error_message returned from ".
1131 $payment_gateway->gateway_module. " -- ".
1132 ( ref($t_response) ? Dumper($t_response) : $t_response );
1136 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
1137 && $conf->exists('emaildecline', $self->agentnum)
1138 && grep { $_ ne 'POST' } $self->invoicing_list
1139 && ! grep { $transaction->error_message =~ /$_/ }
1140 $conf->config('emaildecline-exclude', $self->agentnum)
1143 # Send a decline alert to the customer.
1144 my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
1147 # include the raw error message in the transaction state
1148 $cust_pay_pending->setfield('error', $transaction->error_message);
1149 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
1150 $error = $msg_template->send( 'cust_main' => $self,
1151 'object' => $cust_pay_pending );
1155 my @templ = $conf->config('declinetemplate');
1156 my $template = new Text::Template (
1158 SOURCE => [ map "$_\n", @templ ],
1159 ) or return "($perror) can't create template: $Text::Template::ERROR";
1160 $template->compile()
1161 or return "($perror) can't compile template: $Text::Template::ERROR";
1165 scalar( $conf->config('company_name', $self->agentnum ) ),
1166 'company_address' =>
1167 join("\n", $conf->config('company_address', $self->agentnum ) ),
1168 'error' => $transaction->error_message,
1171 my $error = send_email(
1172 'from' => $conf->invoice_from_full( $self->agentnum ),
1173 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
1174 'subject' => 'Your payment could not be processed',
1175 'body' => [ $template->fill_in(HASH => $templ_hash) ],
1179 $perror .= " (also received error sending decline notification: $error)"
1184 $cust_pay_pending->status('done');
1185 $cust_pay_pending->statustext("declined: $perror");
1186 my $cpp_done_err = $cust_pay_pending->replace;
1187 if ( $cpp_done_err ) {
1188 my $e = "WARNING: $options{method} declined but pending payment not ".
1189 "resolved - error updating status for paypendingnum ".
1190 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
1192 $perror = "$e ($perror)";
1200 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
1202 Verifies successful third party processing of a realtime credit card or
1203 ACH (electronic check) transaction via a
1204 Business::OnlineThirdPartyPayment realtime gateway. See
1205 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
1207 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
1209 The additional options I<payname>, I<city>, I<state>,
1210 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1211 if set, will override the value from the customer record.
1213 I<description> is a free-text field passed to the gateway. It defaults to
1214 "Internet services".
1216 If an I<invnum> is specified, this payment (if successful) is applied to the
1217 specified invoice. If you don't specify an I<invnum> you might want to
1218 call the B<apply_payments> method.
1220 I<quiet> can be set true to surpress email decline notices.
1222 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
1223 resulting paynum, if any.
1225 I<payunique> is a unique identifier for this payment.
1227 Returns a hashref containing elements bill_error (which will be undefined
1228 upon success) and session_id of any associated session.
1232 sub realtime_botpp_capture {
1233 my( $self, $cust_pay_pending, %options ) = @_;
1235 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1238 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
1239 warn " $_ => $options{$_}\n" foreach keys %options;
1242 eval "use Business::OnlineThirdPartyPayment";
1246 # select the gateway
1249 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1251 my $payment_gateway;
1252 my $gatewaynum = $cust_pay_pending->getfield('gatewaynum');
1253 $payment_gateway = $gatewaynum ? qsearchs( 'payment_gateway',
1254 { gatewaynum => $gatewaynum }
1256 : $self->agent->payment_gateway( 'method' => $method,
1257 # 'invnum' => $cust_pay_pending->invnum,
1258 # 'payinfo' => $cust_pay_pending->payinfo,
1261 $options{payment_gateway} = $payment_gateway; # for the helper subs
1267 my @invoicing_list = $self->invoicing_list_emailonly;
1268 if ( $conf->exists('emailinvoiceautoalways')
1269 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1270 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1271 push @invoicing_list, $self->all_emails;
1274 my $email = ($conf->exists('business-onlinepayment-email-override'))
1275 ? $conf->config('business-onlinepayment-email-override')
1276 : $invoicing_list[0];
1280 $content{email_customer} =
1281 ( $conf->exists('business-onlinepayment-email_customer')
1282 || $conf->exists('business-onlinepayment-email-override') );
1285 # run transaction(s)
1289 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1290 $self->_bop_options(\%options),
1293 $transaction->reference({ %options });
1295 $transaction->content(
1297 $self->_bop_auth(\%options),
1298 'action' => 'Post Authorization',
1299 'description' => $options{'description'},
1300 'amount' => $cust_pay_pending->paid,
1301 #'invoice_number' => $options{'invnum'},
1302 'customer_id' => $self->custnum,
1304 #3.0 is a good a time as any to get rid of this... add a config to pass it
1305 # if anyone still needs it
1306 #'referer' => 'http://cleanwhisker.420.am/',
1308 'reference' => $cust_pay_pending->paypendingnum,
1310 'phone' => $self->daytime || $self->night,
1312 # plus whatever is required for bogus capture avoidance
1315 $transaction->submit();
1318 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1320 if ( $options{'apply'} ) {
1321 my $apply_error = $self->apply_payments_and_credits;
1322 if ( $apply_error ) {
1323 warn "WARNING: error applying payment: $apply_error\n";
1328 bill_error => $error,
1329 session_id => $cust_pay_pending->session_id,
1334 =item default_payment_gateway
1336 DEPRECATED -- use agent->payment_gateway
1340 sub default_payment_gateway {
1341 my( $self, $method ) = @_;
1343 die "Real-time processing not enabled\n"
1344 unless $conf->exists('business-onlinepayment');
1346 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1349 my $bop_config = 'business-onlinepayment';
1350 $bop_config .= '-ach'
1351 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1352 my ( $processor, $login, $password, $action, @bop_options ) =
1353 $conf->config($bop_config);
1354 $action ||= 'normal authorization';
1355 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1356 die "No real-time processor is enabled - ".
1357 "did you set the business-onlinepayment configuration value?\n"
1360 ( $processor, $login, $password, $action, @bop_options )
1363 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1365 Refunds a realtime credit card or ACH (electronic check) transaction
1366 via a Business::OnlinePayment realtime gateway. See
1367 L<http://420.am/business-onlinepayment> for supported gateways.
1369 Available methods are: I<CC> or I<ECHECK>
1371 Available options are: I<amount>, I<reasonnum>, I<paynum>, I<paydate>
1373 Most gateways require a reference to an original payment transaction to refund,
1374 so you probably need to specify a I<paynum>.
1376 I<amount> defaults to the original amount of the payment if not specified.
1378 I<reasonnum> specifies a reason for the refund.
1380 I<paydate> specifies the expiration date for a credit card overriding the
1381 value from the customer record or the payment record. Specified as yyyy-mm-dd
1383 Implementation note: If I<amount> is unspecified or equal to the amount of the
1384 orignal payment, first an attempt is made to "void" the transaction via
1385 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1386 the normal attempt is made to "refund" ("credit") the transaction via the
1387 gateway is attempted. No attempt to "void" the transaction is made if the
1388 gateway has introspection data and doesn't support void.
1390 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1391 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1392 #if set, will override the value from the customer record.
1394 #If an I<invnum> is specified, this payment (if successful) is applied to the
1395 #specified invoice. If you don't specify an I<invnum> you might want to
1396 #call the B<apply_payments> method.
1400 #some false laziness w/realtime_bop, not enough to make it worth merging
1401 #but some useful small subs should be pulled out
1402 sub realtime_refund_bop {
1405 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1408 if (ref($_[0]) eq 'HASH') {
1409 %options = %{$_[0]};
1413 $options{method} = $method;
1416 my ($reason, $reason_text);
1417 if ( $options{'reasonnum'} ) {
1418 # do this here, because we need the plain text reason string in case we
1420 $reason = FS::reason->by_key($options{'reasonnum'});
1421 $reason_text = $reason->reason;
1423 # support old 'reason' string parameter in case it's still used,
1424 # or else set a default
1425 $reason_text = $options{'reason'} || 'card or ACH refund';
1427 $reason = FS::reason->new_or_existing(
1428 reason => $reason_text,
1429 type => 'Refund reason',
1433 return "failed to add refund reason: $@";
1438 warn "$me realtime_refund_bop (new): $options{method} refund\n";
1439 warn " $_ => $options{$_}\n" foreach keys %options;
1445 # look up the original payment and optionally a gateway for that payment
1449 my $amount = $options{'amount'};
1451 my( $processor, $login, $password, @bop_options, $namespace ) ;
1452 my( $auth, $order_number ) = ( '', '', '' );
1453 my $gatewaynum = '';
1455 if ( $options{'paynum'} ) {
1457 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
1458 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1459 or return "Unknown paynum $options{'paynum'}";
1460 $amount ||= $cust_pay->paid;
1462 my @cust_bill_pay = qsearch('cust_bill_pay', { paynum=>$cust_pay->paynum });
1463 $content{'invoice_number'} = $cust_bill_pay[0]->invnum if @cust_bill_pay;
1465 if ( $cust_pay->get('processor') ) {
1466 ($gatewaynum, $processor, $auth, $order_number) =
1468 $cust_pay->gatewaynum,
1469 $cust_pay->processor,
1471 $cust_pay->order_number,
1474 # this payment wasn't upgraded, which probably means this won't work,
1476 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1477 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1478 $cust_pay->paybatch;
1479 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1482 if ( $gatewaynum ) { #gateway for the payment to be refunded
1484 my $payment_gateway =
1485 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1486 die "payment gateway $gatewaynum not found"
1487 unless $payment_gateway;
1489 $processor = $payment_gateway->gateway_module;
1490 $login = $payment_gateway->gateway_username;
1491 $password = $payment_gateway->gateway_password;
1492 $namespace = $payment_gateway->gateway_namespace;
1493 @bop_options = $payment_gateway->options;
1495 } else { #try the default gateway
1498 my $payment_gateway =
1499 $self->agent->payment_gateway('method' => $options{method});
1501 ( $conf_processor, $login, $password, $namespace ) =
1502 map { my $method = "gateway_$_"; $payment_gateway->$method }
1503 qw( module username password namespace );
1505 @bop_options = $payment_gateway->gatewaynum
1506 ? $payment_gateway->options
1507 : @{ $payment_gateway->get('options') };
1509 return "processor of payment $options{'paynum'} $processor does not".
1510 " match default processor $conf_processor"
1511 unless $processor eq $conf_processor;
1516 } else { # didn't specify a paynum, so look for agent gateway overrides
1517 # like a normal transaction
1519 my $payment_gateway =
1520 $self->agent->payment_gateway( 'method' => $options{method},
1521 #'payinfo' => $payinfo,
1523 ( $processor, $login, $password, $namespace ) =
1524 map { my $method = "gateway_$_"; $payment_gateway->$method }
1525 qw( module username password namespace );
1527 my @bop_options = $payment_gateway->gatewaynum
1528 ? $payment_gateway->options
1529 : @{ $payment_gateway->get('options') };
1532 return "neither amount nor paynum specified" unless $amount;
1534 eval "use $namespace";
1539 'type' => $options{method},
1541 'password' => $password,
1542 'order_number' => $order_number,
1543 'amount' => $amount,
1545 #3.0 is a good a time as any to get rid of this... add a config to pass it
1546 # if anyone still needs it
1547 #'referer' => 'http://cleanwhisker.420.am/',
1549 $content{authorization} = $auth
1550 if length($auth); #echeck/ACH transactions have an order # but no auth
1551 #(at least with authorize.net)
1553 my $currency = $conf->exists('business-onlinepayment-currency')
1554 && $conf->config('business-onlinepayment-currency');
1555 $content{currency} = $currency if $currency;
1557 my $disable_void_after;
1558 if ($conf->exists('disable_void_after')
1559 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1560 $disable_void_after = $1;
1563 #first try void if applicable
1564 my $void = new Business::OnlinePayment( $processor, @bop_options );
1567 if ($void->can('info')) {
1569 $paytype = 'ECHECK' if $cust_pay && $cust_pay->payby eq 'CHEK';
1570 $paytype = 'CC' if $cust_pay && $cust_pay->payby eq 'CARD';
1571 my %supported_actions = $void->info('supported_actions');
1573 if ( %supported_actions && $paytype
1574 && defined($supported_actions{$paytype})
1575 && !grep{ $_ eq 'Void' } @{$supported_actions{$paytype}} );
1578 if ( $cust_pay && $cust_pay->paid == $amount
1580 ( not defined($disable_void_after) )
1581 || ( time < ($cust_pay->_date + $disable_void_after ) )
1585 warn " attempting void\n" if $DEBUG > 1;
1586 if ( $void->can('info') ) {
1587 if ( $cust_pay->payby eq 'CARD'
1588 && $void->info('CC_void_requires_card') )
1590 $content{'card_number'} = $cust_pay->payinfo;
1591 } elsif ( $cust_pay->payby eq 'CHEK'
1592 && $void->info('ECHECK_void_requires_account') )
1594 ( $content{'account_number'}, $content{'routing_code'} ) =
1595 split('@', $cust_pay->payinfo);
1596 $content{'name'} = $self->get('first'). ' '. $self->get('last');
1599 $void->content( 'action' => 'void', %content );
1600 $void->test_transaction(1)
1601 if $conf->exists('business-onlinepayment-test_transaction');
1603 if ( $void->is_success ) {
1604 my $error = $cust_pay->void($reason_text);
1606 # gah, even with transactions.
1607 my $e = 'WARNING: Card/ACH voided but database not updated - '.
1608 "error voiding payment: $error";
1612 warn " void successful\n" if $DEBUG > 1;
1617 warn " void unsuccessful, trying refund\n"
1621 my $address = $self->address1;
1622 $address .= ", ". $self->address2 if $self->address2;
1624 my($payname, $payfirst, $paylast);
1625 if ( $self->payname && $options{method} ne 'ECHECK' ) {
1626 $payname = $self->payname;
1627 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1628 or return "Illegal payname $payname";
1629 ($payfirst, $paylast) = ($1, $2);
1631 $payfirst = $self->getfield('first');
1632 $paylast = $self->getfield('last');
1633 $payname = "$payfirst $paylast";
1636 my @invoicing_list = $self->invoicing_list_emailonly;
1637 if ( $conf->exists('emailinvoiceautoalways')
1638 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1639 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1640 push @invoicing_list, $self->all_emails;
1643 my $email = ($conf->exists('business-onlinepayment-email-override'))
1644 ? $conf->config('business-onlinepayment-email-override')
1645 : $invoicing_list[0];
1647 my $payip = exists($options{'payip'})
1650 $content{customer_ip} = $payip
1654 if ( $options{method} eq 'CC' ) {
1657 $content{card_number} = $payinfo = $cust_pay->payinfo;
1658 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1659 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1660 ($content{expiration} = "$2/$1"); # where available
1662 $content{card_number} = $payinfo = $self->payinfo;
1663 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1664 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1665 $content{expiration} = "$2/$1";
1668 } elsif ( $options{method} eq 'ECHECK' ) {
1671 $payinfo = $cust_pay->payinfo;
1673 $payinfo = $self->payinfo;
1675 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1676 $content{bank_name} = $self->payname;
1677 $content{account_type} = 'CHECKING';
1678 $content{account_name} = $payname;
1679 $content{customer_org} = $self->company ? 'B' : 'I';
1680 $content{customer_ssn} = $self->ss;
1685 my $refund = new Business::OnlinePayment( $processor, @bop_options );
1686 my %sub_content = $refund->content(
1687 'action' => 'credit',
1688 'customer_id' => $self->custnum,
1689 'last_name' => $paylast,
1690 'first_name' => $payfirst,
1692 'address' => $address,
1693 'city' => $self->city,
1694 'state' => $self->state,
1695 'zip' => $self->zip,
1696 'country' => $self->country,
1698 'phone' => $self->daytime || $self->night,
1701 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
1703 $refund->test_transaction(1)
1704 if $conf->exists('business-onlinepayment-test_transaction');
1707 return "$processor error: ". $refund->error_message
1708 unless $refund->is_success();
1710 $order_number = $refund->order_number if $refund->can('order_number');
1712 # change this to just use $cust_pay->delete_cust_bill_pay?
1713 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1714 my @cust_bill_pay = $cust_pay->cust_bill_pay;
1715 last unless @cust_bill_pay;
1716 my $cust_bill_pay = pop @cust_bill_pay;
1717 my $error = $cust_bill_pay->delete;
1721 my $cust_refund = new FS::cust_refund ( {
1722 'custnum' => $self->custnum,
1723 'paynum' => $options{'paynum'},
1724 'source_paynum' => $options{'paynum'},
1725 'refund' => $amount,
1727 'payby' => $bop_method2payby{$options{method}},
1728 'payinfo' => $payinfo,
1729 'reasonnum' => $reason->reasonnum,
1730 'gatewaynum' => $gatewaynum, # may be null
1731 'processor' => $processor,
1732 'auth' => $refund->authorization,
1733 'order_number' => $order_number,
1735 my $error = $cust_refund->insert;
1737 $cust_refund->paynum(''); #try again with no specific paynum
1738 $cust_refund->source_paynum('');
1739 my $error2 = $cust_refund->insert;
1741 # gah, even with transactions.
1742 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1743 "error inserting refund ($processor): $error2".
1744 " (previously tried insert with paynum #$options{'paynum'}" .
1755 =item realtime_verify_bop [ OPTION => VALUE ... ]
1757 Runs an authorization-only transaction for $1 against this credit card (if
1758 successful, immediatly reverses the authorization).
1760 Returns the empty string if the authorization was sucessful, or an error
1767 I<paydate> specifies the expiration date for a credit card overriding the
1768 value from the customer record or the payment record. Specified as yyyy-mm-dd
1770 #The additional options I<address1>, I<address2>, I<city>, I<state>,
1771 #I<zip> are also available. Any of these options,
1772 #if set, will override the value from the customer record.
1776 #Available methods are: I<CC> or I<ECHECK>
1778 #some false laziness w/realtime_bop and realtime_refund_bop, not enough to make
1779 #it worth merging but some useful small subs should be pulled out
1780 sub realtime_verify_bop {
1783 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1784 my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_verify_bop');
1787 if (ref($_[0]) eq 'HASH') {
1788 %options = %{$_[0]};
1794 warn "$me realtime_verify_bop\n";
1795 warn " $_ => $options{$_}\n" foreach keys %options;
1802 my $payment_gateway = $self->_payment_gateway( \%options );
1803 my $namespace = $payment_gateway->gateway_namespace;
1805 eval "use $namespace";
1809 # check for banned credit card/ACH
1812 my $ban = FS::banned_pay->ban_search(
1813 'payby' => $bop_method2payby{'CC'},
1814 'payinfo' => $options{payinfo} || $self->payinfo,
1816 return "Banned credit card" if $ban && $ban->bantype ne 'warn';
1822 my $bop_content = $self->_bop_content(\%options);
1823 return $bop_content unless ref($bop_content);
1825 my @invoicing_list = $self->invoicing_list_emailonly;
1826 if ( $conf->exists('emailinvoiceautoalways')
1827 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1828 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1829 push @invoicing_list, $self->all_emails;
1832 my $email = ($conf->exists('business-onlinepayment-email-override'))
1833 ? $conf->config('business-onlinepayment-email-override')
1834 : $invoicing_list[0];
1839 if ( $namespace eq 'Business::OnlinePayment' ) {
1841 if ( $options{method} eq 'CC' ) {
1843 $content{card_number} = $options{payinfo} || $self->payinfo;
1844 $paydate = exists($options{'paydate'})
1845 ? $options{'paydate'}
1847 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1848 $content{expiration} = "$2/$1";
1850 my $paycvv = exists($options{'paycvv'})
1851 ? $options{'paycvv'}
1853 $content{cvv2} = $paycvv
1856 my $paystart_month = exists($options{'paystart_month'})
1857 ? $options{'paystart_month'}
1858 : $self->paystart_month;
1860 my $paystart_year = exists($options{'paystart_year'})
1861 ? $options{'paystart_year'}
1862 : $self->paystart_year;
1864 $content{card_start} = "$paystart_month/$paystart_year"
1865 if $paystart_month && $paystart_year;
1867 my $payissue = exists($options{'payissue'})
1868 ? $options{'payissue'}
1870 $content{issue_number} = $payissue if $payissue;
1872 } elsif ( $options{method} eq 'ECHECK' ){
1874 #nop for checks (though it shouldn't be called...)
1877 die "unknown method ". $options{method};
1880 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
1883 die "unknown namespace $namespace";
1887 # run transaction(s)
1891 my $transaction; #need this back so we can do _tokenize_card
1892 # don't mutex the customer here, because they might be uncommitted. and
1893 # this is only verification. it doesn't matter if they have other
1894 # unfinished verifications.
1896 my $cust_pay_pending = new FS::cust_pay_pending {
1897 'custnum_pending' => 1,
1900 'payby' => $bop_method2payby{'CC'},
1901 'payinfo' => $options{payinfo} || $self->payinfo,
1902 'paymask' => $options{paymask} || $self->paymask,
1903 'paydate' => $paydate,
1904 #'recurring_billing' => $content{recurring_billing},
1905 'pkgnum' => $options{'pkgnum'},
1907 'gatewaynum' => $payment_gateway->gatewaynum || '',
1908 'session_id' => $options{session_id} || '',
1909 #'jobnum' => $options{depend_jobnum} || '',
1911 $cust_pay_pending->payunique( $options{payunique} )
1912 if defined($options{payunique}) && length($options{payunique});
1915 # open a separate handle for creating/updating the cust_pay_pending
1917 local $FS::UID::dbh = myconnect();
1918 local $FS::UID::AutoCommit = 1;
1920 # if this is an existing customer (and we can tell now because
1921 # this is a fresh transaction), it's safe to assign their custnum
1922 # to the cust_pay_pending record, and then the verification attempt
1923 # will remain linked to them even if it fails.
1924 if ( FS::cust_main->by_key($self->custnum) ) {
1925 $cust_pay_pending->set('custnum', $self->custnum);
1928 warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n"
1931 # if this fails, just return; everything else will still allow the
1932 # cust_pay_pending to have its custnum set later
1933 my $cpp_new_err = $cust_pay_pending->insert;
1934 return $cpp_new_err if $cpp_new_err;
1936 warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n"
1938 warn Dumper($cust_pay_pending) if $DEBUG > 2;
1940 $transaction = new $namespace( $payment_gateway->gateway_module,
1941 $self->_bop_options(\%options),
1944 $transaction->content(
1946 $self->_bop_auth(\%options),
1947 'action' => 'Authorization Only',
1948 'description' => $options{'description'},
1950 #'invoice_number' => $options{'invnum'},
1951 'customer_id' => $self->custnum,
1953 'reference' => $cust_pay_pending->paypendingnum, #for now
1954 'callback_url' => $payment_gateway->gateway_callback_url,
1955 'cancel_url' => $payment_gateway->gateway_cancel_url,
1960 $cust_pay_pending->status('pending');
1961 my $cpp_pending_err = $cust_pay_pending->replace;
1962 return $cpp_pending_err if $cpp_pending_err;
1964 warn Dumper($transaction) if $DEBUG > 2;
1966 unless ( $BOP_TESTING ) {
1967 $transaction->test_transaction(1)
1968 if $conf->exists('business-onlinepayment-test_transaction');
1969 $transaction->submit();
1971 if ( $BOP_TESTING_SUCCESS ) {
1972 $transaction->is_success(1);
1973 $transaction->authorization('fake auth');
1975 $transaction->is_success(0);
1976 $transaction->error_message('fake failure');
1980 if ( $transaction->is_success() ) {
1982 $cust_pay_pending->status('authorized');
1983 my $cpp_authorized_err = $cust_pay_pending->replace;
1984 return $cpp_authorized_err if $cpp_authorized_err;
1986 my $auth = $transaction->authorization;
1987 my $ordernum = $transaction->can('order_number')
1988 ? $transaction->order_number
1991 my $reverse = new $namespace( $payment_gateway->gateway_module,
1992 $self->_bop_options(\%options),
1995 $reverse->content( 'action' => 'Reverse Authorization',
1996 $self->_bop_auth(\%options),
2000 'authorization' => $transaction->authorization,
2001 'order_number' => $ordernum,
2004 'result_code' => $transaction->result_code,
2005 'txn_date' => $transaction->txn_date,
2009 $reverse->test_transaction(1)
2010 if $conf->exists('business-onlinepayment-test_transaction');
2013 if ( $reverse->is_success ) {
2015 $cust_pay_pending->status('done');
2016 $cust_pay_pending->statustext('reversed');
2017 my $cpp_reversed_err = $cust_pay_pending->replace;
2018 return $cpp_reversed_err if $cpp_reversed_err;
2022 my $e = "Authorization successful but reversal failed, custnum #".
2023 $self->custnum. ': '. $reverse->result_code.
2024 ": ". $reverse->error_message;
2031 ### Address Verification ###
2033 # Single-letter codes vary by cardtype.
2035 # Erring on the side of accepting cards if avs is not available,
2036 # only rejecting if avs occurred and there's been an explicit mismatch
2038 # Charts below taken from vSecure documentation,
2039 # shows codes for Amex/Dscv/MC/Visa
2041 # ACCEPTABLE AVS RESPONSES:
2042 # Both Address and 5-digit postal code match Y A Y Y
2043 # Both address and 9-digit postal code match Y A X Y
2044 # United Kingdom – Address and postal code match _ _ _ F
2045 # International transaction – Address and postal code match _ _ _ D/M
2047 # ACCEPTABLE, BUT ISSUE A WARNING:
2048 # Ineligible transaction; or message contains a content error _ _ _ E
2049 # System unavailable; retry R U R R
2050 # Information unavailable U W U U
2051 # Issuer does not support AVS S U S S
2052 # AVS is not applicable _ _ _ S
2053 # Incompatible formats – Not verified _ _ _ C
2054 # Incompatible formats – Address not verified; postal code matches _ _ _ P
2055 # International transaction – address not verified _ G _ G/I
2057 # UNACCEPTABLE AVS RESPONSES:
2058 # Only Address matches A Y A A
2059 # Only 5-digit postal code matches Z Z Z Z
2060 # Only 9-digit postal code matches Z Z W W
2061 # Neither address nor postal code matches N N N N
2063 if (my $avscode = uc($transaction->avs_code)) {
2065 # map codes to accept/warn/reject
2067 'American Express card' => {
2076 'Discover card' => {
2115 my $cardtype = cardtype($content{card_number});
2116 if ($avs->{$cardtype}) {
2117 my $avsact = $avs->{$cardtype}->{$avscode};
2119 if ($avsact eq 'r') {
2120 return "AVS code verification failed, cardtype $cardtype, code $avscode";
2121 } elsif ($avsact eq 'w') {
2122 $warning = "AVS did not occur, cardtype $cardtype, code $avscode";
2123 } elsif (!$avsact) {
2124 $warning = "AVS code unknown, cardtype $cardtype, code $avscode";
2125 } # else $avsact eq 'a'
2127 $log->warning($warning);
2130 } # else $cardtype avs handling not implemented
2131 } # else !$transaction->avs_code
2133 } else { # is not success
2135 # status is 'done' not 'declined', as in _realtime_bop_result
2136 $cust_pay_pending->status('done');
2137 $error = $transaction->error_message || 'Unknown error';
2138 $cust_pay_pending->statustext($error);
2139 # could also record failure_status here,
2140 # but it's not supported by B::OP::vSecureProcessing...
2141 # need a B::OP module with (reverse) auth only to test it with
2142 my $cpp_declined_err = $cust_pay_pending->replace;
2143 return $cpp_declined_err if $cpp_declined_err;
2147 } # end of IMMEDIATE; we now have our $error and $transaction
2150 # Save the custnum (as part of the main transaction, so it can reference
2154 if (!$cust_pay_pending->custnum) {
2155 $cust_pay_pending->set('custnum', $self->custnum);
2156 my $set_custnum_err = $cust_pay_pending->replace;
2157 if ($set_custnum_err) {
2158 $log->error($set_custnum_err);
2159 $error ||= $set_custnum_err;
2160 # but if there was a real verification error also, return that one
2168 if ( $transaction->can('card_token') && $transaction->card_token ) {
2170 if ( $options{'payinfo'} eq $self->payinfo ) {
2171 $self->payinfo($transaction->card_token);
2172 my $error = $self->replace;
2174 my $warning = "WARNING: error storing token: $error, but proceeding anyway\n";
2175 $log->warning($warning);
2186 # $error contains the transaction error_message, if is_success was false.
2200 L<FS::cust_main>, L<FS::cust_main::Billing>