X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2FCron%2Fpay_batch.pm;h=18532aca82abf9dd4d7b21615b98553b9e8a9c4c;hp=c7cedafb9d151c8a18413dfc55618716d1a3d62b;hb=b48c02a92562395c84dbfe8c47db5c4ba14891a0;hpb=92aedddd3684167abb60cd3f1d77bbc156c592e6 diff --git a/FS/FS/Cron/pay_batch.pm b/FS/FS/Cron/pay_batch.pm index c7cedafb9..18532aca8 100644 --- a/FS/FS/Cron/pay_batch.pm +++ b/FS/FS/Cron/pay_batch.pm @@ -11,7 +11,7 @@ use FS::queue; use FS::agent; @ISA = qw( Exporter ); -@EXPORT_OK = qw ( batch_submit batch_receive ); +@EXPORT_OK = qw ( pay_batch_submit pay_batch_receive ); $DEBUG = 0; $me = '[FS::Cron::pay_batch]'; @@ -22,7 +22,39 @@ $me = '[FS::Cron::pay_batch]'; # -r: Multi-process mode dry run option # -a: Only process customers with the specified agentnum -sub batch_submit { +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}; # if anything goes wrong, don't try to roll back previously submitted batches @@ -31,25 +63,14 @@ sub 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, type => 'DEBIT' }; + $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 ); }; @@ -58,7 +79,7 @@ sub batch_submit { # warn the error and continue. rolling back the transaction once # we've started sending batches is bad. warn "error submitting batch ".$pay_batch->batchnum." to gateway '". - $gateway->label."\n$@\n"; + $gateway->label."': $@\n"; } } @@ -71,7 +92,7 @@ sub batch_submit { 1; } -sub batch_receive { +sub pay_batch_receive { my %opt = @_; local $DEBUG = ($opt{l} || 1) if $opt{v}; local $FS::UID::AutoCommit = 0; @@ -80,38 +101,28 @@ sub 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, 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