503c7a35a56a9740de44e25195ad40be708c0e25
[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   eval {
36     @batches = qsearch('pay_batch', { 'status' => 'O', 'payby' => 'CHEK' })
37   };
38   log_error_and_die ("Fatal database error: $@")
39     if $@;
40
41   log_info_and_die( "Finished: No open batches found.\n" )
42     unless @batches;
43 }
44 else {
45   my $batchnum = shift;
46   die &HELP_MESSAGE if !$batchnum;
47
48   local $@;
49   eval { @batches = qsearchs('pay_batch', { batchnum => $batchnum } ); };
50   log_error_and_die("Fatal database error: $@")
51     if $@;
52
53   log_error_and_die( "Can't find payment batch '$batchnum'\n" ) if !@batches;
54 }
55
56 my $conf = new FS::Conf;
57
58 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
59
60 foreach my $pay_batch (@batches) {
61
62   my $batchnum = $pay_batch->batchnum;
63   my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
64   log_info( "Exporting batch $batchnum to $filename...\n" );
65   my $text = $pay_batch->export_batch(format => 'eft_canada');
66   unless ($text) {
67     log_info( "Batch is empty, resolving..." );
68     next;
69   }
70   open OUT, ">$tmpdir/$filename";
71   print OUT $text;
72   close OUT;
73
74   my @batchconf = $conf->config('batchconfig-eft_canada', $pay_batch->agentnum);
75   my $user = $batchconf[0]
76     or log_error_and_die( "no EFT Canada batch username configured\n" );
77   my $pass = $batchconf[1]
78     or log_error_and_die( "no EFT Canada batch password configured\n" );
79
80   my $host = 'ftp.eftcanada.com';
81   log_info( "Connecting to $user\@$host...\n" );
82
83   my $sftp = Net::SFTP::Foreign->new( host     => $host,
84                                       user     => $user,
85                                       password => $pass,
86                                       timeout  => 30,
87                                     );
88   log_error_and_die("failed to connect to '$user\@$host'\n(".$sftp->error.")\n")
89       if $sftp->error;
90
91   $sftp->put("$tmpdir/$filename", "$filename")
92     or log_error_and_die( "failed to upload file (".$sftp->error.")\n" );
93
94   undef $sftp; #$sftp->disconnect;
95
96   # Auto-approve and close the batch.  Some false laziness with manual_approve.
97   my $batchnum = $pay_batch->batchnum;
98   my $error;
99
100   local $@;
101   eval {
102     foreach my $cpb ( qsearch('cust_pay_batch', { 'batchnum' => $batchnum } )) {
103       $cpb->setfield('paid', $cpb->amount);
104       $error = $cpb->approve($batchnum);
105       last if $error;
106     }
107   };
108   log_error_and_die("Fatal database error: $@")
109     if $@;
110
111   $error ||= $pay_batch->set_status('R');
112   log_error_and_die( "error closing batch $batchnum: $error\n\n" )
113     if $error;
114 }
115
116 log_info( "Finished!\n" );
117
118 sub log_info {
119   my $log_message = shift;
120   $log->info( $log_message );
121   print STDERR $log_message if $opt_v;
122 }
123
124 sub log_info_and_die {
125   my $log_message = shift;
126   $log->info( $log_message );
127   die $log_message;
128 }
129
130 sub log_error_and_die {
131   my $log_message = shift;
132   $log->error( $log_message );
133   die $log_message;
134 }
135
136 =head1 NAME
137
138 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
139
140 =head1 SYNOPSIS
141
142   freeside-eftca-upload [ -a ] [ -v ] user batchnum
143
144 =head1 DESCRIPTION
145
146 Command line tool to upload a payment batch to the EFT Canada gateway.  The 
147 batch will be exported to a comma-delimited file and transmitted via SFTP.
148 Use L<freeside-eftca-download> to retrieve the response file.
149
150 -a: Send all open batches, instead of specifying a batchnum.
151
152 -v: Be verbose.
153
154 user: freeside username
155
156 batchnum: pay_batch primary key
157
158 =head1 SEE ALSO
159
160 L<FS::pay_batch>
161
162 =cut
163
164 1;
165