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