summaryrefslogtreecommitdiff
path: root/FS/FS/Cron/pay_batch.pm
blob: 1e110f2da91f4f1fc7d3ed9b094fd7306a1f0035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package FS::Cron::pay_batch;

use strict;
use vars qw( @ISA @EXPORT_OK $me $DEBUG );
use Exporter;
use Date::Format;
use FS::UID qw(dbh);
use FS::Record qw( qsearch qsearchs );
use FS::Conf;
use FS::queue;
use FS::agent;

@ISA = qw( Exporter );
@EXPORT_OK = qw ( pay_batch_submit pay_batch_receive );
$DEBUG = 0;
$me = '[FS::Cron::pay_batch]';

#freeside-daily %opt:
#  -v: enable debugging
#  -l: debugging level
#  -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
#  -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};
  # if anything goes wrong, don't try to roll back previously submitted batches
  local $FS::UID::AutoCommit = 1;
  
  my $dbh = dbh;

  warn "$me batch_submit\n" if $DEBUG;
  foreach my $config (batch_gateways(%opt)) {
    my ($gateway, $payby, $agentnum) = @$config;
    if ( $gateway->batch_processor->can('default_transport') ) {

      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 ); };

        if ( $@ ) {
          # 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";
        }
      }

    } else { #can't(default_transport)
      warn "Payment gateway '".$gateway->label.
      "' doesn't support automatic transport; skipped.\n";
    }
  } #$payby

  1;
}

sub pay_batch_receive {
  my %opt = @_;
  local $DEBUG = ($opt{l} || 1) if $opt{v};
  local $FS::UID::AutoCommit = 0;

  my $dbh = dbh;
  my $error;

  warn "$me batch_receive\n" if $DEBUG;

  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 "error receiving from gateway '".$gateway->label."':\n$error\n";
    }
  } #$gateway

  # resolve batches if we can
  foreach my $pay_batch (qsearch('pay_batch', { status => 'I' })) {
    warn "Trying to resolve batch ".$pay_batch->batchnum."\n" if $DEBUG;
    $error = $pay_batch->try_to_resolve;
    if ( $error ) {
      $dbh->rollback;
      die "unable to resolve batch ".$pay_batch->batchnum.":\n$error\n";
    }
  }

  $dbh->commit;
}
1;