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;
16 use vars qw( $opt_a $opt_t $opt_v $opt_p );
19 #$Net::SFTP::Foreign::debug = -1;
23 my $log = FS::Log->new('freeside-paymentech-upload');
24 $log->error($message);
30 freeside-paymentech-upload [ -v ] [ -t ] user batchnum
31 freeside-paymentech-upload -a [ -p payby ] [ -v ] [ -t ] user\n
34 my $user = shift or die &usage;
37 my $zip_check = `which zip` or log_and_die("can't find zip executable\n");
42 my %criteria = (status => 'O');
43 $criteria{'payby'} = uc($opt_p) if $opt_p;
46 $extra_sql = " AND ((payby = 'CHEK' AND type != 'CREDIT') OR (payby != 'CHEK'))" unless FS::pay_batch->can_handle_electronic_refunds('paymentech');
50 hashref => \%criteria,
51 extra_sql => $extra_sql,
54 @batches = qsearch(\%hash);
55 log_and_die("No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n")
60 log_and_die("batchnum not passed\n".&usage) if !$batchnum;
61 @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
62 log_and_die("Can't find payment batch '$batchnum'\n") if !@batches;
64 if ($batches[0]->type eq "CREDIT") {
65 warn "running credit\n";
66 log_and_die( "Batch number $batchnum is a credit (batch refund) batch, and this format can not handle batch refunds.\n" )
67 unless FS::pay_batch->can_handle_electronic_refunds('paymentech');
71 my $conf = new FS::Conf;
72 my @batchconf = $conf->config('batchconfig-paymentech');
73 # BIN, terminalID, merchantID, username, password
74 my $username = $batchconf[3] or log_and_die("no Paymentech batch username configured\n");
75 my $password = $batchconf[4] or log_and_die("no Paymentech batch password configured\n");
77 #my $tmpdir = File::Temp->newdir();
78 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
82 foreach my $pay_batch (@batches) {
83 my $batchnum = $pay_batch->batchnum;
84 my $filename = sprintf('%06d',$batchnum) . '-' .time2str('%Y%m%d%H%M%S', time);
85 print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
86 my $text = $pay_batch->export_batch(format => 'paymentech');
88 print STDERR "Batch is empty, resolving..." if $opt_v;
91 $text =~ s!<fileID>FILEID</fileID>!<fileID>$filename</fileID>!
92 or log_and_die("couldn't find FILEID tag\n");
93 open OUT, ">$tmpdir/$filename.xml";
97 system('zip', '-P', $password, '-q', '-j',
98 "$tmpdir/$filename.zip", "$tmpdir/$filename.xml");
100 log_and_die("failed to create zip file\n") if (! -f "$tmpdir/$filename.zip" );
101 push @filenames, $filename;
103 log_and_die("All batches empty\n") if !@filenames;
105 my $host = ($opt_t ? 'orbitalbatchvar.paymentech.net'
106 : 'orbitalbatch.paymentech.net');
107 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
110 my $ssh_retry = 25; # number of times to try connection, needs to be >= 1
111 my $ssh_retry_wait = 60*5; # seconds to wait between tries
112 while ($ssh_retry > 0) {
113 $sftp = Net::SFTP::Foreign->new( host => $host,
115 password => $password,
118 last unless $sftp->error;
120 sleep($ssh_retry_wait) if $ssh_retry > 0;
123 log_and_die("failed to connect to '$username\@$host'\n(".$sftp->error.")\n")
126 foreach my $filename (@filenames) {
127 $sftp->put("$tmpdir/$filename.zip", "$filename.zip")
128 or log_and_die("failed to upload file (".$sftp->error.")\n");
131 print STDERR "Finished!\n" if $opt_v;
135 freeside-paymentech-upload - Transmit a payment batch to Chase Paymentech via SFTP.
139 freeside-paymentech-upload [ -a [ -p PAYBY ] ] [ -v ] [ -t ] user batchnum
143 Command line tool to upload a payment batch to the Chase Paymentech gateway.
144 The batch will be exported to the Paymentech XML format, packaged in a ZIP
145 file, and transmitted via SFTP. Use L<freeside-paymentech-download> to retrieve the
148 -a: Send all open batches, instead of specifying a batchnum.
150 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
154 -t: Send the transaction to the test server.
156 user: freeside username
158 batchnum: pay_batch primary key
162 Passing the zip password on the command line is slightly risky.