[freeside-commits] branch FREESIDE_3_BRANCH updated. 05c1252f0d61a242cf53a29063a7c9d13b644eed

Mark Wells mark at 420.am
Fri Dec 16 16:42:16 PST 2016


The branch, FREESIDE_3_BRANCH has been updated
       via  05c1252f0d61a242cf53a29063a7c9d13b644eed (commit)
       via  20bd8dedc6e03c63fdd6e8e0ef884d72c427e40e (commit)
      from  e031448c5911bd58dcf5daacae6c6755648fec5a (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 05c1252f0d61a242cf53a29063a7c9d13b644eed
Author: Mark Wells <mark at freeside.biz>
Date:   Fri Dec 16 11:57:52 2016 -0800

    prevent B:BP batches from being marked in-transit if uploading the batch fails, #71837

diff --git a/FS/FS/pay_batch.pm b/FS/FS/pay_batch.pm
index 528b0d5..1c0a28a 100644
--- a/FS/FS/pay_batch.pm
+++ b/FS/FS/pay_batch.pm
@@ -14,6 +14,7 @@ use Scalar::Util 'blessed';
 use IO::Scalar;
 use FS::Misc qw(send_email); # for error notification
 use List::Util qw(sum);
+use Try::Tiny;
 
 @ISA = qw(FS::Record);
 
@@ -1080,16 +1081,21 @@ sub export_to_gateway {
   my $processor = $gateway->batch_processor(%proc_opt);
 
   my @items = map { $_->request_item } $self->cust_pay_batch;
-  my $batch = Business::BatchPayment->create(Batch =>
-    batch_id  => $self->batchnum,
-    items     => \@items
-  );
-  $processor->submit($batch);
+  try {
+    my $batch = Business::BatchPayment->create(Batch =>
+      batch_id  => $self->batchnum,
+      items     => \@items
+    );
+    $processor->submit($batch);
 
-  if ($batch->processor_id) {
-    $self->set('processor_id',$batch->processor_id);
-    $self->replace;
-  }
+    if ($batch->processor_id) {
+      $self->set('processor_id',$batch->processor_id);
+      $self->replace;
+    }
+  } catch {
+    $dbh->rollback if $oldAutoCommit;
+    die $_;
+  };
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   '';

commit 20bd8dedc6e03c63fdd6e8e0ef884d72c427e40e
Author: Mark Wells <mark at freeside.biz>
Date:   Thu Dec 15 16:29:54 2016 -0800

    per-agent configuration of batch processors, #71837

diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm
index 5cd4356..adf8b52 100644
--- a/FS/FS/Conf.pm
+++ b/FS/FS/Conf.pm
@@ -744,6 +744,7 @@ my %batch_gateway_options = (
     );
     map { $_->gatewaynum, $_->label } @gateways;
   },
+  'per_agent' => 1,
 );
 
 my %invoice_mode_options = (
diff --git a/FS/FS/Cron/pay_batch.pm b/FS/FS/Cron/pay_batch.pm
index 432271d..cdda681 100644
--- a/FS/FS/Cron/pay_batch.pm
+++ b/FS/FS/Cron/pay_batch.pm
@@ -22,6 +22,38 @@ $me = '[FS::Cron::pay_batch]';
 #  -r: Multi-process mode dry run option
 #  -a: Only process customers with the specified agentnum
 
+sub batch_gateways {
+  my $conf = FS::Conf->new;
+  # returns a list of arrayrefs: [ gateway, payby, agentnum ]
+  my %opt = @_;
+  my @agentnums;
+  if ( $conf->exists('batch-spoolagent') ) {
+    if ( $opt{a} ) {
+      @agentnums = split(',', $opt{a});
+    } else {
+      @agentnums = map { $_->agentnum } qsearch('agent');
+    }
+  } else {
+    @agentnums = ('');
+    if ( $opt{a} ) {
+      warn "Payment batch processing skipped in per-agent mode.\n" if $DEBUG;
+      return;
+    }
+  }
+  my @return;
+  foreach my $agentnum (@agentnums) {
+    my %gateways;
+    foreach my $payby ('CARD', 'CHEK') {
+      my $gatewaynum = $conf->config("batch-gateway-$payby", $agentnum);
+      next if !$gatewaynum;
+      my $gateway = FS::payment_gateway->by_key($gatewaynum)
+        or die "payment_gateway '$gatewaynum' not found\n";
+      push @return, [ $gateway, $payby, $agentnum ];
+    }
+  }
+  @return;
+}
+
 sub pay_batch_submit {
   my %opt = @_;
   local $DEBUG = ($opt{l} || 1) if $opt{v};
@@ -31,25 +63,14 @@ sub pay_batch_submit {
   my $dbh = dbh;
 
   warn "$me batch_submit\n" if $DEBUG;
-  my $conf = FS::Conf->new;
-
-  # need to respect -a somehow, but for now none of this is per-agent
-  if ( $opt{a} ) {
-    warn "Payment batch processing skipped in per-agent mode.\n" if $DEBUG;
-    return;
-  }
-  my %gateways;
-  foreach my $payby ('CARD', 'CHEK') {
-    my $gatewaynum = $conf->config("batch-gateway-$payby");
-    next if !$gatewaynum;
-    my $gateway = FS::payment_gateway->by_key($gatewaynum)
-      or die "payment_gateway '$gatewaynum' not found\n";
-
+  foreach my $config (batch_gateways(%opt)) {
+    my ($gateway, $payby, $agentnum) = @$config;
     if ( $gateway->batch_processor->can('default_transport') ) {
 
-      foreach my $pay_batch ( 
-        qsearch('pay_batch', { status => 'O', payby => $payby }) 
-      ) {
+      my $search = { status => 'O', payby => $payby };
+      $search->{agentnum} = $agentnum if $agentnum;
+
+      foreach my $pay_batch ( qsearch('pay_batch', $search) ) {
 
         warn "Exporting batch ".$pay_batch->batchnum."\n" if $DEBUG;
         eval { $pay_batch->export_to_gateway( $gateway, debug => $DEBUG ); };
@@ -80,38 +101,28 @@ sub pay_batch_receive {
   my $error;
 
   warn "$me batch_receive\n" if $DEBUG;
-  my $conf = FS::Conf->new;
 
-  # need to respect -a somehow, but for now none of this is per-agent
-  if ( $opt{a} ) {
-    warn "Payment batch processing skipped in per-agent mode.\n" if $DEBUG;
-    return;
-  }
-  my %gateways;
-  foreach my $payby ('CARD', 'CHEK') {
-    my $gatewaynum = $conf->config("batch-gateway-$payby");
-    next if !$gatewaynum;
-    # If the same gateway is selected for both paybys, only import it once
-    $gateways{$gatewaynum} = FS::payment_gateway->by_key($gatewaynum);
-    if ( !$gateways{$gatewaynum} ) {
+  my %gateway_done;
+    # If a gateway is selected for more than one payby+agentnum, still
+    # only import from it once; we expect it will send back multiple
+    # result batches.
+  foreach my $config (batch_gateways(%opt)) {
+    my ($gateway, $payby, $agentnum) = @$config;
+    next if $gateway_done{$gateway->gatewaynum};
+    next unless $gateway->batch_processor->can('default_transport');
+    # already warned about this above
+    warn "Importing results from '".$gateway->label."'\n" if $DEBUG;
+    # Note that import_from_gateway is not agent-limited; if a gateway
+    # returns results for batches not associated with this agent, we will
+    # still accept them. Well-behaved gateways will not do that.
+    $error = eval { 
+      FS::pay_batch->import_from_gateway( gateway =>$gateway, debug => $DEBUG ) 
+    } || $@;
+    if ( $error ) {
+      # this we can roll back
       $dbh->rollback;
-      die "batch-gateway-$payby gateway $gatewaynum not found\n";
+      die "error receiving from gateway '".$gateway->label."':\n$error\n";
     }
-  }
-
-  foreach my $gateway (values %gateways) {
-    if ( $gateway->batch_processor->can('default_transport') ) {
-      warn "Importing results from '".$gateway->label."'\n" if $DEBUG;
-      $error = eval { 
-        FS::pay_batch->import_from_gateway( gateway =>$gateway, debug => $DEBUG ) 
-      } || $@;
-      if ( $error ) {
-        # this we can roll back
-        $dbh->rollback;
-        die "error receiving from gateway '".$gateway->label."':\n$error\n";
-      }
-    } 
-    # else we already warned about it above
   } #$gateway
 
   # resolve batches if we can
diff --git a/httemplate/edit/agent_payment_gateway.html b/httemplate/edit/agent_payment_gateway.html
index 41a9f3e..87972e2 100644
--- a/httemplate/edit/agent_payment_gateway.html
+++ b/httemplate/edit/agent_payment_gateway.html
@@ -12,6 +12,8 @@ Use gateway <SELECT NAME="gatewaynum">
 % foreach my $payment_gateway (
 %      qsearch('payment_gateway', { 'disabled' => '' } )
 %    ) {
+%   # don't let these be selected as agent overrides; there's a different mechanism
+%   next if $payment_gateway->gateway_namespace eq 'Business::BatchPayment';
 %
 
   <OPTION VALUE="<% $payment_gateway->gatewaynum %>"><% $payment_gateway->gateway_module %> (<% $payment_gateway->gateway_username %>)

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/Conf.pm                              |    1 +
 FS/FS/Cron/pay_batch.pm                    |  103 +++++++++++++++-------------
 FS/FS/pay_batch.pm                         |   24 ++++---
 httemplate/edit/agent_payment_gateway.html |    2 +
 4 files changed, 75 insertions(+), 55 deletions(-)




More information about the freeside-commits mailing list