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 Digest::MD5 qw(md5_base64);
8 use Business::CreditCard 0.28;
10 use FS::Record qw( qsearch qsearchs );
11 use FS::Misc qw( send_email );
14 use FS::cust_pay_pending;
17 $realtime_bop_decline_quiet = 0;
19 # 1 is mostly method/subroutine entry and options
20 # 2 traces progress of some operations
21 # 3 is even more information including possibly sensitive data
23 $me = '[FS::cust_main::Billing_Realtime]';
25 install_callback FS::UID sub {
27 #yes, need it for stuff below (prolly should be cached)
32 FS::cust_main::Billing_Realtime - Realtime billing mixin for cust_main
38 These methods are available on FS::cust_main objects.
44 =item realtime_collect [ OPTION => VALUE ... ]
46 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
47 via a Business::OnlinePayment or Business::OnlineThirdPartyPayment realtime
48 gateway. See L<http://420.am/business-onlinepayment> and
49 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
51 On failure returns an error message.
53 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.
55 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
57 I<method> is one of: I<CC>, I<ECHECK> and I<LEC>. If none is specified
58 then it is deduced from the customer record.
60 If no I<amount> is specified, then the customer balance is used.
62 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
63 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
64 if set, will override the value from the customer record.
66 I<description> is a free-text field passed to the gateway. It defaults to
67 the value defined by the business-onlinepayment-description configuration
68 option, or "Internet services" if that is unset.
70 If an I<invnum> is specified, this payment (if successful) is applied to the
71 specified invoice. If you don't specify an I<invnum> you might want to
72 call the B<apply_payments> method or set the I<apply> option.
74 I<apply> can be set to true to apply a resulting payment.
76 I<quiet> can be set true to surpress email decline notices.
78 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
79 resulting paynum, if any.
81 I<payunique> is a unique identifier for this payment.
83 I<session_id> is a session identifier associated with this payment.
85 I<depend_jobnum> allows payment capture to unlock export jobs
89 sub realtime_collect {
90 my( $self, %options ) = @_;
93 warn "$me realtime_collect:\n";
94 warn " $_ => $options{$_}\n" foreach keys %options;
97 $options{amount} = $self->balance unless exists( $options{amount} );
98 $options{method} = FS::payby->payby2bop($self->payby)
99 unless exists( $options{method} );
101 return $self->realtime_bop({%options});
105 =item realtime_bop { [ ARG => VALUE ... ] }
107 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
108 via a Business::OnlinePayment realtime gateway. See
109 L<http://420.am/business-onlinepayment> for supported gateways.
111 Required arguments in the hashref are I<method>, and I<amount>
113 Available methods are: I<CC>, I<ECHECK> and I<LEC>
115 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
117 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
118 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
119 if set, will override the value from the customer record.
121 I<description> is a free-text field passed to the gateway. It defaults to
122 the value defined by the business-onlinepayment-description configuration
123 option, or "Internet services" if that is unset.
125 If an I<invnum> is specified, this payment (if successful) is applied to the
126 specified invoice. If you don't specify an I<invnum> you might want to
127 call the B<apply_payments> method or set the I<apply> option.
129 I<apply> can be set to true to apply a resulting payment.
131 I<quiet> can be set true to surpress email decline notices.
133 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
134 resulting paynum, if any.
136 I<payunique> is a unique identifier for this payment.
138 I<session_id> is a session identifier associated with this payment.
140 I<depend_jobnum> allows payment capture to unlock export jobs
142 I<discount_term> attempts to take a discount by prepaying for discount_term
144 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
148 # some helper routines
149 sub _bop_recurring_billing {
150 my( $self, %opt ) = @_;
152 my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
154 if ( defined($method) && $method eq 'transaction_is_recur' ) {
156 return 1 if $opt{'trans_is_recur'};
160 my %hash = ( 'custnum' => $self->custnum,
165 if qsearch('cust_pay', { %hash, 'payinfo' => $opt{'payinfo'} } )
166 || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo('CARD',
176 sub _payment_gateway {
177 my ($self, $options) = @_;
179 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
180 unless exists($options->{payment_gateway});
182 $options->{payment_gateway};
186 my ($self, $options) = @_;
189 'login' => $options->{payment_gateway}->gateway_username,
190 'password' => $options->{payment_gateway}->gateway_password,
195 my ($self, $options) = @_;
197 $options->{payment_gateway}->gatewaynum
198 ? $options->{payment_gateway}->options
199 : @{ $options->{payment_gateway}->get('options') };
204 my ($self, $options) = @_;
206 unless ( $options->{'description'} ) {
207 if ( $conf->exists('business-onlinepayment-description') ) {
208 my $dtempl = $conf->config('business-onlinepayment-description');
210 my $agent = $self->agent->agent;
212 $options->{'description'} = eval qq("$dtempl");
214 $options->{'description'} = 'Internet services';
218 $options->{payinfo} = $self->payinfo unless exists( $options->{payinfo} );
219 $options->{invnum} ||= '';
220 $options->{payname} = $self->payname unless exists( $options->{payname} );
224 my ($self, $options) = @_;
227 my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
228 $content{customer_ip} = $payip if length($payip);
230 $content{invoice_number} = $options->{'invnum'}
231 if exists($options->{'invnum'}) && length($options->{'invnum'});
233 $content{email_customer} =
234 ( $conf->exists('business-onlinepayment-email_customer')
235 || $conf->exists('business-onlinepayment-email-override') );
237 my ($payname, $payfirst, $paylast);
238 if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
239 ($payname = $options->{payname}) =~
240 /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
241 or return "Illegal payname $payname";
242 ($payfirst, $paylast) = ($1, $2);
244 $payfirst = $self->getfield('first');
245 $paylast = $self->getfield('last');
246 $payname = "$payfirst $paylast";
249 $content{last_name} = $paylast;
250 $content{first_name} = $payfirst;
252 $content{name} = $payname;
254 $content{address} = exists($options->{'address1'})
255 ? $options->{'address1'}
257 my $address2 = exists($options->{'address2'})
258 ? $options->{'address2'}
260 $content{address} .= ", ". $address2 if length($address2);
262 $content{city} = exists($options->{city})
265 $content{state} = exists($options->{state})
268 $content{zip} = exists($options->{zip})
271 $content{country} = exists($options->{country})
272 ? $options->{country}
275 $content{referer} = 'http://cleanwhisker.420.am/'; #XXX fix referer :/
276 $content{phone} = $self->daytime || $self->night;
281 my %bop_method2payby = (
291 if (ref($_[0]) eq 'HASH') {
294 my ( $method, $amount ) = ( shift, shift );
296 $options{method} = $method;
297 $options{amount} = $amount;
301 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
302 warn " $_ => $options{$_}\n" foreach keys %options;
305 return $self->fake_bop(%options) if $options{'fake'};
307 $self->_bop_defaults(\%options);
310 # set trans_is_recur based on invnum if there is one
313 my $trans_is_recur = 0;
314 if ( $options{'invnum'} ) {
316 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
317 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
323 $cust_bill->cust_bill_pkg;
326 if grep { $_->freq ne '0' } @part_pkg;
334 my $payment_gateway = $self->_payment_gateway( \%options );
335 my $namespace = $payment_gateway->gateway_namespace;
337 eval "use $namespace";
341 # check for banned credit card/ACH
344 my $ban = qsearchs('banned_pay', {
345 'payby' => $bop_method2payby{$options{method}},
346 'payinfo' => md5_base64($options{payinfo}),
348 return "Banned credit card" if $ban;
354 my $bop_content = $self->_bop_content(\%options);
355 return $bop_content unless ref($bop_content);
357 my @invoicing_list = $self->invoicing_list_emailonly;
358 if ( $conf->exists('emailinvoiceautoalways')
359 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
360 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
361 push @invoicing_list, $self->all_emails;
364 my $email = ($conf->exists('business-onlinepayment-email-override'))
365 ? $conf->config('business-onlinepayment-email-override')
366 : $invoicing_list[0];
370 if ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'CC' ) {
372 $content{card_number} = $options{payinfo};
373 $paydate = exists($options{'paydate'})
374 ? $options{'paydate'}
376 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
377 $content{expiration} = "$2/$1";
379 my $paycvv = exists($options{'paycvv'})
382 $content{cvv2} = $paycvv
385 my $paystart_month = exists($options{'paystart_month'})
386 ? $options{'paystart_month'}
387 : $self->paystart_month;
389 my $paystart_year = exists($options{'paystart_year'})
390 ? $options{'paystart_year'}
391 : $self->paystart_year;
393 $content{card_start} = "$paystart_month/$paystart_year"
394 if $paystart_month && $paystart_year;
396 my $payissue = exists($options{'payissue'})
397 ? $options{'payissue'}
399 $content{issue_number} = $payissue if $payissue;
401 if ( $self->_bop_recurring_billing( 'payinfo' => $options{'payinfo'},
402 'trans_is_recur' => $trans_is_recur,
406 $content{recurring_billing} = 'YES';
407 $content{acct_code} = 'rebill'
408 if $conf->exists('credit_card-recurring_billing_acct_code');
411 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'ECHECK' ){
412 ( $content{account_number}, $content{routing_code} ) =
413 split('@', $options{payinfo});
414 $content{bank_name} = $options{payname};
415 $content{bank_state} = exists($options{'paystate'})
416 ? $options{'paystate'}
417 : $self->getfield('paystate');
418 $content{account_type} = exists($options{'paytype'})
419 ? uc($options{'paytype'}) || 'CHECKING'
420 : uc($self->getfield('paytype')) || 'CHECKING';
421 $content{account_name} = $self->getfield('first'). ' '.
422 $self->getfield('last');
424 $content{customer_org} = $self->company ? 'B' : 'I';
425 $content{state_id} = exists($options{'stateid'})
426 ? $options{'stateid'}
427 : $self->getfield('stateid');
428 $content{state_id_state} = exists($options{'stateid_state'})
429 ? $options{'stateid_state'}
430 : $self->getfield('stateid_state');
431 $content{customer_ssn} = exists($options{'ss'})
434 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'LEC' ) {
435 $content{phone} = $options{payinfo};
436 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
446 my $balance = exists( $options{'balance'} )
447 ? $options{'balance'}
450 $self->select_for_update; #mutex ... just until we get our pending record in
452 #the checks here are intended to catch concurrent payments
453 #double-form-submission prevention is taken care of in cust_pay_pending::check
456 return "The customer's balance has changed; $options{method} transaction aborted."
457 if $self->balance < $balance;
458 #&& $self->balance < $options{amount}; #might as well anyway?
460 #also check and make sure there aren't *other* pending payments for this cust
462 my @pending = qsearch('cust_pay_pending', {
463 'custnum' => $self->custnum,
464 'status' => { op=>'!=', value=>'done' }
466 return "A payment is already being processed for this customer (".
467 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
468 "); $options{method} transaction aborted."
471 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
473 my $cust_pay_pending = new FS::cust_pay_pending {
474 'custnum' => $self->custnum,
475 #'invnum' => $options{'invnum'},
476 'paid' => $options{amount},
478 'payby' => $bop_method2payby{$options{method}},
479 'payinfo' => $options{payinfo},
480 'paydate' => $paydate,
481 'recurring_billing' => $content{recurring_billing},
482 'pkgnum' => $options{'pkgnum'},
484 'gatewaynum' => $payment_gateway->gatewaynum || '',
485 'session_id' => $options{session_id} || '',
486 'jobnum' => $options{depend_jobnum} || '',
488 $cust_pay_pending->payunique( $options{payunique} )
489 if defined($options{payunique}) && length($options{payunique});
490 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
491 return $cpp_new_err if $cpp_new_err;
493 my( $action1, $action2 ) =
494 split( /\s*\,\s*/, $payment_gateway->gateway_action );
496 my $transaction = new $namespace( $payment_gateway->gateway_module,
497 $self->_bop_options(\%options),
500 $transaction->content(
501 'type' => $options{method},
502 $self->_bop_auth(\%options),
503 'action' => $action1,
504 'description' => $options{'description'},
505 'amount' => $options{amount},
506 #'invoice_number' => $options{'invnum'},
507 'customer_id' => $self->custnum,
509 'reference' => $cust_pay_pending->paypendingnum, #for now
514 $cust_pay_pending->status('pending');
515 my $cpp_pending_err = $cust_pay_pending->replace;
516 return $cpp_pending_err if $cpp_pending_err;
520 my $BOP_TESTING_SUCCESS = 1;
522 unless ( $BOP_TESTING ) {
523 $transaction->test_transaction(1)
524 if $conf->exists('business-onlinepayment-test_transaction');
525 $transaction->submit();
527 if ( $BOP_TESTING_SUCCESS ) {
528 $transaction->is_success(1);
529 $transaction->authorization('fake auth');
531 $transaction->is_success(0);
532 $transaction->error_message('fake failure');
536 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
538 return { reference => $cust_pay_pending->paypendingnum,
539 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
541 } elsif ( $transaction->is_success() && $action2 ) {
543 $cust_pay_pending->status('authorized');
544 my $cpp_authorized_err = $cust_pay_pending->replace;
545 return $cpp_authorized_err if $cpp_authorized_err;
547 my $auth = $transaction->authorization;
548 my $ordernum = $transaction->can('order_number')
549 ? $transaction->order_number
553 new Business::OnlinePayment( $payment_gateway->gateway_module,
554 $self->_bop_options(\%options),
559 type => $options{method},
561 $self->_bop_auth(\%options),
562 order_number => $ordernum,
563 amount => $options{amount},
564 authorization => $auth,
565 description => $options{'description'},
568 foreach my $field (qw( authorization_source_code returned_ACI
569 transaction_identifier validation_code
570 transaction_sequence_num local_transaction_date
571 local_transaction_time AVS_result_code )) {
572 $capture{$field} = $transaction->$field() if $transaction->can($field);
575 $capture->content( %capture );
577 $capture->test_transaction(1)
578 if $conf->exists('business-onlinepayment-test_transaction');
581 unless ( $capture->is_success ) {
582 my $e = "Authorization successful but capture failed, custnum #".
583 $self->custnum. ': '. $capture->result_code.
584 ": ". $capture->error_message;
592 # remove paycvv after initial transaction
595 #false laziness w/misc/process/payment.cgi - check both to make sure working
597 if ( length($self->paycvv)
598 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
600 my $error = $self->remove_cvv;
602 warn "WARNING: error removing cvv: $error\n";
611 if ( $transaction->can('card_token') && $transaction->card_token ) {
613 $self->card_token($transaction->card_token);
615 if ( $options{'payinfo'} eq $self->payinfo ) {
616 $self->payinfo($transaction->card_token);
617 my $error = $self->replace;
619 warn "WARNING: error storing token: $error, but proceeding anyway\n";
629 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
641 if (ref($_[0]) eq 'HASH') {
644 my ( $method, $amount ) = ( shift, shift );
646 $options{method} = $method;
647 $options{amount} = $amount;
650 if ( $options{'fake_failure'} ) {
651 return "Error: No error; test failure requested with fake_failure";
655 #if ( $payment_gateway->gatewaynum ) { # agent override
656 # $paybatch = $payment_gateway->gatewaynum. '-';
659 #$paybatch .= "$processor:". $transaction->authorization;
661 #$paybatch .= ':'. $transaction->order_number
662 # if $transaction->can('order_number')
663 # && length($transaction->order_number);
665 my $paybatch = 'FakeProcessor:54:32';
667 my $cust_pay = new FS::cust_pay ( {
668 'custnum' => $self->custnum,
669 'invnum' => $options{'invnum'},
670 'paid' => $options{amount},
672 'payby' => $bop_method2payby{$options{method}},
673 #'payinfo' => $payinfo,
674 'payinfo' => '4111111111111111',
675 'paybatch' => $paybatch,
676 #'paydate' => $paydate,
677 'paydate' => '2012-05-01',
679 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
681 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
684 $cust_pay->invnum(''); #try again with no specific invnum
685 my $error2 = $cust_pay->insert( $options{'manual'} ?
686 ( 'manual' => 1 ) : ()
689 # gah, even with transactions.
690 my $e = 'WARNING: Card/ACH debited but database not updated - '.
691 "error inserting (fake!) payment: $error2".
692 " (previously tried insert with invnum #$options{'invnum'}" .
699 if ( $options{'paynum_ref'} ) {
700 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
708 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
710 # Wraps up processing of a realtime credit card, ACH (electronic check) or
711 # phone bill transaction.
713 sub _realtime_bop_result {
714 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
716 warn "$me _realtime_bop_result: pending transaction ".
717 $cust_pay_pending->paypendingnum. "\n";
718 warn " $_ => $options{$_}\n" foreach keys %options;
721 my $payment_gateway = $options{payment_gateway}
722 or return "no payment gateway in arguments to _realtime_bop_result";
724 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
725 my $cpp_captured_err = $cust_pay_pending->replace;
726 return $cpp_captured_err if $cpp_captured_err;
728 if ( $transaction->is_success() ) {
731 if ( $payment_gateway->gatewaynum ) { # agent override
732 $paybatch = $payment_gateway->gatewaynum. '-';
735 $paybatch .= $payment_gateway->gateway_module. ":".
736 $transaction->authorization;
738 $paybatch .= ':'. $transaction->order_number
739 if $transaction->can('order_number')
740 && length($transaction->order_number);
742 my $cust_pay = new FS::cust_pay ( {
743 'custnum' => $self->custnum,
744 'invnum' => $options{'invnum'},
745 'paid' => $cust_pay_pending->paid,
747 'payby' => $cust_pay_pending->payby,
748 'payinfo' => $options{'payinfo'},
749 'paybatch' => $paybatch,
750 'paydate' => $cust_pay_pending->paydate,
751 'pkgnum' => $cust_pay_pending->pkgnum,
752 'discount_term' => $options{'discount_term'},
754 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
755 $cust_pay->payunique( $options{payunique} )
756 if defined($options{payunique}) && length($options{payunique});
758 my $oldAutoCommit = $FS::UID::AutoCommit;
759 local $FS::UID::AutoCommit = 0;
762 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
764 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
767 $cust_pay->invnum(''); #try again with no specific invnum
768 my $error2 = $cust_pay->insert( $options{'manual'} ?
769 ( 'manual' => 1 ) : ()
772 # gah. but at least we have a record of the state we had to abort in
773 # from cust_pay_pending now.
774 my $e = "WARNING: $options{method} captured but payment not recorded -".
775 " error inserting payment (". $payment_gateway->gateway_module.
777 " (previously tried insert with invnum #$options{'invnum'}" .
778 ": $error ) - pending payment saved as paypendingnum ".
779 $cust_pay_pending->paypendingnum. "\n";
785 my $jobnum = $cust_pay_pending->jobnum;
787 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
789 unless ( $placeholder ) {
790 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
791 my $e = "WARNING: $options{method} captured but job $jobnum not ".
792 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
797 $error = $placeholder->delete;
800 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
801 my $e = "WARNING: $options{method} captured but could not delete ".
802 "job $jobnum for paypendingnum ".
803 $cust_pay_pending->paypendingnum. ": $error\n";
810 if ( $options{'paynum_ref'} ) {
811 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
814 $cust_pay_pending->status('done');
815 $cust_pay_pending->statustext('captured');
816 $cust_pay_pending->paynum($cust_pay->paynum);
817 my $cpp_done_err = $cust_pay_pending->replace;
819 if ( $cpp_done_err ) {
821 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
822 my $e = "WARNING: $options{method} captured but payment not recorded - ".
823 "error updating status for paypendingnum ".
824 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
830 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
832 if ( $options{'apply'} ) {
833 my $apply_error = $self->apply_payments_and_credits;
834 if ( $apply_error ) {
835 warn "WARNING: error applying payment: $apply_error\n";
836 #but we still should return no error cause the payment otherwise went
847 my $perror = $payment_gateway->gateway_module. " error: ".
848 $transaction->error_message;
850 my $jobnum = $cust_pay_pending->jobnum;
852 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
854 if ( $placeholder ) {
855 my $error = $placeholder->depended_delete;
856 $error ||= $placeholder->delete;
857 warn "error removing provisioning jobs after declined paypendingnum ".
858 $cust_pay_pending->paypendingnum. "\n";
860 my $e = "error finding job $jobnum for declined paypendingnum ".
861 $cust_pay_pending->paypendingnum. "\n";
867 unless ( $transaction->error_message ) {
870 if ( $transaction->can('response_page') ) {
872 'page' => ( $transaction->can('response_page')
873 ? $transaction->response_page
876 'code' => ( $transaction->can('response_code')
877 ? $transaction->response_code
880 'headers' => ( $transaction->can('response_headers')
881 ? $transaction->response_headers
887 "No additional debugging information available for ".
888 $payment_gateway->gateway_module;
891 $perror .= "No error_message returned from ".
892 $payment_gateway->gateway_module. " -- ".
893 ( ref($t_response) ? Dumper($t_response) : $t_response );
897 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
898 && $conf->exists('emaildecline')
899 && grep { $_ ne 'POST' } $self->invoicing_list
900 && ! grep { $transaction->error_message =~ /$_/ }
901 $conf->config('emaildecline-exclude')
904 # Send a decline alert to the customer.
905 my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
908 # include the raw error message in the transaction state
909 $cust_pay_pending->setfield('error', $transaction->error_message);
910 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
911 $error = $msg_template->send( 'cust_main' => $self,
912 'object' => $cust_pay_pending );
916 my @templ = $conf->config('declinetemplate');
917 my $template = new Text::Template (
919 SOURCE => [ map "$_\n", @templ ],
920 ) or return "($perror) can't create template: $Text::Template::ERROR";
922 or return "($perror) can't compile template: $Text::Template::ERROR";
926 scalar( $conf->config('company_name', $self->agentnum ) ),
928 join("\n", $conf->config('company_address', $self->agentnum ) ),
929 'error' => $transaction->error_message,
932 my $error = send_email(
933 'from' => $conf->config('invoice_from', $self->agentnum ),
934 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
935 'subject' => 'Your payment could not be processed',
936 'body' => [ $template->fill_in(HASH => $templ_hash) ],
940 $perror .= " (also received error sending decline notification: $error)"
945 $cust_pay_pending->status('done');
946 $cust_pay_pending->statustext("declined: $perror");
947 my $cpp_done_err = $cust_pay_pending->replace;
948 if ( $cpp_done_err ) {
949 my $e = "WARNING: $options{method} declined but pending payment not ".
950 "resolved - error updating status for paypendingnum ".
951 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
953 $perror = "$e ($perror)";
961 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
963 Verifies successful third party processing of a realtime credit card,
964 ACH (electronic check) or phone bill transaction via a
965 Business::OnlineThirdPartyPayment realtime gateway. See
966 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
968 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
970 The additional options I<payname>, I<city>, I<state>,
971 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
972 if set, will override the value from the customer record.
974 I<description> is a free-text field passed to the gateway. It defaults to
977 If an I<invnum> is specified, this payment (if successful) is applied to the
978 specified invoice. If you don't specify an I<invnum> you might want to
979 call the B<apply_payments> method.
981 I<quiet> can be set true to surpress email decline notices.
983 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
984 resulting paynum, if any.
986 I<payunique> is a unique identifier for this payment.
988 Returns a hashref containing elements bill_error (which will be undefined
989 upon success) and session_id of any associated session.
993 sub realtime_botpp_capture {
994 my( $self, $cust_pay_pending, %options ) = @_;
996 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
997 warn " $_ => $options{$_}\n" foreach keys %options;
1000 eval "use Business::OnlineThirdPartyPayment";
1004 # select the gateway
1007 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1009 my $payment_gateway = $cust_pay_pending->gatewaynum
1010 ? qsearchs( 'payment_gateway',
1011 { gatewaynum => $cust_pay_pending->gatewaynum }
1013 : $self->agent->payment_gateway( 'method' => $method,
1014 # 'invnum' => $cust_pay_pending->invnum,
1015 # 'payinfo' => $cust_pay_pending->payinfo,
1018 $options{payment_gateway} = $payment_gateway; # for the helper subs
1024 my @invoicing_list = $self->invoicing_list_emailonly;
1025 if ( $conf->exists('emailinvoiceautoalways')
1026 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1027 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1028 push @invoicing_list, $self->all_emails;
1031 my $email = ($conf->exists('business-onlinepayment-email-override'))
1032 ? $conf->config('business-onlinepayment-email-override')
1033 : $invoicing_list[0];
1037 $content{email_customer} =
1038 ( $conf->exists('business-onlinepayment-email_customer')
1039 || $conf->exists('business-onlinepayment-email-override') );
1042 # run transaction(s)
1046 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1047 $self->_bop_options(\%options),
1050 $transaction->reference({ %options });
1052 $transaction->content(
1054 $self->_bop_auth(\%options),
1055 'action' => 'Post Authorization',
1056 'description' => $options{'description'},
1057 'amount' => $cust_pay_pending->paid,
1058 #'invoice_number' => $options{'invnum'},
1059 'customer_id' => $self->custnum,
1060 'referer' => 'http://cleanwhisker.420.am/',
1061 'reference' => $cust_pay_pending->paypendingnum,
1063 'phone' => $self->daytime || $self->night,
1065 # plus whatever is required for bogus capture avoidance
1068 $transaction->submit();
1071 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1074 bill_error => $error,
1075 session_id => $cust_pay_pending->session_id,
1080 =item default_payment_gateway
1082 DEPRECATED -- use agent->payment_gateway
1086 sub default_payment_gateway {
1087 my( $self, $method ) = @_;
1089 die "Real-time processing not enabled\n"
1090 unless $conf->exists('business-onlinepayment');
1092 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1095 my $bop_config = 'business-onlinepayment';
1096 $bop_config .= '-ach'
1097 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1098 my ( $processor, $login, $password, $action, @bop_options ) =
1099 $conf->config($bop_config);
1100 $action ||= 'normal authorization';
1101 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1102 die "No real-time processor is enabled - ".
1103 "did you set the business-onlinepayment configuration value?\n"
1106 ( $processor, $login, $password, $action, @bop_options )
1109 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1111 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
1112 via a Business::OnlinePayment realtime gateway. See
1113 L<http://420.am/business-onlinepayment> for supported gateways.
1115 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1117 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
1119 Most gateways require a reference to an original payment transaction to refund,
1120 so you probably need to specify a I<paynum>.
1122 I<amount> defaults to the original amount of the payment if not specified.
1124 I<reason> specifies a reason for the refund.
1126 I<paydate> specifies the expiration date for a credit card overriding the
1127 value from the customer record or the payment record. Specified as yyyy-mm-dd
1129 Implementation note: If I<amount> is unspecified or equal to the amount of the
1130 orignal payment, first an attempt is made to "void" the transaction via
1131 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1132 the normal attempt is made to "refund" ("credit") the transaction via the
1133 gateway is attempted.
1135 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1136 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1137 #if set, will override the value from the customer record.
1139 #If an I<invnum> is specified, this payment (if successful) is applied to the
1140 #specified invoice. If you don't specify an I<invnum> you might want to
1141 #call the B<apply_payments> method.
1145 #some false laziness w/realtime_bop, not enough to make it worth merging
1146 #but some useful small subs should be pulled out
1147 sub realtime_refund_bop {
1151 if (ref($_[0]) eq 'HASH') {
1152 %options = %{$_[0]};
1156 $options{method} = $method;
1160 warn "$me realtime_refund_bop (new): $options{method} refund\n";
1161 warn " $_ => $options{$_}\n" foreach keys %options;
1165 # look up the original payment and optionally a gateway for that payment
1169 my $amount = $options{'amount'};
1171 my( $processor, $login, $password, @bop_options, $namespace ) ;
1172 my( $auth, $order_number ) = ( '', '', '' );
1174 if ( $options{'paynum'} ) {
1176 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
1177 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1178 or return "Unknown paynum $options{'paynum'}";
1179 $amount ||= $cust_pay->paid;
1181 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1182 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1183 $cust_pay->paybatch;
1184 my $gatewaynum = '';
1185 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1187 if ( $gatewaynum ) { #gateway for the payment to be refunded
1189 my $payment_gateway =
1190 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1191 die "payment gateway $gatewaynum not found"
1192 unless $payment_gateway;
1194 $processor = $payment_gateway->gateway_module;
1195 $login = $payment_gateway->gateway_username;
1196 $password = $payment_gateway->gateway_password;
1197 $namespace = $payment_gateway->gateway_namespace;
1198 @bop_options = $payment_gateway->options;
1200 } else { #try the default gateway
1203 my $payment_gateway =
1204 $self->agent->payment_gateway('method' => $options{method});
1206 ( $conf_processor, $login, $password, $namespace ) =
1207 map { my $method = "gateway_$_"; $payment_gateway->$method }
1208 qw( module username password namespace );
1210 @bop_options = $payment_gateway->gatewaynum
1211 ? $payment_gateway->options
1212 : @{ $payment_gateway->get('options') };
1214 return "processor of payment $options{'paynum'} $processor does not".
1215 " match default processor $conf_processor"
1216 unless $processor eq $conf_processor;
1221 } else { # didn't specify a paynum, so look for agent gateway overrides
1222 # like a normal transaction
1224 my $payment_gateway =
1225 $self->agent->payment_gateway( 'method' => $options{method},
1226 #'payinfo' => $payinfo,
1228 my( $processor, $login, $password, $namespace ) =
1229 map { my $method = "gateway_$_"; $payment_gateway->$method }
1230 qw( module username password namespace );
1232 my @bop_options = $payment_gateway->gatewaynum
1233 ? $payment_gateway->options
1234 : @{ $payment_gateway->get('options') };
1237 return "neither amount nor paynum specified" unless $amount;
1239 eval "use $namespace";
1243 'type' => $options{method},
1245 'password' => $password,
1246 'order_number' => $order_number,
1247 'amount' => $amount,
1248 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
1250 $content{authorization} = $auth
1251 if length($auth); #echeck/ACH transactions have an order # but no auth
1252 #(at least with authorize.net)
1254 my $disable_void_after;
1255 if ($conf->exists('disable_void_after')
1256 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1257 $disable_void_after = $1;
1260 #first try void if applicable
1261 if ( $cust_pay && $cust_pay->paid == $amount
1263 ( not defined($disable_void_after) )
1264 || ( time < ($cust_pay->_date + $disable_void_after ) )
1267 warn " attempting void\n" if $DEBUG > 1;
1268 my $void = new Business::OnlinePayment( $processor, @bop_options );
1269 if ( $void->can('info') ) {
1270 if ( $cust_pay->payby eq 'CARD'
1271 && $void->info('CC_void_requires_card') )
1273 $content{'card_number'} = $cust_pay->payinfo;
1274 } elsif ( $cust_pay->payby eq 'CHEK'
1275 && $void->info('ECHECK_void_requires_account') )
1277 ( $content{'account_number'}, $content{'routing_code'} ) =
1278 split('@', $cust_pay->payinfo);
1279 $content{'name'} = $self->get('first'). ' '. $self->get('last');
1282 $void->content( 'action' => 'void', %content );
1283 $void->test_transaction(1)
1284 if $conf->exists('business-onlinepayment-test_transaction');
1286 if ( $void->is_success ) {
1287 my $error = $cust_pay->void($options{'reason'});
1289 # gah, even with transactions.
1290 my $e = 'WARNING: Card/ACH voided but database not updated - '.
1291 "error voiding payment: $error";
1295 warn " void successful\n" if $DEBUG > 1;
1300 warn " void unsuccessful, trying refund\n"
1304 my $address = $self->address1;
1305 $address .= ", ". $self->address2 if $self->address2;
1307 my($payname, $payfirst, $paylast);
1308 if ( $self->payname && $options{method} ne 'ECHECK' ) {
1309 $payname = $self->payname;
1310 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1311 or return "Illegal payname $payname";
1312 ($payfirst, $paylast) = ($1, $2);
1314 $payfirst = $self->getfield('first');
1315 $paylast = $self->getfield('last');
1316 $payname = "$payfirst $paylast";
1319 my @invoicing_list = $self->invoicing_list_emailonly;
1320 if ( $conf->exists('emailinvoiceautoalways')
1321 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1322 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1323 push @invoicing_list, $self->all_emails;
1326 my $email = ($conf->exists('business-onlinepayment-email-override'))
1327 ? $conf->config('business-onlinepayment-email-override')
1328 : $invoicing_list[0];
1330 my $payip = exists($options{'payip'})
1333 $content{customer_ip} = $payip
1337 if ( $options{method} eq 'CC' ) {
1340 $content{card_number} = $payinfo = $cust_pay->payinfo;
1341 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1342 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1343 ($content{expiration} = "$2/$1"); # where available
1345 $content{card_number} = $payinfo = $self->payinfo;
1346 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1347 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1348 $content{expiration} = "$2/$1";
1351 } elsif ( $options{method} eq 'ECHECK' ) {
1354 $payinfo = $cust_pay->payinfo;
1356 $payinfo = $self->payinfo;
1358 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1359 $content{bank_name} = $self->payname;
1360 $content{account_type} = 'CHECKING';
1361 $content{account_name} = $payname;
1362 $content{customer_org} = $self->company ? 'B' : 'I';
1363 $content{customer_ssn} = $self->ss;
1364 } elsif ( $options{method} eq 'LEC' ) {
1365 $content{phone} = $payinfo = $self->payinfo;
1369 my $refund = new Business::OnlinePayment( $processor, @bop_options );
1370 my %sub_content = $refund->content(
1371 'action' => 'credit',
1372 'customer_id' => $self->custnum,
1373 'last_name' => $paylast,
1374 'first_name' => $payfirst,
1376 'address' => $address,
1377 'city' => $self->city,
1378 'state' => $self->state,
1379 'zip' => $self->zip,
1380 'country' => $self->country,
1382 'phone' => $self->daytime || $self->night,
1385 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
1387 $refund->test_transaction(1)
1388 if $conf->exists('business-onlinepayment-test_transaction');
1391 return "$processor error: ". $refund->error_message
1392 unless $refund->is_success();
1394 my $paybatch = "$processor:". $refund->authorization;
1395 $paybatch .= ':'. $refund->order_number
1396 if $refund->can('order_number') && $refund->order_number;
1398 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1399 my @cust_bill_pay = $cust_pay->cust_bill_pay;
1400 last unless @cust_bill_pay;
1401 my $cust_bill_pay = pop @cust_bill_pay;
1402 my $error = $cust_bill_pay->delete;
1406 my $cust_refund = new FS::cust_refund ( {
1407 'custnum' => $self->custnum,
1408 'paynum' => $options{'paynum'},
1409 'refund' => $amount,
1411 'payby' => $bop_method2payby{$options{method}},
1412 'payinfo' => $payinfo,
1413 'paybatch' => $paybatch,
1414 'reason' => $options{'reason'} || 'card or ACH refund',
1416 my $error = $cust_refund->insert;
1418 $cust_refund->paynum(''); #try again with no specific paynum
1419 my $error2 = $cust_refund->insert;
1421 # gah, even with transactions.
1422 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1423 "error inserting refund ($processor): $error2".
1424 " (previously tried insert with paynum #$options{'paynum'}" .
1443 L<FS::cust_main>, L<FS::cust_main::Billing>