default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / bin / freeside-paymentech-download
index 48dfe9b..d300f3e 100755 (executable)
@@ -2,57 +2,77 @@
 
 use strict;
 use Getopt::Std;
+use Date::Format qw(time2str);
+use File::Temp qw(tempdir); #0.19 for ->newdir() interface, not in 5.10.0
 use Net::SFTP::Foreign;
+use Expect;
 use FS::UID qw(adminsuidsetup datasrc);
 use FS::Record qw(qsearch qsearchs);
 use FS::pay_batch;
 use FS::cust_pay_batch;
 use FS::Conf;
-
-use Date::Format 'time2str';
-use File::Temp;
+use FS::Log;
 
 use vars qw( $opt_t $opt_v $opt_a );
 getopts('vta:');
 
 #$Net::SFTP::Foreign::debug = -1;
-sub usage { '
-  Usage:
-      paymentech-download [ -v ] [ -t ] [ -a archivedir ] user
 
-'
+sub log_and_die {
+  my $message = shift;
+  my $log = FS::Log->new('freeside-paymentech-download');
+  $log->error($message);
+  die $message; 
 }
 
+sub usage { "
+  Usage:
+      freeside-paymentech-download [ -v ] [ -t ] [ -a archivedir ] user\n
+" }
+
 my $user = shift or die &usage;
 adminsuidsetup $user;
 
 if ( $opt_a ) {
-  die "no such directory: $opt_a\n"
+  log_and_die("no such directory: $opt_a\n")
     unless -d $opt_a;
-  die "archive directory $opt_a is not writable by the freeside user\n"
+  log_and_die("archive directory $opt_a is not writable by the freeside user\n")
     unless -w $opt_a;
 }
 
-my $tmpdir = File::Temp->newdir();
+my $unzip_check = `which unzip` or log_and_die("can't find unzip executable\n");
+
+#my $tmpdir = File::Temp->newdir();
+my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
 
 my $conf = new FS::Conf;
 my @batchconf = $conf->config('batchconfig-paymentech');
 # BIN, terminalID, merchantID, username, password
-my $username = $batchconf[3] or die "no Paymentech batch username configured\n";
-my $password = $batchconf[4] or die "no Paymentech batch password configured\n";
+my $username = $batchconf[3] or log_and_die("no Paymentech batch username configured\n");
+my $password = $batchconf[4] or log_and_die("no Paymentech batch password configured\n");
 
-my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net' : 'orbitalbatch.paymentech.net');
+my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net'
+                   : 'orbitalbatch.paymentech.net');
 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
 
-my $sftp = Net::SFTP::Foreign->new( host => $host,
-                                    user => $username,
-                                    password => $password,
-                                    timeout => 30,
-                                    );
-die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" if $sftp->error;
+my $sftp;
+my $ssh_retry      = 25;   # number of times to try connection, needs to be >= 1
+my $ssh_retry_wait = 60*5; # seconds to wait between tries
+while ($ssh_retry > 0) {
+  $sftp = Net::SFTP::Foreign->new( host => $host,
+                                   user => $username,
+                                   password => $password,
+                                   timeout => 300,
+                                 );
+  last unless $sftp->error;
+  $ssh_retry -= 1;
+  sleep($ssh_retry_wait) if $ssh_retry > 0;
+}
+
+log_and_die("failed to connect to '$username\@$host'\n(".$sftp->error.")\n") if $sftp->error;
 
 my @files = map { $_->{filename} } @{ $sftp->ls('.', wanted => qr/_resp\.zip$/) };
-die "no response files found\n" if !@files;
+log_and_die("no response files found\n") if !@files;
 
 BATCH: foreach my $filename (@files) {
 
@@ -66,9 +86,10 @@ BATCH: foreach my $filename (@files) {
   }
 
   #unzip file
-  system("unzip -P $password -q $tmpdir/${filename}_resp.zip -d $tmpdir");
+  system('unzip', '-P', $password, '-q',
+           "$tmpdir/${filename}_resp.zip", '-d', $tmpdir);
   if(! -f "$tmpdir/${filename}_resp.xml") {
-    warn "failed to extract ${filename}_resp.xml\n";
+    warn "failed to extract ${filename}_resp.xml from ${filename}_resp.zip\n";
     next BATCH;
   }
 
@@ -102,9 +123,7 @@ print STDERR "Finished!\n" if $opt_v;
 
 =head1 NAME
 
-paymentech-download
-
-paymentech-download - Retrieve payment batch responses from Chase Paymentech.
+freeside-paymentech-download - Retrieve payment batch responses from Chase Paymentech.
 
 =head1 SYNOPSIS
 
@@ -114,7 +133,7 @@ paymentech-download - Retrieve payment batch responses from Chase Paymentech.
 
 Command line tool to download payment batch responses from the Chase Paymentech
 gateway.  These are XML files packaged in ZIP files.  This script downloads them 
-by SFTP, extracts the contents, and passes them to L<FS::pay_batch::import_result>.
+by SFTP, extracts the contents, and passes them to L<FS::pay_batch/import_results>.
 
 -v: Be verbose.