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