enable CardFortress in test database, #71513
[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   unless ($text) {
51     print STDERR "Batch is empty, resolving..." if $opt_v;
52     next;
53   }
54   open OUT, ">$tmpdir/$filename";
55   print OUT $text;
56   close OUT;
57
58   my @batchconf = $conf->config('batchconfig-eft_canada', $pay_batch->agentnum);
59   my $user = $batchconf[0] or die "no EFT Canada batch username configured\n";
60   my $pass = $batchconf[1] or die "no EFT Canada batch password configured\n";
61
62   my $host = 'ftp.eftcanada.com';
63   print STDERR "Connecting to $user\@$host...\n" if $opt_v;
64
65   my $sftp = Net::SFTP::Foreign->new( host     => $host,
66                                       user     => $user,
67                                       password => $pass,
68                                       timeout  => 30,
69                                     );
70   die "failed to connect to '$user\@$host'\n(".$sftp->error.")\n" 
71       if $sftp->error;
72
73   $sftp->put("$tmpdir/$filename", "$filename")
74     or die "failed to upload file (".$sftp->error.")\n";
75
76   undef $sftp; #$sftp->disconnect;
77
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
90 print STDERR "Finished!\n" if $opt_v;
91
92 =head1 NAME
93
94 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
95
96 =head1 SYNOPSIS
97
98   freeside-paymentech-upload [ -a ] [ -v ] user batchnum
99
100 =head1 DESCRIPTION
101
102 Command line tool to upload a payment batch to the EFT Canada gateway.  The 
103 batch will be exported to a comma-delimited file and transmitted via SFTP.
104 Use L<freeside-eftca-download> to retrieve the response file.
105
106 -a: Send all open batches, instead of specifying a batchnum.
107
108 -v: Be verbose.
109
110 user: freeside username
111
112 batchnum: pay_batch primary key
113
114 =head1 SEE ALSO
115
116 L<FS::pay_batch>
117
118 =cut
119
120 1;
121