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