1 package FS::Cron::pay_batch;
4 use vars qw( @ISA @EXPORT_OK $me $DEBUG );
8 use FS::Record qw( qsearch qsearchs );
13 @ISA = qw( Exporter );
14 @EXPORT_OK = qw ( batch_submit batch_receive );
16 $me = '[FS::Cron::pay_batch]';
19 # -v: enable debugging
21 # -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
22 # -r: Multi-process mode dry run option
23 # -a: Only process customers with the specified agentnum
27 local $DEBUG = ($opt{l} || 1) if $opt{v};
28 # if anything goes wrong, don't try to roll back previously submitted batches
29 local $FS::UID::AutoCommit = 1;
33 warn "$me batch_submit\n" if $DEBUG;
34 my $conf = FS::Conf->new;
36 # need to respect -a somehow, but for now none of this is per-agent
38 warn "Payment batch processing skipped in per-agent mode.\n" if $DEBUG;
42 foreach my $payby ('CARD', 'CHEK') {
43 my $gatewaynum = $conf->config("batch-gateway-$payby");
45 my $gateway = FS::payment_gateway->by_key($gatewaynum)
46 or die "payment_gateway '$gatewaynum' not found\n";
48 if ( $gateway->batch_processor->can('default_transport') ) {
50 foreach my $pay_batch (
51 qsearch('pay_batch', { status => 'O', payby => $payby })
54 warn "Exporting batch ".$pay_batch->batchnum."\n" if $DEBUG;
55 eval { $pay_batch->export_to_gateway( $gateway, debug => $DEBUG ); };
58 # warn the error and continue. rolling back the transaction once
59 # we've started sending batches is bad.
60 warn "error submitting batch ".$pay_batch->batchnum." to gateway '".
61 $gateway->label."\n$@\n";
65 } else { #can't(default_transport)
66 warn "Payment gateway '".$gateway->label.
67 "' doesn't support automatic transport; skipped.\n";
76 local $DEBUG = ($opt{l} || 1) if $opt{v};
77 local $FS::UID::AutoCommit = 0;
82 warn "$me batch_receive\n" if $DEBUG;
83 my $conf = FS::Conf->new;
85 # need to respect -a somehow, but for now none of this is per-agent
87 warn "Payment batch processing skipped in per-agent mode.\n" if $DEBUG;
91 foreach my $payby ('CARD', 'CHEK') {
92 my $gatewaynum = $conf->config("batch-gateway-$payby");
94 # If the same gateway is selected for both paybys, only import it once
95 $gateways{$gatewaynum} = FS::payment_gateway->by_key($gatewaynum);
96 if ( !$gateways{$gatewaynum} ) {
98 die "batch-gateway-$payby gateway $gatewaynum not found\n";
102 foreach my $gateway (values %gateways) {
103 if ( $gateway->batch_processor->can('default_transport') ) {
104 warn "Importing results from '".$gateway->label."'\n" if $DEBUG;
106 FS::pay_batch->import_from_gateway( gateway =>$gateway, debug => $DEBUG )
109 # this we can roll back
111 die "error receiving from gateway '".$gateway->label."':\n$error\n";
114 # else we already warned about it above
117 # resolve batches if we can
118 foreach my $pay_batch (qsearch('pay_batch', { status => 'I' })) {
119 warn "Trying to resolve batch ".$pay_batch->batchnum."\n" if $DEBUG;
120 $error = $pay_batch->try_to_resolve;
123 die "unable to resolve batch ".$pay_batch->batchnum.":\n$error\n";