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
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
42 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
43
44 foreach my $pay_batch (@batches) {
45
46   my $batchnum = $pay_batch->batchnum;
47   my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
48   print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
49   my $text = $pay_batch->export_batch(format => 'eft_canada');
50   open OUT, ">$tmpdir/$filename";
51   print OUT $text;
52   close OUT;
53
54   my @batchconf = $conf->config('batchconfig-eft_canada', $pay_batch->agentnum);
55   my $user = $batchconf[0] or die "no EFT Canada batch username configured\n";
56   my $pass = $batchconf[1] or die "no EFT Canada batch password configured\n";
57
58   my $host = 'ftp.eftcanada.com';
59   print STDERR "Connecting to $user\@$host...\n" if $opt_v;
60
61   my $sftp = Net::SFTP::Foreign->new( host     => $host,
62                                       user     => $user,
63                                       password => $pass,
64                                       timeout  => 30,
65                                     );
66   die "failed to connect to '$user\@$host'\n(".$sftp->error.")\n" 
67       if $sftp->error;
68
69   $sftp->put("$tmpdir/$filename", "$filename")
70     or die "failed to upload file (".$sftp->error.")\n";
71
72   undef $sftp; #$sftp->disconnect;
73
74   # Auto-approve and close the batch.  Some false laziness with manual_approve.
75   my $batchnum = $pay_batch->batchnum;
76   my $error;
77   foreach my $cpb ( qsearch('cust_pay_batch', { 'batchnum' => $batchnum } ) ) {
78     $cpb->setfield('paid', $cpb->amount);
79     $error = $cpb->approve($batchnum);
80     last if $error;
81   }
82   $error ||= $pay_batch->set_status('R');
83   die "error closing batch $batchnum: $error\n\n" if $error;
84 }
85
86 print STDERR "Finished!\n" if $opt_v;
87
88 =head1 NAME
89
90 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
91
92 =head1 SYNOPSIS
93
94   freeside-paymentech-upload [ -a ] [ -v ] user batchnum
95
96 =head1 DESCRIPTION
97
98 Command line tool to upload a payment batch to the EFT Canada gateway.  The 
99 batch will be exported to a comma-delimited file and transmitted via SFTP.
100 Use L<freeside-eftca-download> to retrieve the response file.
101
102 -a: Send all open batches, instead of specifying a batchnum.
103
104 -v: Be verbose.
105
106 user: freeside username
107
108 batchnum: pay_batch primary key
109
110 =head1 SEE ALSO
111
112 L<FS::pay_batch>
113
114 =cut
115
116 1;
117