1 package FS::cust_main::Billing_Realtime;
4 use vars qw( $conf $DEBUG $me );
5 use vars qw( $realtime_bop_decline_quiet ); #ugh
8 use Business::CreditCard 0.35;
9 use FS::UID qw( dbh myconnect );
10 use FS::Record qw( qsearch qsearchs );
13 use FS::cust_pay_pending;
14 use FS::cust_bill_pay;
18 $realtime_bop_decline_quiet = 0;
20 # 1 is mostly method/subroutine entry and options
21 # 2 traces progress of some operations
22 # 3 is even more information including possibly sensitive data
24 $me = '[FS::cust_main::Billing_Realtime]';
27 our $BOP_TESTING_SUCCESS = 1;
29 install_callback FS::UID sub {
31 #yes, need it for stuff below (prolly should be cached)
36 FS::cust_main::Billing_Realtime - Realtime billing mixin for cust_main
42 These methods are available on FS::cust_main objects.
48 =item realtime_cust_payby
52 sub realtime_cust_payby {
53 my( $self, %options ) = @_;
55 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
57 $options{amount} = $self->balance unless exists( $options{amount} );
59 my @cust_payby = $self->cust_payby('CARD','CHEK');
62 foreach my $cust_payby (@cust_payby) {
63 $error = $cust_payby->realtime_bop( %options, );
67 #XXX what about the earlier errors?
73 =item realtime_collect [ OPTION => VALUE ... ]
75 Attempt to collect the customer's current balance with a realtime credit
76 card or electronic check transaction (see realtime_bop() below).
78 Returns the result of realtime_bop(): nothing, an error message, or a
79 hashref of state information for a third-party transaction.
81 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
83 I<method> is one of: I<CC> or I<ECHECK>. If none is specified
84 then it is deduced from the customer record.
86 If no I<amount> is specified, then the customer balance is used.
88 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
89 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
90 if set, will override the value from the customer record.
92 I<description> is a free-text field passed to the gateway. It defaults to
93 the value defined by the business-onlinepayment-description configuration
94 option, or "Internet services" if that is unset.
96 If an I<invnum> is specified, this payment (if successful) is applied to the
99 I<apply> will automatically apply a resulting payment.
101 I<quiet> can be set true to suppress email decline notices.
103 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
104 resulting paynum, if any.
106 I<payunique> is a unique identifier for this payment.
108 I<session_id> is a session identifier associated with this payment.
110 I<depend_jobnum> allows payment capture to unlock export jobs
114 # Currently only used by ClientAPI
115 # NOT 4.x COMPATIBLE (see below)
116 sub realtime_collect {
117 my( $self, %options ) = @_;
119 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
122 warn "$me realtime_collect:\n";
123 warn " $_ => $options{$_}\n" foreach keys %options;
126 $options{amount} = $self->balance unless exists( $options{amount} );
127 return '' unless $options{amount} > 0;
129 #### NOT 4.x COMPATIBLE
130 $options{method} = FS::payby->payby2bop($self->payby)
131 unless exists( $options{method} );
133 return $self->realtime_bop({%options});
137 =item realtime_bop { [ ARG => VALUE ... ] }
139 Runs a realtime credit card or ACH (electronic check) transaction
140 via a Business::OnlinePayment realtime gateway. See
141 L<http://420.am/business-onlinepayment> for supported gateways.
143 Required arguments in the hashref are I<amount> and either
144 I<cust_payby> or I<method>, I<payinfo> and (as applicable for method)
145 I<payname>, I<address1>, I<address2>, I<city>, I<state>, I<zip> and I<paydate>.
147 Available methods are: I<CC>, I<ECHECK>, or I<PAYPAL>
149 Available optional arguments are: I<description>, I<invnum>, I<apply>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
151 I<description> is a free-text field passed to the gateway. It defaults to
152 the value defined by the business-onlinepayment-description configuration
153 option, or "Internet services" if that is unset.
155 If an I<invnum> is specified, this payment (if successful) is applied to the
156 specified invoice. If the customer has exactly one open invoice, that
157 invoice number will be assumed. If you don't specify an I<invnum> you might
158 want to call the B<apply_payments> method or set the I<apply> option.
160 I<no_invnum> can be set to true to prevent that default invnum from being set.
162 I<apply> can be set to true to run B<apply_payments_and_credits> on success.
164 I<no_auto_apply> can be set to true to set that flag on the resulting payment
165 (prevents payment from being applied by B<apply_payments> or B<apply_payments_and_credits>,
166 but will still be applied if I<invnum> exists...use with I<no_invnum> for intended effect.)
168 I<quiet> can be set true to surpress email decline notices.
170 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
171 resulting paynum, if any.
173 I<payunique> is a unique identifier for this payment.
175 I<session_id> is a session identifier associated with this payment.
177 I<depend_jobnum> allows payment capture to unlock export jobs
179 I<discount_term> attempts to take a discount by prepaying for discount_term.
180 The payment will fail if I<amount> is incorrect for this discount term.
182 A direct (Business::OnlinePayment) transaction will return nothing on success,
183 or an error message on failure.
185 A third-party transaction will return a hashref containing:
187 - popup_url: the URL to which a browser should be redirected to complete
189 - collectitems: an arrayref of name-value pairs to be posted to popup_url.
190 - reference: a reference ID for the transaction, to show the customer.
192 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
196 # some helper routines
198 # _bop_recurring_billing: Checks whether this payment should have the
199 # recurring_billing flag used by some B:OP interfaces (IPPay, PlugnPay,
200 # vSecure, etc.). This works in two different modes:
201 # - actual_oncard (default): treat the payment as recurring if the customer
202 # has made a payment using this card before.
203 # - transaction_is_recur: treat the payment as recurring if the invoice
204 # being paid has any recurring package charges.
206 sub _bop_recurring_billing {
207 my( $self, %opt ) = @_;
209 my $method = scalar($conf->config('credit_card-recurring_billing_flag'));
211 if ( defined($method) && $method eq 'transaction_is_recur' ) {
213 return 1 if $opt{'trans_is_recur'};
217 # return 1 if the payinfo has been used for another payment
218 return $self->payinfo_used($opt{'payinfo'}); # in payinfo_Mixin
226 sub _payment_gateway {
227 my ($self, $options) = @_;
229 if ( $options->{'selfservice'} ) {
230 my $gatewaynum = FS::Conf->new->config('selfservice-payment_gateway');
232 return $options->{payment_gateway} ||=
233 qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
237 if ( $options->{'fake_gatewaynum'} ) {
238 $options->{payment_gateway} =
239 qsearchs('payment_gateway',
240 { 'gatewaynum' => $options->{'fake_gatewaynum'}, }
244 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
245 unless exists($options->{payment_gateway});
247 $options->{payment_gateway};
251 my ($self, $options) = @_;
254 'login' => $options->{payment_gateway}->gateway_username,
255 'password' => $options->{payment_gateway}->gateway_password,
260 my ($self, $options) = @_;
262 $options->{payment_gateway}->gatewaynum
263 ? $options->{payment_gateway}->options
264 : @{ $options->{payment_gateway}->get('options') };
269 my ($self, $options) = @_;
271 unless ( $options->{'description'} ) {
272 if ( $conf->exists('business-onlinepayment-description') ) {
273 my $dtempl = $conf->config('business-onlinepayment-description');
275 my $agent = $self->agent->agent;
277 $options->{'description'} = eval qq("$dtempl");
279 $options->{'description'} = 'Internet services';
283 # Default invoice number if the customer has exactly one open invoice.
284 unless ( $options->{'invnum'} || $options->{'no_invnum'} ) {
285 $options->{'invnum'} = '';
286 my @open = $self->open_cust_bill;
287 $options->{'invnum'} = $open[0]->invnum if scalar(@open) == 1;
292 sub _bop_cust_payby_options {
293 my ($self,$options) = @_;
294 my $cust_payby = $options->{'cust_payby'};
297 $options->{'method'} = FS::payby->payby2bop( $cust_payby->payby );
299 if ($cust_payby->payby =~ /^(CARD|DCRD)$/) {
300 # false laziness with cust_payby->check
301 # which might not have been run yet
303 if ( $cust_payby->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
304 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
305 } elsif ( $cust_payby->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
306 ( $m, $y ) = ( $2, "19$1" );
307 } elsif ( $cust_payby->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
308 ( $m, $y ) = ( $3, "20$2" );
310 return "Illegal expiration date: ". $cust_payby->paydate;
312 $m = sprintf('%02d',$m);
313 $options->{paydate} = "$y-$m-01";
315 $options->{paydate} = '';
318 $options->{$_} = $cust_payby->$_()
319 for qw( payinfo paycvv paymask paystart_month paystart_year
320 payissue payname paystate paytype payip );
322 if ( $cust_payby->locationnum ) {
323 my $cust_location = $cust_payby->cust_location;
324 $options->{$_} = $cust_location->$_() for qw( address1 address2 city state zip );
330 my ($self, $options) = @_;
333 my $payip = $options->{'payip'};
334 $content{customer_ip} = $payip if length($payip);
336 $content{invoice_number} = $options->{'invnum'}
337 if exists($options->{'invnum'}) && length($options->{'invnum'});
339 $content{email_customer} =
340 ( $conf->exists('business-onlinepayment-email_customer')
341 || $conf->exists('business-onlinepayment-email-override') );
343 my ($payname, $payfirst, $paylast);
344 if ( $options->{payname} && $options->{method} ne 'ECHECK' ) {
345 ($payname = $options->{payname}) =~
346 /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
347 or return "Illegal payname $payname";
348 ($payfirst, $paylast) = ($1, $2);
350 $payfirst = $self->getfield('first');
351 $paylast = $self->getfield('last');
352 $payname = "$payfirst $paylast";
355 $content{last_name} = $paylast;
356 $content{first_name} = $payfirst;
358 $content{name} = $payname;
360 $content{address} = $options->{'address1'};
361 my $address2 = $options->{'address2'};
362 $content{address} .= ", ". $address2 if length($address2);
364 $content{city} = $options->{'city'};
365 $content{state} = $options->{'state'};
366 $content{zip} = $options->{'zip'};
367 $content{country} = $options->{'country'};
369 $content{phone} = $self->daytime || $self->night;
371 my $currency = $conf->exists('business-onlinepayment-currency')
372 && $conf->config('business-onlinepayment-currency');
373 $content{currency} = $currency if $currency;
379 my ($self,$transaction,$cust_payby,$log,%opt) = @_;
382 and $transaction->can('card_token')
383 and $transaction->card_token
384 and $cust_payby->payinfo !~ /^99\d{14}$/ #not already tokenized
387 $cust_payby->payinfo($transaction->card_token);
390 $error = $cust_payby->replace if $opt{'replace'};
392 $log->error('Error storing token for cust '.$self->custnum.', cust_payby '.$cust_payby->custpaybynum.': '.$error);
395 $log->debug('Tokenized card for cust '.$self->custnum.', cust_payby '.$cust_payby->custpaybynum);
403 my %bop_method2payby = (
412 confess "Can't call realtime_bop within another transaction ".
413 '($FS::UID::AutoCommit is false)'
414 unless $FS::UID::AutoCommit;
416 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
418 my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_bop');
421 if (ref($_[0]) eq 'HASH') {
424 my ( $method, $amount ) = ( shift, shift );
426 $options{method} = $method;
427 $options{amount} = $amount;
430 # set fields from passed cust_payby
431 $self->_bop_cust_payby_options(\%options);
434 # optional credit card surcharge
437 my $cc_surcharge = 0;
438 my $cc_surcharge_pct = 0;
439 $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage', $self->agentnum)
440 if $conf->config('credit-card-surcharge-percentage', $self->agentnum)
441 && $options{method} eq 'CC';
443 # always add cc surcharge if called from event
444 if($options{'cc_surcharge_from_event'} && $cc_surcharge_pct > 0) {
445 $cc_surcharge = $options{'amount'} * $cc_surcharge_pct / 100;
446 $options{'amount'} += $cc_surcharge;
447 $options{'amount'} = sprintf("%.2f", $options{'amount'}); # round (again)?
449 elsif($cc_surcharge_pct > 0) { # we're called not from event (i.e. from a
450 # payment screen), so consider the given
451 # amount as post-surcharge
452 $cc_surcharge = $options{'amount'} - ($options{'amount'} / ( 1 + $cc_surcharge_pct/100 ));
455 $cc_surcharge = sprintf("%.2f",$cc_surcharge) if $cc_surcharge > 0;
456 $options{'cc_surcharge'} = $cc_surcharge;
460 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
461 warn " cc_surcharge = $cc_surcharge\n";
464 warn " $_ => $options{$_}\n" foreach keys %options;
467 return $self->fake_bop(\%options) if $options{'fake'};
469 $self->_bop_defaults(\%options);
471 return "Missing payinfo"
472 unless $options{'payinfo'};
475 # set trans_is_recur based on invnum if there is one
478 my $trans_is_recur = 0;
479 if ( $options{'invnum'} ) {
481 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
482 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
488 $cust_bill->cust_bill_pkg;
491 if grep { $_->freq ne '0' } @part_pkg;
499 my $payment_gateway = $self->_payment_gateway( \%options );
500 my $namespace = $payment_gateway->gateway_namespace;
502 eval "use $namespace";
506 # check for banned credit card/ACH
509 my $ban = FS::banned_pay->ban_search(
510 'payby' => $bop_method2payby{$options{method}},
511 'payinfo' => $options{payinfo},
513 return "Banned credit card" if $ban && $ban->bantype ne 'warn';
516 # check for term discount validity
519 my $discount_term = $options{discount_term};
520 if ( $discount_term ) {
521 my $bill = ($self->cust_bill)[-1]
522 or return "Can't apply a term discount to an unbilled customer";
523 my $plan = FS::discount_plan->new(
525 months => $discount_term
526 ) or return "No discount available for term '$discount_term'";
528 if ( $plan->discounted_total != $options{amount} ) {
529 return "Incorrect term prepayment amount (term $discount_term, amount $options{amount}, requires ".$plan->discounted_total.")";
537 my $bop_content = $self->_bop_content(\%options);
538 return $bop_content unless ref($bop_content);
540 my @invoicing_list = $self->invoicing_list_emailonly;
541 if ( $conf->exists('emailinvoiceautoalways')
542 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
543 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
544 push @invoicing_list, $self->all_emails;
547 my $email = ($conf->exists('business-onlinepayment-email-override'))
548 ? $conf->config('business-onlinepayment-email-override')
549 : $invoicing_list[0];
554 if ( $namespace eq 'Business::OnlinePayment' ) {
556 if ( $options{method} eq 'CC' ) {
558 $content{card_number} = $options{payinfo};
559 $paydate = $options{'paydate'};
560 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
561 $content{expiration} = "$2/$1";
563 $content{cvv2} = $options{'paycvv'}
564 if length($options{'paycvv'});
566 my $paystart_month = $options{'paystart_month'};
567 my $paystart_year = $options{'paystart_year'};
568 $content{card_start} = "$paystart_month/$paystart_year"
569 if $paystart_month && $paystart_year;
571 my $payissue = $options{'payissue'};
572 $content{issue_number} = $payissue if $payissue;
574 if ( $self->_bop_recurring_billing(
575 'payinfo' => $options{'payinfo'},
576 'trans_is_recur' => $trans_is_recur,
580 $content{recurring_billing} = 'YES';
581 $content{acct_code} = 'rebill'
582 if $conf->exists('credit_card-recurring_billing_acct_code');
585 } elsif ( $options{method} eq 'ECHECK' ){
587 ( $content{account_number}, $content{routing_code} ) =
588 split('@', $options{payinfo});
589 $content{bank_name} = $options{payname};
590 $content{bank_state} = $options{'paystate'};
591 $content{account_type}= uc($options{'paytype'}) || 'PERSONAL CHECKING';
593 $content{company} = $self->company if $self->company;
595 if ( $content{account_type} =~ /BUSINESS/i && $self->company ) {
596 $content{account_name} = $self->company;
598 $content{account_name} = $self->getfield('first'). ' '.
599 $self->getfield('last');
602 $content{customer_org} = $self->company ? 'B' : 'I';
603 $content{state_id} = exists($options{'stateid'})
604 ? $options{'stateid'}
605 : $self->getfield('stateid');
606 $content{state_id_state} = exists($options{'stateid_state'})
607 ? $options{'stateid_state'}
608 : $self->getfield('stateid_state');
609 $content{customer_ssn} = exists($options{'ss'})
614 die "unknown method ". $options{method};
617 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
620 die "unknown namespace $namespace";
627 my $balance = exists( $options{'balance'} )
628 ? $options{'balance'}
631 warn "claiming mutex on customer ". $self->custnum. "\n" if $DEBUG > 1;
632 $self->select_for_update; #mutex ... just until we get our pending record in
633 warn "obtained mutex on customer ". $self->custnum. "\n" if $DEBUG > 1;
635 #the checks here are intended to catch concurrent payments
636 #double-form-submission prevention is taken care of in cust_pay_pending::check
639 return "The customer's balance has changed; $options{method} transaction aborted."
640 if $self->balance < $balance;
642 #also check and make sure there aren't *other* pending payments for this cust
644 my @pending = qsearch('cust_pay_pending', {
645 'custnum' => $self->custnum,
646 'status' => { op=>'!=', value=>'done' }
649 #for third-party payments only, remove pending payments if they're in the
650 #'thirdparty' (waiting for customer action) state.
651 if ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
652 foreach ( grep { $_->status eq 'thirdparty' } @pending ) {
653 my $error = $_->delete;
654 warn "error deleting unfinished third-party payment ".
655 $_->paypendingnum . ": $error\n"
658 @pending = grep { $_->status ne 'thirdparty' } @pending;
661 return "A payment is already being processed for this customer (".
662 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
663 "); $options{method} transaction aborted."
666 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
668 my $cust_pay_pending = new FS::cust_pay_pending {
669 'custnum' => $self->custnum,
670 'paid' => $options{amount},
672 'payby' => $bop_method2payby{$options{method}},
673 'payinfo' => $options{payinfo},
674 'paymask' => $options{paymask},
675 'paydate' => $paydate,
676 'recurring_billing' => $content{recurring_billing},
677 'pkgnum' => $options{'pkgnum'},
679 'gatewaynum' => $payment_gateway->gatewaynum || '',
680 'session_id' => $options{session_id} || '',
681 'jobnum' => $options{depend_jobnum} || '',
683 $cust_pay_pending->payunique( $options{payunique} )
684 if defined($options{payunique}) && length($options{payunique});
686 warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n"
688 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
689 return $cpp_new_err if $cpp_new_err;
691 warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n"
693 warn Dumper($cust_pay_pending) if $DEBUG > 2;
695 my( $action1, $action2 ) =
696 split( /\s*\,\s*/, $payment_gateway->gateway_action );
698 my $transaction = new $namespace( $payment_gateway->gateway_module,
699 $self->_bop_options(\%options),
702 $transaction->content(
703 'type' => $options{method},
704 $self->_bop_auth(\%options),
705 'action' => $action1,
706 'description' => $options{'description'},
707 'amount' => $options{amount},
708 #'invoice_number' => $options{'invnum'},
709 'customer_id' => $self->custnum,
711 'reference' => $cust_pay_pending->paypendingnum, #for now
712 'callback_url' => $payment_gateway->gateway_callback_url,
713 'cancel_url' => $payment_gateway->gateway_cancel_url,
718 $cust_pay_pending->status('pending');
719 my $cpp_pending_err = $cust_pay_pending->replace;
720 return $cpp_pending_err if $cpp_pending_err;
722 warn Dumper($transaction) if $DEBUG > 2;
724 unless ( $BOP_TESTING ) {
725 $transaction->test_transaction(1)
726 if $conf->exists('business-onlinepayment-test_transaction');
727 $transaction->submit();
729 if ( $BOP_TESTING_SUCCESS ) {
730 $transaction->is_success(1);
731 $transaction->authorization('fake auth');
733 $transaction->is_success(0);
734 $transaction->error_message('fake failure');
738 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
740 $cust_pay_pending->status('thirdparty');
741 my $cpp_err = $cust_pay_pending->replace;
742 return { error => $cpp_err } if $cpp_err;
743 return { reference => $cust_pay_pending->paypendingnum,
744 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
746 } elsif ( $transaction->is_success() && $action2 ) {
748 $cust_pay_pending->status('authorized');
749 my $cpp_authorized_err = $cust_pay_pending->replace;
750 return $cpp_authorized_err if $cpp_authorized_err;
752 my $auth = $transaction->authorization;
753 my $ordernum = $transaction->can('order_number')
754 ? $transaction->order_number
758 new Business::OnlinePayment( $payment_gateway->gateway_module,
759 $self->_bop_options(\%options),
764 type => $options{method},
766 $self->_bop_auth(\%options),
767 order_number => $ordernum,
768 amount => $options{amount},
769 authorization => $auth,
770 description => $options{'description'},
773 foreach my $field (qw( authorization_source_code returned_ACI
774 transaction_identifier validation_code
775 transaction_sequence_num local_transaction_date
776 local_transaction_time AVS_result_code )) {
777 $capture{$field} = $transaction->$field() if $transaction->can($field);
780 $capture->content( %capture );
782 $capture->test_transaction(1)
783 if $conf->exists('business-onlinepayment-test_transaction');
786 unless ( $capture->is_success ) {
787 my $e = "Authorization successful but capture failed, custnum #".
788 $self->custnum. ': '. $capture->result_code.
789 ": ". $capture->error_message;
797 # remove paycvv after initial transaction
800 # compare to FS::cust_main::save_cust_payby - check both to make sure working correctly
801 if ( length($options{'paycvv'})
802 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
804 my $error = $self->remove_cvv_from_cust_payby($options{payinfo});
806 warn "WARNING: error removing cvv: $error\n";
814 my $error = $self->_tokenize_card($transaction,$options{'cust_payby'},$log,'replace' => 1);
815 return $error if $error;
821 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
833 if (ref($_[0]) eq 'HASH') {
836 my ( $method, $amount ) = ( shift, shift );
838 $options{method} = $method;
839 $options{amount} = $amount;
842 if ( $options{'fake_failure'} ) {
843 return "Error: No error; test failure requested with fake_failure";
846 my $cust_pay = new FS::cust_pay ( {
847 'custnum' => $self->custnum,
848 'invnum' => $options{'invnum'},
849 'paid' => $options{amount},
851 'payby' => $bop_method2payby{$options{method}},
852 #'payinfo' => $payinfo,
853 'payinfo' => '4111111111111111',
854 #'paydate' => $paydate,
855 'paydate' => '2012-05-01',
856 'processor' => 'FakeProcessor',
858 'order_number' => '32',
860 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
863 warn "fake_bop\n cust_pay: ". Dumper($cust_pay) . "\n options: ";
864 warn " $_ => $options{$_}\n" foreach keys %options;
867 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
870 $cust_pay->invnum(''); #try again with no specific invnum
871 my $error2 = $cust_pay->insert( $options{'manual'} ?
872 ( 'manual' => 1 ) : ()
875 # gah, even with transactions.
876 my $e = 'WARNING: Card/ACH debited but database not updated - '.
877 "error inserting (fake!) payment: $error2".
878 " (previously tried insert with invnum #$options{'invnum'}" .
885 if ( $options{'paynum_ref'} ) {
886 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
894 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
896 # Wraps up processing of a realtime credit card or ACH (electronic check)
899 sub _realtime_bop_result {
900 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
902 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
905 warn "$me _realtime_bop_result: pending transaction ".
906 $cust_pay_pending->paypendingnum. "\n";
907 warn " $_ => $options{$_}\n" foreach keys %options;
910 my $payment_gateway = $options{payment_gateway}
911 or return "no payment gateway in arguments to _realtime_bop_result";
913 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
914 my $cpp_captured_err = $cust_pay_pending->replace;
915 return $cpp_captured_err if $cpp_captured_err;
917 if ( $transaction->is_success() ) {
919 my $order_number = $transaction->order_number
920 if $transaction->can('order_number');
922 my $cust_pay = new FS::cust_pay ( {
923 'custnum' => $self->custnum,
924 'invnum' => $options{'invnum'},
925 'paid' => $cust_pay_pending->paid,
927 'payby' => $cust_pay_pending->payby,
928 'payinfo' => $options{'payinfo'},
929 'paymask' => $options{'paymask'} || $cust_pay_pending->paymask,
930 'paydate' => $cust_pay_pending->paydate,
931 'pkgnum' => $cust_pay_pending->pkgnum,
932 'discount_term' => $options{'discount_term'},
933 'gatewaynum' => ($payment_gateway->gatewaynum || ''),
934 'processor' => $payment_gateway->gateway_module,
935 'auth' => $transaction->authorization,
936 'order_number' => $order_number || '',
937 'no_auto_apply' => $options{'no_auto_apply'} ? 'Y' : '',
939 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
940 $cust_pay->payunique( $options{payunique} )
941 if defined($options{payunique}) && length($options{payunique});
943 my $oldAutoCommit = $FS::UID::AutoCommit;
944 local $FS::UID::AutoCommit = 0;
947 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
949 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
952 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
953 $cust_pay->invnum(''); #try again with no specific invnum
954 $cust_pay->paynum('');
955 my $error2 = $cust_pay->insert( $options{'manual'} ?
956 ( 'manual' => 1 ) : ()
959 # gah. but at least we have a record of the state we had to abort in
960 # from cust_pay_pending now.
961 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
962 my $e = "WARNING: $options{method} captured but payment not recorded -".
963 " error inserting payment (". $payment_gateway->gateway_module.
965 " (previously tried insert with invnum #$options{'invnum'}" .
966 ": $error ) - pending payment saved as paypendingnum ".
967 $cust_pay_pending->paypendingnum. "\n";
973 my $jobnum = $cust_pay_pending->jobnum;
975 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
977 unless ( $placeholder ) {
978 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
979 my $e = "WARNING: $options{method} captured but job $jobnum not ".
980 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
985 $error = $placeholder->delete;
988 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
989 my $e = "WARNING: $options{method} captured but could not delete ".
990 "job $jobnum for paypendingnum ".
991 $cust_pay_pending->paypendingnum. ": $error\n";
996 $cust_pay_pending->set('jobnum','');
1000 if ( $options{'paynum_ref'} ) {
1001 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
1004 $cust_pay_pending->status('done');
1005 $cust_pay_pending->statustext('captured');
1006 $cust_pay_pending->paynum($cust_pay->paynum);
1007 my $cpp_done_err = $cust_pay_pending->replace;
1009 if ( $cpp_done_err ) {
1011 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
1012 my $e = "WARNING: $options{method} captured but payment not recorded - ".
1013 "error updating status for paypendingnum ".
1014 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
1020 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1022 if ( $options{'apply'} ) {
1023 my $apply_error = $self->apply_payments_and_credits;
1024 if ( $apply_error ) {
1025 warn "WARNING: error applying payment: $apply_error\n";
1026 #but we still should return no error cause the payment otherwise went
1031 # have a CC surcharge portion --> one-time charge
1032 if ( $options{'cc_surcharge'} > 0 ) {
1033 # XXX: this whole block needs to be in a transaction?
1036 $invnum = $options{'invnum'} if $options{'invnum'};
1037 unless ( $invnum ) { # probably from a payment screen
1038 # do we have any open invoices? pick earliest
1039 # uses the fact that cust_main->cust_bill sorts by date ascending
1040 my @open = $self->open_cust_bill;
1041 $invnum = $open[0]->invnum if scalar(@open);
1044 unless ( $invnum ) { # still nothing? pick last closed invoice
1045 # again uses fact that cust_main->cust_bill sorts by date ascending
1046 my @closed = $self->cust_bill;
1047 $invnum = $closed[$#closed]->invnum if scalar(@closed);
1050 unless ( $invnum ) {
1051 # XXX: unlikely case - pre-paying before any invoices generated
1052 # what it should do is create a new invoice and pick it
1053 warn 'CC SURCHARGE AND NO INVOICES PICKED TO APPLY IT!';
1058 my $charge_error = $self->charge({
1059 'amount' => $options{'cc_surcharge'},
1060 'pkg' => 'Credit Card Surcharge',
1062 'cust_pkg_ref' => \$cust_pkg,
1065 warn 'Unable to add CC surcharge cust_pkg';
1069 $cust_pkg->setup(time);
1070 my $cp_error = $cust_pkg->replace;
1072 warn 'Unable to set setup time on cust_pkg for cc surcharge';
1076 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
1077 unless ( $cust_bill ) {
1078 warn "race condition + invoice deletion just happened";
1083 $cust_bill->add_cc_surcharge($cust_pkg->pkgnum,$options{'cc_surcharge'});
1085 warn "cannot add CC surcharge to invoice #$invnum: $grand_error"
1089 return ''; #no error
1095 my $perror = $transaction->error_message;
1096 #$payment_gateway->gateway_module. " error: ".
1097 # removed for conciseness
1099 my $jobnum = $cust_pay_pending->jobnum;
1101 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
1103 if ( $placeholder ) {
1104 my $error = $placeholder->depended_delete;
1105 $error ||= $placeholder->delete;
1106 $cust_pay_pending->set('jobnum','');
1107 warn "error removing provisioning jobs after declined paypendingnum ".
1108 $cust_pay_pending->paypendingnum. ": $error\n" if $error;
1110 my $e = "error finding job $jobnum for declined paypendingnum ".
1111 $cust_pay_pending->paypendingnum. "\n";
1117 unless ( $transaction->error_message ) {
1120 if ( $transaction->can('response_page') ) {
1122 'page' => ( $transaction->can('response_page')
1123 ? $transaction->response_page
1126 'code' => ( $transaction->can('response_code')
1127 ? $transaction->response_code
1130 'headers' => ( $transaction->can('response_headers')
1131 ? $transaction->response_headers
1137 "No additional debugging information available for ".
1138 $payment_gateway->gateway_module;
1141 $perror .= "No error_message returned from ".
1142 $payment_gateway->gateway_module. " -- ".
1143 ( ref($t_response) ? Dumper($t_response) : $t_response );
1147 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
1148 && $conf->exists('emaildecline', $self->agentnum)
1149 && grep { $_ ne 'POST' } $self->invoicing_list
1150 && ! grep { $transaction->error_message =~ /$_/ }
1151 $conf->config('emaildecline-exclude', $self->agentnum)
1154 # Send a decline alert to the customer.
1155 my $msgnum = $conf->config('decline_msgnum', $self->agentnum);
1158 # include the raw error message in the transaction state
1159 $cust_pay_pending->setfield('error', $transaction->error_message);
1160 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
1161 $error = $msg_template->send( 'cust_main' => $self,
1162 'object' => $cust_pay_pending );
1166 $perror .= " (also received error sending decline notification: $error)"
1171 $cust_pay_pending->status('done');
1172 $cust_pay_pending->statustext($perror);
1173 #'declined:': no, that's failure_status
1174 if ( $transaction->can('failure_status') ) {
1175 $cust_pay_pending->failure_status( $transaction->failure_status );
1177 my $cpp_done_err = $cust_pay_pending->replace;
1178 if ( $cpp_done_err ) {
1179 my $e = "WARNING: $options{method} declined but pending payment not ".
1180 "resolved - error updating status for paypendingnum ".
1181 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
1183 $perror = "$e ($perror)";
1191 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
1193 Verifies successful third party processing of a realtime credit card or
1194 ACH (electronic check) transaction via a
1195 Business::OnlineThirdPartyPayment realtime gateway. See
1196 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
1198 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
1200 The additional options I<payname>, I<city>, I<state>,
1201 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1202 if set, will override the value from the customer record.
1204 I<description> is a free-text field passed to the gateway. It defaults to
1205 "Internet services".
1207 If an I<invnum> is specified, this payment (if successful) is applied to the
1208 specified invoice. If you don't specify an I<invnum> you might want to
1209 call the B<apply_payments> method.
1211 I<quiet> can be set true to surpress email decline notices.
1213 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
1214 resulting paynum, if any.
1216 I<payunique> is a unique identifier for this payment.
1218 Returns a hashref containing elements bill_error (which will be undefined
1219 upon success) and session_id of any associated session.
1223 sub realtime_botpp_capture {
1224 my( $self, $cust_pay_pending, %options ) = @_;
1226 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1229 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
1230 warn " $_ => $options{$_}\n" foreach keys %options;
1233 eval "use Business::OnlineThirdPartyPayment";
1237 # select the gateway
1240 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
1242 my $payment_gateway;
1243 my $gatewaynum = $cust_pay_pending->getfield('gatewaynum');
1244 $payment_gateway = $gatewaynum ? qsearchs( 'payment_gateway',
1245 { gatewaynum => $gatewaynum }
1247 : $self->agent->payment_gateway( 'method' => $method,
1248 # 'invnum' => $cust_pay_pending->invnum,
1249 # 'payinfo' => $cust_pay_pending->payinfo,
1252 $options{payment_gateway} = $payment_gateway; # for the helper subs
1258 my @invoicing_list = $self->invoicing_list_emailonly;
1259 if ( $conf->exists('emailinvoiceautoalways')
1260 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1261 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1262 push @invoicing_list, $self->all_emails;
1265 my $email = ($conf->exists('business-onlinepayment-email-override'))
1266 ? $conf->config('business-onlinepayment-email-override')
1267 : $invoicing_list[0];
1271 $content{email_customer} =
1272 ( $conf->exists('business-onlinepayment-email_customer')
1273 || $conf->exists('business-onlinepayment-email-override') );
1276 # run transaction(s)
1280 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
1281 $self->_bop_options(\%options),
1284 $transaction->reference({ %options });
1286 $transaction->content(
1288 $self->_bop_auth(\%options),
1289 'action' => 'Post Authorization',
1290 'description' => $options{'description'},
1291 'amount' => $cust_pay_pending->paid,
1292 #'invoice_number' => $options{'invnum'},
1293 'customer_id' => $self->custnum,
1294 'reference' => $cust_pay_pending->paypendingnum,
1296 'phone' => $self->daytime || $self->night,
1298 # plus whatever is required for bogus capture avoidance
1301 $transaction->submit();
1304 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
1306 if ( $options{'apply'} ) {
1307 my $apply_error = $self->apply_payments_and_credits;
1308 if ( $apply_error ) {
1309 warn "WARNING: error applying payment: $apply_error\n";
1314 bill_error => $error,
1315 session_id => $cust_pay_pending->session_id,
1320 =item default_payment_gateway
1322 DEPRECATED -- use agent->payment_gateway
1326 sub default_payment_gateway {
1327 my( $self, $method ) = @_;
1329 die "Real-time processing not enabled\n"
1330 unless $conf->exists('business-onlinepayment');
1332 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
1335 my $bop_config = 'business-onlinepayment';
1336 $bop_config .= '-ach'
1337 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
1338 my ( $processor, $login, $password, $action, @bop_options ) =
1339 $conf->config($bop_config);
1340 $action ||= 'normal authorization';
1341 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1342 die "No real-time processor is enabled - ".
1343 "did you set the business-onlinepayment configuration value?\n"
1346 ( $processor, $login, $password, $action, @bop_options )
1349 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
1351 Refunds a realtime credit card or ACH (electronic check) transaction
1352 via a Business::OnlinePayment realtime gateway. See
1353 L<http://420.am/business-onlinepayment> for supported gateways.
1355 Available methods are: I<CC> or I<ECHECK>
1357 Available options are: I<amount>, I<reasonnum>, I<paynum>, I<paydate>
1359 Most gateways require a reference to an original payment transaction to refund,
1360 so you probably need to specify a I<paynum>.
1362 I<amount> defaults to the original amount of the payment if not specified.
1364 I<reasonnum> specified an existing refund reason for the refund
1366 I<paydate> specifies the expiration date for a credit card overriding the
1367 value from the customer record or the payment record. Specified as yyyy-mm-dd
1369 Implementation note: If I<amount> is unspecified or equal to the amount of the
1370 orignal payment, first an attempt is made to "void" the transaction via
1371 the gateway (to cancel a not-yet settled transaction) and then if that fails,
1372 the normal attempt is made to "refund" ("credit") the transaction via the
1373 gateway is attempted. No attempt to "void" the transaction is made if the
1374 gateway has introspection data and doesn't support void.
1376 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1377 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1378 #if set, will override the value from the customer record.
1380 #If an I<invnum> is specified, this payment (if successful) is applied to the
1381 #specified invoice. If you don't specify an I<invnum> you might want to
1382 #call the B<apply_payments> method.
1386 #some false laziness w/realtime_bop, not enough to make it worth merging
1387 #but some useful small subs should be pulled out
1388 sub realtime_refund_bop {
1391 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1394 if (ref($_[0]) eq 'HASH') {
1395 %options = %{$_[0]};
1399 $options{method} = $method;
1403 warn "$me realtime_refund_bop (new): $options{method} refund\n";
1404 warn " $_ => $options{$_}\n" foreach keys %options;
1407 return "No reason specified" unless $options{'reasonnum'} =~ /^\d+$/;
1412 # look up the original payment and optionally a gateway for that payment
1416 my $amount = $options{'amount'};
1418 my( $processor, $login, $password, @bop_options, $namespace ) ;
1419 my( $auth, $order_number ) = ( '', '', '' );
1420 my $gatewaynum = '';
1422 if ( $options{'paynum'} ) {
1424 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
1425 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
1426 or return "Unknown paynum $options{'paynum'}";
1427 $amount ||= $cust_pay->paid;
1429 my @cust_bill_pay = qsearch('cust_bill_pay', { paynum=>$cust_pay->paynum });
1430 $content{'invoice_number'} = $cust_bill_pay[0]->invnum if @cust_bill_pay;
1432 if ( $cust_pay->get('processor') ) {
1433 ($gatewaynum, $processor, $auth, $order_number) =
1435 $cust_pay->gatewaynum,
1436 $cust_pay->processor,
1438 $cust_pay->order_number,
1441 # this payment wasn't upgraded, which probably means this won't work,
1443 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
1444 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
1445 $cust_pay->paybatch;
1446 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
1449 if ( $gatewaynum ) { #gateway for the payment to be refunded
1451 my $payment_gateway =
1452 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
1453 die "payment gateway $gatewaynum not found"
1454 unless $payment_gateway;
1456 $processor = $payment_gateway->gateway_module;
1457 $login = $payment_gateway->gateway_username;
1458 $password = $payment_gateway->gateway_password;
1459 $namespace = $payment_gateway->gateway_namespace;
1460 @bop_options = $payment_gateway->options;
1462 } else { #try the default gateway
1465 my $payment_gateway =
1466 $self->agent->payment_gateway('method' => $options{method});
1468 ( $conf_processor, $login, $password, $namespace ) =
1469 map { my $method = "gateway_$_"; $payment_gateway->$method }
1470 qw( module username password namespace );
1472 @bop_options = $payment_gateway->gatewaynum
1473 ? $payment_gateway->options
1474 : @{ $payment_gateway->get('options') };
1476 return "processor of payment $options{'paynum'} $processor does not".
1477 " match default processor $conf_processor"
1478 unless $processor eq $conf_processor;
1483 } else { # didn't specify a paynum, so look for agent gateway overrides
1484 # like a normal transaction
1486 my $payment_gateway =
1487 $self->agent->payment_gateway( 'method' => $options{method},
1488 #'payinfo' => $payinfo,
1490 my( $processor, $login, $password, $namespace ) =
1491 map { my $method = "gateway_$_"; $payment_gateway->$method }
1492 qw( module username password namespace );
1494 my @bop_options = $payment_gateway->gatewaynum
1495 ? $payment_gateway->options
1496 : @{ $payment_gateway->get('options') };
1499 return "neither amount nor paynum specified" unless $amount;
1501 eval "use $namespace";
1506 'type' => $options{method},
1508 'password' => $password,
1509 'order_number' => $order_number,
1510 'amount' => $amount,
1512 $content{authorization} = $auth
1513 if length($auth); #echeck/ACH transactions have an order # but no auth
1514 #(at least with authorize.net)
1516 my $currency = $conf->exists('business-onlinepayment-currency')
1517 && $conf->config('business-onlinepayment-currency');
1518 $content{currency} = $currency if $currency;
1520 my $disable_void_after;
1521 if ($conf->exists('disable_void_after')
1522 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
1523 $disable_void_after = $1;
1526 #first try void if applicable
1527 my $void = new Business::OnlinePayment( $processor, @bop_options );
1530 if ($void->can('info')) {
1532 $paytype = 'ECHECK' if $cust_pay && $cust_pay->payby eq 'CHEK';
1533 $paytype = 'CC' if $cust_pay && $cust_pay->payby eq 'CARD';
1534 my %supported_actions = $void->info('supported_actions');
1536 if ( %supported_actions && $paytype
1537 && defined($supported_actions{$paytype})
1538 && !grep{ $_ eq 'Void' } @{$supported_actions{$paytype}} );
1541 if ( $cust_pay && $cust_pay->paid == $amount
1543 ( not defined($disable_void_after) )
1544 || ( time < ($cust_pay->_date + $disable_void_after ) )
1548 warn " attempting void\n" if $DEBUG > 1;
1549 if ( $void->can('info') ) {
1550 if ( $cust_pay->payby eq 'CARD'
1551 && $void->info('CC_void_requires_card') )
1553 $content{'card_number'} = $cust_pay->payinfo;
1554 } elsif ( $cust_pay->payby eq 'CHEK'
1555 && $void->info('ECHECK_void_requires_account') )
1557 ( $content{'account_number'}, $content{'routing_code'} ) =
1558 split('@', $cust_pay->payinfo);
1559 $content{'name'} = $self->get('first'). ' '. $self->get('last');
1562 $void->content( 'action' => 'void', %content );
1563 $void->test_transaction(1)
1564 if $conf->exists('business-onlinepayment-test_transaction');
1566 if ( $void->is_success ) {
1567 # specified as a refund reason, but now we want a payment void reason
1568 # extract just the reason text, let cust_pay::void handle new_or_existing
1569 my $reason = qsearchs('reason',{ 'reasonnum' => $options{'reasonnum'} });
1571 $error = 'Reason could not be loaded' unless $reason;
1572 $error = $cust_pay->void($reason->reason) unless $error;
1574 # gah, even with transactions.
1575 my $e = 'WARNING: Card/ACH voided but database not updated - '.
1576 "error voiding payment: $error";
1580 warn " void successful\n" if $DEBUG > 1;
1585 warn " void unsuccessful, trying refund\n"
1589 my $address = $self->address1;
1590 $address .= ", ". $self->address2 if $self->address2;
1592 my($payname, $payfirst, $paylast);
1593 if ( $self->payname && $options{method} ne 'ECHECK' ) {
1594 $payname = $self->payname;
1595 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1596 or return "Illegal payname $payname";
1597 ($payfirst, $paylast) = ($1, $2);
1599 $payfirst = $self->getfield('first');
1600 $paylast = $self->getfield('last');
1601 $payname = "$payfirst $paylast";
1604 my @invoicing_list = $self->invoicing_list_emailonly;
1605 if ( $conf->exists('emailinvoiceautoalways')
1606 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1607 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1608 push @invoicing_list, $self->all_emails;
1611 my $email = ($conf->exists('business-onlinepayment-email-override'))
1612 ? $conf->config('business-onlinepayment-email-override')
1613 : $invoicing_list[0];
1615 my $payip = exists($options{'payip'})
1618 $content{customer_ip} = $payip
1622 if ( $options{method} eq 'CC' ) {
1625 $content{card_number} = $payinfo = $cust_pay->payinfo;
1626 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
1627 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
1628 ($content{expiration} = "$2/$1"); # where available
1630 $content{card_number} = $payinfo = $self->payinfo;
1631 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
1632 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1633 $content{expiration} = "$2/$1";
1636 } elsif ( $options{method} eq 'ECHECK' ) {
1639 $payinfo = $cust_pay->payinfo;
1641 $payinfo = $self->payinfo;
1643 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
1644 $content{bank_name} = $self->payname;
1645 $content{account_type} = 'CHECKING';
1646 $content{account_name} = $payname;
1647 $content{customer_org} = $self->company ? 'B' : 'I';
1648 $content{customer_ssn} = $self->ss;
1653 my $refund = new Business::OnlinePayment( $processor, @bop_options );
1654 my %sub_content = $refund->content(
1655 'action' => 'credit',
1656 'customer_id' => $self->custnum,
1657 'last_name' => $paylast,
1658 'first_name' => $payfirst,
1660 'address' => $address,
1661 'city' => $self->city,
1662 'state' => $self->state,
1663 'zip' => $self->zip,
1664 'country' => $self->country,
1666 'phone' => $self->daytime || $self->night,
1669 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
1671 $refund->test_transaction(1)
1672 if $conf->exists('business-onlinepayment-test_transaction');
1675 return "$processor error: ". $refund->error_message
1676 unless $refund->is_success();
1678 $order_number = $refund->order_number if $refund->can('order_number');
1680 # change this to just use $cust_pay->delete_cust_bill_pay?
1681 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
1682 my @cust_bill_pay = $cust_pay->cust_bill_pay;
1683 last unless @cust_bill_pay;
1684 my $cust_bill_pay = pop @cust_bill_pay;
1685 my $error = $cust_bill_pay->delete;
1689 my $cust_refund = new FS::cust_refund ( {
1690 'custnum' => $self->custnum,
1691 'paynum' => $options{'paynum'},
1692 'source_paynum' => $options{'paynum'},
1693 'refund' => $amount,
1695 'payby' => $bop_method2payby{$options{method}},
1696 'payinfo' => $payinfo,
1697 'reasonnum' => $options{'reasonnum'},
1698 'gatewaynum' => $gatewaynum, # may be null
1699 'processor' => $processor,
1700 'auth' => $refund->authorization,
1701 'order_number' => $order_number,
1703 my $error = $cust_refund->insert;
1705 $cust_refund->paynum(''); #try again with no specific paynum
1706 $cust_refund->source_paynum('');
1707 my $error2 = $cust_refund->insert;
1709 # gah, even with transactions.
1710 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
1711 "error inserting refund ($processor): $error2".
1712 " (previously tried insert with paynum #$options{'paynum'}" .
1723 =item realtime_verify_bop [ OPTION => VALUE ... ]
1725 Runs an authorization-only transaction for $1 against this credit card (if
1726 successful, immediatly reverses the authorization).
1728 Returns the empty string if the authorization was sucessful, or an error
1731 Option I<cust_payby> should be passed, even if it's not yet been inserted.
1732 Object will be tokenized if possible, but that change will not be
1733 updated in database (must be inserted/replaced afterwards.)
1735 Currently only succeeds for Business::OnlinePayment CC transactions.
1739 #some false laziness w/realtime_bop and realtime_refund_bop, not enough to make
1740 #it worth merging but some useful small subs should be pulled out
1741 sub realtime_verify_bop {
1744 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1745 my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_verify_bop');
1748 if (ref($_[0]) eq 'HASH') {
1749 %options = %{$_[0]};
1755 warn "$me realtime_verify_bop\n";
1756 warn " $_ => $options{$_}\n" foreach keys %options;
1759 # set fields from passed cust_payby
1760 return "No cust_payby" unless $options{'cust_payby'};
1761 $self->_bop_cust_payby_options(\%options);
1767 my $payment_gateway = $self->_payment_gateway( \%options );
1768 my $namespace = $payment_gateway->gateway_namespace;
1770 eval "use $namespace";
1774 # check for banned credit card/ACH
1777 my $ban = FS::banned_pay->ban_search(
1778 'payby' => $bop_method2payby{'CC'},
1779 'payinfo' => $options{payinfo},
1781 return "Banned credit card" if $ban && $ban->bantype ne 'warn';
1787 my $bop_content = $self->_bop_content(\%options);
1788 return $bop_content unless ref($bop_content);
1790 my @invoicing_list = $self->invoicing_list_emailonly;
1791 if ( $conf->exists('emailinvoiceautoalways')
1792 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
1793 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1794 push @invoicing_list, $self->all_emails;
1797 my $email = ($conf->exists('business-onlinepayment-email-override'))
1798 ? $conf->config('business-onlinepayment-email-override')
1799 : $invoicing_list[0];
1804 if ( $namespace eq 'Business::OnlinePayment' ) {
1806 if ( $options{method} eq 'CC' ) {
1808 $content{card_number} = $options{payinfo};
1809 $paydate = $options{'paydate'};
1810 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1811 $content{expiration} = "$2/$1";
1813 $content{cvv2} = $options{'paycvv'}
1814 if length($options{'paycvv'});
1816 my $paystart_month = $options{'paystart_month'};
1817 my $paystart_year = $options{'paystart_year'};
1819 $content{card_start} = "$paystart_month/$paystart_year"
1820 if $paystart_month && $paystart_year;
1822 my $payissue = $options{'payissue'};
1823 $content{issue_number} = $payissue if $payissue;
1825 } elsif ( $options{method} eq 'ECHECK' ){
1826 #cannot verify, move along (though it shouldn't be called...)
1829 return "unknown method ". $options{method};
1831 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
1832 #cannot verify, move along
1835 return "unknown namespace $namespace";
1839 # run transaction(s)
1843 my $transaction; #need this back so we can do _tokenize_card
1845 # don't mutex the customer here, because they might be uncommitted. and
1846 # this is only verification. it doesn't matter if they have other
1847 # unfinished verifications.
1849 my $cust_pay_pending = new FS::cust_pay_pending {
1850 'custnum_pending' => 1,
1853 'payby' => $bop_method2payby{'CC'},
1854 'payinfo' => $options{payinfo},
1855 'paymask' => $options{paymask},
1856 'paydate' => $paydate,
1857 'pkgnum' => $options{'pkgnum'},
1859 'gatewaynum' => $payment_gateway->gatewaynum || '',
1860 'session_id' => $options{session_id} || '',
1862 $cust_pay_pending->payunique( $options{payunique} )
1863 if defined($options{payunique}) && length($options{payunique});
1866 # open a separate handle for creating/updating the cust_pay_pending
1868 local $FS::UID::dbh = myconnect();
1869 local $FS::UID::AutoCommit = 1;
1871 # if this is an existing customer (and we can tell now because
1872 # this is a fresh transaction), it's safe to assign their custnum
1873 # to the cust_pay_pending record, and then the verification attempt
1874 # will remain linked to them even if it fails.
1875 if ( FS::cust_main->by_key($self->custnum) ) {
1876 $cust_pay_pending->set('custnum', $self->custnum);
1879 warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n"
1882 # if this fails, just return; everything else will still allow the
1883 # cust_pay_pending to have its custnum set later
1884 my $cpp_new_err = $cust_pay_pending->insert;
1885 return $cpp_new_err if $cpp_new_err;
1887 warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n"
1889 warn Dumper($cust_pay_pending) if $DEBUG > 2;
1891 $transaction = new $namespace( $payment_gateway->gateway_module,
1892 $self->_bop_options(\%options),
1895 $transaction->content(
1897 $self->_bop_auth(\%options),
1898 'action' => 'Authorization Only',
1899 'description' => $options{'description'},
1901 'customer_id' => $self->custnum,
1903 'reference' => $cust_pay_pending->paypendingnum, #for now
1908 $cust_pay_pending->status('pending');
1909 my $cpp_pending_err = $cust_pay_pending->replace;
1910 return $cpp_pending_err if $cpp_pending_err;
1912 warn Dumper($transaction) if $DEBUG > 2;
1914 unless ( $BOP_TESTING ) {
1915 $transaction->test_transaction(1)
1916 if $conf->exists('business-onlinepayment-test_transaction');
1917 $transaction->submit();
1919 if ( $BOP_TESTING_SUCCESS ) {
1920 $transaction->is_success(1);
1921 $transaction->authorization('fake auth');
1923 $transaction->is_success(0);
1924 $transaction->error_message('fake failure');
1928 if ( $transaction->is_success() ) {
1930 $cust_pay_pending->status('authorized');
1931 my $cpp_authorized_err = $cust_pay_pending->replace;
1932 return $cpp_authorized_err if $cpp_authorized_err;
1934 my $auth = $transaction->authorization;
1935 my $ordernum = $transaction->can('order_number')
1936 ? $transaction->order_number
1939 my $reverse = new $namespace( $payment_gateway->gateway_module,
1940 $self->_bop_options(\%options),
1943 $reverse->content( 'action' => 'Reverse Authorization',
1944 $self->_bop_auth(\%options),
1948 'authorization' => $transaction->authorization,
1949 'order_number' => $ordernum,
1952 'result_code' => $transaction->result_code,
1953 'txn_date' => $transaction->txn_date,
1957 $reverse->test_transaction(1)
1958 if $conf->exists('business-onlinepayment-test_transaction');
1961 if ( $reverse->is_success ) {
1963 $cust_pay_pending->status('done');
1964 $cust_pay_pending->statustext('reversed');
1965 my $cpp_reversed_err = $cust_pay_pending->replace;
1966 return $cpp_reversed_err if $cpp_reversed_err;
1970 my $e = "Authorization successful but reversal failed, custnum #".
1971 $self->custnum. ': '. $reverse->result_code.
1972 ": ". $reverse->error_message;
1979 ### Address Verification ###
1981 # Single-letter codes vary by cardtype.
1983 # Erring on the side of accepting cards if avs is not available,
1984 # only rejecting if avs occurred and there's been an explicit mismatch
1986 # Charts below taken from vSecure documentation,
1987 # shows codes for Amex/Dscv/MC/Visa
1989 # ACCEPTABLE AVS RESPONSES:
1990 # Both Address and 5-digit postal code match Y A Y Y
1991 # Both address and 9-digit postal code match Y A X Y
1992 # United Kingdom – Address and postal code match _ _ _ F
1993 # International transaction – Address and postal code match _ _ _ D/M
1995 # ACCEPTABLE, BUT ISSUE A WARNING:
1996 # Ineligible transaction; or message contains a content error _ _ _ E
1997 # System unavailable; retry R U R R
1998 # Information unavailable U W U U
1999 # Issuer does not support AVS S U S S
2000 # AVS is not applicable _ _ _ S
2001 # Incompatible formats – Not verified _ _ _ C
2002 # Incompatible formats – Address not verified; postal code matches _ _ _ P
2003 # International transaction – address not verified _ G _ G/I
2005 # UNACCEPTABLE AVS RESPONSES:
2006 # Only Address matches A Y A A
2007 # Only 5-digit postal code matches Z Z Z Z
2008 # Only 9-digit postal code matches Z Z W W
2009 # Neither address nor postal code matches N N N N
2011 if (my $avscode = uc($transaction->avs_code)) {
2013 # map codes to accept/warn/reject
2015 'American Express card' => {
2024 'Discover card' => {
2063 my $cardtype = cardtype($content{card_number});
2064 if ($avs->{$cardtype}) {
2065 my $avsact = $avs->{$cardtype}->{$avscode};
2067 if ($avsact eq 'r') {
2068 return "AVS code verification failed, cardtype $cardtype, code $avscode";
2069 } elsif ($avsact eq 'w') {
2070 $warning = "AVS did not occur, cardtype $cardtype, code $avscode";
2071 } elsif (!$avsact) {
2072 $warning = "AVS code unknown, cardtype $cardtype, code $avscode";
2073 } # else $avsact eq 'a'
2075 $log->warning($warning);
2078 } # else $cardtype avs handling not implemented
2079 } # else !$transaction->avs_code
2081 } else { # is not success
2083 # status is 'done' not 'declined', as in _realtime_bop_result
2084 $cust_pay_pending->status('done');
2085 $error = $transaction->error_message || 'Unknown error';
2086 $cust_pay_pending->statustext($error);
2087 # could also record failure_status here,
2088 # but it's not supported by B::OP::vSecureProcessing...
2089 # need a B::OP module with (reverse) auth only to test it with
2090 my $cpp_declined_err = $cust_pay_pending->replace;
2091 return $cpp_declined_err if $cpp_declined_err;
2095 } # end of IMMEDIATE; we now have our $error and $transaction
2098 # Save the custnum (as part of the main transaction, so it can reference
2102 if (!$cust_pay_pending->custnum) {
2103 $cust_pay_pending->set('custnum', $self->custnum);
2104 my $set_custnum_err = $cust_pay_pending->replace;
2105 if ($set_custnum_err) {
2106 $log->error($set_custnum_err);
2107 $error ||= $set_custnum_err;
2108 # but if there was a real verification error also, return that one
2116 #important that we not pass replace option here,
2117 #because cust_payby->replace uses realtime_verify_bop!
2118 $self->_tokenize_card($transaction,$options{'cust_payby'},$log);
2124 # $error contains the transaction error_message, if is_success was false.
2130 =item realtime_tokenize [ OPTION => VALUE ... ]
2132 If possible, runs a tokenize transaction.
2133 In order to be possible, a credit card cust_payby record
2134 must be passed and a Business::OnlinePayment gateway capable
2135 of Tokenize transactions must be configured for this user.
2137 Returns the empty string if the authorization was sucessful
2138 or was not possible (thus allowing this to be safely called with
2139 non-tokenizable records/gateways, without having to perform separate tests),
2140 or an error message otherwise.
2142 Option I<cust_payby> should be passed, even if it's not yet been inserted.
2143 Object will be tokenized if possible, but that change will not be
2144 updated in database (must be inserted/replaced afterwards.)
2148 sub realtime_tokenize {
2151 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
2152 my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_tokenize');
2155 if (ref($_[0]) eq 'HASH') {
2156 %options = %{$_[0]};
2161 # set fields from passed cust_payby
2162 return "No cust_payby" unless $options{'cust_payby'};
2163 $self->_bop_cust_payby_options(\%options);
2164 return '' unless $options{method} eq 'CC';
2165 return '' if $options{payinfo} =~ /^99\d{14}$/; #already tokenized
2171 $options{'nofatal'} = 1;
2172 my $payment_gateway = $self->_payment_gateway( \%options );
2173 return '' unless $payment_gateway;
2174 my $namespace = $payment_gateway->gateway_namespace;
2175 return '' unless $namespace eq 'Business::OnlinePayment';
2177 eval "use $namespace";
2181 # check for tokenize ability
2184 # just create transaction now, so it loads gateway_module
2185 my $transaction = new $namespace( $payment_gateway->gateway_module,
2186 $self->_bop_options(\%options),
2189 my %supported_actions = $transaction->info('supported_actions');
2190 return '' unless $supported_actions{'CC'} and grep(/^Tokenize$/,@{$supported_actions{'CC'}});
2193 # check for banned credit card/ACH
2196 my $ban = FS::banned_pay->ban_search(
2197 'payby' => $bop_method2payby{'CC'},
2198 'payinfo' => $options{payinfo},
2200 return "Banned credit card" if $ban && $ban->bantype ne 'warn';
2206 my $bop_content = $self->_bop_content(\%options);
2207 return $bop_content unless ref($bop_content);
2212 $content{card_number} = $options{payinfo};
2213 $paydate = $options{'paydate'};
2214 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2215 $content{expiration} = "$2/$1";
2217 $content{cvv2} = $options{'paycvv'}
2218 if length($options{'paycvv'});
2220 my $paystart_month = $options{'paystart_month'};
2221 my $paystart_year = $options{'paystart_year'};
2223 $content{card_start} = "$paystart_month/$paystart_year"
2224 if $paystart_month && $paystart_year;
2226 my $payissue = $options{'payissue'};
2227 $content{issue_number} = $payissue if $payissue;
2235 # no cust_pay_pending---this is not a financial transaction
2237 $transaction->content(
2239 $self->_bop_auth(\%options),
2240 'action' => 'Tokenize',
2241 'description' => $options{'description'},
2242 'customer_id' => $self->custnum,
2247 # no $BOP_TESTING handling for this
2248 $transaction->test_transaction(1)
2249 if $conf->exists('business-onlinepayment-test_transaction');
2250 $transaction->submit();
2252 if ( $transaction->card_token() ) { # no is_success flag
2254 #important that we not pass replace option here,
2255 #because cust_payby->replace uses realtime_tokenize!
2256 $self->_tokenize_card($transaction,$options{'cust_payby'},$log);
2260 $error = $transaction->error_message || 'Unknown error';
2276 L<FS::cust_main>, L<FS::cust_main::Billing>