b5011536572893cdced6199227ff5953db67f33d
[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
13 use vars qw( $opt_a $opt_v );
14 getopts('av');
15
16 #$Net::SFTP::Foreign::debug = -1;
17
18 sub HELP_MESSAGE { "
19   Usage:
20     freeside-eftca-upload [ -v ] user batchnum
21     freeside-eftca-upload -a [ -v ] user\n
22 " }
23
24 my $user = shift or die &HELP_MESSAGE;
25 adminsuidsetup $user;
26
27 my @batches; 
28
29 if($opt_a) {
30   @batches = qsearch('pay_batch', { 'status' => 'O', 'payby' => 'CHEK' })
31     or die "No open batches found.\n";
32 }
33 else {
34   my $batchnum = shift;
35   die &HELP_MESSAGE if !$batchnum;
36   @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
37   die "Can't find payment batch '$batchnum'\n" if !@batches;
38 }
39
40 my $conf = new FS::Conf;
41 my @batchconf = $conf->config('batchconfig-eft_canada');
42 my $username = $batchconf[0] or die "no EFT Canada batch username configured\n";
43 my $password = $batchconf[1] or die "no EFT Canada batch password configured\n";
44
45 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
46
47 my @filenames;
48
49 foreach my $pay_batch (@batches) {
50   my $batchnum = $pay_batch->batchnum;
51   my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
52   print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
53   my $text = $pay_batch->export_batch('eft_canada');
54   open OUT, ">$tmpdir/$filename";
55   print OUT $text;
56   close OUT;
57   push @filenames, $filename;
58 }
59
60 my $host = 'ftp.eftcanada.com';
61 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
62
63 my $sftp = Net::SFTP::Foreign->new( host => $host,
64                                     user => $username,
65                                     password => $password,
66                                     timeout => 30,
67                                     );
68 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" 
69     if $sftp->error;
70
71 foreach my $filename (@filenames) {
72   $sftp->put("$tmpdir/$filename", "$filename")
73     or die "failed to upload file (".$sftp->error.")\n";
74 }
75
76 $FS::UID::AutoCommit = 0;
77 foreach my $pay_batch (@batches) {
78   # Auto-approve and close the batch.  Some false laziness with manual_approve.
79   my $batchnum = $pay_batch->batchnum;
80   my $error;
81   foreach my $cpb ( qsearch('cust_pay_batch', { 'batchnum' => $batchnum } ) ) {
82     $cpb->setfield('paid', $cpb->amount);
83     $error = $cpb->approve($batchnum);
84     last if $error;
85   }
86   $error ||= $pay_batch->set_status('R');
87   die "error closing batch $batchnum: $error\n\n" if $error;
88 }
89 dbh->commit;
90
91 print STDERR "Finished!\n" if $opt_v;
92
93 =head1 NAME
94
95 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
96
97 =head1 SYNOPSIS
98
99   freeside-paymentech-upload [ -a ] [ -v ] user batchnum
100
101 =head1 DESCRIPTION
102
103 Command line tool to upload a payment batch to the EFT Canada gateway.  The 
104 batch will be exported to a comma-delimited file and transmitted via SFTP.
105 Use L<freeside-eftca-download> to retrieve the response file.
106
107 -a: Send all open batches, instead of specifying a batchnum.
108
109 -v: Be verbose.
110
111 user: freeside username
112
113 batchnum: pay_batch primary key
114
115 =head1 SEE ALSO
116
117 L<FS::pay_batch>
118
119 =cut
120
121 1;
122