1 package FS::cust_main::Billing_Realtime;
4 use vars qw( $conf $DEBUG $me );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::Misc qw( send_email );
10 use FS::cust_pay_pending;
13 #$realtime_bop_decline_quiet = 0;
15 # 1 is mostly method/subroutine entry and options
16 # 2 traces progress of some operations
17 # 3 is even more information including possibly sensitive data
19 $me = '[FS::cust_main::Billing_Realtime]';
21 install_callback FS::UID sub {
23 #yes, need it for stuff below (prolly should be cached)
28 FS::cust_main::Billing_Realtime - Realtime billing mixin for cust_main
34 These methods are available on FS::cust_main objects.
40 =item realtime_collect [ OPTION => VALUE ... ]
42 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
43 via a Business::OnlinePayment or Business::OnlineThirdPartyPayment realtime
44 gateway. See L<http://420.am/business-onlinepayment> and
45 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
47 On failure returns an error message.
49 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.
51 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
53 I<method> is one of: I<CC>, I<ECHECK> and I<LEC>. If none is specified
54 then it is deduced from the customer record.
56 If no I<amount> is specified, then the customer balance is used.
58 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
59 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
60 if set, will override the value from the customer record.
62 I<description> is a free-text field passed to the gateway. It defaults to
63 the value defined by the business-onlinepayment-description configuration
64 option, or "Internet services" if that is unset.
66 If an I<invnum> is specified, this payment (if successful) is applied to the
67 specified invoice. If you don't specify an I<invnum> you might want to
68 call the B<apply_payments> method or set the I<apply> option.
70 I<apply> can be set to true to apply a resulting payment.
72 I<quiet> can be set true to surpress email decline notices.
74 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
75 resulting paynum, if any.
77 I<payunique> is a unique identifier for this payment.
79 I<session_id> is a session identifier associated with this payment.
81 I<depend_jobnum> allows payment capture to unlock export jobs
85 sub realtime_collect {
86 my( $self, %options ) = @_;
89 warn "$me realtime_collect:\n";
90 warn " $_ => $options{$_}\n" foreach keys %options;
93 $options{amount} = $self->balance unless exists( $options{amount} );
94 $options{method} = FS::payby->payby2bop($self->payby)
95 unless exists( $options{method} );
97 return $self->realtime_bop({%options});
101 =item realtime_bop { [ ARG => VALUE ... ] }
103 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
104 via a Business::OnlinePayment realtime gateway. See
105 L<http://420.am/business-onlinepayment> for supported gateways.
107 Required arguments in the hashref are I<method>, and I<amount>
109 Available methods are: I<CC>, I<ECHECK> and I<LEC>
111 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
113 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
114 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
115 if set, will override the value from the customer record.
117 I<description> is a free-text field passed to the gateway. It defaults to
118 the value defined by the business-onlinepayment-description configuration
119 option, or "Internet services" if that is unset.
121 If an I<invnum> is specified, this payment (if successful) is applied to the
122 specified invoice. If you don't specify an I<invnum> you might want to
123 call the B<apply_payments> method or set the I<apply> option.
125 I<apply> can be set to true to apply a resulting payment.
127 I<quiet> can be set true to surpress email decline notices.
129 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
130 resulting paynum, if any.
132 I<payunique> is a unique identifier for this payment.
134 I<session_id> is a session identifier associated with this payment.
136 I<depend_jobnum> allows payment capture to unlock export jobs
138 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
142 # some helper routines
143 sub _bop_recurring_billing {
144 my( $self, %opt ) = @_;
146 my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
148 if ( defined($method) && $method eq 'transaction_is_recur' ) {
150 return 1 if $opt{'trans_is_recur'};
154 my %hash = ( 'custnum' => $self->custnum,
159 if qsearch('cust_pay', { %hash, 'payinfo' => $opt{'payinfo'} } )
160 || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo('CARD',
170 sub _payment_gateway {
171 my ($self, $options) = @_;
173 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
174 unless exists($options->{payment_gateway});
176 $options->{payment_gateway};
180 my ($self, $options) = @_;
183 'login' => $options->{payment_gateway}->gateway_username,
184 'password' => $options->{payment_gateway}->gateway_password,
189 my ($self, $options) = @_;
191 $options->{payment_gateway}->gatewaynum
192 ? $options->{payment_gateway}->options
193 : @{ $options->{payment_gateway}->get('options') };
198 my ($self, $options) = @_;
200 unless ( $options->{'description'} ) {
201 if ( $conf->exists('business-onlinepayment-description') ) {
202 my $dtempl = $conf->config('business-onlinepayment-description');
204 my $agent = $self->agent->agent;
206 $options->{'description'} = eval qq("$dtempl");
208 $options->{'description'} = 'Internet services';
212 $options->{payinfo} = $self->payinfo unless exists( $options->{payinfo} );
213 $options->{invnum} ||= '';
214 $options->{payname} = $self->payname unless exists( $options->{payname} );
218 my ($self, $options) = @_;
221 my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
222 $content{customer_ip} = $payip if length($payip);
224 $content{invoice_number} = $options->{'invnum'}
225 if exists($options->{'invnum'}) && length($options->{'invnum'});
227 $content{email_customer} =
228 ( $conf->exists('business-onlinepayment-email_customer')
229 || $conf->exists('business-onlinepayment-email-override') );
231 my ($payname, $payfirst, $paylast);
232 if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
233 ($payname = $options->{payname}) =~
234 /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
235 or return "Illegal payname $payname";
236 ($payfirst, $paylast) = ($1, $2);
238 $payfirst = $self->getfield('first');
239 $paylast = $self->getfield('last');
240 $payname = "$payfirst $paylast";
243 $content{last_name} = $paylast;
244 $content{first_name} = $payfirst;
246 $content{name} = $payname;
248 $content{address} = exists($options->{'address1'})
249 ? $options->{'address1'}
251 my $address2 = exists($options->{'address2'})
252 ? $options->{'address2'}
254 $content{address} .= ", ". $address2 if length($address2);
256 $content{city} = exists($options->{city})
259 $content{state} = exists($options->{state})
262 $content{zip} = exists($options->{zip})
265 $content{country} = exists($options->{country})
266 ? $options->{country}
269 $content{referer} = 'http://cleanwhisker.420.am/'; #XXX fix referer :/
270 $content{phone} = $self->daytime || $self->night;
275 my %bop_method2payby = (
285 if (ref($_[0]) eq 'HASH') {
288 my ( $method, $amount ) = ( shift, shift );
290 $options{method} = $method;
291 $options{amount} = $amount;
295 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
296 warn " $_ => $options{$_}\n" foreach keys %options;
299 return $self->fake_bop(%options) if $options{'fake'};
301 $self->_bop_defaults(\%options);
304 # set trans_is_recur based on invnum if there is one
307 my $trans_is_recur = 0;
308 if ( $options{'invnum'} ) {
310 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
311 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
317 $cust_bill->cust_bill_pkg;
320 if grep { $_->freq ne '0' } @part_pkg;
328 my $payment_gateway = $self->_payment_gateway( \%options );
329 my $namespace = $payment_gateway->gateway_namespace;
331 eval "use $namespace";
335 # check for banned credit card/ACH
338 my $ban = qsearchs('banned_pay', {
339 'payby' => $bop_method2payby{$options{method}},
340 'payinfo' => md5_base64($options{payinfo}),
342 return "Banned credit card" if $ban;
348 my $bop_content = $self->_bop_content(\%options);
349 return $bop_content unless ref($bop_content);
351 my @invoicing_list = $self->invoicing_list_emailonly;
352 if ( $conf->exists('emailinvoiceautoalways')
353 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
354 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
355 push @invoicing_list, $self->all_emails;
358 my $email = ($conf->exists('business-onlinepayment-email-override'))
359 ? $conf->config('business-onlinepayment-email-override')
360 : $invoicing_list[0];
364 if ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'CC' ) {
366 $content{card_number} = $options{payinfo};
367 $paydate = exists($options{'paydate'})
368 ? $options{'paydate'}
370 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
371 $content{expiration} = "$2/$1";
373 my $paycvv = exists($options{'paycvv'})
376 $content{cvv2} = $paycvv
379 my $paystart_month = exists($options{'paystart_month'})
380 ? $options{'paystart_month'}
381 : $self->paystart_month;
383 my $paystart_year = exists($options{'paystart_year'})
384 ? $options{'paystart_year'}
385 : $self->paystart_year;
387 $content{card_start} = "$paystart_month/$paystart_year"
388 if $paystart_month && $paystart_year;
390 my $payissue = exists($options{'payissue'})
391 ? $options{'payissue'}
393 $content{issue_number} = $payissue if $payissue;
395 if ( $self->_bop_recurring_billing( 'payinfo' => $options{'payinfo'},
396 'trans_is_recur' => $trans_is_recur,
400 $content{recurring_billing} = 'YES';
401 $content{acct_code} = 'rebill'
402 if $conf->exists('credit_card-recurring_billing_acct_code');
405 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'ECHECK' ){
406 ( $content{account_number}, $content{routing_code} ) =
407 split('@', $options{payinfo});
408 $content{bank_name} = $options{payname};
409 $content{bank_state} = exists($options{'paystate'})
410 ? $options{'paystate'}
411 : $self->getfield('paystate');
412 $content{account_type} = exists($options{'paytype'})
413 ? uc($options{'paytype'}) || 'CHECKING'
414 : uc($self->getfield('paytype')) || 'CHECKING';
415 $content{account_name} = $self->getfield('first'). ' '.
416 $self->getfield('last');
418 $content{customer_org} = $self->company ? 'B' : 'I';
419 $content{state_id} = exists($options{'stateid'})
420 ? $options{'stateid'}
421 : $self->getfield('stateid');
422 $content{state_id_state} = exists($options{'stateid_state'})
423 ? $options{'stateid_state'}
424 : $self->getfield('stateid_state');
425 $content{customer_ssn} = exists($options{'ss'})
428 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'LEC' ) {
429 $content{phone} = $options{payinfo};
430 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
440 my $balance = exists( $options{'balance'} )
441 ? $options{'balance'}
444 $self->select_for_update; #mutex ... just until we get our pending record in
446 #the checks here are intended to catch concurrent payments
447 #double-form-submission prevention is taken care of in cust_pay_pending::check
450 return "The customer's balance has changed; $options{method} transaction aborted."
451 if $self->balance < $balance;
452 #&& $self->balance < $options{amount}; #might as well anyway?
454 #also check and make sure there aren't *other* pending payments for this cust
456 my @pending = qsearch('cust_pay_pending', {
457 'custnum' => $self->custnum,
458 'status' => { op=>'!=', value=>'done' }
460 return "A payment is already being processed for this customer (".
461 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
462 "); $options{method} transaction aborted."
465 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
467 my $cust_pay_pending = new FS::cust_pay_pending {
468 'custnum' => $self->custnum,
469 #'invnum' => $options{'invnum'},
470 'paid' => $options{amount},
472 'payby' => $bop_method2payby{$options{method}},
473 'payinfo' => $options{payinfo},
474 'paydate' => $paydate,
475 'recurring_billing' => $content{recurring_billing},
476 'pkgnum' => $options{'pkgnum'},
478 'gatewaynum' => $payment_gateway->gatewaynum || '',
479 'session_id' => $options{session_id} || '',
480 'jobnum' => $options{depend_jobnum} || '',
482 $cust_pay_pending->payunique( $options{payunique} )
483 if defined($options{payunique}) && length($options{payunique});
484 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
485 return $cpp_new_err if $cpp_new_err;
487 my( $action1, $action2 ) =
488 split( /\s*\,\s*/, $payment_gateway->gateway_action );
490 my $transaction = new $namespace( $payment_gateway->gateway_module,
491 $self->_bop_options(\%options),
494 $transaction->content(
495 'type' => $options{method},
496 $self->_bop_auth(\%options),
497 'action' => $action1,
498 'description' => $options{'description'},
499 'amount' => $options{amount},
500 #'invoice_number' => $options{'invnum'},
501 'customer_id' => $self->custnum,
503 'reference' => $cust_pay_pending->paypendingnum, #for now
508 $cust_pay_pending->status('pending');
509 my $cpp_pending_err = $cust_pay_pending->replace;
510 return $cpp_pending_err if $cpp_pending_err;
514 my $BOP_TESTING_SUCCESS = 1;
516 unless ( $BOP_TESTING ) {
517 $transaction->test_transaction(1)
518 if $conf->exists('business-onlinepayment-test_transaction');
519 $transaction->submit();
521 if ( $BOP_TESTING_SUCCESS ) {
522 $transaction->is_success(1);
523 $transaction->authorization('fake auth');
525 $transaction->is_success(0);
526 $transaction->error_message('fake failure');
530 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
532 return { reference => $cust_pay_pending->paypendingnum,
533 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
535 } elsif ( $transaction->is_success() && $action2 ) {
537 $cust_pay_pending->status('authorized');
538 my $cpp_authorized_err = $cust_pay_pending->replace;
539 return $cpp_authorized_err if $cpp_authorized_err;
541 my $auth = $transaction->authorization;
542 my $ordernum = $transaction->can('order_number')
543 ? $transaction->order_number
547 new Business::OnlinePayment( $payment_gateway->gateway_module,
548 $self->_bop_options(\%options),
553 type => $options{method},
555 $self->_bop_auth(\%options),
556 order_number => $ordernum,
557 amount => $options{amount},
558 authorization => $auth,
559 description => $options{'description'},
562 foreach my $field (qw( authorization_source_code returned_ACI
563 transaction_identifier validation_code
564 transaction_sequence_num local_transaction_date
565 local_transaction_time AVS_result_code )) {
566 $capture{$field} = $transaction->$field() if $transaction->can($field);
569 $capture->content( %capture );
571 $capture->test_transaction(1)
572 if $conf->exists('business-onlinepayment-test_transaction');
575 unless ( $capture->is_success ) {
576 my $e = "Authorization successful but capture failed, custnum #".
577 $self->custnum. ': '. $capture->result_code.
578 ": ". $capture->error_message;
586 # remove paycvv after initial transaction
589 #false laziness w/misc/process/payment.cgi - check both to make sure working
591 if ( length($self->paycvv)
592 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
594 my $error = $self->remove_cvv;
596 warn "WARNING: error removing cvv: $error\n";
605 if ( $transaction->can('card_token') && $transaction->card_token ) {
607 $self->card_token($transaction->card_token);
609 if ( $options{'payinfo'} eq $self->payinfo ) {
610 $self->payinfo($transaction->card_token);
611 my $error = $self->replace;
613 warn "WARNING: error storing token: $error, but proceeding anyway\n";
623 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
635 if (ref($_[0]) eq 'HASH') {
638 my ( $method, $amount ) = ( shift, shift );
640 $options{method} = $method;
641 $options{amount} = $amount;
644 if ( $options{'fake_failure'} ) {
645 return "Error: No error; test failure requested with fake_failure";
649 #if ( $payment_gateway->gatewaynum ) { # agent override
650 # $paybatch = $payment_gateway->gatewaynum. '-';
653 #$paybatch .= "$processor:". $transaction->authorization;
655 #$paybatch .= ':'. $transaction->order_number
656 # if $transaction->can('order_number')
657 # && length($transaction->order_number);
659 my $paybatch = 'FakeProcessor:54:32';
661 my $cust_pay = new FS::cust_pay ( {
662 'custnum' => $self->custnum,
663 'invnum' => $options{'invnum'},
664 'paid' => $options{amount},
666 'payby' => $bop_method2payby{$options{method}},
667 #'payinfo' => $payinfo,
668 'payinfo' => '4111111111111111',
669 'paybatch' => $paybatch,
670 #'paydate' => $paydate,
671 'paydate' => '2012-05-01',
673 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
675 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
678 $cust_pay->invnum(''); #try again with no specific invnum
679 my $error2 = $cust_pay->insert( $options{'manual'} ?
680 ( 'manual' => 1 ) : ()
683 # gah, even with transactions.
684 my $e = 'WARNING: Card/ACH debited but database not updated - '.
685 "error inserting (fake!) payment: $error2".
686 " (previously tried insert with invnum #$options{'invnum'}" .
693 if ( $options{'paynum_ref'} ) {
694 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
702 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
704 # Wraps up processing of a realtime credit card, ACH (electronic check) or
705 # phone bill transaction.
707 sub _realtime_bop_result {
708 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
710 warn "$me _realtime_bop_result: pending transaction ".
711 $cust_pay_pending->paypendingnum. "\n";
712 warn " $_ => $options{$_}\n" foreach keys %options;
715 my $payment_gateway = $options{payment_gateway}
716 or return "no payment gateway in arguments to _realtime_bop_result";
718 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
719 my $cpp_captured_err = $cust_pay_pending->replace;
720 return $cpp_captured_err if $cpp_captured_err;
722 if ( $transaction->is_success() ) {
725 if ( $payment_gateway->gatewaynum ) { # agent override
726 $paybatch = $payment_gateway->gatewaynum. '-';
729 $paybatch .= $payment_gateway->gateway_module. ":".
730 $transaction->authorization;
732 $paybatch .= ':'. $transaction->order_number
733 if $transaction->can('order_number')
734 && length($transaction->order_number);
736 my $cust_pay = new FS::cust_pay ( {
737 'custnum' => $self->custnum,
738 'invnum' => $options{'invnum'},
739 'paid' => $cust_pay_pending->paid,
741 'payby' => $cust_pay_pending->payby,
742 'payinfo' => $options{'payinfo'},
743 'paybatch' => $paybatch,
744 'paydate' => $cust_pay_pending->paydate,
745 'pkgnum' => $cust_pay_pending->pkgnum,
747 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
748 $cust_pay->payunique( $options{payunique} )
749 if defined($options{payunique}) && length($options{payunique});
751 my $oldAutoCommit = $FS::UID::AutoCommit;
752 local $FS::UID::AutoCommit = 0;
755 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
757 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
760 $cust_pay->invnum(''); #try again with no specific invnum
761 my $error2 = $cust_pay->insert( $options{'manual'} ?
762 ( 'manual' => 1 ) : ()
765 # gah. but at least we have a record of the state we had to abort in
766 # from cust_pay_pending now.
767 my $e = "WARNING: $options{method} captured but payment not recorded -".
768 " error inserting payment (". $payment_gateway->gateway_module.
770 " (previously tried insert with invnum #$options{'invnum'}" .
771 ": $error ) - pending payment saved as paypendingnum ".
772 $cust_pay_pending->paypendingnum. "\n";
778 my $jobnum = $cust_pay_pending->jobnum;
780 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
782 unless ( $placeholder ) {
783 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
784 my $e = "WARNING: $options{method} captured but job $jobnum not ".
785 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
790 $error = $placeholder->delete;
793 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
794 my $e = "WARNING: $options{method} captured but could not delete ".
795 "job $jobnum for paypendingnum ".
796 $cust_pay_pending->paypendingnum. ": $error\n";
803 if ( $options{'paynum_ref'} ) {
804 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
807 $cust_pay_pending->status('done');
808 $cust_pay_pending->statustext('captured');
809 $cust_pay_pending->paynum($cust_pay->paynum);
810 my $cpp_done_err = $cust_pay_pending->replace;
812 if ( $cpp_done_err ) {
814 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
815 my $e = "WARNING: $options{method} captured but payment not recorded - ".
816 "error updating status for paypendingnum ".
817 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
823 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
825 if ( $options{'apply'} ) {
826 my $apply_error = $self->apply_payments_and_credits;
827 if ( $apply_error ) {
828 warn "WARNING: error applying payment: $apply_error\n";
829 #but we still should return no error cause the payment otherwise went
840 my $perror = $payment_gateway->gateway_module. " error: ".
841 $transaction->error_message;
843 my $jobnum = $cust_pay_pending->jobnum;
845 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
847 if ( $placeholder ) {
848 my $error = $placeholder->depended_delete;
849 $error ||= $placeholder->delete;
850 warn "error removing provisioning jobs after declined paypendingnum ".
851 $cust_pay_pending->paypendingnum. "\n";
853 my $e = "error finding job $jobnum for declined paypendingnum ".
854 $cust_pay_pending->paypendingnum. "\n";
860 unless ( $transaction->error_message ) {
863 if ( $transaction->can('response_page') ) {
865 'page' => ( $transaction->can('response_page')
866 ? $transaction->response_page
869 'code' => ( $transaction->can('response_code')
870 ? $transaction->response_code
873 'headers' => ( $transaction->can('response_headers')
874 ? $transaction->response_headers
880 "No additional debugging information available for ".
881 $payment_gateway->gateway_module;
884 $perror .= "No error_message returned from ".
885 $payment_gateway->gateway_module. " -- ".
886 ( ref($t_response) ? Dumper($t_response) : $t_response );
890 if ( !$options{'quiet'} && !$FS::cust_main::realtime_bop_decline_quiet
891 && $conf->exists('emaildecline')
892 && grep { $_ ne 'POST' } $self->invoicing_list
893 && ! grep { $transaction->error_message =~ /$_/ }
894 $conf->config('emaildecline-exclude')
897 # Send a decline alert to the customer.
898 my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
901 # include the raw error message in the transaction state
902 $cust_pay_pending->setfield('error', $transaction->error_message);
903 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
904 $error = $msg_template->send( 'cust_main' => $self,
905 'object' => $cust_pay_pending );
909 my @templ = $conf->config('declinetemplate');
910 my $template = new Text::Template (
912 SOURCE => [ map "$_\n", @templ ],
913 ) or return "($perror) can't create template: $Text::Template::ERROR";
915 or return "($perror) can't compile template: $Text::Template::ERROR";
919 scalar( $conf->config('company_name', $self->agentnum ) ),
921 join("\n", $conf->config('company_address', $self->agentnum ) ),
922 'error' => $transaction->error_message,
925 my $error = send_email(
926 'from' => $conf->config('invoice_from', $self->agentnum ),
927 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
928 'subject' => 'Your payment could not be processed',
929 'body' => [ $template->fill_in(HASH => $templ_hash) ],
933 $perror .= " (also received error sending decline notification: $error)"
938 $cust_pay_pending->status('done');
939 $cust_pay_pending->statustext("declined: $perror");
940 my $cpp_done_err = $cust_pay_pending->replace;
941 if ( $cpp_done_err ) {
942 my $e = "WARNING: $options{method} declined but pending payment not ".
943 "resolved - error updating status for paypendingnum ".
944 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
946 $perror = "$e ($perror)";
954 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
956 Verifies successful third party processing of a realtime credit card,
957 ACH (electronic check) or phone bill transaction via a
958 Business::OnlineThirdPartyPayment realtime gateway. See
959 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
961 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
963 The additional options I<payname>, I<city>, I<state>,
964 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
965 if set, will override the value from the customer record.
967 I<description> is a free-text field passed to the gateway. It defaults to
970 If an I<invnum> is specified, this payment (if successful) is applied to the
971 specified invoice. If you don't specify an I<invnum> you might want to
972 call the B<apply_payments> method.
974 I<quiet> can be set true to surpress email decline notices.
976 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
977 resulting paynum, if any.
979 I<payunique> is a unique identifier for this payment.
981 Returns a hashref containing elements bill_error (which will be undefined
982 upon success) and session_id of any associated session.
986 sub realtime_botpp_capture {
987 my( $self, $cust_pay_pending, %options ) = @_;
989 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
990 warn " $_ => $options{$_}\n" foreach keys %options;
993 eval "use Business::OnlineThirdPartyPayment";
1000 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1002 my $payment_gateway = $cust_pay_pending->gatewaynum
1003 ? qsearchs( 'payment_gateway',
1004 { gatewaynum => $cust_pay_pending->gatewaynum }
1006 : $self->agent->payment_gateway( 'method' => $method,
1007 # 'invnum' => $cust_pay_pending->invnum,
1008 # 'payinfo' => $cust_pay_pending->payinfo,
1011 $options{payment_gateway} = $payment_gateway; # for the helper subs
1017 my @invoicing_list = $self->invoicing_list_emailonly;
1018 if ( $conf->exists('emailinvoiceautoalways')
1019 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1020 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1021 push @invoicing_list, $self->all_emails;
1024 my $email = ($conf->exists('business-onlinepayment-email-override'))
1025 ? $conf->config('business-onlinepayment-email-override')
1026 : $invoicing_list[0];
1030 $content{email_customer} =
1031 ( $conf->exists('business-onlinepayment-email_customer')
1032 || $conf->exists('business-onlinepayment-email-override') );
1035 # run transaction(s)
1039 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1040 $self->_bop_options(\%options),
1043 $transaction->reference({ %options });
1045 $transaction->content(
1047 $self->_bop_auth(\%options),
1048 'action' => 'Post Authorization',
1049 'description' => $options{'description'},
1050 'amount' => $cust_pay_pending->paid,
1051 #'invoice_number' => $options{'invnum'},
1052 'customer_id' => $self->custnum,
1053 'referer' => 'http://cleanwhisker.420.am/',
1054 'reference' => $cust_pay_pending->paypendingnum,
1056 'phone' => $self->daytime || $self->night,
1058 # plus whatever is required for bogus capture avoidance
1061 $transaction->submit();
1064 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1067 bill_error => $error,
1068 session_id => $cust_pay_pending->session_id,
1073 =item default_payment_gateway
1075 DEPRECATED -- use agent->payment_gateway
1079 sub default_payment_gateway {
1080 my( $self, $method ) = @_;
1082 die "Real-time processing not enabled\n"
1083 unless $conf->exists('business-onlinepayment');
1085 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1088 my $bop_config = 'business-onlinepayment';
1089 $bop_config .= '-ach'
1090 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1091 my ( $processor, $login, $password, $action, @bop_options ) =
1092 $conf->config($bop_config);
1093 $action ||= 'normal authorization';
1094 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1095 die "No real-time processor is enabled - ".
1096 "did you set the business-onlinepayment configuration value?\n"
1099 ( $processor, $login, $password, $action, @bop_options )
1102 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1104 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
1105 via a Business::OnlinePayment realtime gateway. See
1106 L<http://420.am/business-onlinepayment> for supported gateways.
1108 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1110 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
1112 Most gateways require a reference to an original payment transaction to refund,
1113 so you probably need to specify a I<paynum>.
1115 I<amount> defaults to the original amount of the payment if not specified.
1117 I<reason> specifies a reason for the refund.
1119 I<paydate> specifies the expiration date for a credit card overriding the
1120 value from the customer record or the payment record. Specified as yyyy-mm-dd
1122 Implementation note: If I<amount> is unspecified or equal to the amount of the
1123 orignal payment, first an attempt is made to "void" the transaction via
1124 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1125 the normal attempt is made to "refund" ("credit") the transaction via the
1126 gateway is attempted.
1128 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1129 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1130 #if set, will override the value from the customer record.
1132 #If an I<invnum> is specified, this payment (if successful) is applied to the
1133 #specified invoice. If you don't specify an I<invnum> you might want to
1134 #call the B<apply_payments> method.
1138 #some false laziness w/realtime_bop, not enough to make it worth merging
1139 #but some useful small subs should be pulled out
1140 sub realtime_refund_bop {
1144 if (ref($_[0]) eq 'HASH') {
1145 %options = %{$_[0]};
1149 $options{method} = $method;
1153 warn "$me realtime_refund_bop (new): $options{method} refund\n";
1154 warn " $_ => $options{$_}\n" foreach keys %options;
1158 # look up the original payment and optionally a gateway for that payment
1162 my $amount = $options{'amount'};
1164 my( $processor, $login, $password, @bop_options, $namespace ) ;
1165 my( $auth, $order_number ) = ( '', '', '' );
1167 if ( $options{'paynum'} ) {
1169 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
1170 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1171 or return "Unknown paynum $options{'paynum'}";
1172 $amount ||= $cust_pay->paid;
1174 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1175 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1176 $cust_pay->paybatch;
1177 my $gatewaynum = '';
1178 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1180 if ( $gatewaynum ) { #gateway for the payment to be refunded
1182 my $payment_gateway =
1183 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1184 die "payment gateway $gatewaynum not found"
1185 unless $payment_gateway;
1187 $processor = $payment_gateway->gateway_module;
1188 $login = $payment_gateway->gateway_username;
1189 $password = $payment_gateway->gateway_password;
1190 $namespace = $payment_gateway->gateway_namespace;
1191 @bop_options = $payment_gateway->options;
1193 } else { #try the default gateway
1196 my $payment_gateway =
1197 $self->agent->payment_gateway('method' => $options{method});
1199 ( $conf_processor, $login, $password, $namespace ) =
1200 map { my $method = "gateway_$_"; $payment_gateway->$method }
1201 qw( module username password namespace );
1203 @bop_options = $payment_gateway->gatewaynum
1204 ? $payment_gateway->options
1205 : @{ $payment_gateway->get('options') };
1207 return "processor of payment $options{'paynum'} $processor does not".
1208 " match default processor $conf_processor"
1209 unless $processor eq $conf_processor;
1214 } else { # didn't specify a paynum, so look for agent gateway overrides
1215 # like a normal transaction
1217 my $payment_gateway =
1218 $self->agent->payment_gateway( 'method' => $options{method},
1219 #'payinfo' => $payinfo,
1221 my( $processor, $login, $password, $namespace ) =
1222 map { my $method = "gateway_$_"; $payment_gateway->$method }
1223 qw( module username password namespace );
1225 my @bop_options = $payment_gateway->gatewaynum
1226 ? $payment_gateway->options
1227 : @{ $payment_gateway->get('options') };
1230 return "neither amount nor paynum specified" unless $amount;
1232 eval "use $namespace";
1236 'type' => $options{method},
1238 'password' => $password,
1239 'order_number' => $order_number,
1240 'amount' => $amount,
1241 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
1243 $content{authorization} = $auth
1244 if length($auth); #echeck/ACH transactions have an order # but no auth
1245 #(at least with authorize.net)
1247 my $disable_void_after;
1248 if ($conf->exists('disable_void_after')
1249 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1250 $disable_void_after = $1;
1253 #first try void if applicable
1254 if ( $cust_pay && $cust_pay->paid == $amount
1256 ( not defined($disable_void_after) )
1257 || ( time < ($cust_pay->_date + $disable_void_after ) )
1260 warn " attempting void\n" if $DEBUG > 1;
1261 my $void = new Business::OnlinePayment( $processor, @bop_options );
1262 if ( $void->can('info') ) {
1263 if ( $cust_pay->payby eq 'CARD'
1264 && $void->info('CC_void_requires_card') )
1266 $content{'card_number'} = $cust_pay->payinfo;
1267 } elsif ( $cust_pay->payby eq 'CHEK'
1268 && $void->info('ECHECK_void_requires_account') )
1270 ( $content{'account_number'}, $content{'routing_code'} ) =
1271 split('@', $cust_pay->payinfo);
1272 $content{'name'} = $self->get('first'). ' '. $self->get('last');
1275 $void->content( 'action' => 'void', %content );
1276 $void->test_transaction(1)
1277 if $conf->exists('business-onlinepayment-test_transaction');
1279 if ( $void->is_success ) {
1280 my $error = $cust_pay->void($options{'reason'});
1282 # gah, even with transactions.
1283 my $e = 'WARNING: Card/ACH voided but database not updated - '.
1284 "error voiding payment: $error";
1288 warn " void successful\n" if $DEBUG > 1;
1293 warn " void unsuccessful, trying refund\n"
1297 my $address = $self->address1;
1298 $address .= ", ". $self->address2 if $self->address2;
1300 my($payname, $payfirst, $paylast);
1301 if ( $self->payname && $options{method} ne 'ECHECK' ) {
1302 $payname = $self->payname;
1303 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1304 or return "Illegal payname $payname";
1305 ($payfirst, $paylast) = ($1, $2);
1307 $payfirst = $self->getfield('first');
1308 $paylast = $self->getfield('last');
1309 $payname = "$payfirst $paylast";
1312 my @invoicing_list = $self->invoicing_list_emailonly;
1313 if ( $conf->exists('emailinvoiceautoalways')
1314 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1315 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1316 push @invoicing_list, $self->all_emails;
1319 my $email = ($conf->exists('business-onlinepayment-email-override'))
1320 ? $conf->config('business-onlinepayment-email-override')
1321 : $invoicing_list[0];
1323 my $payip = exists($options{'payip'})
1326 $content{customer_ip} = $payip
1330 if ( $options{method} eq 'CC' ) {
1333 $content{card_number} = $payinfo = $cust_pay->payinfo;
1334 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1335 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1336 ($content{expiration} = "$2/$1"); # where available
1338 $content{card_number} = $payinfo = $self->payinfo;
1339 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1340 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1341 $content{expiration} = "$2/$1";
1344 } elsif ( $options{method} eq 'ECHECK' ) {
1347 $payinfo = $cust_pay->payinfo;
1349 $payinfo = $self->payinfo;
1351 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1352 $content{bank_name} = $self->payname;
1353 $content{account_type} = 'CHECKING';
1354 $content{account_name} = $payname;
1355 $content{customer_org} = $self->company ? 'B' : 'I';
1356 $content{customer_ssn} = $self->ss;
1357 } elsif ( $options{method} eq 'LEC' ) {
1358 $content{phone} = $payinfo = $self->payinfo;
1362 my $refund = new Business::OnlinePayment( $processor, @bop_options );
1363 my %sub_content = $refund->content(
1364 'action' => 'credit',
1365 'customer_id' => $self->custnum,
1366 'last_name' => $paylast,
1367 'first_name' => $payfirst,
1369 'address' => $address,
1370 'city' => $self->city,
1371 'state' => $self->state,
1372 'zip' => $self->zip,
1373 'country' => $self->country,
1375 'phone' => $self->daytime || $self->night,
1378 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
1380 $refund->test_transaction(1)
1381 if $conf->exists('business-onlinepayment-test_transaction');
1384 return "$processor error: ". $refund->error_message
1385 unless $refund->is_success();
1387 my $paybatch = "$processor:". $refund->authorization;
1388 $paybatch .= ':'. $refund->order_number
1389 if $refund->can('order_number') && $refund->order_number;
1391 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1392 my @cust_bill_pay = $cust_pay->cust_bill_pay;
1393 last unless @cust_bill_pay;
1394 my $cust_bill_pay = pop @cust_bill_pay;
1395 my $error = $cust_bill_pay->delete;
1399 my $cust_refund = new FS::cust_refund ( {
1400 'custnum' => $self->custnum,
1401 'paynum' => $options{'paynum'},
1402 'refund' => $amount,
1404 'payby' => $bop_method2payby{$options{method}},
1405 'payinfo' => $payinfo,
1406 'paybatch' => $paybatch,
1407 'reason' => $options{'reason'} || 'card or ACH refund',
1409 my $error = $cust_refund->insert;
1411 $cust_refund->paynum(''); #try again with no specific paynum
1412 my $error2 = $cust_refund->insert;
1414 # gah, even with transactions.
1415 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1416 "error inserting refund ($processor): $error2".
1417 " (previously tried insert with paynum #$options{'paynum'}" .
1436 L<FS::cust_main>, L<FS::cust_main::Billing>