5 use Net::SFTP::Foreign;
6 use FS::UID qw(adminsuidsetup datasrc);
7 use FS::Record qw(qsearch qsearchs);
9 use FS::cust_pay_batch;
12 use Date::Format 'time2str';
15 use vars qw( $opt_t $opt_v $opt_a );
18 #$Net::SFTP::Foreign::debug = -1;
21 paymentech-download [ -v ] [ -t ] [ -a archivedir ] user
26 my $user = shift or die &usage;
30 die "no such directory: $opt_a\n"
32 die "archive directory $opt_a is not writable by the freeside user\n"
36 my $tmpdir = File::Temp->newdir();
38 my $conf = new FS::Conf;
39 my @batchconf = $conf->config('batchconfig-paymentech');
40 # BIN, terminalID, merchantID, username, password
41 my $username = $batchconf[3] or die "no Paymentech batch username configured\n";
42 my $password = $batchconf[4] or die "no Paymentech batch password configured\n";
44 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net' : 'orbitalbatch.paymentech.net');
45 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
47 my $sftp = Net::SFTP::Foreign->new( host => $host,
49 password => $password,
52 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" if $sftp->error;
54 my @files = map { $_->{filename} } @{ $sftp->ls('.', wanted => qr/_resp\.zip$/) };
55 die "no response files found\n" if !@files;
57 BATCH: foreach my $filename (@files) {
60 $filename =~ s/_resp\.zip$//;
61 print STDERR "Retrieving $filename\n" if $opt_v;
62 $sftp->get("$filename\_resp.zip", "$tmpdir/${filename}_resp.zip");
64 warn "failed to download $filename\n";
69 system("unzip -P $password -q $tmpdir/${filename}_resp.zip -d $tmpdir");
70 if(! -f "$tmpdir/${filename}_resp.xml") {
71 warn "failed to extract ${filename}_resp.xml\n";
77 print STDERR "Copying $tmpdir/${filename}_resp.xml to archive dir $opt_a\n"
79 system 'cp', "$tmpdir/${filename}_resp.xml", $opt_a;
80 warn "failed to copy $tmpdir/${filename}_resp.xml to $opt_a: $@" if $@;
83 #get batchnum & retrieve pending batch
84 open my $fh, "<$tmpdir/${filename}_resp.xml";
85 my ($batchnum) = split ('-', $filename);
86 $batchnum = sprintf("%d", $batchnum); # remove leading zeroes
87 my $batch = qsearchs('pay_batch', { batchnum => $batchnum });
89 warn "batch '$batchnum' not found\n";
94 print STDERR "Importing batch #$batchnum\n" if $opt_v;
95 my $error = $batch->import_results( filehandle => $fh,
96 format => 'paymentech' );
97 warn "error: $error\n" if $error;
101 print STDERR "Finished!\n" if $opt_v;
107 paymentech-download - Retrieve payment batch responses from Chase Paymentech.
111 paymentech-download [ -v ] [ -t ] [ -a archivedir ] user
115 Command line tool to download payment batch responses from the Chase Paymentech
116 gateway. These are XML files packaged in ZIP files. This script downloads them
117 by SFTP, extracts the contents, and passes them to L<FS::pay_batch::import_result>.
121 -t: Use the test server.
123 -a directory: Archive response files in the provided directory.
125 user: freeside username