fix DBI connection, RT#39250
[freeside.git] / FS / bin / freeside-paymentech-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); #0.19 for ->newdir() interface, not in 5.10.0
7 use Net::SFTP::Foreign;
8 use Expect;
9 use FS::UID qw(adminsuidsetup datasrc);
10 use FS::Record qw(qsearch qsearchs);
11 use FS::pay_batch;
12 use FS::cust_pay_batch;
13 use FS::Conf;
14 use FS::Log;
15
16 use vars qw( $opt_a $opt_t $opt_v $opt_p );
17 getopts('avtp:');
18
19 #$Net::SFTP::Foreign::debug = -1;
20
21 sub log_and_die {
22   my $message = shift;
23   my $log = FS::Log->new('freeside-paymentech-upload');
24   $log->error($message);
25   die $message; 
26 }
27
28 sub usage { "
29   Usage:
30     freeside-paymentech-upload [ -v ] [ -t ] user batchnum
31     freeside-paymentech-upload -a [ -p payby ] [ -v ] [ -t ] user\n
32 " }
33
34 my $user = shift or die &usage;
35 adminsuidsetup $user;
36
37 my $zip_check = `which zip` or log_and_die("can't find zip executable\n");
38
39 my @batches; 
40
41 if($opt_a) {
42   my %criteria = (status => 'O');
43   $criteria{'payby'} = uc($opt_p) if $opt_p;
44   @batches = qsearch('pay_batch', \%criteria);
45   log_and_die("No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n")
46     if !@batches;
47 }
48 else {
49   my $batchnum = shift;
50   log_and_die("batchnum not passed\n".&usage) if !$batchnum;
51   @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
52   log_and_die("Can't find payment batch '$batchnum'\n") if !@batches;
53 }
54
55 my $conf = new FS::Conf;
56 my @batchconf = $conf->config('batchconfig-paymentech');
57 # BIN, terminalID, merchantID, username, password
58 my $username = $batchconf[3] or log_and_die("no Paymentech batch username configured\n");
59 my $password = $batchconf[4] or log_and_die("no Paymentech batch password configured\n");
60
61 #my $tmpdir = File::Temp->newdir();
62 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
63
64 my @filenames;
65
66 foreach my $pay_batch (@batches) {
67   my $batchnum = $pay_batch->batchnum;
68   my $filename = sprintf('%06d',$batchnum) . '-' .time2str('%Y%m%d%H%M%S', time);
69   print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
70   my $text = $pay_batch->export_batch(format => 'paymentech');
71   $text =~ s!<fileID>FILEID</fileID>!<fileID>$filename</fileID>! 
72     or log_and_die("couldn't find FILEID tag\n");
73   open OUT, ">$tmpdir/$filename.xml";
74   print OUT $text;
75   close OUT;
76
77   system('zip', '-P', $password, '-q', '-j',
78            "$tmpdir/$filename.zip", "$tmpdir/$filename.xml");
79
80   log_and_die("failed to create zip file\n") if (! -f "$tmpdir/$filename.zip" );
81   push @filenames, $filename;
82 }
83
84 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net'
85                    : 'orbitalbatch.paymentech.net');
86 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
87
88 my $sftp;
89 my $ssh_retry      = 25;   # number of times to try connection, needs to be >= 1
90 my $ssh_retry_wait = 60*5; # seconds to wait between tries
91 while ($ssh_retry > 0) {
92   $sftp = Net::SFTP::Foreign->new( host => $host,
93                                    user => $username,
94                                    password => $password,
95                                    timeout => 30,
96                                  );
97   last unless $sftp->error;
98   $ssh_retry -= 1;
99   sleep($ssh_retry_wait) if $ssh_retry > 0;
100 }
101
102 log_and_die("failed to connect to '$username\@$host'\n(".$sftp->error.")\n")
103     if $sftp->error;
104
105 foreach my $filename (@filenames) {
106   $sftp->put("$tmpdir/$filename.zip", "$filename.zip")
107     or log_and_die("failed to upload file (".$sftp->error.")\n");
108 }
109
110 print STDERR "Finished!\n" if $opt_v;
111
112 =head1 NAME
113
114 freeside-paymentech-upload - Transmit a payment batch to Chase Paymentech via SFTP.
115
116 =head1 SYNOPSIS
117
118   freeside-paymentech-upload [ -a [ -p PAYBY ] ] [ -v ] [ -t ] user batchnum
119
120 =head1 DESCRIPTION
121
122 Command line tool to upload a payment batch to the Chase Paymentech gateway.
123 The batch will be exported to the Paymentech XML format, packaged in a ZIP 
124 file, and transmitted via SFTP.  Use L<paymentech-download> to retrieve the 
125 response file.
126
127 -a: Send all open batches, instead of specifying a batchnum.
128
129 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
130
131 -v: Be verbose.
132
133 -t: Send the transaction to the test server.
134
135 user: freeside username
136
137 batchnum: pay_batch primary key
138
139 =head1 BUGS
140
141 Passing the zip password on the command line is slightly risky.
142
143 =head1 SEE ALSO
144
145 L<FS::pay_batch>
146
147 =cut
148
149 1;
150