9903dd4aa6e3bf2638ed71b62e58bd086c68ac02
[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   $criteria{'type'} = 'DEBIT' unless FS::pay_batch->can_handle_electronic_refunds('paymentech');
45   @batches = qsearch('pay_batch', \%criteria);
46   log_and_die("No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n")
47     if !@batches;
48 }
49 else {
50   my $batchnum = shift;
51   log_and_die("batchnum not passed\n".&usage) if !$batchnum;
52   @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
53   log_and_die("Can't find payment batch '$batchnum'\n") if !@batches;
54
55   if ($batches[0]->type eq "CREDIT") {
56     warn "running credit\n";
57     log_and_die( "Batch number $batchnum is a credit (batch refund) batch, and this format can not handle batch refunds.\n" )
58       unless FS::pay_batch->can_handle_electronic_refunds('paymentech');
59   }
60 }
61
62 my $conf = new FS::Conf;
63 my @batchconf = $conf->config('batchconfig-paymentech');
64 # BIN, terminalID, merchantID, username, password
65 my $username = $batchconf[3] or log_and_die("no Paymentech batch username configured\n");
66 my $password = $batchconf[4] or log_and_die("no Paymentech batch password configured\n");
67
68 #my $tmpdir = File::Temp->newdir();
69 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
70
71 my @filenames;
72
73 foreach my $pay_batch (@batches) {
74   my $batchnum = $pay_batch->batchnum;
75   my $filename = sprintf('%06d',$batchnum) . '-' .time2str('%Y%m%d%H%M%S', time);
76   print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
77   my $text = $pay_batch->export_batch(format => 'paymentech');
78   unless ($text) {
79     print STDERR "Batch is empty, resolving..." if $opt_v;
80     next;
81   }
82   $text =~ s!<fileID>FILEID</fileID>!<fileID>$filename</fileID>! 
83     or log_and_die("couldn't find FILEID tag\n");
84   open OUT, ">$tmpdir/$filename.xml";
85   print OUT $text;
86   close OUT;
87
88   system('zip', '-P', $password, '-q', '-j',
89            "$tmpdir/$filename.zip", "$tmpdir/$filename.xml");
90
91   log_and_die("failed to create zip file\n") if (! -f "$tmpdir/$filename.zip" );
92   push @filenames, $filename;
93 }
94 log_and_die("All batches empty\n") if !@filenames;
95
96 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net'
97                    : 'orbitalbatch.paymentech.net');
98 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
99
100 my $sftp;
101 my $ssh_retry      = 25;   # number of times to try connection, needs to be >= 1
102 my $ssh_retry_wait = 60*5; # seconds to wait between tries
103 while ($ssh_retry > 0) {
104   $sftp = Net::SFTP::Foreign->new( host => $host,
105                                    user => $username,
106                                    password => $password,
107                                    timeout => 300,
108                                  );
109   last unless $sftp->error;
110   $ssh_retry -= 1;
111   sleep($ssh_retry_wait) if $ssh_retry > 0;
112 }
113
114 log_and_die("failed to connect to '$username\@$host'\n(".$sftp->error.")\n")
115     if $sftp->error;
116
117 foreach my $filename (@filenames) {
118   $sftp->put("$tmpdir/$filename.zip", "$filename.zip")
119     or log_and_die("failed to upload file (".$sftp->error.")\n");
120 }
121
122 print STDERR "Finished!\n" if $opt_v;
123
124 =head1 NAME
125
126 freeside-paymentech-upload - Transmit a payment batch to Chase Paymentech via SFTP.
127
128 =head1 SYNOPSIS
129
130   freeside-paymentech-upload [ -a [ -p PAYBY ] ] [ -v ] [ -t ] user batchnum
131
132 =head1 DESCRIPTION
133
134 Command line tool to upload a payment batch to the Chase Paymentech gateway.
135 The batch will be exported to the Paymentech XML format, packaged in a ZIP 
136 file, and transmitted via SFTP.  Use L<freeside-paymentech-download> to retrieve the
137 response file.
138
139 -a: Send all open batches, instead of specifying a batchnum.
140
141 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
142
143 -v: Be verbose.
144
145 -t: Send the transaction to the test server.
146
147 user: freeside username
148
149 batchnum: pay_batch primary key
150
151 =head1 BUGS
152
153 Passing the zip password on the command line is slightly risky.
154
155 =head1 SEE ALSO
156
157 L<FS::pay_batch>
158
159 =cut
160
161 1;
162