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