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 FS::Record qw( qsearch qsearchs );
8 use FS::Misc qw( send_email );
11 use FS::cust_pay_pending;
14 $realtime_bop_decline_quiet = 0;
16 # 1 is mostly method/subroutine entry and options
17 # 2 traces progress of some operations
18 # 3 is even more information including possibly sensitive data
20 $me = '[FS::cust_main::Billing_Realtime]';
22 install_callback FS::UID sub {
24 #yes, need it for stuff below (prolly should be cached)
29 FS::cust_main::Billing_Realtime - Realtime billing mixin for cust_main
35 These methods are available on FS::cust_main objects.
41 =item realtime_collect [ OPTION => VALUE ... ]
43 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
44 via a Business::OnlinePayment or Business::OnlineThirdPartyPayment realtime
45 gateway. See L<http://420.am/business-onlinepayment> and
46 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
48 On failure returns an error message.
50 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.
52 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
54 I<method> is one of: I<CC>, I<ECHECK> and I<LEC>. If none is specified
55 then it is deduced from the customer record.
57 If no I<amount> is specified, then the customer balance is used.
59 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
60 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
61 if set, will override the value from the customer record.
63 I<description> is a free-text field passed to the gateway. It defaults to
64 the value defined by the business-onlinepayment-description configuration
65 option, or "Internet services" if that is unset.
67 If an I<invnum> is specified, this payment (if successful) is applied to the
68 specified invoice. If you don't specify an I<invnum> you might want to
69 call the B<apply_payments> method or set the I<apply> option.
71 I<apply> can be set to true to apply a resulting payment.
73 I<quiet> can be set true to surpress email decline notices.
75 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
76 resulting paynum, if any.
78 I<payunique> is a unique identifier for this payment.
80 I<session_id> is a session identifier associated with this payment.
82 I<depend_jobnum> allows payment capture to unlock export jobs
86 sub realtime_collect {
87 my( $self, %options ) = @_;
90 warn "$me realtime_collect:\n";
91 warn " $_ => $options{$_}\n" foreach keys %options;
94 $options{amount} = $self->balance unless exists( $options{amount} );
95 $options{method} = FS::payby->payby2bop($self->payby)
96 unless exists( $options{method} );
98 return $self->realtime_bop({%options});
102 =item realtime_bop { [ ARG => VALUE ... ] }
104 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
105 via a Business::OnlinePayment realtime gateway. See
106 L<http://420.am/business-onlinepayment> for supported gateways.
108 Required arguments in the hashref are I<method>, and I<amount>
110 Available methods are: I<CC>, I<ECHECK> and I<LEC>
112 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
114 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
115 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
116 if set, will override the value from the customer record.
118 I<description> is a free-text field passed to the gateway. It defaults to
119 the value defined by the business-onlinepayment-description configuration
120 option, or "Internet services" if that is unset.
122 If an I<invnum> is specified, this payment (if successful) is applied to the
123 specified invoice. If you don't specify an I<invnum> you might want to
124 call the B<apply_payments> method or set the I<apply> option.
126 I<apply> can be set to true to apply a resulting payment.
128 I<quiet> can be set true to surpress email decline notices.
130 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
131 resulting paynum, if any.
133 I<payunique> is a unique identifier for this payment.
135 I<session_id> is a session identifier associated with this payment.
137 I<depend_jobnum> allows payment capture to unlock export jobs
139 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
143 # some helper routines
144 sub _bop_recurring_billing {
145 my( $self, %opt ) = @_;
147 my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
149 if ( defined($method) && $method eq 'transaction_is_recur' ) {
151 return 1 if $opt{'trans_is_recur'};
155 my %hash = ( 'custnum' => $self->custnum,
160 if qsearch('cust_pay', { %hash, 'payinfo' => $opt{'payinfo'} } )
161 || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo('CARD',
171 sub _payment_gateway {
172 my ($self, $options) = @_;
174 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
175 unless exists($options->{payment_gateway});
177 $options->{payment_gateway};
181 my ($self, $options) = @_;
184 'login' => $options->{payment_gateway}->gateway_username,
185 'password' => $options->{payment_gateway}->gateway_password,
190 my ($self, $options) = @_;
192 $options->{payment_gateway}->gatewaynum
193 ? $options->{payment_gateway}->options
194 : @{ $options->{payment_gateway}->get('options') };
199 my ($self, $options) = @_;
201 unless ( $options->{'description'} ) {
202 if ( $conf->exists('business-onlinepayment-description') ) {
203 my $dtempl = $conf->config('business-onlinepayment-description');
205 my $agent = $self->agent->agent;
207 $options->{'description'} = eval qq("$dtempl");
209 $options->{'description'} = 'Internet services';
213 $options->{payinfo} = $self->payinfo unless exists( $options->{payinfo} );
214 $options->{invnum} ||= '';
215 $options->{payname} = $self->payname unless exists( $options->{payname} );
219 my ($self, $options) = @_;
222 my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
223 $content{customer_ip} = $payip if length($payip);
225 $content{invoice_number} = $options->{'invnum'}
226 if exists($options->{'invnum'}) && length($options->{'invnum'});
228 $content{email_customer} =
229 ( $conf->exists('business-onlinepayment-email_customer')
230 || $conf->exists('business-onlinepayment-email-override') );
232 my ($payname, $payfirst, $paylast);
233 if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
234 ($payname = $options->{payname}) =~
235 /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
236 or return "Illegal payname $payname";
237 ($payfirst, $paylast) = ($1, $2);
239 $payfirst = $self->getfield('first');
240 $paylast = $self->getfield('last');
241 $payname = "$payfirst $paylast";
244 $content{last_name} = $paylast;
245 $content{first_name} = $payfirst;
247 $content{name} = $payname;
249 $content{address} = exists($options->{'address1'})
250 ? $options->{'address1'}
252 my $address2 = exists($options->{'address2'})
253 ? $options->{'address2'}
255 $content{address} .= ", ". $address2 if length($address2);
257 $content{city} = exists($options->{city})
260 $content{state} = exists($options->{state})
263 $content{zip} = exists($options->{zip})
266 $content{country} = exists($options->{country})
267 ? $options->{country}
270 $content{referer} = 'http://cleanwhisker.420.am/'; #XXX fix referer :/
271 $content{phone} = $self->daytime || $self->night;
276 my %bop_method2payby = (
286 if (ref($_[0]) eq 'HASH') {
289 my ( $method, $amount ) = ( shift, shift );
291 $options{method} = $method;
292 $options{amount} = $amount;
296 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
297 warn " $_ => $options{$_}\n" foreach keys %options;
300 return $self->fake_bop(%options) if $options{'fake'};
302 $self->_bop_defaults(\%options);
305 # set trans_is_recur based on invnum if there is one
308 my $trans_is_recur = 0;
309 if ( $options{'invnum'} ) {
311 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
312 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
318 $cust_bill->cust_bill_pkg;
321 if grep { $_->freq ne '0' } @part_pkg;
329 my $payment_gateway = $self->_payment_gateway( \%options );
330 my $namespace = $payment_gateway->gateway_namespace;
332 eval "use $namespace";
336 # check for banned credit card/ACH
339 my $ban = qsearchs('banned_pay', {
340 'payby' => $bop_method2payby{$options{method}},
341 'payinfo' => md5_base64($options{payinfo}),
343 return "Banned credit card" if $ban;
349 my $bop_content = $self->_bop_content(\%options);
350 return $bop_content unless ref($bop_content);
352 my @invoicing_list = $self->invoicing_list_emailonly;
353 if ( $conf->exists('emailinvoiceautoalways')
354 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
355 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
356 push @invoicing_list, $self->all_emails;
359 my $email = ($conf->exists('business-onlinepayment-email-override'))
360 ? $conf->config('business-onlinepayment-email-override')
361 : $invoicing_list[0];
365 if ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'CC' ) {
367 $content{card_number} = $options{payinfo};
368 $paydate = exists($options{'paydate'})
369 ? $options{'paydate'}
371 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
372 $content{expiration} = "$2/$1";
374 my $paycvv = exists($options{'paycvv'})
377 $content{cvv2} = $paycvv
380 my $paystart_month = exists($options{'paystart_month'})
381 ? $options{'paystart_month'}
382 : $self->paystart_month;
384 my $paystart_year = exists($options{'paystart_year'})
385 ? $options{'paystart_year'}
386 : $self->paystart_year;
388 $content{card_start} = "$paystart_month/$paystart_year"
389 if $paystart_month && $paystart_year;
391 my $payissue = exists($options{'payissue'})
392 ? $options{'payissue'}
394 $content{issue_number} = $payissue if $payissue;
396 if ( $self->_bop_recurring_billing( 'payinfo' => $options{'payinfo'},
397 'trans_is_recur' => $trans_is_recur,
401 $content{recurring_billing} = 'YES';
402 $content{acct_code} = 'rebill'
403 if $conf->exists('credit_card-recurring_billing_acct_code');
406 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'ECHECK' ){
407 ( $content{account_number}, $content{routing_code} ) =
408 split('@', $options{payinfo});
409 $content{bank_name} = $options{payname};
410 $content{bank_state} = exists($options{'paystate'})
411 ? $options{'paystate'}
412 : $self->getfield('paystate');
413 $content{account_type} = exists($options{'paytype'})
414 ? uc($options{'paytype'}) || 'CHECKING'
415 : uc($self->getfield('paytype')) || 'CHECKING';
416 $content{account_name} = $self->getfield('first'). ' '.
417 $self->getfield('last');
419 $content{customer_org} = $self->company ? 'B' : 'I';
420 $content{state_id} = exists($options{'stateid'})
421 ? $options{'stateid'}
422 : $self->getfield('stateid');
423 $content{state_id_state} = exists($options{'stateid_state'})
424 ? $options{'stateid_state'}
425 : $self->getfield('stateid_state');
426 $content{customer_ssn} = exists($options{'ss'})
429 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'LEC' ) {
430 $content{phone} = $options{payinfo};
431 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
441 my $balance = exists( $options{'balance'} )
442 ? $options{'balance'}
445 $self->select_for_update; #mutex ... just until we get our pending record in
447 #the checks here are intended to catch concurrent payments
448 #double-form-submission prevention is taken care of in cust_pay_pending::check
451 return "The customer's balance has changed; $options{method} transaction aborted."
452 if $self->balance < $balance;
453 #&& $self->balance < $options{amount}; #might as well anyway?
455 #also check and make sure there aren't *other* pending payments for this cust
457 my @pending = qsearch('cust_pay_pending', {
458 'custnum' => $self->custnum,
459 'status' => { op=>'!=', value=>'done' }
461 return "A payment is already being processed for this customer (".
462 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
463 "); $options{method} transaction aborted."
466 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
468 my $cust_pay_pending = new FS::cust_pay_pending {
469 'custnum' => $self->custnum,
470 #'invnum' => $options{'invnum'},
471 'paid' => $options{amount},
473 'payby' => $bop_method2payby{$options{method}},
474 'payinfo' => $options{payinfo},
475 'paydate' => $paydate,
476 'recurring_billing' => $content{recurring_billing},
477 'pkgnum' => $options{'pkgnum'},
479 'gatewaynum' => $payment_gateway->gatewaynum || '',
480 'session_id' => $options{session_id} || '',
481 'jobnum' => $options{depend_jobnum} || '',
483 $cust_pay_pending->payunique( $options{payunique} )
484 if defined($options{payunique}) && length($options{payunique});
485 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
486 return $cpp_new_err if $cpp_new_err;
488 my( $action1, $action2 ) =
489 split( /\s*\,\s*/, $payment_gateway->gateway_action );
491 my $transaction = new $namespace( $payment_gateway->gateway_module,
492 $self->_bop_options(\%options),
495 $transaction->content(
496 'type' => $options{method},
497 $self->_bop_auth(\%options),
498 'action' => $action1,
499 'description' => $options{'description'},
500 'amount' => $options{amount},
501 #'invoice_number' => $options{'invnum'},
502 'customer_id' => $self->custnum,
504 'reference' => $cust_pay_pending->paypendingnum, #for now
509 $cust_pay_pending->status('pending');
510 my $cpp_pending_err = $cust_pay_pending->replace;
511 return $cpp_pending_err if $cpp_pending_err;
515 my $BOP_TESTING_SUCCESS = 1;
517 unless ( $BOP_TESTING ) {
518 $transaction->test_transaction(1)
519 if $conf->exists('business-onlinepayment-test_transaction');
520 $transaction->submit();
522 if ( $BOP_TESTING_SUCCESS ) {
523 $transaction->is_success(1);
524 $transaction->authorization('fake auth');
526 $transaction->is_success(0);
527 $transaction->error_message('fake failure');
531 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
533 return { reference => $cust_pay_pending->paypendingnum,
534 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
536 } elsif ( $transaction->is_success() && $action2 ) {
538 $cust_pay_pending->status('authorized');
539 my $cpp_authorized_err = $cust_pay_pending->replace;
540 return $cpp_authorized_err if $cpp_authorized_err;
542 my $auth = $transaction->authorization;
543 my $ordernum = $transaction->can('order_number')
544 ? $transaction->order_number
548 new Business::OnlinePayment( $payment_gateway->gateway_module,
549 $self->_bop_options(\%options),
554 type => $options{method},
556 $self->_bop_auth(\%options),
557 order_number => $ordernum,
558 amount => $options{amount},
559 authorization => $auth,
560 description => $options{'description'},
563 foreach my $field (qw( authorization_source_code returned_ACI
564 transaction_identifier validation_code
565 transaction_sequence_num local_transaction_date
566 local_transaction_time AVS_result_code )) {
567 $capture{$field} = $transaction->$field() if $transaction->can($field);
570 $capture->content( %capture );
572 $capture->test_transaction(1)
573 if $conf->exists('business-onlinepayment-test_transaction');
576 unless ( $capture->is_success ) {
577 my $e = "Authorization successful but capture failed, custnum #".
578 $self->custnum. ': '. $capture->result_code.
579 ": ". $capture->error_message;
587 # remove paycvv after initial transaction
590 #false laziness w/misc/process/payment.cgi - check both to make sure working
592 if ( length($self->paycvv)
593 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
595 my $error = $self->remove_cvv;
597 warn "WARNING: error removing cvv: $error\n";
606 if ( $transaction->can('card_token') && $transaction->card_token ) {
608 $self->card_token($transaction->card_token);
610 if ( $options{'payinfo'} eq $self->payinfo ) {
611 $self->payinfo($transaction->card_token);
612 my $error = $self->replace;
614 warn "WARNING: error storing token: $error, but proceeding anyway\n";
624 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
636 if (ref($_[0]) eq 'HASH') {
639 my ( $method, $amount ) = ( shift, shift );
641 $options{method} = $method;
642 $options{amount} = $amount;
645 if ( $options{'fake_failure'} ) {
646 return "Error: No error; test failure requested with fake_failure";
650 #if ( $payment_gateway->gatewaynum ) { # agent override
651 # $paybatch = $payment_gateway->gatewaynum. '-';
654 #$paybatch .= "$processor:". $transaction->authorization;
656 #$paybatch .= ':'. $transaction->order_number
657 # if $transaction->can('order_number')
658 # && length($transaction->order_number);
660 my $paybatch = 'FakeProcessor:54:32';
662 my $cust_pay = new FS::cust_pay ( {
663 'custnum' => $self->custnum,
664 'invnum' => $options{'invnum'},
665 'paid' => $options{amount},
667 'payby' => $bop_method2payby{$options{method}},
668 #'payinfo' => $payinfo,
669 'payinfo' => '4111111111111111',
670 'paybatch' => $paybatch,
671 #'paydate' => $paydate,
672 'paydate' => '2012-05-01',
674 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
676 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
679 $cust_pay->invnum(''); #try again with no specific invnum
680 my $error2 = $cust_pay->insert( $options{'manual'} ?
681 ( 'manual' => 1 ) : ()
684 # gah, even with transactions.
685 my $e = 'WARNING: Card/ACH debited but database not updated - '.
686 "error inserting (fake!) payment: $error2".
687 " (previously tried insert with invnum #$options{'invnum'}" .
694 if ( $options{'paynum_ref'} ) {
695 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
703 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
705 # Wraps up processing of a realtime credit card, ACH (electronic check) or
706 # phone bill transaction.
708 sub _realtime_bop_result {
709 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
711 warn "$me _realtime_bop_result: pending transaction ".
712 $cust_pay_pending->paypendingnum. "\n";
713 warn " $_ => $options{$_}\n" foreach keys %options;
716 my $payment_gateway = $options{payment_gateway}
717 or return "no payment gateway in arguments to _realtime_bop_result";
719 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
720 my $cpp_captured_err = $cust_pay_pending->replace;
721 return $cpp_captured_err if $cpp_captured_err;
723 if ( $transaction->is_success() ) {
726 if ( $payment_gateway->gatewaynum ) { # agent override
727 $paybatch = $payment_gateway->gatewaynum. '-';
730 $paybatch .= $payment_gateway->gateway_module. ":".
731 $transaction->authorization;
733 $paybatch .= ':'. $transaction->order_number
734 if $transaction->can('order_number')
735 && length($transaction->order_number);
737 my $cust_pay = new FS::cust_pay ( {
738 'custnum' => $self->custnum,
739 'invnum' => $options{'invnum'},
740 'paid' => $cust_pay_pending->paid,
742 'payby' => $cust_pay_pending->payby,
743 'payinfo' => $options{'payinfo'},
744 'paybatch' => $paybatch,
745 'paydate' => $cust_pay_pending->paydate,
746 'pkgnum' => $cust_pay_pending->pkgnum,
748 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
749 $cust_pay->payunique( $options{payunique} )
750 if defined($options{payunique}) && length($options{payunique});
752 my $oldAutoCommit = $FS::UID::AutoCommit;
753 local $FS::UID::AutoCommit = 0;
756 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
758 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
761 $cust_pay->invnum(''); #try again with no specific invnum
762 my $error2 = $cust_pay->insert( $options{'manual'} ?
763 ( 'manual' => 1 ) : ()
766 # gah. but at least we have a record of the state we had to abort in
767 # from cust_pay_pending now.
768 my $e = "WARNING: $options{method} captured but payment not recorded -".
769 " error inserting payment (". $payment_gateway->gateway_module.
771 " (previously tried insert with invnum #$options{'invnum'}" .
772 ": $error ) - pending payment saved as paypendingnum ".
773 $cust_pay_pending->paypendingnum. "\n";
779 my $jobnum = $cust_pay_pending->jobnum;
781 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
783 unless ( $placeholder ) {
784 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
785 my $e = "WARNING: $options{method} captured but job $jobnum not ".
786 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
791 $error = $placeholder->delete;
794 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
795 my $e = "WARNING: $options{method} captured but could not delete ".
796 "job $jobnum for paypendingnum ".
797 $cust_pay_pending->paypendingnum. ": $error\n";
804 if ( $options{'paynum_ref'} ) {
805 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
808 $cust_pay_pending->status('done');
809 $cust_pay_pending->statustext('captured');
810 $cust_pay_pending->paynum($cust_pay->paynum);
811 my $cpp_done_err = $cust_pay_pending->replace;
813 if ( $cpp_done_err ) {
815 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
816 my $e = "WARNING: $options{method} captured but payment not recorded - ".
817 "error updating status for paypendingnum ".
818 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
824 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
826 if ( $options{'apply'} ) {
827 my $apply_error = $self->apply_payments_and_credits;
828 if ( $apply_error ) {
829 warn "WARNING: error applying payment: $apply_error\n";
830 #but we still should return no error cause the payment otherwise went
841 my $perror = $payment_gateway->gateway_module. " error: ".
842 $transaction->error_message;
844 my $jobnum = $cust_pay_pending->jobnum;
846 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
848 if ( $placeholder ) {
849 my $error = $placeholder->depended_delete;
850 $error ||= $placeholder->delete;
851 warn "error removing provisioning jobs after declined paypendingnum ".
852 $cust_pay_pending->paypendingnum. "\n";
854 my $e = "error finding job $jobnum for declined paypendingnum ".
855 $cust_pay_pending->paypendingnum. "\n";
861 unless ( $transaction->error_message ) {
864 if ( $transaction->can('response_page') ) {
866 'page' => ( $transaction->can('response_page')
867 ? $transaction->response_page
870 'code' => ( $transaction->can('response_code')
871 ? $transaction->response_code
874 'headers' => ( $transaction->can('response_headers')
875 ? $transaction->response_headers
881 "No additional debugging information available for ".
882 $payment_gateway->gateway_module;
885 $perror .= "No error_message returned from ".
886 $payment_gateway->gateway_module. " -- ".
887 ( ref($t_response) ? Dumper($t_response) : $t_response );
891 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
892 && $conf->exists('emaildecline')
893 && grep { $_ ne 'POST' } $self->invoicing_list
894 && ! grep { $transaction->error_message =~ /$_/ }
895 $conf->config('emaildecline-exclude')
898 # Send a decline alert to the customer.
899 my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
902 # include the raw error message in the transaction state
903 $cust_pay_pending->setfield('error', $transaction->error_message);
904 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
905 $error = $msg_template->send( 'cust_main' => $self,
906 'object' => $cust_pay_pending );
910 my @templ = $conf->config('declinetemplate');
911 my $template = new Text::Template (
913 SOURCE => [ map "$_\n", @templ ],
914 ) or return "($perror) can't create template: $Text::Template::ERROR";
916 or return "($perror) can't compile template: $Text::Template::ERROR";
920 scalar( $conf->config('company_name', $self->agentnum ) ),
922 join("\n", $conf->config('company_address', $self->agentnum ) ),
923 'error' => $transaction->error_message,
926 my $error = send_email(
927 'from' => $conf->config('invoice_from', $self->agentnum ),
928 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
929 'subject' => 'Your payment could not be processed',
930 'body' => [ $template->fill_in(HASH => $templ_hash) ],
934 $perror .= " (also received error sending decline notification: $error)"
939 $cust_pay_pending->status('done');
940 $cust_pay_pending->statustext("declined: $perror");
941 my $cpp_done_err = $cust_pay_pending->replace;
942 if ( $cpp_done_err ) {
943 my $e = "WARNING: $options{method} declined but pending payment not ".
944 "resolved - error updating status for paypendingnum ".
945 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
947 $perror = "$e ($perror)";
955 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
957 Verifies successful third party processing of a realtime credit card,
958 ACH (electronic check) or phone bill transaction via a
959 Business::OnlineThirdPartyPayment realtime gateway. See
960 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
962 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
964 The additional options I<payname>, I<city>, I<state>,
965 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
966 if set, will override the value from the customer record.
968 I<description> is a free-text field passed to the gateway. It defaults to
971 If an I<invnum> is specified, this payment (if successful) is applied to the
972 specified invoice. If you don't specify an I<invnum> you might want to
973 call the B<apply_payments> method.
975 I<quiet> can be set true to surpress email decline notices.
977 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
978 resulting paynum, if any.
980 I<payunique> is a unique identifier for this payment.
982 Returns a hashref containing elements bill_error (which will be undefined
983 upon success) and session_id of any associated session.
987 sub realtime_botpp_capture {
988 my( $self, $cust_pay_pending, %options ) = @_;
990 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
991 warn " $_ => $options{$_}\n" foreach keys %options;
994 eval "use Business::OnlineThirdPartyPayment";
1001 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1003 my $payment_gateway = $cust_pay_pending->gatewaynum
1004 ? qsearchs( 'payment_gateway',
1005 { gatewaynum => $cust_pay_pending->gatewaynum }
1007 : $self->agent->payment_gateway( 'method' => $method,
1008 # 'invnum' => $cust_pay_pending->invnum,
1009 # 'payinfo' => $cust_pay_pending->payinfo,
1012 $options{payment_gateway} = $payment_gateway; # for the helper subs
1018 my @invoicing_list = $self->invoicing_list_emailonly;
1019 if ( $conf->exists('emailinvoiceautoalways')
1020 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1021 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1022 push @invoicing_list, $self->all_emails;
1025 my $email = ($conf->exists('business-onlinepayment-email-override'))
1026 ? $conf->config('business-onlinepayment-email-override')
1027 : $invoicing_list[0];
1031 $content{email_customer} =
1032 ( $conf->exists('business-onlinepayment-email_customer')
1033 || $conf->exists('business-onlinepayment-email-override') );
1036 # run transaction(s)
1040 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1041 $self->_bop_options(\%options),
1044 $transaction->reference({ %options });
1046 $transaction->content(
1048 $self->_bop_auth(\%options),
1049 'action' => 'Post Authorization',
1050 'description' => $options{'description'},
1051 'amount' => $cust_pay_pending->paid,
1052 #'invoice_number' => $options{'invnum'},
1053 'customer_id' => $self->custnum,
1054 'referer' => 'http://cleanwhisker.420.am/',
1055 'reference' => $cust_pay_pending->paypendingnum,
1057 'phone' => $self->daytime || $self->night,
1059 # plus whatever is required for bogus capture avoidance
1062 $transaction->submit();
1065 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1068 bill_error => $error,
1069 session_id => $cust_pay_pending->session_id,
1074 =item default_payment_gateway
1076 DEPRECATED -- use agent->payment_gateway
1080 sub default_payment_gateway {
1081 my( $self, $method ) = @_;
1083 die "Real-time processing not enabled\n"
1084 unless $conf->exists('business-onlinepayment');
1086 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1089 my $bop_config = 'business-onlinepayment';
1090 $bop_config .= '-ach'
1091 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1092 my ( $processor, $login, $password, $action, @bop_options ) =
1093 $conf->config($bop_config);
1094 $action ||= 'normal authorization';
1095 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1096 die "No real-time processor is enabled - ".
1097 "did you set the business-onlinepayment configuration value?\n"
1100 ( $processor, $login, $password, $action, @bop_options )
1103 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1105 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
1106 via a Business::OnlinePayment realtime gateway. See
1107 L<http://420.am/business-onlinepayment> for supported gateways.
1109 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1111 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
1113 Most gateways require a reference to an original payment transaction to refund,
1114 so you probably need to specify a I<paynum>.
1116 I<amount> defaults to the original amount of the payment if not specified.
1118 I<reason> specifies a reason for the refund.
1120 I<paydate> specifies the expiration date for a credit card overriding the
1121 value from the customer record or the payment record. Specified as yyyy-mm-dd
1123 Implementation note: If I<amount> is unspecified or equal to the amount of the
1124 orignal payment, first an attempt is made to "void" the transaction via
1125 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1126 the normal attempt is made to "refund" ("credit") the transaction via the
1127 gateway is attempted.
1129 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1130 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1131 #if set, will override the value from the customer record.
1133 #If an I<invnum> is specified, this payment (if successful) is applied to the
1134 #specified invoice. If you don't specify an I<invnum> you might want to
1135 #call the B<apply_payments> method.
1139 #some false laziness w/realtime_bop, not enough to make it worth merging
1140 #but some useful small subs should be pulled out
1141 sub realtime_refund_bop {
1145 if (ref($_[0]) eq 'HASH') {
1146 %options = %{$_[0]};
1150 $options{method} = $method;
1154 warn "$me realtime_refund_bop (new): $options{method} refund\n";
1155 warn " $_ => $options{$_}\n" foreach keys %options;
1159 # look up the original payment and optionally a gateway for that payment
1163 my $amount = $options{'amount'};
1165 my( $processor, $login, $password, @bop_options, $namespace ) ;
1166 my( $auth, $order_number ) = ( '', '', '' );
1168 if ( $options{'paynum'} ) {
1170 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
1171 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1172 or return "Unknown paynum $options{'paynum'}";
1173 $amount ||= $cust_pay->paid;
1175 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1176 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1177 $cust_pay->paybatch;
1178 my $gatewaynum = '';
1179 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1181 if ( $gatewaynum ) { #gateway for the payment to be refunded
1183 my $payment_gateway =
1184 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1185 die "payment gateway $gatewaynum not found"
1186 unless $payment_gateway;
1188 $processor = $payment_gateway->gateway_module;
1189 $login = $payment_gateway->gateway_username;
1190 $password = $payment_gateway->gateway_password;
1191 $namespace = $payment_gateway->gateway_namespace;
1192 @bop_options = $payment_gateway->options;
1194 } else { #try the default gateway
1197 my $payment_gateway =
1198 $self->agent->payment_gateway('method' => $options{method});
1200 ( $conf_processor, $login, $password, $namespace ) =
1201 map { my $method = "gateway_$_"; $payment_gateway->$method }
1202 qw( module username password namespace );
1204 @bop_options = $payment_gateway->gatewaynum
1205 ? $payment_gateway->options
1206 : @{ $payment_gateway->get('options') };
1208 return "processor of payment $options{'paynum'} $processor does not".
1209 " match default processor $conf_processor"
1210 unless $processor eq $conf_processor;
1215 } else { # didn't specify a paynum, so look for agent gateway overrides
1216 # like a normal transaction
1218 my $payment_gateway =
1219 $self->agent->payment_gateway( 'method' => $options{method},
1220 #'payinfo' => $payinfo,
1222 my( $processor, $login, $password, $namespace ) =
1223 map { my $method = "gateway_$_"; $payment_gateway->$method }
1224 qw( module username password namespace );
1226 my @bop_options = $payment_gateway->gatewaynum
1227 ? $payment_gateway->options
1228 : @{ $payment_gateway->get('options') };
1231 return "neither amount nor paynum specified" unless $amount;
1233 eval "use $namespace";
1237 'type' => $options{method},
1239 'password' => $password,
1240 'order_number' => $order_number,
1241 'amount' => $amount,
1242 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
1244 $content{authorization} = $auth
1245 if length($auth); #echeck/ACH transactions have an order # but no auth
1246 #(at least with authorize.net)
1248 my $disable_void_after;
1249 if ($conf->exists('disable_void_after')
1250 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1251 $disable_void_after = $1;
1254 #first try void if applicable
1255 if ( $cust_pay && $cust_pay->paid == $amount
1257 ( not defined($disable_void_after) )
1258 || ( time < ($cust_pay->_date + $disable_void_after ) )
1261 warn " attempting void\n" if $DEBUG > 1;
1262 my $void = new Business::OnlinePayment( $processor, @bop_options );
1263 if ( $void->can('info') ) {
1264 if ( $cust_pay->payby eq 'CARD'
1265 && $void->info('CC_void_requires_card') )
1267 $content{'card_number'} = $cust_pay->payinfo;
1268 } elsif ( $cust_pay->payby eq 'CHEK'
1269 && $void->info('ECHECK_void_requires_account') )
1271 ( $content{'account_number'}, $content{'routing_code'} ) =
1272 split('@', $cust_pay->payinfo);
1273 $content{'name'} = $self->get('first'). ' '. $self->get('last');
1276 $void->content( 'action' => 'void', %content );
1277 $void->test_transaction(1)
1278 if $conf->exists('business-onlinepayment-test_transaction');
1280 if ( $void->is_success ) {
1281 my $error = $cust_pay->void($options{'reason'});
1283 # gah, even with transactions.
1284 my $e = 'WARNING: Card/ACH voided but database not updated - '.
1285 "error voiding payment: $error";
1289 warn " void successful\n" if $DEBUG > 1;
1294 warn " void unsuccessful, trying refund\n"
1298 my $address = $self->address1;
1299 $address .= ", ". $self->address2 if $self->address2;
1301 my($payname, $payfirst, $paylast);
1302 if ( $self->payname && $options{method} ne 'ECHECK' ) {
1303 $payname = $self->payname;
1304 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1305 or return "Illegal payname $payname";
1306 ($payfirst, $paylast) = ($1, $2);
1308 $payfirst = $self->getfield('first');
1309 $paylast = $self->getfield('last');
1310 $payname = "$payfirst $paylast";
1313 my @invoicing_list = $self->invoicing_list_emailonly;
1314 if ( $conf->exists('emailinvoiceautoalways')
1315 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1316 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1317 push @invoicing_list, $self->all_emails;
1320 my $email = ($conf->exists('business-onlinepayment-email-override'))
1321 ? $conf->config('business-onlinepayment-email-override')
1322 : $invoicing_list[0];
1324 my $payip = exists($options{'payip'})
1327 $content{customer_ip} = $payip
1331 if ( $options{method} eq 'CC' ) {
1334 $content{card_number} = $payinfo = $cust_pay->payinfo;
1335 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1336 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1337 ($content{expiration} = "$2/$1"); # where available
1339 $content{card_number} = $payinfo = $self->payinfo;
1340 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1341 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1342 $content{expiration} = "$2/$1";
1345 } elsif ( $options{method} eq 'ECHECK' ) {
1348 $payinfo = $cust_pay->payinfo;
1350 $payinfo = $self->payinfo;
1352 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1353 $content{bank_name} = $self->payname;
1354 $content{account_type} = 'CHECKING';
1355 $content{account_name} = $payname;
1356 $content{customer_org} = $self->company ? 'B' : 'I';
1357 $content{customer_ssn} = $self->ss;
1358 } elsif ( $options{method} eq 'LEC' ) {
1359 $content{phone} = $payinfo = $self->payinfo;
1363 my $refund = new Business::OnlinePayment( $processor, @bop_options );
1364 my %sub_content = $refund->content(
1365 'action' => 'credit',
1366 'customer_id' => $self->custnum,
1367 'last_name' => $paylast,
1368 'first_name' => $payfirst,
1370 'address' => $address,
1371 'city' => $self->city,
1372 'state' => $self->state,
1373 'zip' => $self->zip,
1374 'country' => $self->country,
1376 'phone' => $self->daytime || $self->night,
1379 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
1381 $refund->test_transaction(1)
1382 if $conf->exists('business-onlinepayment-test_transaction');
1385 return "$processor error: ". $refund->error_message
1386 unless $refund->is_success();
1388 my $paybatch = "$processor:". $refund->authorization;
1389 $paybatch .= ':'. $refund->order_number
1390 if $refund->can('order_number') && $refund->order_number;
1392 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1393 my @cust_bill_pay = $cust_pay->cust_bill_pay;
1394 last unless @cust_bill_pay;
1395 my $cust_bill_pay = pop @cust_bill_pay;
1396 my $error = $cust_bill_pay->delete;
1400 my $cust_refund = new FS::cust_refund ( {
1401 'custnum' => $self->custnum,
1402 'paynum' => $options{'paynum'},
1403 'refund' => $amount,
1405 'payby' => $bop_method2payby{$options{method}},
1406 'payinfo' => $payinfo,
1407 'paybatch' => $paybatch,
1408 'reason' => $options{'reason'} || 'card or ACH refund',
1410 my $error = $cust_refund->insert;
1412 $cust_refund->paynum(''); #try again with no specific paynum
1413 my $error2 = $cust_refund->insert;
1415 # gah, even with transactions.
1416 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1417 "error inserting refund ($processor): $error2".
1418 " (previously tried insert with paynum #$options{'paynum'}" .
1437 L<FS::cust_main>, L<FS::cust_main::Billing>