+sub capture_payment {
+ my $packet = shift;
+
+ warn "$me capture_payment called on $packet\n" if $DEBUG;
+
+ ###
+ # identify processor/gateway from called back URL
+ ###
+
+ my $conf = new FS::Conf;
+
+ my $payment_gateway;
+ if ( my $gwnum = $conf->config('selfservice-payment_gateway') ) {
+ $payment_gateway = qsearchs('payment_gateway', { 'gatewaynum' => $gwnum })
+ or die "configured gatewaynum $gwnum not found!";
+ }
+ else {
+ my $url = $packet->{url};
+
+ $payment_gateway = qsearchs('payment_gateway',
+ { 'gateway_callback_url' => popurl(0, $url) }
+ );
+ if (!$payment_gateway) {
+
+ my ( $processor, $login, $password, $action, @bop_options ) =
+ $conf->config('business-onlinepayment');
+ $action ||= 'normal authorization';
+ pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
+ die "No real-time processor is enabled - ".
+ "did you set the business-onlinepayment configuration value?\n"
+ unless $processor;
+
+ $payment_gateway = new FS::payment_gateway( {
+ gateway_namespace => $conf->config('business-onlinepayment-namespace'),
+ gateway_module => $processor,
+ gateway_username => $login,
+ gateway_password => $password,
+ gateway_action => $action,
+ options => [ ( @bop_options ) ],
+ });
+ }
+ }
+
+ die "No real-time third party processor is enabled - ".
+ "did you set the business-onlinepayment configuration value?\n*"
+ unless $payment_gateway->gateway_namespace eq 'Business::OnlineThirdPartyPayment';
+
+ ###
+ # locate pending transaction
+ ###
+
+ eval "use Business::OnlineThirdPartyPayment";
+ die $@ if $@;
+
+ my $transaction =
+ new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
+ @{ [ $payment_gateway->options ] },
+ );
+
+ my $paypendingnum = $transaction->reference($packet->{data});
+
+ my $cust_pay_pending =
+ qsearchs('cust_pay_pending', { paypendingnum => $paypendingnum } );
+
+ unless ($cust_pay_pending) {
+ my $bill_error = "No payment is being processed with id $paypendingnum".
+ "; Transaction aborted.";
+ return { error => '_decline', bill_error => $bill_error };
+ }
+
+ if ($cust_pay_pending->status ne 'thirdparty') {
+ my $bill_error = "Payment with id $paypendingnum is not thirdparty, but ".
+ $cust_pay_pending->status. "; Transaction aborted.";
+ return { error => '_decline', bill_error => $bill_error };
+ }
+
+ my $cust_main = $cust_pay_pending->cust_main;
+ if ( $packet->{cancel} ) {
+ # the user has chosen not to make this payment
+ # (probably should be a separate API call, but I don't want to duplicate
+ # all of the above...which should eventually go away)
+ my $error = $cust_pay_pending->delete;
+ # don't show any errors related to this; they're not meaningful
+ warn "error canceling pending payment $paypendingnum: $error\n" if $error;
+ return { 'error' => '_cancel',
+ 'session_id' => $cust_pay_pending->session_id };
+ } else {
+ # create the payment
+ my $bill_error =
+ $cust_main->realtime_botpp_capture( $cust_pay_pending,
+ %{$packet->{data}},
+ apply => 1,
+ );
+
+ return { 'error' => ( $bill_error->{bill_error} ? '_decline' : '' ),
+ %$bill_error,
+ };
+ }
+
+}
+