fix usage of File::Temp->newdir, not in 0.18 File::Temp on perl 5.10.0, 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 FS::UID qw(adminsuidsetup datasrc);
9 use FS::Record qw(qsearch qsearchs);
10 use FS::pay_batch;
11 use FS::cust_pay_batch;
12 use FS::Conf;
13
14 use vars qw( $opt_t $opt_v $opt_a );
15 getopts('vta:');
16
17 #$Net::SFTP::Foreign::debug = -1;
18 sub usage { "
19   Usage:
20       paymentech-download [ -v ] [ -t ] [ -a archivedir ] user\n
21 " }
22
23 my $user = shift or die &usage;
24 adminsuidsetup $user;
25
26 if ( $opt_a ) {
27   die "no such directory: $opt_a\n"
28     unless -d $opt_a;
29   die "archive directory $opt_a is not writable by the freeside user\n"
30     unless -w $opt_a;
31 }
32
33 #my $tmpdir = File::Temp->newdir();
34 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
35
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";
41
42 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net' : 'orbitalbatch.paymentech.net');
43 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
44
45 my $sftp = Net::SFTP::Foreign->new( host => $host,
46                                     user => $username,
47                                     password => $password,
48                                     timeout => 30,
49                                     );
50 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" if $sftp->error;
51
52 my @files = map { $_->{filename} } @{ $sftp->ls('.', wanted => qr/_resp\.zip$/) };
53 die "no response files found\n" if !@files;
54
55 BATCH: foreach my $filename (@files) {
56
57   #get file
58   $filename =~ s/_resp\.zip$//;
59   print STDERR "Retrieving $filename\n" if $opt_v;
60   $sftp->get("$filename\_resp.zip", "$tmpdir/${filename}_resp.zip");
61   if($sftp->error) {
62     warn "failed to download $filename\n";
63     next BATCH;
64   }
65
66   #unzip file
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";
70     next BATCH;
71   }
72
73   #copy to archive dir
74   if ( $opt_a ) {
75     print STDERR "Copying $tmpdir/${filename}_resp.xml to archive dir $opt_a\n"
76       if $opt_v;
77     system 'cp', "$tmpdir/${filename}_resp.xml", $opt_a;
78     warn "failed to copy $tmpdir/${filename}_resp.xml to $opt_a: $@" if $@;
79   }
80
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 });
86   if(! $batch) {
87     warn "batch '$batchnum' not found\n";
88     next BATCH;
89   }
90
91   #and import results
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;
96
97 }
98
99 print STDERR "Finished!\n" if $opt_v;
100
101 =head1 NAME
102
103 paymentech-download
104
105 paymentech-download - Retrieve payment batch responses from Chase Paymentech.
106
107 =head1 SYNOPSIS
108
109   paymentech-download [ -v ] [ -t ] [ -a archivedir ] user
110
111 =head1 DESCRIPTION
112
113 Command line tool to download payment batch responses from the Chase Paymentech
114 gateway.  These are XML files packaged in ZIP files.  This script downloads them 
115 by SFTP, extracts the contents, and passes them to L<FS::pay_batch::import_result>.
116
117 -v: Be verbose.
118
119 -t: Use the test server.
120
121 -a directory: Archive response files in the provided directory.
122
123 user: freeside username
124
125 =head1 BUGS
126
127 =head1 SEE ALSO
128
129 L<FS::pay_batch>
130
131 =cut
132
133 1;
134