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