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 ) = @_;
92 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
95 warn "$me realtime_collect:\n";
96 warn " $_ => $options{$_}\n" foreach keys %options;
99 $options{amount} = $self->balance unless exists( $options{amount} );
100 $options{method} = FS::payby->payby2bop($self->payby)
101 unless exists( $options{method} );
103 return $self->realtime_bop({%options});
107 =item realtime_bop { [ ARG => VALUE ... ] }
109 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
110 via a Business::OnlinePayment realtime gateway. See
111 L<http://420.am/business-onlinepayment> for supported gateways.
113 Required arguments in the hashref are I<method>, and I<amount>
115 Available methods are: I<CC>, I<ECHECK> and I<LEC>
117 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
119 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
120 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
121 if set, will override the value from the customer record.
123 I<description> is a free-text field passed to the gateway. It defaults to
124 the value defined by the business-onlinepayment-description configuration
125 option, or "Internet services" if that is unset.
127 If an I<invnum> is specified, this payment (if successful) is applied to the
128 specified invoice. If you don't specify an I<invnum> you might want to
129 call the B<apply_payments> method or set the I<apply> option.
131 I<apply> can be set to true to apply a resulting payment.
133 I<quiet> can be set true to surpress email decline notices.
135 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
136 resulting paynum, if any.
138 I<payunique> is a unique identifier for this payment.
140 I<session_id> is a session identifier associated with this payment.
142 I<depend_jobnum> allows payment capture to unlock export jobs
144 I<discount_term> attempts to take a discount by prepaying for discount_term
146 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
150 # some helper routines
151 sub _bop_recurring_billing {
152 my( $self, %opt ) = @_;
154 my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
156 if ( defined($method) && $method eq 'transaction_is_recur' ) {
158 return 1 if $opt{'trans_is_recur'};
162 my %hash = ( 'custnum' => $self->custnum,
167 if qsearch('cust_pay', { %hash, 'payinfo' => $opt{'payinfo'} } )
168 || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo('CARD',
178 sub _payment_gateway {
179 my ($self, $options) = @_;
181 if ( $options->{'selfservice'} ) {
182 my $gatewaynum = FS::Conf->new->config('selfservice-payment_gateway');
184 return $options->{payment_gateway} ||=
185 qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
189 if ( $options->{'fake_gatewaynum'} ) {
190 $options->{payment_gateway} =
191 qsearchs('payment_gateway',
192 { 'gatewaynum' => $options->{'fake_gatewaynum'}, }
196 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
197 unless exists($options->{payment_gateway});
199 $options->{payment_gateway};
203 my ($self, $options) = @_;
206 'login' => $options->{payment_gateway}->gateway_username,
207 'password' => $options->{payment_gateway}->gateway_password,
212 my ($self, $options) = @_;
214 $options->{payment_gateway}->gatewaynum
215 ? $options->{payment_gateway}->options
216 : @{ $options->{payment_gateway}->get('options') };
221 my ($self, $options) = @_;
223 unless ( $options->{'description'} ) {
224 if ( $conf->exists('business-onlinepayment-description') ) {
225 my $dtempl = $conf->config('business-onlinepayment-description');
227 my $agent = $self->agent->agent;
229 $options->{'description'} = eval qq("$dtempl");
231 $options->{'description'} = 'Internet services';
235 $options->{payinfo} = $self->payinfo unless exists( $options->{payinfo} );
236 $options->{invnum} ||= '';
237 $options->{payname} = $self->payname unless exists( $options->{payname} );
241 my ($self, $options) = @_;
244 my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
245 $content{customer_ip} = $payip if length($payip);
247 $content{invoice_number} = $options->{'invnum'}
248 if exists($options->{'invnum'}) && length($options->{'invnum'});
250 $content{email_customer} =
251 ( $conf->exists('business-onlinepayment-email_customer')
252 || $conf->exists('business-onlinepayment-email-override') );
254 my ($payname, $payfirst, $paylast);
255 if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
256 ($payname = $options->{payname}) =~
257 /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
258 or return "Illegal payname $payname";
259 ($payfirst, $paylast) = ($1, $2);
261 $payfirst = $self->getfield('first');
262 $paylast = $self->getfield('last');
263 $payname = "$payfirst $paylast";
266 $content{last_name} = $paylast;
267 $content{first_name} = $payfirst;
269 $content{name} = $payname;
271 $content{address} = exists($options->{'address1'})
272 ? $options->{'address1'}
274 my $address2 = exists($options->{'address2'})
275 ? $options->{'address2'}
277 $content{address} .= ", ". $address2 if length($address2);
279 $content{city} = exists($options->{city})
282 $content{state} = exists($options->{state})
285 $content{zip} = exists($options->{zip})
288 $content{country} = exists($options->{country})
289 ? $options->{country}
292 $content{referer} = 'http://cleanwhisker.420.am/'; #XXX fix referer :/
293 $content{phone} = $self->daytime || $self->night;
298 my %bop_method2payby = (
307 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
310 if (ref($_[0]) eq 'HASH') {
313 my ( $method, $amount ) = ( shift, shift );
315 $options{method} = $method;
316 $options{amount} = $amount;
321 # optional credit card surcharge
324 my $cc_surcharge = 0;
325 my $cc_surcharge_pct = 0;
326 $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage')
327 if $conf->config('credit-card-surcharge-percentage');
329 # always add cc surcharge if called from event
330 if($options{'cc_surcharge_from_event'} && $cc_surcharge_pct > 0) {
331 $cc_surcharge = $options{'amount'} * $cc_surcharge_pct / 100;
332 $options{'amount'} += $cc_surcharge;
333 $options{'amount'} = sprintf("%.2f", $options{'amount'}); # round (again)?
335 elsif($cc_surcharge_pct > 0) { # we're called not from event (i.e. from a
336 # payment screen), so consider the given
337 # amount as post-surcharge
338 $cc_surcharge = $options{'amount'} - ($options{'amount'} / ( 1 + $cc_surcharge_pct/100 ));
341 $cc_surcharge = sprintf("%.2f",$cc_surcharge) if $cc_surcharge > 0;
342 $options{'cc_surcharge'} = $cc_surcharge;
346 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
347 warn " cc_surcharge = $cc_surcharge\n";
348 warn " $_ => $options{$_}\n" foreach keys %options;
351 return $self->fake_bop(\%options) if $options{'fake'};
353 $self->_bop_defaults(\%options);
356 # set trans_is_recur based on invnum if there is one
359 my $trans_is_recur = 0;
360 if ( $options{'invnum'} ) {
362 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
363 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
369 $cust_bill->cust_bill_pkg;
372 if grep { $_->freq ne '0' } @part_pkg;
380 my $payment_gateway = $self->_payment_gateway( \%options );
381 my $namespace = $payment_gateway->gateway_namespace;
383 eval "use $namespace";
387 # check for banned credit card/ACH
390 my $ban = qsearchs('banned_pay', {
391 'payby' => $bop_method2payby{$options{method}},
392 'payinfo' => md5_base64($options{payinfo}),
394 return "Banned credit card" if $ban;
400 my $bop_content = $self->_bop_content(\%options);
401 return $bop_content unless ref($bop_content);
403 my @invoicing_list = $self->invoicing_list_emailonly;
404 if ( $conf->exists('emailinvoiceautoalways')
405 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
406 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
407 push @invoicing_list, $self->all_emails;
410 my $email = ($conf->exists('business-onlinepayment-email-override'))
411 ? $conf->config('business-onlinepayment-email-override')
412 : $invoicing_list[0];
416 if ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'CC' ) {
418 $content{card_number} = $options{payinfo};
419 $paydate = exists($options{'paydate'})
420 ? $options{'paydate'}
422 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
423 $content{expiration} = "$2/$1";
425 my $paycvv = exists($options{'paycvv'})
428 $content{cvv2} = $paycvv
431 my $paystart_month = exists($options{'paystart_month'})
432 ? $options{'paystart_month'}
433 : $self->paystart_month;
435 my $paystart_year = exists($options{'paystart_year'})
436 ? $options{'paystart_year'}
437 : $self->paystart_year;
439 $content{card_start} = "$paystart_month/$paystart_year"
440 if $paystart_month && $paystart_year;
442 my $payissue = exists($options{'payissue'})
443 ? $options{'payissue'}
445 $content{issue_number} = $payissue if $payissue;
447 if ( $self->_bop_recurring_billing( 'payinfo' => $options{'payinfo'},
448 'trans_is_recur' => $trans_is_recur,
452 $content{recurring_billing} = 'YES';
453 $content{acct_code} = 'rebill'
454 if $conf->exists('credit_card-recurring_billing_acct_code');
457 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'ECHECK' ){
458 ( $content{account_number}, $content{routing_code} ) =
459 split('@', $options{payinfo});
460 $content{bank_name} = $options{payname};
461 $content{bank_state} = exists($options{'paystate'})
462 ? $options{'paystate'}
463 : $self->getfield('paystate');
464 $content{account_type}= (exists($options{'paytype'}) && $options{'paytype'})
465 ? uc($options{'paytype'})
466 : uc($self->getfield('paytype')) || 'PERSONAL CHECKING';
467 $content{account_name} = $self->getfield('first'). ' '.
468 $self->getfield('last');
470 $content{customer_org} = $self->company ? 'B' : 'I';
471 $content{state_id} = exists($options{'stateid'})
472 ? $options{'stateid'}
473 : $self->getfield('stateid');
474 $content{state_id_state} = exists($options{'stateid_state'})
475 ? $options{'stateid_state'}
476 : $self->getfield('stateid_state');
477 $content{customer_ssn} = exists($options{'ss'})
480 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'LEC' ) {
481 $content{phone} = $options{payinfo};
482 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
492 my $balance = exists( $options{'balance'} )
493 ? $options{'balance'}
496 $self->select_for_update; #mutex ... just until we get our pending record in
498 #the checks here are intended to catch concurrent payments
499 #double-form-submission prevention is taken care of in cust_pay_pending::check
502 return "The customer's balance has changed; $options{method} transaction aborted."
503 if $self->balance < $balance;
504 #&& $self->balance < $options{amount}; #might as well anyway?
506 #also check and make sure there aren't *other* pending payments for this cust
508 my @pending = qsearch('cust_pay_pending', {
509 'custnum' => $self->custnum,
510 'status' => { op=>'!=', value=>'done' }
512 # This is a problem. A self-service third party payment that fails somehow
513 # can't be retried, EVER, until someone manually clears it. Totally
514 # arbitrary fix: if the existing payment is more than two minutes old,
515 # kill it. This doesn't limit how long it can take the pending payment
516 # to complete, only how long it will obstruct new payments.
519 if ( time - $_->_date > 120 ) {
520 my $error = $_->delete;
521 warn "error deleting stale pending payment ".$_->paypendingnum.": $error"
522 if $error; # not fatal, it will fail anyway
525 push @still_pending, $_;
528 @pending = @still_pending;
530 return "A payment is already being processed for this customer (".
531 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
532 "); $options{method} transaction aborted."
535 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
537 my $cust_pay_pending = new FS::cust_pay_pending {
538 'custnum' => $self->custnum,
539 #'invnum' => $options{'invnum'},
540 'paid' => $options{amount},
542 'payby' => $bop_method2payby{$options{method}},
543 'payinfo' => $options{payinfo},
544 'paydate' => $paydate,
545 'recurring_billing' => $content{recurring_billing},
546 'pkgnum' => $options{'pkgnum'},
548 'gatewaynum' => $payment_gateway->gatewaynum || '',
549 'session_id' => $options{session_id} || '',
550 'jobnum' => $options{depend_jobnum} || '',
552 $cust_pay_pending->payunique( $options{payunique} )
553 if defined($options{payunique}) && length($options{payunique});
554 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
555 return $cpp_new_err if $cpp_new_err;
557 my( $action1, $action2 ) =
558 split( /\s*\,\s*/, $payment_gateway->gateway_action );
560 my $transaction = new $namespace( $payment_gateway->gateway_module,
561 $self->_bop_options(\%options),
564 $transaction->content(
565 'type' => $options{method},
566 $self->_bop_auth(\%options),
567 'action' => $action1,
568 'description' => $options{'description'},
569 'amount' => $options{amount},
570 #'invoice_number' => $options{'invnum'},
571 'customer_id' => $self->custnum,
573 'reference' => $cust_pay_pending->paypendingnum, #for now
574 'callback_url' => $payment_gateway->gateway_callback_url,
579 $cust_pay_pending->status('pending');
580 my $cpp_pending_err = $cust_pay_pending->replace;
581 return $cpp_pending_err if $cpp_pending_err;
585 my $BOP_TESTING_SUCCESS = 1;
587 unless ( $BOP_TESTING ) {
588 $transaction->test_transaction(1)
589 if $conf->exists('business-onlinepayment-test_transaction');
590 $transaction->submit();
592 if ( $BOP_TESTING_SUCCESS ) {
593 $transaction->is_success(1);
594 $transaction->authorization('fake auth');
596 $transaction->is_success(0);
597 $transaction->error_message('fake failure');
601 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
603 return { reference => $cust_pay_pending->paypendingnum,
604 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
606 } elsif ( $transaction->is_success() && $action2 ) {
608 $cust_pay_pending->status('authorized');
609 my $cpp_authorized_err = $cust_pay_pending->replace;
610 return $cpp_authorized_err if $cpp_authorized_err;
612 my $auth = $transaction->authorization;
613 my $ordernum = $transaction->can('order_number')
614 ? $transaction->order_number
618 new Business::OnlinePayment( $payment_gateway->gateway_module,
619 $self->_bop_options(\%options),
624 type => $options{method},
626 $self->_bop_auth(\%options),
627 order_number => $ordernum,
628 amount => $options{amount},
629 authorization => $auth,
630 description => $options{'description'},
633 foreach my $field (qw( authorization_source_code returned_ACI
634 transaction_identifier validation_code
635 transaction_sequence_num local_transaction_date
636 local_transaction_time AVS_result_code )) {
637 $capture{$field} = $transaction->$field() if $transaction->can($field);
640 $capture->content( %capture );
642 $capture->test_transaction(1)
643 if $conf->exists('business-onlinepayment-test_transaction');
646 unless ( $capture->is_success ) {
647 my $e = "Authorization successful but capture failed, custnum #".
648 $self->custnum. ': '. $capture->result_code.
649 ": ". $capture->error_message;
657 # remove paycvv after initial transaction
660 #false laziness w/misc/process/payment.cgi - check both to make sure working
662 if ( length($self->paycvv)
663 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
665 my $error = $self->remove_cvv;
667 warn "WARNING: error removing cvv: $error\n";
676 if ( $transaction->can('card_token') && $transaction->card_token ) {
678 $self->card_token($transaction->card_token);
680 if ( $options{'payinfo'} eq $self->payinfo ) {
681 $self->payinfo($transaction->card_token);
682 my $error = $self->replace;
684 warn "WARNING: error storing token: $error, but proceeding anyway\n";
694 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
706 if (ref($_[0]) eq 'HASH') {
709 my ( $method, $amount ) = ( shift, shift );
711 $options{method} = $method;
712 $options{amount} = $amount;
715 if ( $options{'fake_failure'} ) {
716 return "Error: No error; test failure requested with fake_failure";
720 #if ( $payment_gateway->gatewaynum ) { # agent override
721 # $paybatch = $payment_gateway->gatewaynum. '-';
724 #$paybatch .= "$processor:". $transaction->authorization;
726 #$paybatch .= ':'. $transaction->order_number
727 # if $transaction->can('order_number')
728 # && length($transaction->order_number);
730 my $paybatch = 'FakeProcessor:54:32';
732 my $cust_pay = new FS::cust_pay ( {
733 'custnum' => $self->custnum,
734 'invnum' => $options{'invnum'},
735 'paid' => $options{amount},
737 'payby' => $bop_method2payby{$options{method}},
738 #'payinfo' => $payinfo,
739 'payinfo' => '4111111111111111',
740 'paybatch' => $paybatch,
741 #'paydate' => $paydate,
742 'paydate' => '2012-05-01',
744 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
747 warn "fake_bop\n cust_pay: ". Dumper($cust_pay) . "\n options: ";
748 warn " $_ => $options{$_}\n" foreach keys %options;
751 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
754 $cust_pay->invnum(''); #try again with no specific invnum
755 my $error2 = $cust_pay->insert( $options{'manual'} ?
756 ( 'manual' => 1 ) : ()
759 # gah, even with transactions.
760 my $e = 'WARNING: Card/ACH debited but database not updated - '.
761 "error inserting (fake!) payment: $error2".
762 " (previously tried insert with invnum #$options{'invnum'}" .
769 if ( $options{'paynum_ref'} ) {
770 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
778 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
780 # Wraps up processing of a realtime credit card, ACH (electronic check) or
781 # phone bill transaction.
783 sub _realtime_bop_result {
784 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
786 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
789 warn "$me _realtime_bop_result: pending transaction ".
790 $cust_pay_pending->paypendingnum. "\n";
791 warn " $_ => $options{$_}\n" foreach keys %options;
794 my $payment_gateway = $options{payment_gateway}
795 or return "no payment gateway in arguments to _realtime_bop_result";
797 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
798 my $cpp_captured_err = $cust_pay_pending->replace;
799 return $cpp_captured_err if $cpp_captured_err;
801 if ( $transaction->is_success() ) {
804 if ( $payment_gateway->gatewaynum ) { # agent override
805 $paybatch = $payment_gateway->gatewaynum. '-';
808 $paybatch .= $payment_gateway->gateway_module. ":".
809 $transaction->authorization;
811 $paybatch .= ':'. $transaction->order_number
812 if $transaction->can('order_number')
813 && length($transaction->order_number);
815 my $cust_pay = new FS::cust_pay ( {
816 'custnum' => $self->custnum,
817 'invnum' => $options{'invnum'},
818 'paid' => $cust_pay_pending->paid,
820 'payby' => $cust_pay_pending->payby,
821 'payinfo' => $options{'payinfo'},
822 'paybatch' => $paybatch,
823 'paydate' => $cust_pay_pending->paydate,
824 'pkgnum' => $cust_pay_pending->pkgnum,
825 'discount_term' => $options{'discount_term'},
827 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
828 $cust_pay->payunique( $options{payunique} )
829 if defined($options{payunique}) && length($options{payunique});
831 my $oldAutoCommit = $FS::UID::AutoCommit;
832 local $FS::UID::AutoCommit = 0;
835 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
837 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
840 $cust_pay->invnum(''); #try again with no specific invnum
841 my $error2 = $cust_pay->insert( $options{'manual'} ?
842 ( 'manual' => 1 ) : ()
845 # gah. but at least we have a record of the state we had to abort in
846 # from cust_pay_pending now.
847 my $e = "WARNING: $options{method} captured but payment not recorded -".
848 " error inserting payment (". $payment_gateway->gateway_module.
850 " (previously tried insert with invnum #$options{'invnum'}" .
851 ": $error ) - pending payment saved as paypendingnum ".
852 $cust_pay_pending->paypendingnum. "\n";
858 my $jobnum = $cust_pay_pending->jobnum;
860 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
862 unless ( $placeholder ) {
863 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
864 my $e = "WARNING: $options{method} captured but job $jobnum not ".
865 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
870 $error = $placeholder->delete;
873 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
874 my $e = "WARNING: $options{method} captured but could not delete ".
875 "job $jobnum for paypendingnum ".
876 $cust_pay_pending->paypendingnum. ": $error\n";
883 if ( $options{'paynum_ref'} ) {
884 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
887 $cust_pay_pending->status('done');
888 $cust_pay_pending->statustext('captured');
889 $cust_pay_pending->paynum($cust_pay->paynum);
890 my $cpp_done_err = $cust_pay_pending->replace;
892 if ( $cpp_done_err ) {
894 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
895 my $e = "WARNING: $options{method} captured but payment not recorded - ".
896 "error updating status for paypendingnum ".
897 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
903 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
905 if ( $options{'apply'} ) {
906 my $apply_error = $self->apply_payments_and_credits;
907 if ( $apply_error ) {
908 warn "WARNING: error applying payment: $apply_error\n";
909 #but we still should return no error cause the payment otherwise went
914 # have a CC surcharge portion --> one-time charge
915 if ( $options{'cc_surcharge'} > 0 ) {
916 # XXX: this whole block needs to be in a transaction?
919 $invnum = $options{'invnum'} if $options{'invnum'};
920 unless ( $invnum ) { # probably from a payment screen
921 # do we have any open invoices? pick earliest
922 # uses the fact that cust_main->cust_bill sorts by date ascending
923 my @open = $self->open_cust_bill;
924 $invnum = $open[0]->invnum if scalar(@open);
927 unless ( $invnum ) { # still nothing? pick last closed invoice
928 # again uses fact that cust_main->cust_bill sorts by date ascending
929 my @closed = $self->cust_bill;
930 $invnum = $closed[$#closed]->invnum if scalar(@closed);
934 # XXX: unlikely case - pre-paying before any invoices generated
935 # what it should do is create a new invoice and pick it
936 warn 'CC SURCHARGE AND NO INVOICES PICKED TO APPLY IT!';
941 my $charge_error = $self->charge({
942 'amount' => $options{'cc_surcharge'},
943 'pkg' => 'Credit Card Surcharge',
945 'cust_pkg_ref' => \$cust_pkg,
948 warn 'Unable to add CC surcharge cust_pkg';
952 $cust_pkg->setup(time);
953 my $cp_error = $cust_pkg->replace;
955 warn 'Unable to set setup time on cust_pkg for cc surcharge';
959 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
960 unless ( $cust_bill ) {
961 warn "race condition + invoice deletion just happened";
966 $cust_bill->add_cc_surcharge($cust_pkg->pkgnum,$options{'cc_surcharge'});
968 warn "cannot add CC surcharge to invoice #$invnum: $grand_error"
978 my $perror = $payment_gateway->gateway_module. " error: ".
979 $transaction->error_message;
981 my $jobnum = $cust_pay_pending->jobnum;
983 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
985 if ( $placeholder ) {
986 my $error = $placeholder->depended_delete;
987 $error ||= $placeholder->delete;
988 warn "error removing provisioning jobs after declined paypendingnum ".
989 $cust_pay_pending->paypendingnum. ": $error\n";
991 my $e = "error finding job $jobnum for declined paypendingnum ".
992 $cust_pay_pending->paypendingnum. "\n";
998 unless ( $transaction->error_message ) {
1001 if ( $transaction->can('response_page') ) {
1003 'page' => ( $transaction->can('response_page')
1004 ? $transaction->response_page
1007 'code' => ( $transaction->can('response_code')
1008 ? $transaction->response_code
1011 'headers' => ( $transaction->can('response_headers')
1012 ? $transaction->response_headers
1018 "No additional debugging information available for ".
1019 $payment_gateway->gateway_module;
1022 $perror .= "No error_message returned from ".
1023 $payment_gateway->gateway_module. " -- ".
1024 ( ref($t_response) ? Dumper($t_response) : $t_response );
1028 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
1029 && $conf->exists('emaildecline', $self->agentnum)
1030 && grep { $_ ne 'POST' } $self->invoicing_list
1031 && ! grep { $transaction->error_message =~ /$_/ }
1032 $conf->config('emaildecline-exclude', $self->agentnum)
1035 # Send a decline alert to the customer.
1036 my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
1039 # include the raw error message in the transaction state
1040 $cust_pay_pending->setfield('error', $transaction->error_message);
1041 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
1042 $error = $msg_template->send( 'cust_main' => $self,
1043 'object' => $cust_pay_pending );
1047 my @templ = $conf->config('declinetemplate');
1048 my $template = new Text::Template (
1050 SOURCE => [ map "$_\n", @templ ],
1051 ) or return "($perror) can't create template: $Text::Template::ERROR";
1052 $template->compile()
1053 or return "($perror) can't compile template: $Text::Template::ERROR";
1057 scalar( $conf->config('company_name', $self->agentnum ) ),
1058 'company_address' =>
1059 join("\n", $conf->config('company_address', $self->agentnum ) ),
1060 'error' => $transaction->error_message,
1063 my $error = send_email(
1064 'from' => $conf->config('invoice_from', $self->agentnum ),
1065 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
1066 'subject' => 'Your payment could not be processed',
1067 'body' => [ $template->fill_in(HASH => $templ_hash) ],
1071 $perror .= " (also received error sending decline notification: $error)"
1076 $cust_pay_pending->status('done');
1077 $cust_pay_pending->statustext("declined: $perror");
1078 my $cpp_done_err = $cust_pay_pending->replace;
1079 if ( $cpp_done_err ) {
1080 my $e = "WARNING: $options{method} declined but pending payment not ".
1081 "resolved - error updating status for paypendingnum ".
1082 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
1084 $perror = "$e ($perror)";
1092 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
1094 Verifies successful third party processing of a realtime credit card,
1095 ACH (electronic check) or phone bill transaction via a
1096 Business::OnlineThirdPartyPayment realtime gateway. See
1097 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
1099 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
1101 The additional options I<payname>, I<city>, I<state>,
1102 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1103 if set, will override the value from the customer record.
1105 I<description> is a free-text field passed to the gateway. It defaults to
1106 "Internet services".
1108 If an I<invnum> is specified, this payment (if successful) is applied to the
1109 specified invoice. If you don't specify an I<invnum> you might want to
1110 call the B<apply_payments> method.
1112 I<quiet> can be set true to surpress email decline notices.
1114 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
1115 resulting paynum, if any.
1117 I<payunique> is a unique identifier for this payment.
1119 Returns a hashref containing elements bill_error (which will be undefined
1120 upon success) and session_id of any associated session.
1124 sub realtime_botpp_capture {
1125 my( $self, $cust_pay_pending, %options ) = @_;
1127 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1130 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
1131 warn " $_ => $options{$_}\n" foreach keys %options;
1134 eval "use Business::OnlineThirdPartyPayment";
1138 # select the gateway
1141 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1143 my $payment_gateway;
1144 my $gatewaynum = $cust_pay_pending->getfield('gatewaynum');
1145 $payment_gateway = $gatewaynum ? qsearchs( 'payment_gateway',
1146 { gatewaynum => $gatewaynum }
1148 : $self->agent->payment_gateway( 'method' => $method,
1149 # 'invnum' => $cust_pay_pending->invnum,
1150 # 'payinfo' => $cust_pay_pending->payinfo,
1153 $options{payment_gateway} = $payment_gateway; # for the helper subs
1159 my @invoicing_list = $self->invoicing_list_emailonly;
1160 if ( $conf->exists('emailinvoiceautoalways')
1161 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1162 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1163 push @invoicing_list, $self->all_emails;
1166 my $email = ($conf->exists('business-onlinepayment-email-override'))
1167 ? $conf->config('business-onlinepayment-email-override')
1168 : $invoicing_list[0];
1172 $content{email_customer} =
1173 ( $conf->exists('business-onlinepayment-email_customer')
1174 || $conf->exists('business-onlinepayment-email-override') );
1177 # run transaction(s)
1181 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1182 $self->_bop_options(\%options),
1185 $transaction->reference({ %options });
1187 $transaction->content(
1189 $self->_bop_auth(\%options),
1190 'action' => 'Post Authorization',
1191 'description' => $options{'description'},
1192 'amount' => $cust_pay_pending->paid,
1193 #'invoice_number' => $options{'invnum'},
1194 'customer_id' => $self->custnum,
1195 'referer' => 'http://cleanwhisker.420.am/',
1196 'reference' => $cust_pay_pending->paypendingnum,
1198 'phone' => $self->daytime || $self->night,
1200 # plus whatever is required for bogus capture avoidance
1203 $transaction->submit();
1206 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1208 if ( $options{'apply'} ) {
1209 my $apply_error = $self->apply_payments_and_credits;
1210 if ( $apply_error ) {
1211 warn "WARNING: error applying payment: $apply_error\n";
1216 bill_error => $error,
1217 session_id => $cust_pay_pending->session_id,
1222 =item default_payment_gateway
1224 DEPRECATED -- use agent->payment_gateway
1228 sub default_payment_gateway {
1229 my( $self, $method ) = @_;
1231 die "Real-time processing not enabled\n"
1232 unless $conf->exists('business-onlinepayment');
1234 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1237 my $bop_config = 'business-onlinepayment';
1238 $bop_config .= '-ach'
1239 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1240 my ( $processor, $login, $password, $action, @bop_options ) =
1241 $conf->config($bop_config);
1242 $action ||= 'normal authorization';
1243 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1244 die "No real-time processor is enabled - ".
1245 "did you set the business-onlinepayment configuration value?\n"
1248 ( $processor, $login, $password, $action, @bop_options )
1251 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1253 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
1254 via a Business::OnlinePayment realtime gateway. See
1255 L<http://420.am/business-onlinepayment> for supported gateways.
1257 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1259 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
1261 Most gateways require a reference to an original payment transaction to refund,
1262 so you probably need to specify a I<paynum>.
1264 I<amount> defaults to the original amount of the payment if not specified.
1266 I<reason> specifies a reason for the refund.
1268 I<paydate> specifies the expiration date for a credit card overriding the
1269 value from the customer record or the payment record. Specified as yyyy-mm-dd
1271 Implementation note: If I<amount> is unspecified or equal to the amount of the
1272 orignal payment, first an attempt is made to "void" the transaction via
1273 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1274 the normal attempt is made to "refund" ("credit") the transaction via the
1275 gateway is attempted.
1277 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1278 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1279 #if set, will override the value from the customer record.
1281 #If an I<invnum> is specified, this payment (if successful) is applied to the
1282 #specified invoice. If you don't specify an I<invnum> you might want to
1283 #call the B<apply_payments> method.
1287 #some false laziness w/realtime_bop, not enough to make it worth merging
1288 #but some useful small subs should be pulled out
1289 sub realtime_refund_bop {
1292 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1295 if (ref($_[0]) eq 'HASH') {
1296 %options = %{$_[0]};
1300 $options{method} = $method;
1304 warn "$me realtime_refund_bop (new): $options{method} refund\n";
1305 warn " $_ => $options{$_}\n" foreach keys %options;
1309 # look up the original payment and optionally a gateway for that payment
1313 my $amount = $options{'amount'};
1315 my( $processor, $login, $password, @bop_options, $namespace ) ;
1316 my( $auth, $order_number ) = ( '', '', '' );
1318 if ( $options{'paynum'} ) {
1320 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
1321 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1322 or return "Unknown paynum $options{'paynum'}";
1323 $amount ||= $cust_pay->paid;
1325 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1326 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1327 $cust_pay->paybatch;
1328 my $gatewaynum = '';
1329 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1331 if ( $gatewaynum ) { #gateway for the payment to be refunded
1333 my $payment_gateway =
1334 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1335 die "payment gateway $gatewaynum not found"
1336 unless $payment_gateway;
1338 $processor = $payment_gateway->gateway_module;
1339 $login = $payment_gateway->gateway_username;
1340 $password = $payment_gateway->gateway_password;
1341 $namespace = $payment_gateway->gateway_namespace;
1342 @bop_options = $payment_gateway->options;
1344 } else { #try the default gateway
1347 my $payment_gateway =
1348 $self->agent->payment_gateway('method' => $options{method});
1350 ( $conf_processor, $login, $password, $namespace ) =
1351 map { my $method = "gateway_$_"; $payment_gateway->$method }
1352 qw( module username password namespace );
1354 @bop_options = $payment_gateway->gatewaynum
1355 ? $payment_gateway->options
1356 : @{ $payment_gateway->get('options') };
1358 return "processor of payment $options{'paynum'} $processor does not".
1359 " match default processor $conf_processor"
1360 unless $processor eq $conf_processor;
1365 } else { # didn't specify a paynum, so look for agent gateway overrides
1366 # like a normal transaction
1368 my $payment_gateway =
1369 $self->agent->payment_gateway( 'method' => $options{method},
1370 #'payinfo' => $payinfo,
1372 my( $processor, $login, $password, $namespace ) =
1373 map { my $method = "gateway_$_"; $payment_gateway->$method }
1374 qw( module username password namespace );
1376 my @bop_options = $payment_gateway->gatewaynum
1377 ? $payment_gateway->options
1378 : @{ $payment_gateway->get('options') };
1381 return "neither amount nor paynum specified" unless $amount;
1383 eval "use $namespace";
1387 'type' => $options{method},
1389 'password' => $password,
1390 'order_number' => $order_number,
1391 'amount' => $amount,
1392 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
1394 $content{authorization} = $auth
1395 if length($auth); #echeck/ACH transactions have an order # but no auth
1396 #(at least with authorize.net)
1398 my $disable_void_after;
1399 if ($conf->exists('disable_void_after')
1400 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1401 $disable_void_after = $1;
1404 #first try void if applicable
1405 if ( $cust_pay && $cust_pay->paid == $amount
1407 ( not defined($disable_void_after) )
1408 || ( time < ($cust_pay->_date + $disable_void_after ) )
1411 warn " attempting void\n" if $DEBUG > 1;
1412 my $void = new Business::OnlinePayment( $processor, @bop_options );
1413 if ( $void->can('info') ) {
1414 if ( $cust_pay->payby eq 'CARD'
1415 && $void->info('CC_void_requires_card') )
1417 $content{'card_number'} = $cust_pay->payinfo;
1418 } elsif ( $cust_pay->payby eq 'CHEK'
1419 && $void->info('ECHECK_void_requires_account') )
1421 ( $content{'account_number'}, $content{'routing_code'} ) =
1422 split('@', $cust_pay->payinfo);
1423 $content{'name'} = $self->get('first'). ' '. $self->get('last');
1426 $void->content( 'action' => 'void', %content );
1427 $void->test_transaction(1)
1428 if $conf->exists('business-onlinepayment-test_transaction');
1430 if ( $void->is_success ) {
1431 my $error = $cust_pay->void($options{'reason'});
1433 # gah, even with transactions.
1434 my $e = 'WARNING: Card/ACH voided but database not updated - '.
1435 "error voiding payment: $error";
1439 warn " void successful\n" if $DEBUG > 1;
1444 warn " void unsuccessful, trying refund\n"
1448 my $address = $self->address1;
1449 $address .= ", ". $self->address2 if $self->address2;
1451 my($payname, $payfirst, $paylast);
1452 if ( $self->payname && $options{method} ne 'ECHECK' ) {
1453 $payname = $self->payname;
1454 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1455 or return "Illegal payname $payname";
1456 ($payfirst, $paylast) = ($1, $2);
1458 $payfirst = $self->getfield('first');
1459 $paylast = $self->getfield('last');
1460 $payname = "$payfirst $paylast";
1463 my @invoicing_list = $self->invoicing_list_emailonly;
1464 if ( $conf->exists('emailinvoiceautoalways')
1465 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1466 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1467 push @invoicing_list, $self->all_emails;
1470 my $email = ($conf->exists('business-onlinepayment-email-override'))
1471 ? $conf->config('business-onlinepayment-email-override')
1472 : $invoicing_list[0];
1474 my $payip = exists($options{'payip'})
1477 $content{customer_ip} = $payip
1481 if ( $options{method} eq 'CC' ) {
1484 $content{card_number} = $payinfo = $cust_pay->payinfo;
1485 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1486 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1487 ($content{expiration} = "$2/$1"); # where available
1489 $content{card_number} = $payinfo = $self->payinfo;
1490 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1491 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1492 $content{expiration} = "$2/$1";
1495 } elsif ( $options{method} eq 'ECHECK' ) {
1498 $payinfo = $cust_pay->payinfo;
1500 $payinfo = $self->payinfo;
1502 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1503 $content{bank_name} = $self->payname;
1504 $content{account_type} = 'CHECKING';
1505 $content{account_name} = $payname;
1506 $content{customer_org} = $self->company ? 'B' : 'I';
1507 $content{customer_ssn} = $self->ss;
1508 } elsif ( $options{method} eq 'LEC' ) {
1509 $content{phone} = $payinfo = $self->payinfo;
1513 my $refund = new Business::OnlinePayment( $processor, @bop_options );
1514 my %sub_content = $refund->content(
1515 'action' => 'credit',
1516 'customer_id' => $self->custnum,
1517 'last_name' => $paylast,
1518 'first_name' => $payfirst,
1520 'address' => $address,
1521 'city' => $self->city,
1522 'state' => $self->state,
1523 'zip' => $self->zip,
1524 'country' => $self->country,
1526 'phone' => $self->daytime || $self->night,
1529 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
1531 $refund->test_transaction(1)
1532 if $conf->exists('business-onlinepayment-test_transaction');
1535 return "$processor error: ". $refund->error_message
1536 unless $refund->is_success();
1538 my $paybatch = "$processor:". $refund->authorization;
1539 $paybatch .= ':'. $refund->order_number
1540 if $refund->can('order_number') && $refund->order_number;
1542 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1543 my @cust_bill_pay = $cust_pay->cust_bill_pay;
1544 last unless @cust_bill_pay;
1545 my $cust_bill_pay = pop @cust_bill_pay;
1546 my $error = $cust_bill_pay->delete;
1550 my $cust_refund = new FS::cust_refund ( {
1551 'custnum' => $self->custnum,
1552 'paynum' => $options{'paynum'},
1553 'refund' => $amount,
1555 'payby' => $bop_method2payby{$options{method}},
1556 'payinfo' => $payinfo,
1557 'paybatch' => $paybatch,
1558 'reason' => $options{'reason'} || 'card or ACH refund',
1560 my $error = $cust_refund->insert;
1562 $cust_refund->paynum(''); #try again with no specific paynum
1563 my $error2 = $cust_refund->insert;
1565 # gah, even with transactions.
1566 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1567 "error inserting refund ($processor): $error2".
1568 " (previously tried insert with paynum #$options{'paynum'}" .
1587 L<FS::cust_main>, L<FS::cust_main::Billing>