back to old host name, info from customer/paymentech was bogus, RT#5650
[freeside.git] / FS / bin / freeside-paymentech-download
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_t $opt_v $opt_a );
16 getopts('vta:');
17
18 #$Net::SFTP::Foreign::debug = -1;
19 sub usage { "
20   Usage:
21       freeside-paymentech-download [ -v ] [ -t ] [ -a archivedir ] user\n
22 " }
23
24 my $user = shift or die &usage;
25 adminsuidsetup $user;
26
27 if ( $opt_a ) {
28   die "no such directory: $opt_a\n"
29     unless -d $opt_a;
30   die "archive directory $opt_a is not writable by the freeside user\n"
31     unless -w $opt_a;
32 }
33
34 my $unzip_check = `which unzip` or die "can't find unzip executable\n";
35
36 #my $tmpdir = File::Temp->newdir();
37 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
38
39 my $conf = new FS::Conf;
40 my @batchconf = $conf->config('batchconfig-paymentech');
41 # BIN, terminalID, merchantID, username, password
42 my $username = $batchconf[3] or die "no Paymentech batch username configured\n";
43 my $password = $batchconf[4] or die "no Paymentech batch password configured\n";
44
45 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net'
46                    : 'orbitalbatch.paymentech.net');
47 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
48
49 my $sftp = Net::SFTP::Foreign->new( host => $host,
50                                     user => $username,
51                                     password => $password,
52                                     timeout => 30,
53                                     );
54 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" if $sftp->error;
55
56 my @files = map { $_->{filename} } @{ $sftp->ls('.', wanted => qr/_resp\.zip$/) };
57 die "no response files found\n" if !@files;
58
59 BATCH: foreach my $filename (@files) {
60
61   #get file
62   $filename =~ s/_resp\.zip$//;
63   print STDERR "Retrieving $filename\n" if $opt_v;
64   $sftp->get("$filename\_resp.zip", "$tmpdir/${filename}_resp.zip");
65   if($sftp->error) {
66     warn "failed to download $filename\n";
67     next BATCH;
68   }
69
70   #unzip file
71   system('unzip', '-P', '$password', '-q',
72            "$tmpdir/${filename}_resp.zip", '-d', $tmpdir);
73   if(! -f "$tmpdir/${filename}_resp.xml") {
74     warn "failed to extract ${filename}_resp.xml from ${filename}_resp.zip\n";
75     next BATCH;
76   }
77
78   #copy to archive dir
79   if ( $opt_a ) {
80     print STDERR "Copying $tmpdir/${filename}_resp.xml to archive dir $opt_a\n"
81       if $opt_v;
82     system 'cp', "$tmpdir/${filename}_resp.xml", $opt_a;
83     warn "failed to copy $tmpdir/${filename}_resp.xml to $opt_a: $@" if $@;
84   }
85
86   #get batchnum & retrieve pending batch
87   open my $fh, "<$tmpdir/${filename}_resp.xml";
88   my ($batchnum) = split ('-', $filename); 
89   $batchnum = sprintf("%d", $batchnum); # remove leading zeroes
90   my $batch = qsearchs('pay_batch', { batchnum => $batchnum });
91   if(! $batch) {
92     warn "batch '$batchnum' not found\n";
93     next BATCH;
94   }
95
96   #and import results
97   print STDERR "Importing batch #$batchnum\n" if $opt_v;
98   my $error = $batch->import_results( filehandle => $fh,
99                                       format     => 'paymentech' );
100   warn "error: $error\n" if $error;
101
102 }
103
104 print STDERR "Finished!\n" if $opt_v;
105
106 =head1 NAME
107
108 freeside-paymentech-download - Retrieve payment batch responses from Chase Paymentech.
109
110 =head1 SYNOPSIS
111
112   paymentech-download [ -v ] [ -t ] [ -a archivedir ] user
113
114 =head1 DESCRIPTION
115
116 Command line tool to download payment batch responses from the Chase Paymentech
117 gateway.  These are XML files packaged in ZIP files.  This script downloads them 
118 by SFTP, extracts the contents, and passes them to L<FS::pay_batch::import_result>.
119
120 -v: Be verbose.
121
122 -t: Use the test server.
123
124 -a directory: Archive response files in the provided directory.
125
126 user: freeside username
127
128 =head1 BUGS
129
130 =head1 SEE ALSO
131
132 L<FS::pay_batch>
133
134 =cut
135
136 1;
137