RT# 82988 - added check for batch payment upload scripts to make sure they can handle...
[freeside.git] / FS / bin / freeside-eftca-upload
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use Date::Format qw(time2str);
6 use File::Temp qw(tempdir);
7 use Net::SFTP::Foreign;
8 use FS::UID qw(adminsuidsetup dbh);
9 use FS::Record qw(qsearch qsearchs);
10 use FS::pay_batch;
11 use FS::Conf;
12 use FS::Log;
13
14 use vars qw( $opt_a $opt_v );
15 getopts('av');
16
17 #$Net::SFTP::Foreign::debug = -1;
18
19 sub HELP_MESSAGE { "
20   Usage:
21     freeside-eftca-upload [ -v ] user batchnum
22     freeside-eftca-upload -a [ -v ] user\n
23 " }
24
25 my $user = shift or die &HELP_MESSAGE;
26 adminsuidsetup $user;
27
28 my $log = FS::Log->new('freeside-eftca-upload');
29 log_info( "EFT Canada upload started\n" );
30
31 my @batches; 
32
33 if($opt_a) {
34   local $@;
35
36   my %criteria= ( 'status' => 'O', 'payby' => 'CHEK' );
37   $criteria{'type'} = 'DEBIT' unless FS::pay_batch->can_handle_electronic_refunds('eft_canada');
38
39   eval {
40     @batches = qsearch('pay_batch', \%criteria)
41   };
42   log_error_and_die ("Fatal database error: $@")
43     if $@;
44
45   log_info_and_die( "Finished: No open batches found.\n" )
46     unless @batches;
47 }
48 else {
49   my $batchnum = shift;
50   die &HELP_MESSAGE if !$batchnum;
51
52   local $@;
53   eval { @batches = qsearchs('pay_batch', { batchnum => $batchnum } ); };
54   log_error_and_die("Fatal database error: $@")
55     if $@;
56
57   log_error_and_die( "Can't find payment batch '$batchnum'\n" ) if !@batches;
58
59   if ($batches[0]->type eq "CREDIT") {
60     warn "running credit\n";
61     log_error_and_die( "Batch number $batchnum is a credit (batch refund) batch, and this format can not handle batch refunds.\n" )
62       unless FS::pay_batch->can_handle_electronic_refunds('eft_canada');
63   }
64 }
65
66 my $conf = new FS::Conf;
67
68 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
69
70 foreach my $pay_batch (@batches) {
71
72   my $batchnum = $pay_batch->batchnum;
73   my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
74   log_info( "Exporting batch $batchnum to $filename...\n" );
75   my $text = $pay_batch->export_batch(format => 'eft_canada');
76   unless ($text) {
77     log_info( "Batch is empty, resolving..." );
78     next;
79   }
80   open OUT, ">$tmpdir/$filename";
81   print OUT $text;
82   close OUT;
83
84   my @batchconf = $conf->config('batchconfig-eft_canada', $pay_batch->agentnum);
85   my $user = $batchconf[0]
86     or log_error_and_die( "no EFT Canada batch username configured\n" );
87   my $pass = $batchconf[1]
88     or log_error_and_die( "no EFT Canada batch password configured\n" );
89
90   my $host = 'ftp.eftcanada.com';
91   log_info( "Connecting to $user\@$host...\n" );
92
93   my $sftp = Net::SFTP::Foreign->new( host     => $host,
94                                       user     => $user,
95                                       password => $pass,
96                                       timeout  => 30,
97                                     );
98   log_error_and_die("failed to connect to '$user\@$host'\n(".$sftp->error.")\n")
99       if $sftp->error;
100
101   $sftp->put("$tmpdir/$filename", "$filename")
102     or log_error_and_die( "failed to upload file (".$sftp->error.")\n" );
103
104   undef $sftp; #$sftp->disconnect;
105
106   # Auto-approve and close the batch.  Some false laziness with manual_approve.
107   my $batchnum = $pay_batch->batchnum;
108   my $error;
109
110   local $@;
111   eval {
112     foreach my $cpb ( qsearch('cust_pay_batch', { 'batchnum' => $batchnum } )) {
113       $cpb->setfield('paid', $cpb->amount);
114       $error = $cpb->approve($batchnum);
115       last if $error;
116     }
117   };
118   log_error_and_die("Fatal database error: $@")
119     if $@;
120
121   $error ||= $pay_batch->set_status('R');
122   log_error_and_die( "error closing batch $batchnum: $error\n\n" )
123     if $error;
124 }
125
126 log_info( "Finished!\n" );
127
128 sub log_info {
129   my $log_message = shift;
130   $log->info( $log_message );
131   print STDERR $log_message if $opt_v;
132 }
133
134 sub log_info_and_die {
135   my $log_message = shift;
136   $log->info( $log_message );
137   die $log_message;
138 }
139
140 sub log_error_and_die {
141   my $log_message = shift;
142   $log->error( $log_message );
143   die $log_message;
144 }
145
146 =head1 NAME
147
148 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
149
150 =head1 SYNOPSIS
151
152   freeside-eftca-upload [ -a ] [ -v ] user batchnum
153
154 =head1 DESCRIPTION
155
156 Command line tool to upload a payment batch to the EFT Canada gateway.  The 
157 batch will be exported to a comma-delimited file and transmitted via SFTP.
158 Use L<freeside-eftca-download> to retrieve the response file.
159
160 -a: Send all open batches, instead of specifying a batchnum.
161
162 -v: Be verbose.
163
164 user: freeside username
165
166 batchnum: pay_batch primary key
167
168 =head1 SEE ALSO
169
170 L<FS::pay_batch>
171
172 =cut
173
174 1;
175