Scripts for paymentech batch transfer
[freeside.git] / bin / paymentech-upload
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
20 sub usage { '
21   Usage:
22     paymentech-upload [ -v ] [ -t ] user batchnum
23
24 '
25 }
26
27 my $user = shift or die &usage;
28 adminsuidsetup $user;
29
30 my $batchnum = shift or die &usage;
31 my $pay_batch = qsearchs('pay_batch', { batchnum => $batchnum })
32   or die "can't find payment batch '$batchnum'\n";
33
34 my $filename = sprintf('%06d',$batchnum) . '-' .time2str('%Y%m%d%H%M%S', time);
35 print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
36 my $tmpdir = File::Temp->newdir();
37 my $text = $pay_batch->export_batch('paymentech');
38 $text =~ s!<fileID>FILEID</fileID>!<fileID>$filename</fileID>! 
39   or die "couldn't find FILEID tag\n";
40 open OUT, ">$tmpdir/$filename.xml";
41 print OUT $text;
42 close OUT;
43
44 my $conf = new FS::Conf;
45 my @batchconf = $conf->config('batchconfig-paymentech');
46 # BIN, terminalID, merchantID, username, password
47 my $username = $batchconf[3] or die "no Paymentech batch username configured\n";
48 my $password = $batchconf[4] or die "no Paymentech batch password configured\n";
49
50 # This is less than ideal.
51 system("zip -P $password -q -j $tmpdir/$filename.zip $tmpdir/$filename.xml");
52
53 die "failed to create zip file\n" if (! -f "$tmpdir/$filename.zip" );
54
55
56 #my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net' : 'orbitalbatch.paymentech.net');
57 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net' : '127.0.0.1');
58 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
59
60 my $sftp = Net::SFTP::Foreign->new( host => $host,
61                                     user => $username,
62                                     password => $password,
63                                     timeout => 30,
64                                     );
65 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n" if $sftp->error;
66
67 $sftp->put("$tmpdir/$filename.zip", "$filename.zip")
68   or die "failed to upload file (".$sftp->error.")\n";
69
70 print STDERR "Finished!\n" if $opt_v;
71
72 =head1 NAME
73
74 paymentech-upload
75
76 paymentech-upload - Transmit a payment batch to Chase Paymentech via SFTP.
77
78 =head1 SYNOPSIS
79
80   paymentech-upload [ -v ] [ -t ] user batchnum
81
82 =head1 DESCRIPTION
83
84 Command line tool to upload a payment batch to the Chase Paymentech gateway.
85 The batch will be exported to the Paymentech XML format, packaged in a ZIP 
86 file, and transmitted via SFTP.  Use L<paymentech-download> to retrieve the 
87 response file.
88
89 -v: Be verbose.
90
91 -t: Send the transaction to the test server.
92
93 user: freeside username
94
95 batchnum: pay_batch primary key
96
97 =head1 BUGS
98
99 Passing the zip password on the command line is slightly risky.
100
101 =head1 SEE ALSO
102
103 L<FS::pay_batch>
104
105 =cut
106
107 1;
108