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);
11 use FS::cust_pay_batch;
14 use vars qw( $opt_t $opt_v $opt_a );
17 #$Net::SFTP::Foreign::debug = -1;
20 freeside-paymentech-download [ -v ] [ -t ] [ -a archivedir ] user\n
23 my $user = shift or die &usage;
27 die "no such directory: $opt_a\n"
29 die "archive directory $opt_a is not writable by the freeside user\n"
33 #my $tmpdir = File::Temp->newdir();
34 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
36 my $conf = new FS::Conf;
37 my @batchconf = $conf->config('batchconfig-paymentech');
38 # BIN, terminalID, merchantID, username, password
39 my $username = $batchconf[3] or die "no Paymentech batch username configured\n";
40 my $password = $batchconf[4] or die "no Paymentech batch password configured\n";
42 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net' : 'orbitalbatch.paymentech.net');
43 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
45 my $sftp = Net::SFTP::Foreign->new( host => $host,
47 password => $password,
50 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" if $sftp->error;
52 my @files = map { $_->{filename} } @{ $sftp->ls('.', wanted => qr/_resp\.zip$/) };
53 die "no response files found\n" if !@files;
55 BATCH: foreach my $filename (@files) {
58 $filename =~ s/_resp\.zip$//;
59 print STDERR "Retrieving $filename\n" if $opt_v;
60 $sftp->get("$filename\_resp.zip", "$tmpdir/${filename}_resp.zip");
62 warn "failed to download $filename\n";
67 system("unzip -P $password -q $tmpdir/${filename}_resp.zip -d $tmpdir");
68 if(! -f "$tmpdir/${filename}_resp.xml") {
69 warn "failed to extract ${filename}_resp.xml\n";
75 print STDERR "Copying $tmpdir/${filename}_resp.xml to archive dir $opt_a\n"
77 system 'cp', "$tmpdir/${filename}_resp.xml", $opt_a;
78 warn "failed to copy $tmpdir/${filename}_resp.xml to $opt_a: $@" if $@;
81 #get batchnum & retrieve pending batch
82 open my $fh, "<$tmpdir/${filename}_resp.xml";
83 my ($batchnum) = split ('-', $filename);
84 $batchnum = sprintf("%d", $batchnum); # remove leading zeroes
85 my $batch = qsearchs('pay_batch', { batchnum => $batchnum });
87 warn "batch '$batchnum' not found\n";
92 print STDERR "Importing batch #$batchnum\n" if $opt_v;
93 my $error = $batch->import_results( filehandle => $fh,
94 format => 'paymentech' );
95 warn "error: $error\n" if $error;
99 print STDERR "Finished!\n" if $opt_v;
103 freeside-paymentech-download - Retrieve payment batch responses from Chase Paymentech.
107 paymentech-download [ -v ] [ -t ] [ -a archivedir ] user
111 Command line tool to download payment batch responses from the Chase Paymentech
112 gateway. These are XML files packaged in ZIP files. This script downloads them
113 by SFTP, extracts the contents, and passes them to L<FS::pay_batch::import_result>.
117 -t: Use the test server.
119 -a directory: Archive response files in the provided directory.
121 user: freeside username