Scripts for paymentech batch transfer
[freeside.git] / bin / paymentech-download
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use Net::SFTP::Foreign;
6 use FS::UID qw(adminsuidsetup datasrc);
7 use FS::Record qw(qsearch qsearchs);
8 use FS::pay_batch;
9 use FS::cust_pay_batch;
10 use FS::Conf;
11
12 use Date::Format 'time2str';
13 use File::Temp;
14
15 use vars qw( $opt_t $opt_v );
16 getopts('vt');
17
18 #$Net::SFTP::Foreign::debug = -1;
19 sub usage { '
20   Usage:
21       paymentech-download [ -v ] [ -t ] user
22
23 '
24 }
25
26 my $user = shift or die &usage;
27 adminsuidsetup $user;
28
29 my $tmpdir = File::Temp->newdir();
30
31 my $conf = new FS::Conf;
32 my @batchconf = $conf->config('batchconfig-paymentech');
33 # BIN, terminalID, merchantID, username, password
34 my $username = $batchconf[3] or die "no Paymentech batch username configured\n";
35 my $password = $batchconf[4] or die "no Paymentech batch password configured\n";
36
37 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net' : 'orbitalbatch.paymentech.net');
38 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
39
40 my $sftp = Net::SFTP::Foreign->new( host => $host,
41                                     user => $username,
42                                     password => $password,
43                                     timeout => 30,
44                                     );
45 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" if $sftp->error;
46
47 my @files = map { $_->{filename} } @{ $sftp->ls('.', wanted => qr/_resp\.zip$/) };
48 die "no response files found\n" if !@files;
49
50 BATCH: foreach my $filename (@files) {
51   $filename =~ s/\_resp\.zip$//;
52   print STDERR "Retrieving $filename\n" if $opt_v;
53   $sftp->get("$filename\_resp.zip", "$tmpdir/$filename\_resp.zip");
54   if($sftp->error) {
55     warn "failed to download $filename\n";
56     next BATCH;
57   }
58   system("unzip -P $password -q $tmpdir/$filename\_resp.zip -d $tmpdir");
59   if(! -f "$tmpdir/$filename\_resp.xml") {
60     warn "failed to extract $filename\_resp.xml\n";
61     next BATCH;
62   }
63   open my $fh, "<$tmpdir/$filename\_resp.xml";
64   my ($batchnum) = split ('-', $filename); 
65   $batchnum = sprintf("%d", $batchnum); # remove leading zeroes
66   my $batch = qsearchs('pay_batch', { batchnum => $batchnum });
67   if(! $batch) {
68     warn "batch '$batchnum' not found\n";
69     next BATCH;
70   }
71   print STDERR "Importing batch #$batchnum\n" if $opt_v;
72   my $error = $batch->import_results( filehandle => $fh,
73                                       format     => 'paymentech' );
74   warn "error: $error\n" if $error;
75 };
76 print STDERR "Finished!\n" if $opt_v;
77
78 =head1 NAME
79
80 paymentech-download
81
82 paymentech-download - Retrieve payment batch responses from Chase Paymentech.
83
84 =head1 SYNOPSIS
85
86   paymentech-download [ -v ] [ -t ] user
87
88 =head1 DESCRIPTION
89
90 Command line tool to download payment batch responses from the Chase Paymentech
91 gateway.  These are XML files packaged in ZIP files.  This script downloads them 
92 by SFTP, extracts the contents, and passes them to L<FS::pay_batch::import_result>.
93
94 -v: Be verbose.
95
96 -t: Use the test server.
97
98 user: freeside username
99
100 =head1 BUGS
101
102 =head1 SEE ALSO
103
104 L<FS::pay_batch>
105
106 =cut
107
108 1;
109