1 package FS::cust_main::Billing_Realtime;
4 use vars qw( $conf $DEBUG $me );
5 use vars qw( $realtime_bop_decline_quiet ); #ugh
6 use Digest::MD5 qw(md5_base64);
7 use Business::CreditCard 0.28;
9 use FS::Record qw( qsearch qsearchs );
10 use FS::Misc qw( send_email );
13 use FS::cust_pay_pending;
16 $realtime_bop_decline_quiet = 0;
18 # 1 is mostly method/subroutine entry and options
19 # 2 traces progress of some operations
20 # 3 is even more information including possibly sensitive data
22 $me = '[FS::cust_main::Billing_Realtime]';
24 install_callback FS::UID sub {
26 #yes, need it for stuff below (prolly should be cached)
31 FS::cust_main::Billing_Realtime - Realtime billing mixin for cust_main
37 These methods are available on FS::cust_main objects.
43 =item realtime_collect [ OPTION => VALUE ... ]
45 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
46 via a Business::OnlinePayment or Business::OnlineThirdPartyPayment realtime
47 gateway. See L<http://420.am/business-onlinepayment> and
48 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
50 On failure returns an error message.
52 Returns false or a hashref upon success. The hashref contains keys popup_url reference, and collectitems. The first is a URL to which a browser should be redirected for completion of collection. The second is a reference id for the transaction suitable for the end user. The collectitems is a reference to a list of name value pairs suitable for assigning to a html form and posted to popup_url.
54 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
56 I<method> is one of: I<CC>, I<ECHECK> and I<LEC>. If none is specified
57 then it is deduced from the customer record.
59 If no I<amount> is specified, then the customer balance is used.
61 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
62 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
63 if set, will override the value from the customer record.
65 I<description> is a free-text field passed to the gateway. It defaults to
66 the value defined by the business-onlinepayment-description configuration
67 option, or "Internet services" if that is unset.
69 If an I<invnum> is specified, this payment (if successful) is applied to the
70 specified invoice. If you don't specify an I<invnum> you might want to
71 call the B<apply_payments> method or set the I<apply> option.
73 I<apply> can be set to true to apply a resulting payment.
75 I<quiet> can be set true to surpress email decline notices.
77 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
78 resulting paynum, if any.
80 I<payunique> is a unique identifier for this payment.
82 I<session_id> is a session identifier associated with this payment.
84 I<depend_jobnum> allows payment capture to unlock export jobs
88 sub realtime_collect {
89 my( $self, %options ) = @_;
92 warn "$me realtime_collect:\n";
93 warn " $_ => $options{$_}\n" foreach keys %options;
96 $options{amount} = $self->balance unless exists( $options{amount} );
97 $options{method} = FS::payby->payby2bop($self->payby)
98 unless exists( $options{method} );
100 return $self->realtime_bop({%options});
104 =item realtime_bop { [ ARG => VALUE ... ] }
106 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
107 via a Business::OnlinePayment realtime gateway. See
108 L<http://420.am/business-onlinepayment> for supported gateways.
110 Required arguments in the hashref are I<method>, and I<amount>
112 Available methods are: I<CC>, I<ECHECK> and I<LEC>
114 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
116 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
117 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
118 if set, will override the value from the customer record.
120 I<description> is a free-text field passed to the gateway. It defaults to
121 the value defined by the business-onlinepayment-description configuration
122 option, or "Internet services" if that is unset.
124 If an I<invnum> is specified, this payment (if successful) is applied to the
125 specified invoice. If you don't specify an I<invnum> you might want to
126 call the B<apply_payments> method or set the I<apply> option.
128 I<apply> can be set to true to apply a resulting payment.
130 I<quiet> can be set true to surpress email decline notices.
132 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
133 resulting paynum, if any.
135 I<payunique> is a unique identifier for this payment.
137 I<session_id> is a session identifier associated with this payment.
139 I<depend_jobnum> allows payment capture to unlock export jobs
141 I<discount_term> attempts to take a discount by prepaying for discount_term
143 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
147 # some helper routines
148 sub _bop_recurring_billing {
149 my( $self, %opt ) = @_;
151 my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
153 if ( defined($method) && $method eq 'transaction_is_recur' ) {
155 return 1 if $opt{'trans_is_recur'};
159 my %hash = ( 'custnum' => $self->custnum,
164 if qsearch('cust_pay', { %hash, 'payinfo' => $opt{'payinfo'} } )
165 || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo('CARD',
175 sub _payment_gateway {
176 my ($self, $options) = @_;
178 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
179 unless exists($options->{payment_gateway});
181 $options->{payment_gateway};
185 my ($self, $options) = @_;
188 'login' => $options->{payment_gateway}->gateway_username,
189 'password' => $options->{payment_gateway}->gateway_password,
194 my ($self, $options) = @_;
196 $options->{payment_gateway}->gatewaynum
197 ? $options->{payment_gateway}->options
198 : @{ $options->{payment_gateway}->get('options') };
203 my ($self, $options) = @_;
205 unless ( $options->{'description'} ) {
206 if ( $conf->exists('business-onlinepayment-description') ) {
207 my $dtempl = $conf->config('business-onlinepayment-description');
209 my $agent = $self->agent->agent;
211 $options->{'description'} = eval qq("$dtempl");
213 $options->{'description'} = 'Internet services';
217 $options->{payinfo} = $self->payinfo unless exists( $options->{payinfo} );
218 $options->{invnum} ||= '';
219 $options->{payname} = $self->payname unless exists( $options->{payname} );
223 my ($self, $options) = @_;
226 my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
227 $content{customer_ip} = $payip if length($payip);
229 $content{invoice_number} = $options->{'invnum'}
230 if exists($options->{'invnum'}) && length($options->{'invnum'});
232 $content{email_customer} =
233 ( $conf->exists('business-onlinepayment-email_customer')
234 || $conf->exists('business-onlinepayment-email-override') );
236 my ($payname, $payfirst, $paylast);
237 if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
238 ($payname = $options->{payname}) =~
239 /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
240 or return "Illegal payname $payname";
241 ($payfirst, $paylast) = ($1, $2);
243 $payfirst = $self->getfield('first');
244 $paylast = $self->getfield('last');
245 $payname = "$payfirst $paylast";
248 $content{last_name} = $paylast;
249 $content{first_name} = $payfirst;
251 $content{name} = $payname;
253 $content{address} = exists($options->{'address1'})
254 ? $options->{'address1'}
256 my $address2 = exists($options->{'address2'})
257 ? $options->{'address2'}
259 $content{address} .= ", ". $address2 if length($address2);
261 $content{city} = exists($options->{city})
264 $content{state} = exists($options->{state})
267 $content{zip} = exists($options->{zip})
270 $content{country} = exists($options->{country})
271 ? $options->{country}
274 $content{referer} = 'http://cleanwhisker.420.am/'; #XXX fix referer :/
275 $content{phone} = $self->daytime || $self->night;
280 my %bop_method2payby = (
290 if (ref($_[0]) eq 'HASH') {
293 my ( $method, $amount ) = ( shift, shift );
295 $options{method} = $method;
296 $options{amount} = $amount;
300 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
301 warn " $_ => $options{$_}\n" foreach keys %options;
304 return $self->fake_bop(%options) if $options{'fake'};
306 $self->_bop_defaults(\%options);
309 # set trans_is_recur based on invnum if there is one
312 my $trans_is_recur = 0;
313 if ( $options{'invnum'} ) {
315 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
316 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
322 $cust_bill->cust_bill_pkg;
325 if grep { $_->freq ne '0' } @part_pkg;
333 my $payment_gateway = $self->_payment_gateway( \%options );
334 my $namespace = $payment_gateway->gateway_namespace;
336 eval "use $namespace";
340 # check for banned credit card/ACH
343 my $ban = qsearchs('banned_pay', {
344 'payby' => $bop_method2payby{$options{method}},
345 'payinfo' => md5_base64($options{payinfo}),
347 return "Banned credit card" if $ban;
353 my $bop_content = $self->_bop_content(\%options);
354 return $bop_content unless ref($bop_content);
356 my @invoicing_list = $self->invoicing_list_emailonly;
357 if ( $conf->exists('emailinvoiceautoalways')
358 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
359 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
360 push @invoicing_list, $self->all_emails;
363 my $email = ($conf->exists('business-onlinepayment-email-override'))
364 ? $conf->config('business-onlinepayment-email-override')
365 : $invoicing_list[0];
369 if ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'CC' ) {
371 $content{card_number} = $options{payinfo};
372 $paydate = exists($options{'paydate'})
373 ? $options{'paydate'}
375 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
376 $content{expiration} = "$2/$1";
378 my $paycvv = exists($options{'paycvv'})
381 $content{cvv2} = $paycvv
384 my $paystart_month = exists($options{'paystart_month'})
385 ? $options{'paystart_month'}
386 : $self->paystart_month;
388 my $paystart_year = exists($options{'paystart_year'})
389 ? $options{'paystart_year'}
390 : $self->paystart_year;
392 $content{card_start} = "$paystart_month/$paystart_year"
393 if $paystart_month && $paystart_year;
395 my $payissue = exists($options{'payissue'})
396 ? $options{'payissue'}
398 $content{issue_number} = $payissue if $payissue;
400 if ( $self->_bop_recurring_billing( 'payinfo' => $options{'payinfo'},
401 'trans_is_recur' => $trans_is_recur,
405 $content{recurring_billing} = 'YES';
406 $content{acct_code} = 'rebill'
407 if $conf->exists('credit_card-recurring_billing_acct_code');
410 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'ECHECK' ){
411 ( $content{account_number}, $content{routing_code} ) =
412 split('@', $options{payinfo});
413 $content{bank_name} = $options{payname};
414 $content{bank_state} = exists($options{'paystate'})
415 ? $options{'paystate'}
416 : $self->getfield('paystate');
417 $content{account_type} = exists($options{'paytype'})
418 ? uc($options{'paytype'}) || 'CHECKING'
419 : uc($self->getfield('paytype')) || 'CHECKING';
420 $content{account_name} = $self->getfield('first'). ' '.
421 $self->getfield('last');
423 $content{customer_org} = $self->company ? 'B' : 'I';
424 $content{state_id} = exists($options{'stateid'})
425 ? $options{'stateid'}
426 : $self->getfield('stateid');
427 $content{state_id_state} = exists($options{'stateid_state'})
428 ? $options{'stateid_state'}
429 : $self->getfield('stateid_state');
430 $content{customer_ssn} = exists($options{'ss'})
433 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'LEC' ) {
434 $content{phone} = $options{payinfo};
435 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
445 my $balance = exists( $options{'balance'} )
446 ? $options{'balance'}
449 $self->select_for_update; #mutex ... just until we get our pending record in
451 #the checks here are intended to catch concurrent payments
452 #double-form-submission prevention is taken care of in cust_pay_pending::check
455 return "The customer's balance has changed; $options{method} transaction aborted."
456 if $self->balance < $balance;
457 #&& $self->balance < $options{amount}; #might as well anyway?
459 #also check and make sure there aren't *other* pending payments for this cust
461 my @pending = qsearch('cust_pay_pending', {
462 'custnum' => $self->custnum,
463 'status' => { op=>'!=', value=>'done' }
465 return "A payment is already being processed for this customer (".
466 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
467 "); $options{method} transaction aborted."
470 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
472 my $cust_pay_pending = new FS::cust_pay_pending {
473 'custnum' => $self->custnum,
474 #'invnum' => $options{'invnum'},
475 'paid' => $options{amount},
477 'payby' => $bop_method2payby{$options{method}},
478 'payinfo' => $options{payinfo},
479 'paydate' => $paydate,
480 'recurring_billing' => $content{recurring_billing},
481 'pkgnum' => $options{'pkgnum'},
483 'gatewaynum' => $payment_gateway->gatewaynum || '',
484 'session_id' => $options{session_id} || '',
485 'jobnum' => $options{depend_jobnum} || '',
487 $cust_pay_pending->payunique( $options{payunique} )
488 if defined($options{payunique}) && length($options{payunique});
489 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
490 return $cpp_new_err if $cpp_new_err;
492 my( $action1, $action2 ) =
493 split( /\s*\,\s*/, $payment_gateway->gateway_action );
495 my $transaction = new $namespace( $payment_gateway->gateway_module,
496 $self->_bop_options(\%options),
499 $transaction->content(
500 'type' => $options{method},
501 $self->_bop_auth(\%options),
502 'action' => $action1,
503 'description' => $options{'description'},
504 'amount' => $options{amount},
505 #'invoice_number' => $options{'invnum'},
506 'customer_id' => $self->custnum,
508 'reference' => $cust_pay_pending->paypendingnum, #for now
513 $cust_pay_pending->status('pending');
514 my $cpp_pending_err = $cust_pay_pending->replace;
515 return $cpp_pending_err if $cpp_pending_err;
519 my $BOP_TESTING_SUCCESS = 1;
521 unless ( $BOP_TESTING ) {
522 $transaction->test_transaction(1)
523 if $conf->exists('business-onlinepayment-test_transaction');
524 $transaction->submit();
526 if ( $BOP_TESTING_SUCCESS ) {
527 $transaction->is_success(1);
528 $transaction->authorization('fake auth');
530 $transaction->is_success(0);
531 $transaction->error_message('fake failure');
535 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
537 return { reference => $cust_pay_pending->paypendingnum,
538 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
540 } elsif ( $transaction->is_success() && $action2 ) {
542 $cust_pay_pending->status('authorized');
543 my $cpp_authorized_err = $cust_pay_pending->replace;
544 return $cpp_authorized_err if $cpp_authorized_err;
546 my $auth = $transaction->authorization;
547 my $ordernum = $transaction->can('order_number')
548 ? $transaction->order_number
552 new Business::OnlinePayment( $payment_gateway->gateway_module,
553 $self->_bop_options(\%options),
558 type => $options{method},
560 $self->_bop_auth(\%options),
561 order_number => $ordernum,
562 amount => $options{amount},
563 authorization => $auth,
564 description => $options{'description'},
567 foreach my $field (qw( authorization_source_code returned_ACI
568 transaction_identifier validation_code
569 transaction_sequence_num local_transaction_date
570 local_transaction_time AVS_result_code )) {
571 $capture{$field} = $transaction->$field() if $transaction->can($field);
574 $capture->content( %capture );
576 $capture->test_transaction(1)
577 if $conf->exists('business-onlinepayment-test_transaction');
580 unless ( $capture->is_success ) {
581 my $e = "Authorization successful but capture failed, custnum #".
582 $self->custnum. ': '. $capture->result_code.
583 ": ". $capture->error_message;
591 # remove paycvv after initial transaction
594 #false laziness w/misc/process/payment.cgi - check both to make sure working
596 if ( length($self->paycvv)
597 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
599 my $error = $self->remove_cvv;
601 warn "WARNING: error removing cvv: $error\n";
610 if ( $transaction->can('card_token') && $transaction->card_token ) {
612 $self->card_token($transaction->card_token);
614 if ( $options{'payinfo'} eq $self->payinfo ) {
615 $self->payinfo($transaction->card_token);
616 my $error = $self->replace;
618 warn "WARNING: error storing token: $error, but proceeding anyway\n";
628 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
640 if (ref($_[0]) eq 'HASH') {
643 my ( $method, $amount ) = ( shift, shift );
645 $options{method} = $method;
646 $options{amount} = $amount;
649 if ( $options{'fake_failure'} ) {
650 return "Error: No error; test failure requested with fake_failure";
654 #if ( $payment_gateway->gatewaynum ) { # agent override
655 # $paybatch = $payment_gateway->gatewaynum. '-';
658 #$paybatch .= "$processor:". $transaction->authorization;
660 #$paybatch .= ':'. $transaction->order_number
661 # if $transaction->can('order_number')
662 # && length($transaction->order_number);
664 my $paybatch = 'FakeProcessor:54:32';
666 my $cust_pay = new FS::cust_pay ( {
667 'custnum' => $self->custnum,
668 'invnum' => $options{'invnum'},
669 'paid' => $options{amount},
671 'payby' => $bop_method2payby{$options{method}},
672 #'payinfo' => $payinfo,
673 'payinfo' => '4111111111111111',
674 'paybatch' => $paybatch,
675 #'paydate' => $paydate,
676 'paydate' => '2012-05-01',
678 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
680 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
683 $cust_pay->invnum(''); #try again with no specific invnum
684 my $error2 = $cust_pay->insert( $options{'manual'} ?
685 ( 'manual' => 1 ) : ()
688 # gah, even with transactions.
689 my $e = 'WARNING: Card/ACH debited but database not updated - '.
690 "error inserting (fake!) payment: $error2".
691 " (previously tried insert with invnum #$options{'invnum'}" .
698 if ( $options{'paynum_ref'} ) {
699 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
707 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
709 # Wraps up processing of a realtime credit card, ACH (electronic check) or
710 # phone bill transaction.
712 sub _realtime_bop_result {
713 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
715 warn "$me _realtime_bop_result: pending transaction ".
716 $cust_pay_pending->paypendingnum. "\n";
717 warn " $_ => $options{$_}\n" foreach keys %options;
720 my $payment_gateway = $options{payment_gateway}
721 or return "no payment gateway in arguments to _realtime_bop_result";
723 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
724 my $cpp_captured_err = $cust_pay_pending->replace;
725 return $cpp_captured_err if $cpp_captured_err;
727 if ( $transaction->is_success() ) {
730 if ( $payment_gateway->gatewaynum ) { # agent override
731 $paybatch = $payment_gateway->gatewaynum. '-';
734 $paybatch .= $payment_gateway->gateway_module. ":".
735 $transaction->authorization;
737 $paybatch .= ':'. $transaction->order_number
738 if $transaction->can('order_number')
739 && length($transaction->order_number);
741 my $cust_pay = new FS::cust_pay ( {
742 'custnum' => $self->custnum,
743 'invnum' => $options{'invnum'},
744 'paid' => $cust_pay_pending->paid,
746 'payby' => $cust_pay_pending->payby,
747 'payinfo' => $options{'payinfo'},
748 'paybatch' => $paybatch,
749 'paydate' => $cust_pay_pending->paydate,
750 'pkgnum' => $cust_pay_pending->pkgnum,
751 'discount_term' => $options{'discount_term'},
753 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
754 $cust_pay->payunique( $options{payunique} )
755 if defined($options{payunique}) && length($options{payunique});
757 my $oldAutoCommit = $FS::UID::AutoCommit;
758 local $FS::UID::AutoCommit = 0;
761 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
763 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
766 $cust_pay->invnum(''); #try again with no specific invnum
767 my $error2 = $cust_pay->insert( $options{'manual'} ?
768 ( 'manual' => 1 ) : ()
771 # gah. but at least we have a record of the state we had to abort in
772 # from cust_pay_pending now.
773 my $e = "WARNING: $options{method} captured but payment not recorded -".
774 " error inserting payment (". $payment_gateway->gateway_module.
776 " (previously tried insert with invnum #$options{'invnum'}" .
777 ": $error ) - pending payment saved as paypendingnum ".
778 $cust_pay_pending->paypendingnum. "\n";
784 my $jobnum = $cust_pay_pending->jobnum;
786 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
788 unless ( $placeholder ) {
789 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
790 my $e = "WARNING: $options{method} captured but job $jobnum not ".
791 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
796 $error = $placeholder->delete;
799 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
800 my $e = "WARNING: $options{method} captured but could not delete ".
801 "job $jobnum for paypendingnum ".
802 $cust_pay_pending->paypendingnum. ": $error\n";
809 if ( $options{'paynum_ref'} ) {
810 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
813 $cust_pay_pending->status('done');
814 $cust_pay_pending->statustext('captured');
815 $cust_pay_pending->paynum($cust_pay->paynum);
816 my $cpp_done_err = $cust_pay_pending->replace;
818 if ( $cpp_done_err ) {
820 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
821 my $e = "WARNING: $options{method} captured but payment not recorded - ".
822 "error updating status for paypendingnum ".
823 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
829 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
831 if ( $options{'apply'} ) {
832 my $apply_error = $self->apply_payments_and_credits;
833 if ( $apply_error ) {
834 warn "WARNING: error applying payment: $apply_error\n";
835 #but we still should return no error cause the payment otherwise went
846 my $perror = $payment_gateway->gateway_module. " error: ".
847 $transaction->error_message;
849 my $jobnum = $cust_pay_pending->jobnum;
851 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
853 if ( $placeholder ) {
854 my $error = $placeholder->depended_delete;
855 $error ||= $placeholder->delete;
856 warn "error removing provisioning jobs after declined paypendingnum ".
857 $cust_pay_pending->paypendingnum. "\n";
859 my $e = "error finding job $jobnum for declined paypendingnum ".
860 $cust_pay_pending->paypendingnum. "\n";
866 unless ( $transaction->error_message ) {
869 if ( $transaction->can('response_page') ) {
871 'page' => ( $transaction->can('response_page')
872 ? $transaction->response_page
875 'code' => ( $transaction->can('response_code')
876 ? $transaction->response_code
879 'headers' => ( $transaction->can('response_headers')
880 ? $transaction->response_headers
886 "No additional debugging information available for ".
887 $payment_gateway->gateway_module;
890 $perror .= "No error_message returned from ".
891 $payment_gateway->gateway_module. " -- ".
892 ( ref($t_response) ? Dumper($t_response) : $t_response );
896 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
897 && $conf->exists('emaildecline')
898 && grep { $_ ne 'POST' } $self->invoicing_list
899 && ! grep { $transaction->error_message =~ /$_/ }
900 $conf->config('emaildecline-exclude')
903 # Send a decline alert to the customer.
904 my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
907 # include the raw error message in the transaction state
908 $cust_pay_pending->setfield('error', $transaction->error_message);
909 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
910 $error = $msg_template->send( 'cust_main' => $self,
911 'object' => $cust_pay_pending );
915 my @templ = $conf->config('declinetemplate');
916 my $template = new Text::Template (
918 SOURCE => [ map "$_\n", @templ ],
919 ) or return "($perror) can't create template: $Text::Template::ERROR";
921 or return "($perror) can't compile template: $Text::Template::ERROR";
925 scalar( $conf->config('company_name', $self->agentnum ) ),
927 join("\n", $conf->config('company_address', $self->agentnum ) ),
928 'error' => $transaction->error_message,
931 my $error = send_email(
932 'from' => $conf->config('invoice_from', $self->agentnum ),
933 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
934 'subject' => 'Your payment could not be processed',
935 'body' => [ $template->fill_in(HASH => $templ_hash) ],
939 $perror .= " (also received error sending decline notification: $error)"
944 $cust_pay_pending->status('done');
945 $cust_pay_pending->statustext("declined: $perror");
946 my $cpp_done_err = $cust_pay_pending->replace;
947 if ( $cpp_done_err ) {
948 my $e = "WARNING: $options{method} declined but pending payment not ".
949 "resolved - error updating status for paypendingnum ".
950 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
952 $perror = "$e ($perror)";
960 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
962 Verifies successful third party processing of a realtime credit card,
963 ACH (electronic check) or phone bill transaction via a
964 Business::OnlineThirdPartyPayment realtime gateway. See
965 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
967 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
969 The additional options I<payname>, I<city>, I<state>,
970 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
971 if set, will override the value from the customer record.
973 I<description> is a free-text field passed to the gateway. It defaults to
976 If an I<invnum> is specified, this payment (if successful) is applied to the
977 specified invoice. If you don't specify an I<invnum> you might want to
978 call the B<apply_payments> method.
980 I<quiet> can be set true to surpress email decline notices.
982 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
983 resulting paynum, if any.
985 I<payunique> is a unique identifier for this payment.
987 Returns a hashref containing elements bill_error (which will be undefined
988 upon success) and session_id of any associated session.
992 sub realtime_botpp_capture {
993 my( $self, $cust_pay_pending, %options ) = @_;
995 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
996 warn " $_ => $options{$_}\n" foreach keys %options;
999 eval "use Business::OnlineThirdPartyPayment";
1003 # select the gateway
1006 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1008 my $payment_gateway = $cust_pay_pending->gatewaynum
1009 ? qsearchs( 'payment_gateway',
1010 { gatewaynum => $cust_pay_pending->gatewaynum }
1012 : $self->agent->payment_gateway( 'method' => $method,
1013 # 'invnum' => $cust_pay_pending->invnum,
1014 # 'payinfo' => $cust_pay_pending->payinfo,
1017 $options{payment_gateway} = $payment_gateway; # for the helper subs
1023 my @invoicing_list = $self->invoicing_list_emailonly;
1024 if ( $conf->exists('emailinvoiceautoalways')
1025 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1026 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1027 push @invoicing_list, $self->all_emails;
1030 my $email = ($conf->exists('business-onlinepayment-email-override'))
1031 ? $conf->config('business-onlinepayment-email-override')
1032 : $invoicing_list[0];
1036 $content{email_customer} =
1037 ( $conf->exists('business-onlinepayment-email_customer')
1038 || $conf->exists('business-onlinepayment-email-override') );
1041 # run transaction(s)
1045 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1046 $self->_bop_options(\%options),
1049 $transaction->reference({ %options });
1051 $transaction->content(
1053 $self->_bop_auth(\%options),
1054 'action' => 'Post Authorization',
1055 'description' => $options{'description'},
1056 'amount' => $cust_pay_pending->paid,
1057 #'invoice_number' => $options{'invnum'},
1058 'customer_id' => $self->custnum,
1059 'referer' => 'http://cleanwhisker.420.am/',
1060 'reference' => $cust_pay_pending->paypendingnum,
1062 'phone' => $self->daytime || $self->night,
1064 # plus whatever is required for bogus capture avoidance
1067 $transaction->submit();
1070 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1073 bill_error => $error,
1074 session_id => $cust_pay_pending->session_id,
1079 =item default_payment_gateway
1081 DEPRECATED -- use agent->payment_gateway
1085 sub default_payment_gateway {
1086 my( $self, $method ) = @_;
1088 die "Real-time processing not enabled\n"
1089 unless $conf->exists('business-onlinepayment');
1091 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1094 my $bop_config = 'business-onlinepayment';
1095 $bop_config .= '-ach'
1096 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1097 my ( $processor, $login, $password, $action, @bop_options ) =
1098 $conf->config($bop_config);
1099 $action ||= 'normal authorization';
1100 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1101 die "No real-time processor is enabled - ".
1102 "did you set the business-onlinepayment configuration value?\n"
1105 ( $processor, $login, $password, $action, @bop_options )
1108 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1110 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
1111 via a Business::OnlinePayment realtime gateway. See
1112 L<http://420.am/business-onlinepayment> for supported gateways.
1114 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1116 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
1118 Most gateways require a reference to an original payment transaction to refund,
1119 so you probably need to specify a I<paynum>.
1121 I<amount> defaults to the original amount of the payment if not specified.
1123 I<reason> specifies a reason for the refund.
1125 I<paydate> specifies the expiration date for a credit card overriding the
1126 value from the customer record or the payment record. Specified as yyyy-mm-dd
1128 Implementation note: If I<amount> is unspecified or equal to the amount of the
1129 orignal payment, first an attempt is made to "void" the transaction via
1130 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1131 the normal attempt is made to "refund" ("credit") the transaction via the
1132 gateway is attempted.
1134 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1135 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1136 #if set, will override the value from the customer record.
1138 #If an I<invnum> is specified, this payment (if successful) is applied to the
1139 #specified invoice. If you don't specify an I<invnum> you might want to
1140 #call the B<apply_payments> method.
1144 #some false laziness w/realtime_bop, not enough to make it worth merging
1145 #but some useful small subs should be pulled out
1146 sub realtime_refund_bop {
1150 if (ref($_[0]) eq 'HASH') {
1151 %options = %{$_[0]};
1155 $options{method} = $method;
1159 warn "$me realtime_refund_bop (new): $options{method} refund\n";
1160 warn " $_ => $options{$_}\n" foreach keys %options;
1164 # look up the original payment and optionally a gateway for that payment
1168 my $amount = $options{'amount'};
1170 my( $processor, $login, $password, @bop_options, $namespace ) ;
1171 my( $auth, $order_number ) = ( '', '', '' );
1173 if ( $options{'paynum'} ) {
1175 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
1176 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1177 or return "Unknown paynum $options{'paynum'}";
1178 $amount ||= $cust_pay->paid;
1180 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1181 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1182 $cust_pay->paybatch;
1183 my $gatewaynum = '';
1184 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1186 if ( $gatewaynum ) { #gateway for the payment to be refunded
1188 my $payment_gateway =
1189 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1190 die "payment gateway $gatewaynum not found"
1191 unless $payment_gateway;
1193 $processor = $payment_gateway->gateway_module;
1194 $login = $payment_gateway->gateway_username;
1195 $password = $payment_gateway->gateway_password;
1196 $namespace = $payment_gateway->gateway_namespace;
1197 @bop_options = $payment_gateway->options;
1199 } else { #try the default gateway
1202 my $payment_gateway =
1203 $self->agent->payment_gateway('method' => $options{method});
1205 ( $conf_processor, $login, $password, $namespace ) =
1206 map { my $method = "gateway_$_"; $payment_gateway->$method }
1207 qw( module username password namespace );
1209 @bop_options = $payment_gateway->gatewaynum
1210 ? $payment_gateway->options
1211 : @{ $payment_gateway->get('options') };
1213 return "processor of payment $options{'paynum'} $processor does not".
1214 " match default processor $conf_processor"
1215 unless $processor eq $conf_processor;
1220 } else { # didn't specify a paynum, so look for agent gateway overrides
1221 # like a normal transaction
1223 my $payment_gateway =
1224 $self->agent->payment_gateway( 'method' => $options{method},
1225 #'payinfo' => $payinfo,
1227 my( $processor, $login, $password, $namespace ) =
1228 map { my $method = "gateway_$_"; $payment_gateway->$method }
1229 qw( module username password namespace );
1231 my @bop_options = $payment_gateway->gatewaynum
1232 ? $payment_gateway->options
1233 : @{ $payment_gateway->get('options') };
1236 return "neither amount nor paynum specified" unless $amount;
1238 eval "use $namespace";
1242 'type' => $options{method},
1244 'password' => $password,
1245 'order_number' => $order_number,
1246 'amount' => $amount,
1247 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
1249 $content{authorization} = $auth
1250 if length($auth); #echeck/ACH transactions have an order # but no auth
1251 #(at least with authorize.net)
1253 my $disable_void_after;
1254 if ($conf->exists('disable_void_after')
1255 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1256 $disable_void_after = $1;
1259 #first try void if applicable
1260 if ( $cust_pay && $cust_pay->paid == $amount
1262 ( not defined($disable_void_after) )
1263 || ( time < ($cust_pay->_date + $disable_void_after ) )
1266 warn " attempting void\n" if $DEBUG > 1;
1267 my $void = new Business::OnlinePayment( $processor, @bop_options );
1268 if ( $void->can('info') ) {
1269 if ( $cust_pay->payby eq 'CARD'
1270 && $void->info('CC_void_requires_card') )
1272 $content{'card_number'} = $cust_pay->payinfo;
1273 } elsif ( $cust_pay->payby eq 'CHEK'
1274 && $void->info('ECHECK_void_requires_account') )
1276 ( $content{'account_number'}, $content{'routing_code'} ) =
1277 split('@', $cust_pay->payinfo);
1278 $content{'name'} = $self->get('first'). ' '. $self->get('last');
1281 $void->content( 'action' => 'void', %content );
1282 $void->test_transaction(1)
1283 if $conf->exists('business-onlinepayment-test_transaction');
1285 if ( $void->is_success ) {
1286 my $error = $cust_pay->void($options{'reason'});
1288 # gah, even with transactions.
1289 my $e = 'WARNING: Card/ACH voided but database not updated - '.
1290 "error voiding payment: $error";
1294 warn " void successful\n" if $DEBUG > 1;
1299 warn " void unsuccessful, trying refund\n"
1303 my $address = $self->address1;
1304 $address .= ", ". $self->address2 if $self->address2;
1306 my($payname, $payfirst, $paylast);
1307 if ( $self->payname && $options{method} ne 'ECHECK' ) {
1308 $payname = $self->payname;
1309 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1310 or return "Illegal payname $payname";
1311 ($payfirst, $paylast) = ($1, $2);
1313 $payfirst = $self->getfield('first');
1314 $paylast = $self->getfield('last');
1315 $payname = "$payfirst $paylast";
1318 my @invoicing_list = $self->invoicing_list_emailonly;
1319 if ( $conf->exists('emailinvoiceautoalways')
1320 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1321 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1322 push @invoicing_list, $self->all_emails;
1325 my $email = ($conf->exists('business-onlinepayment-email-override'))
1326 ? $conf->config('business-onlinepayment-email-override')
1327 : $invoicing_list[0];
1329 my $payip = exists($options{'payip'})
1332 $content{customer_ip} = $payip
1336 if ( $options{method} eq 'CC' ) {
1339 $content{card_number} = $payinfo = $cust_pay->payinfo;
1340 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1341 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1342 ($content{expiration} = "$2/$1"); # where available
1344 $content{card_number} = $payinfo = $self->payinfo;
1345 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1346 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1347 $content{expiration} = "$2/$1";
1350 } elsif ( $options{method} eq 'ECHECK' ) {
1353 $payinfo = $cust_pay->payinfo;
1355 $payinfo = $self->payinfo;
1357 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1358 $content{bank_name} = $self->payname;
1359 $content{account_type} = 'CHECKING';
1360 $content{account_name} = $payname;
1361 $content{customer_org} = $self->company ? 'B' : 'I';
1362 $content{customer_ssn} = $self->ss;
1363 } elsif ( $options{method} eq 'LEC' ) {
1364 $content{phone} = $payinfo = $self->payinfo;
1368 my $refund = new Business::OnlinePayment( $processor, @bop_options );
1369 my %sub_content = $refund->content(
1370 'action' => 'credit',
1371 'customer_id' => $self->custnum,
1372 'last_name' => $paylast,
1373 'first_name' => $payfirst,
1375 'address' => $address,
1376 'city' => $self->city,
1377 'state' => $self->state,
1378 'zip' => $self->zip,
1379 'country' => $self->country,
1381 'phone' => $self->daytime || $self->night,
1384 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
1386 $refund->test_transaction(1)
1387 if $conf->exists('business-onlinepayment-test_transaction');
1390 return "$processor error: ". $refund->error_message
1391 unless $refund->is_success();
1393 my $paybatch = "$processor:". $refund->authorization;
1394 $paybatch .= ':'. $refund->order_number
1395 if $refund->can('order_number') && $refund->order_number;
1397 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1398 my @cust_bill_pay = $cust_pay->cust_bill_pay;
1399 last unless @cust_bill_pay;
1400 my $cust_bill_pay = pop @cust_bill_pay;
1401 my $error = $cust_bill_pay->delete;
1405 my $cust_refund = new FS::cust_refund ( {
1406 'custnum' => $self->custnum,
1407 'paynum' => $options{'paynum'},
1408 'refund' => $amount,
1410 'payby' => $bop_method2payby{$options{method}},
1411 'payinfo' => $payinfo,
1412 'paybatch' => $paybatch,
1413 'reason' => $options{'reason'} || 'card or ACH refund',
1415 my $error = $cust_refund->insert;
1417 $cust_refund->paynum(''); #try again with no specific paynum
1418 my $error2 = $cust_refund->insert;
1420 # gah, even with transactions.
1421 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1422 "error inserting refund ($processor): $error2".
1423 " (previously tried insert with paynum #$options{'paynum'}" .
1442 L<FS::cust_main>, L<FS::cust_main::Billing>