X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FBilling_Realtime.pm;h=54b3847c9778975a32dd438e81a79ff7212f41ad;hb=5ebcddd3d2cb95a02eaba0a8fc301b9c0d7e3e73;hp=7c194e102dbdad5144ffb02314352e1dac6e31b3;hpb=e5aee97b7d4c0cd8d6d0c3f0b1bca05adb676d7d;p=freeside.git diff --git a/FS/FS/cust_main/Billing_Realtime.pm b/FS/FS/cust_main/Billing_Realtime.pm index 7c194e102..54b3847c9 100644 --- a/FS/FS/cust_main/Billing_Realtime.pm +++ b/FS/FS/cust_main/Billing_Realtime.pm @@ -5,7 +5,8 @@ use vars qw( $conf $DEBUG $me ); use vars qw( $realtime_bop_decline_quiet ); #ugh use Data::Dumper; use Business::CreditCard 0.35; -use FS::UID qw( dbh ); +use Business::OnlinePayment; +use FS::UID qw( dbh myconnect ); use FS::Record qw( qsearch qsearchs ); use FS::Misc qw( send_email ); use FS::payby; @@ -353,20 +354,28 @@ sub realtime_bop { my $cc_surcharge = 0; my $cc_surcharge_pct = 0; - $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage') - if $conf->config('credit-card-surcharge-percentage') + $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage', $self->agentnum) + if $conf->config('credit-card-surcharge-percentage', $self->agentnum) + && $options{method} eq 'CC'; + + my $cc_surcharge_flat = 0; + $cc_surcharge_flat = $conf->config('credit-card-surcharge-flatfee', $self->agentnum) + if $conf->config('credit-card-surcharge-flatfee', $self->agentnum) && $options{method} eq 'CC'; # always add cc surcharge if called from event - if($options{'cc_surcharge_from_event'} && $cc_surcharge_pct > 0) { - $cc_surcharge = $options{'amount'} * $cc_surcharge_pct / 100; + if($options{'cc_surcharge_from_event'} && ($cc_surcharge_pct > 0 || $cc_surcharge_flat > 0)) { + if ($options{'amount'} > 0) { + $cc_surcharge = ($options{'amount'} * ($cc_surcharge_pct / 100)) + $cc_surcharge_flat; $options{'amount'} += $cc_surcharge; $options{'amount'} = sprintf("%.2f", $options{'amount'}); # round (again)? + } } - elsif($cc_surcharge_pct > 0) { # we're called not from event (i.e. from a - # payment screen), so consider the given - # amount as post-surcharge - $cc_surcharge = $options{'amount'} - ($options{'amount'} / ( 1 + $cc_surcharge_pct/100 )); + elsif($cc_surcharge_pct > 0 || $cc_surcharge_flat > 0) { + # we're called not from event (i.e. from a + # payment screen), so consider the given + # amount as post-surcharge-processing_fee + $cc_surcharge = $options{'amount'} - $options{'processing-fee'} - (($options{'amount'} - ($cc_surcharge_flat + $options{'processing-fee'})) / ( 1 + $cc_surcharge_pct/100 )) if $options{'amount'} > 0; } $cc_surcharge = sprintf("%.2f",$cc_surcharge) if $cc_surcharge > 0; @@ -888,16 +897,19 @@ sub _realtime_bop_result { my $dbh = dbh; #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction - - my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () ); + my $error = $cust_pay->insert( + $options{'manual'} ? ( 'manual' => 1 ) : (), + $options{'processing-fee'} > 0 ? ( 'processing-fee' => $options{'processing-fee'} ) : (), + ); if ( $error ) { $dbh->rollback or die $dbh->errstr if $oldAutoCommit; $cust_pay->invnum(''); #try again with no specific invnum $cust_pay->paynum(''); - my $error2 = $cust_pay->insert( $options{'manual'} ? - ( 'manual' => 1 ) : () - ); + my $error2 = $cust_pay->insert( + $options{'manual'} ? ( 'manual' => 1 ) : (), + $options{'processing-fee'} > 0 ? ( 'processing-fee' => $options{'processing-fee'} ) : (), + ); if ( $error2 ) { # gah. but at least we have a record of the state we had to abort in # from cust_pay_pending now. @@ -970,7 +982,7 @@ sub _realtime_bop_result { } # have a CC surcharge portion --> one-time charge - if ( $options{'cc_surcharge'} > 0 ) { + if ( $options{'cc_surcharge'} > 0 || $options{'processing-fee'} > 0) { # XXX: this whole block needs to be in a transaction? my $invnum; @@ -991,42 +1003,119 @@ sub _realtime_bop_result { unless ( $invnum ) { # XXX: unlikely case - pre-paying before any invoices generated # what it should do is create a new invoice and pick it - warn 'CC SURCHARGE AND NO INVOICES PICKED TO APPLY IT!'; + warn 'CC SURCHARGE OR PROCESS FEE AND NO INVOICES PICKED TO APPLY IT!'; return ''; } - my $cust_pkg; - my $charge_error = $self->charge({ + if ($options{'cc_surcharge'} > 0) { + my $cust_pkg; + my $cc_surcharge_text = 'Credit Card Surcharge'; + $cc_surcharge_text = $conf->config('credit-card-surcharge-text', $self->agentnum) if $conf->exists('credit-card-surcharge-text', $self->agentnum); + my $charge_error = $self->charge({ 'amount' => $options{'cc_surcharge'}, - 'pkg' => 'Credit Card Surcharge', + 'pkg' => $cc_surcharge_text, 'setuptax' => 'Y', 'cust_pkg_ref' => \$cust_pkg, - }); - if($charge_error) { - warn 'Unable to add CC surcharge cust_pkg'; - return ''; - } + }); + + if($charge_error) { + warn 'Unable to add CC surcharge cust_pkg'; + return ''; + } + + $cust_pkg->setup(time); + my $cp_error = $cust_pkg->replace; + if($cp_error) { + warn 'Unable to set setup time on cust_pkg for cc surcharge'; + # but keep going... + } - $cust_pkg->setup(time); - my $cp_error = $cust_pkg->replace; - if($cp_error) { - warn 'Unable to set setup time on cust_pkg for cc surcharge'; - # but keep going... - } - - my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum }); - unless ( $cust_bill ) { - warn "race condition + invoice deletion just happened"; - return ''; - } + my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum }); + unless ( $cust_bill ) { + warn "race condition + invoice deletion just happened"; + return ''; + } + + my $grand_error = + $cust_bill->add_cc_surcharge($cust_pkg->pkgnum,$options{'cc_surcharge'}); + + warn "cannot add CC surcharge to invoice #$invnum: $grand_error" + if $grand_error; + } # end if $options{'cc_surcharge'} + + if ($options{'processing-fee'} > 0) { + my $pf_cust_pkg; + my $processing_fee_text = 'Payment Processing Fee'; + + my $conf = new FS::Conf; - my $grand_error = - $cust_bill->add_cc_surcharge($cust_pkg->pkgnum,$options{'cc_surcharge'}); + my $pf_seperate_bill; + my $pf_bill_now; + if ($conf->exists('processing-fee_on_separate_invoice')) { + $pf_seperate_bill = 'Y'; + $pf_bill_now = '1'; + } + + my $pf_change_error = $self->charge({ + 'amount' => $options{'processing-fee'}, + 'pkg' => $processing_fee_text, + 'setuptax' => 'Y', + 'cust_pkg_ref' => \$pf_cust_pkg, + 'separate_bill' => $pf_seperate_bill, + 'bill_now' => $pf_bill_now, + }); + + if($pf_change_error) { + warn 'Unable to add payment processing fee'; + return ''; + } - warn "cannot add CC surcharge to invoice #$invnum: $grand_error" - if $grand_error; + $pf_cust_pkg->setup(time); + my $pf_error = $pf_cust_pkg->replace; + if($pf_error) { + warn 'Unable to set setup time on cust_pkg for processing fee'; + # but keep going... } + if ($conf->exists('processing-fee_on_separate_invoice')) { + my $cust_bill_pkg = qsearchs( 'cust_bill_pkg', { 'pkgnum' => $pf_cust_pkg->pkgnum } ); + + my $pf_cust_bill = qsearchs('cust_bill', { 'invnum' => $cust_bill_pkg->invnum }); + unless ( $pf_cust_bill ) { + warn "no processing fee inv found!"; + return ''; + } + + my $pf_apply_error = $pf_cust_bill->apply_payments_and_credits; + + my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum }); + unless ( $cust_bill ) { + warn "race condition + invoice deletion just happened"; + return ''; + } + + my $grand_pf_error = $cust_bill->apply_payments_and_credits; + + warn "cannot apply Processing fee to invoice #$invnum: $grand_pf_error - $pf_apply_error" + if $grand_pf_error || $pf_apply_error; + } ## processing-fee_on_separate_invoice + else { + my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum }); + unless ( $cust_bill ) { + warn "race condition + invoice deletion just happened"; + return ''; + } + + my $grand_pf_error = + $cust_bill->add_cc_surcharge($pf_cust_pkg->pkgnum,$options{'processing-fee'}); + + warn "cannot add Processing fee to invoice #$invnum: $grand_pf_error" + if $grand_pf_error; + } ## no processing-fee_on_separate_invoice + } #end if $options{'processing-fee'} + + } #end if ( $options{'cc_surcharge'} > 0 || $options{'processing-fee'} > 0) + return ''; #no error } @@ -1470,7 +1559,7 @@ sub realtime_refund_bop { $self->agent->payment_gateway( 'method' => $options{method}, #'payinfo' => $payinfo, ); - my( $processor, $login, $password, $namespace ) = + ( $processor, $login, $password, $namespace ) = map { my $method = "gateway_$_"; $payment_gateway->$method } qw( module username password namespace ); @@ -1731,6 +1820,7 @@ sub realtime_verify_bop { my $self = shift; local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG; + my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_verify_bop'); my %options = (); if (ref($_[0]) eq 'HASH') { @@ -1836,29 +1926,14 @@ sub realtime_verify_bop { # run transaction(s) ### - warn "claiming mutex on customer ". $self->custnum. "\n" if $DEBUG > 1; - $self->select_for_update; #mutex ... just until we get our pending record in - warn "obtained mutex on customer ". $self->custnum. "\n" if $DEBUG > 1; - - #the checks here are intended to catch concurrent payments - #double-form-submission prevention is taken care of in cust_pay_pending::check - - #also check and make sure there aren't *other* pending payments for this cust - - my @pending = qsearch('cust_pay_pending', { - 'custnum' => $self->custnum, - 'status' => { op=>'!=', value=>'done' } - }); - - return "A payment is already being processed for this customer (". - join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ). - "); verification transaction aborted." - if scalar(@pending); - - #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out + my $error; + my $transaction; #need this back so we can do _tokenize_card + # don't mutex the customer here, because they might be uncommitted. and + # this is only verification. it doesn't matter if they have other + # unfinished verifications. my $cust_pay_pending = new FS::cust_pay_pending { - 'custnum' => $self->custnum, + 'custnum_pending' => 1, 'paid' => '1.00', '_date' => '', 'payby' => $bop_method2payby{'CC'}, @@ -1875,221 +1950,254 @@ sub realtime_verify_bop { $cust_pay_pending->payunique( $options{payunique} ) if defined($options{payunique}) && length($options{payunique}); - warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n" - if $DEBUG > 1; - my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted - return $cpp_new_err if $cpp_new_err; + IMMEDIATE: { + # open a separate handle for creating/updating the cust_pay_pending + # record + local $FS::UID::dbh = myconnect(); + local $FS::UID::AutoCommit = 1; + + # if this is an existing customer (and we can tell now because + # this is a fresh transaction), it's safe to assign their custnum + # to the cust_pay_pending record, and then the verification attempt + # will remain linked to them even if it fails. + if ( FS::cust_main->by_key($self->custnum) ) { + $cust_pay_pending->set('custnum', $self->custnum); + } - warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n" - if $DEBUG > 1; - warn Dumper($cust_pay_pending) if $DEBUG > 2; + warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n" + if $DEBUG > 1; - my $transaction = new $namespace( $payment_gateway->gateway_module, - $self->_bop_options(\%options), - ); + # if this fails, just return; everything else will still allow the + # cust_pay_pending to have its custnum set later + my $cpp_new_err = $cust_pay_pending->insert; + return $cpp_new_err if $cpp_new_err; - $transaction->content( - 'type' => 'CC', - $self->_bop_auth(\%options), - 'action' => 'Authorization Only', - 'description' => $options{'description'}, - 'amount' => '1.00', - #'invoice_number' => $options{'invnum'}, - 'customer_id' => $self->custnum, - %$bop_content, - 'reference' => $cust_pay_pending->paypendingnum, #for now - 'callback_url' => $payment_gateway->gateway_callback_url, - 'cancel_url' => $payment_gateway->gateway_cancel_url, - 'email' => $email, - %content, #after - ); + warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n" + if $DEBUG > 1; + warn Dumper($cust_pay_pending) if $DEBUG > 2; - $cust_pay_pending->status('pending'); - my $cpp_pending_err = $cust_pay_pending->replace; - return $cpp_pending_err if $cpp_pending_err; + $transaction = new $namespace( $payment_gateway->gateway_module, + $self->_bop_options(\%options), + ); - warn Dumper($transaction) if $DEBUG > 2; + $transaction->content( + 'type' => 'CC', + $self->_bop_auth(\%options), + 'action' => 'Authorization Only', + 'description' => $options{'description'}, + 'amount' => '1.00', + #'invoice_number' => $options{'invnum'}, + 'customer_id' => $self->custnum, + %$bop_content, + 'reference' => $cust_pay_pending->paypendingnum, #for now + 'callback_url' => $payment_gateway->gateway_callback_url, + 'cancel_url' => $payment_gateway->gateway_cancel_url, + 'email' => $email, + %content, #after + ); - unless ( $BOP_TESTING ) { - $transaction->test_transaction(1) - if $conf->exists('business-onlinepayment-test_transaction'); - $transaction->submit(); - } else { - if ( $BOP_TESTING_SUCCESS ) { - $transaction->is_success(1); - $transaction->authorization('fake auth'); + $cust_pay_pending->status('pending'); + my $cpp_pending_err = $cust_pay_pending->replace; + return $cpp_pending_err if $cpp_pending_err; + + warn Dumper($transaction) if $DEBUG > 2; + + unless ( $BOP_TESTING ) { + $transaction->test_transaction(1) + if $conf->exists('business-onlinepayment-test_transaction'); + $transaction->submit(); } else { - $transaction->is_success(0); - $transaction->error_message('fake failure'); + if ( $BOP_TESTING_SUCCESS ) { + $transaction->is_success(1); + $transaction->authorization('fake auth'); + } else { + $transaction->is_success(0); + $transaction->error_message('fake failure'); + } } - } - my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_verify_bop'); + if ( $transaction->is_success() ) { - if ( $transaction->is_success() ) { + $cust_pay_pending->status('authorized'); + my $cpp_authorized_err = $cust_pay_pending->replace; + return $cpp_authorized_err if $cpp_authorized_err; - $cust_pay_pending->status('authorized'); - my $cpp_authorized_err = $cust_pay_pending->replace; - return $cpp_authorized_err if $cpp_authorized_err; + my $auth = $transaction->authorization; + my $ordernum = $transaction->can('order_number') + ? $transaction->order_number + : ''; - my $auth = $transaction->authorization; - my $ordernum = $transaction->can('order_number') - ? $transaction->order_number - : ''; + my $reverse = new $namespace( $payment_gateway->gateway_module, + $self->_bop_options(\%options), + ); - my $reverse = new $namespace( $payment_gateway->gateway_module, - $self->_bop_options(\%options), - ); + $reverse->content( 'action' => 'Reverse Authorization', + $self->_bop_auth(\%options), - $reverse->content( 'action' => 'Reverse Authorization', - $self->_bop_auth(\%options), + # B:OP + 'amount' => '1.00', + 'authorization' => $transaction->authorization, + 'order_number' => $ordernum, - # B:OP - 'amount' => '1.00', - 'authorization' => $transaction->authorization, - 'order_number' => $ordernum, + # vsecure + 'result_code' => $transaction->result_code, + 'txn_date' => $transaction->txn_date, - # vsecure - 'result_code' => $transaction->result_code, - 'txn_date' => $transaction->txn_date, + %content, + ); + $reverse->test_transaction(1) + if $conf->exists('business-onlinepayment-test_transaction'); + $reverse->submit(); - %content, - ); - $reverse->test_transaction(1) - if $conf->exists('business-onlinepayment-test_transaction'); - $reverse->submit(); + if ( $reverse->is_success ) { - if ( $reverse->is_success ) { + $cust_pay_pending->status('done'); + $cust_pay_pending->statustext('reversed'); + my $cpp_reversed_err = $cust_pay_pending->replace; + return $cpp_reversed_err if $cpp_reversed_err; - $cust_pay_pending->status('done'); - $cust_pay_pending->statustext('reversed'); - my $cpp_authorized_err = $cust_pay_pending->replace; - return $cpp_authorized_err if $cpp_authorized_err; + } else { - } else { + my $e = "Authorization successful but reversal failed, custnum #". + $self->custnum. ': '. $reverse->result_code. + ": ". $reverse->error_message; + $log->warning($e); + warn $e; + return $e; - my $e = "Authorization successful but reversal failed, custnum #". - $self->custnum. ': '. $reverse->result_code. - ": ". $reverse->error_message; - $log->warning($e); - warn $e; - return $e; + } - } + ### Address Verification ### + # + # Single-letter codes vary by cardtype. + # + # Erring on the side of accepting cards if avs is not available, + # only rejecting if avs occurred and there's been an explicit mismatch + # + # Charts below taken from vSecure documentation, + # shows codes for Amex/Dscv/MC/Visa + # + # ACCEPTABLE AVS RESPONSES: + # Both Address and 5-digit postal code match Y A Y Y + # Both address and 9-digit postal code match Y A X Y + # United Kingdom – Address and postal code match _ _ _ F + # International transaction – Address and postal code match _ _ _ D/M + # + # ACCEPTABLE, BUT ISSUE A WARNING: + # Ineligible transaction; or message contains a content error _ _ _ E + # System unavailable; retry R U R R + # Information unavailable U W U U + # Issuer does not support AVS S U S S + # AVS is not applicable _ _ _ S + # Incompatible formats – Not verified _ _ _ C + # Incompatible formats – Address not verified; postal code matches _ _ _ P + # International transaction – address not verified _ G _ G/I + # + # UNACCEPTABLE AVS RESPONSES: + # Only Address matches A Y A A + # Only 5-digit postal code matches Z Z Z Z + # Only 9-digit postal code matches Z Z W W + # Neither address nor postal code matches N N N N + + if (my $avscode = uc($transaction->avs_code)) { + + # map codes to accept/warn/reject + my $avs = { + 'American Express card' => { + 'A' => 'r', + 'N' => 'r', + 'R' => 'w', + 'S' => 'w', + 'U' => 'w', + 'Y' => 'a', + 'Z' => 'r', + }, + 'Discover card' => { + 'A' => 'a', + 'G' => 'w', + 'N' => 'r', + 'U' => 'w', + 'W' => 'w', + 'Y' => 'r', + 'Z' => 'r', + }, + 'MasterCard' => { + 'A' => 'r', + 'N' => 'r', + 'R' => 'w', + 'S' => 'w', + 'U' => 'w', + 'W' => 'r', + 'X' => 'a', + 'Y' => 'a', + 'Z' => 'r', + }, + 'VISA card' => { + 'A' => 'r', + 'C' => 'w', + 'D' => 'a', + 'E' => 'w', + 'F' => 'a', + 'G' => 'w', + 'I' => 'w', + 'M' => 'a', + 'N' => 'r', + 'P' => 'w', + 'R' => 'w', + 'S' => 'w', + 'U' => 'w', + 'W' => 'r', + 'Y' => 'a', + 'Z' => 'r', + }, + }; + my $cardtype = cardtype($content{card_number}); + if ($avs->{$cardtype}) { + my $avsact = $avs->{$cardtype}->{$avscode}; + my $warning = ''; + if ($avsact eq 'r') { + return "AVS code verification failed, cardtype $cardtype, code $avscode"; + } elsif ($avsact eq 'w') { + $warning = "AVS did not occur, cardtype $cardtype, code $avscode"; + } elsif (!$avsact) { + $warning = "AVS code unknown, cardtype $cardtype, code $avscode"; + } # else $avsact eq 'a' + if ($warning) { + $log->warning($warning); + warn $warning; + } + } # else $cardtype avs handling not implemented + } # else !$transaction->avs_code + + } else { # is not success + + # status is 'done' not 'declined', as in _realtime_bop_result + $cust_pay_pending->status('done'); + $error = $transaction->error_message || 'Unknown error'; + $cust_pay_pending->statustext($error); + # could also record failure_status here, + # but it's not supported by B::OP::vSecureProcessing... + # need a B::OP module with (reverse) auth only to test it with + my $cpp_declined_err = $cust_pay_pending->replace; + return $cpp_declined_err if $cpp_declined_err; - ### Address Verification ### - # - # Single-letter codes vary by cardtype. - # - # Erring on the side of accepting cards if avs is not available, - # only rejecting if avs occurred and there's been an explicit mismatch - # - # Charts below taken from vSecure documentation, - # shows codes for Amex/Dscv/MC/Visa - # - # ACCEPTABLE AVS RESPONSES: - # Both Address and 5-digit postal code match Y A Y Y - # Both address and 9-digit postal code match Y A X Y - # United Kingdom – Address and postal code match _ _ _ F - # International transaction – Address and postal code match _ _ _ D/M - # - # ACCEPTABLE, BUT ISSUE A WARNING: - # Ineligible transaction; or message contains a content error _ _ _ E - # System unavailable; retry R U R R - # Information unavailable U W U U - # Issuer does not support AVS S U S S - # AVS is not applicable _ _ _ S - # Incompatible formats – Not verified _ _ _ C - # Incompatible formats – Address not verified; postal code matches _ _ _ P - # International transaction – address not verified _ G _ G/I - # - # UNACCEPTABLE AVS RESPONSES: - # Only Address matches A Y A A - # Only 5-digit postal code matches Z Z Z Z - # Only 9-digit postal code matches Z Z W W - # Neither address nor postal code matches N N N N - - if (my $avscode = uc($transaction->avs_code)) { - - # map codes to accept/warn/reject - my $avs = { - 'American Express card' => { - 'A' => 'r', - 'N' => 'r', - 'R' => 'w', - 'S' => 'w', - 'U' => 'w', - 'Y' => 'a', - 'Z' => 'r', - }, - 'Discover card' => { - 'A' => 'a', - 'G' => 'w', - 'N' => 'r', - 'U' => 'w', - 'W' => 'w', - 'Y' => 'r', - 'Z' => 'r', - }, - 'MasterCard' => { - 'A' => 'r', - 'N' => 'r', - 'R' => 'w', - 'S' => 'w', - 'U' => 'w', - 'W' => 'r', - 'X' => 'a', - 'Y' => 'a', - 'Z' => 'r', - }, - 'VISA card' => { - 'A' => 'r', - 'C' => 'w', - 'D' => 'a', - 'E' => 'w', - 'F' => 'a', - 'G' => 'w', - 'I' => 'w', - 'M' => 'a', - 'N' => 'r', - 'P' => 'w', - 'R' => 'w', - 'S' => 'w', - 'U' => 'w', - 'W' => 'r', - 'Y' => 'a', - 'Z' => 'r', - }, - }; - my $cardtype = cardtype($content{card_number}); - if ($avs->{$cardtype}) { - my $avsact = $avs->{$cardtype}->{$avscode}; - my $warning = ''; - if ($avsact eq 'r') { - return "AVS code verification failed, cardtype $cardtype, code $avscode"; - } elsif ($avsact eq 'w') { - $warning = "AVS did not occur, cardtype $cardtype, code $avscode"; - } elsif (!$avsact) { - $warning = "AVS code unknown, cardtype $cardtype, code $avscode"; - } # else $avsact eq 'a' - if ($warning) { - $log->warning($warning); - warn $warning; - } - } # else $cardtype avs handling not implemented - } # else !$transaction->avs_code + } - } else { # is not success + } # end of IMMEDIATE; we now have our $error and $transaction - # status is 'done' not 'declined', as in _realtime_bop_result - $cust_pay_pending->status('done'); - $cust_pay_pending->statustext( $transaction->error_message || 'Unknown error' ); - # could also record failure_status here, - # but it's not supported by B::OP::vSecureProcessing... - # need a B::OP module with (reverse) auth only to test it with - my $cpp_declined_err = $cust_pay_pending->replace; - return $cpp_declined_err if $cpp_declined_err; + ### + # Save the custnum (as part of the main transaction, so it can reference + # the cust_main) + ### + if (!$cust_pay_pending->custnum) { + $cust_pay_pending->set('custnum', $self->custnum); + my $set_custnum_err = $cust_pay_pending->replace; + if ($set_custnum_err) { + $log->error($set_custnum_err); + $error ||= $set_custnum_err; + # but if there was a real verification error also, return that one + } } ### @@ -2114,7 +2222,9 @@ sub realtime_verify_bop { # result handling ### - $transaction->is_success() ? '' : $transaction->error_message(); + # $error contains the transaction error_message, if is_success was false. + + return $error; }