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;
9 use FS::UID qw(adminsuidsetup datasrc);
10 use FS::Record qw(qsearch qsearchs);
12 use FS::cust_pay_batch;
15 use vars qw( $opt_a $opt_t $opt_v $opt_p );
18 #$Net::SFTP::Foreign::debug = -1;
22 freeside-paymentech-upload [ -v ] [ -t ] user batchnum
23 freeside-paymentech-upload -a [ -p payby ] [ -v ] [ -t ] user\n
26 my $user = shift or die &usage;
29 my $zip_check = `which zip` or die "can't find zip executable\n";
34 my %criteria = (status => 'O');
35 $criteria{'payby'} = uc($opt_p) if $opt_p;
36 @batches = qsearch('pay_batch', \%criteria);
37 die "No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n"
42 die &usage if !$batchnum;
43 @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
44 die "Can't find payment batch '$batchnum'\n" if !@batches;
47 my $conf = new FS::Conf;
48 my @batchconf = $conf->config('batchconfig-paymentech');
49 # BIN, terminalID, merchantID, username, password
50 my $username = $batchconf[3] or die "no Paymentech batch username configured\n";
51 my $password = $batchconf[4] or die "no Paymentech batch password configured\n";
53 #my $tmpdir = File::Temp->newdir();
54 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
58 foreach my $pay_batch (@batches) {
59 my $batchnum = $pay_batch->batchnum;
60 my $filename = sprintf('%06d',$batchnum) . '-' .time2str('%Y%m%d%H%M%S', time);
61 print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
62 my $text = $pay_batch->export_batch(format => 'paymentech');
63 $text =~ s!<fileID>FILEID</fileID>!<fileID>$filename</fileID>!
64 or die "couldn't find FILEID tag\n";
65 open OUT, ">$tmpdir/$filename.xml";
69 system('zip', '-P', $password, '-q', '-j',
70 "$tmpdir/$filename.zip", "$tmpdir/$filename.xml");
72 die "failed to create zip file\n" if (! -f "$tmpdir/$filename.zip" );
73 push @filenames, $filename;
76 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net'
77 : 'orbitalbatch.paymentech.net');
78 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
80 my $sftp = Net::SFTP::Foreign->new( host => $host,
82 password => $password,
85 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n"
88 foreach my $filename (@filenames) {
89 $sftp->put("$tmpdir/$filename.zip", "$filename.zip")
90 or die "failed to upload file (".$sftp->error.")\n";
93 print STDERR "Finished!\n" if $opt_v;
97 freeside-paymentech-upload - Transmit a payment batch to Chase Paymentech via SFTP.
101 freeside-paymentech-upload [ -a [ -p PAYBY ] ] [ -v ] [ -t ] user batchnum
105 Command line tool to upload a payment batch to the Chase Paymentech gateway.
106 The batch will be exported to the Paymentech XML format, packaged in a ZIP
107 file, and transmitted via SFTP. Use L<paymentech-download> to retrieve the
110 -a: Send all open batches, instead of specifying a batchnum.
112 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
116 -t: Send the transaction to the test server.
118 user: freeside username
120 batchnum: pay_batch primary key
124 Passing the zip password on the command line is slightly risky.