+ my $amount = sprintf("%.2f", $cust_main->balance - $cust_main->in_transit_payments);
+ return '' unless $amount > 0;
+
+ if ($options{'realtime'}) {
+ return $cust_main->realtime_bop ( $FS::payby::payby2bop{$cust_main->payby}, $amount,
+ %options,
+ );
+ }
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $pay_batch = qsearchs('pay_batch', {'status' => 'O'});
+
+ unless ( $pay_batch ) {
+ $pay_batch = new FS::pay_batch;
+ $pay_batch->setfield('status' => 'O');
+ my $error = $pay_batch->insert;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ die "error creating new batch: $error\n";
+ }
+ }
+
+ my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
+ 'batchnum' => $pay_batch->getfield('batchnum'),
+ 'custnum' => $cust_main->getfield('custnum'),
+ } );
+