5 use Date::Format qw(time2str);
6 use File::Temp qw(tempdir);
7 use Net::SFTP::Foreign;
8 use FS::UID qw(adminsuidsetup dbh);
9 use FS::Record qw(qsearch qsearchs);
13 use vars qw( $opt_a $opt_v );
16 #$Net::SFTP::Foreign::debug = -1;
20 freeside-eftca-upload [ -v ] user batchnum
21 freeside-eftca-upload -a [ -v ] user\n
24 my $user = shift or die &HELP_MESSAGE;
30 @batches = qsearch('pay_batch', { 'status' => 'O', 'payby' => 'CHEK' })
31 or die "No open batches found.\n";
35 die &HELP_MESSAGE if !$batchnum;
36 @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
37 die "Can't find payment batch '$batchnum'\n" if !@batches;
40 my $conf = new FS::Conf;
42 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
44 foreach my $pay_batch (@batches) {
46 my $batchnum = $pay_batch->batchnum;
47 my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
48 print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
49 my $text = $pay_batch->export_batch(format => 'eft_canada');
50 open OUT, ">$tmpdir/$filename";
54 my @batchconf = $conf->config('batchconfig-eft_canada', $pay_batch->agentnum);
55 my $user = $batchconf[0] or die "no EFT Canada batch username configured\n";
56 my $pass = $batchconf[1] or die "no EFT Canada batch password configured\n";
58 my $host = 'ftp.eftcanada.com';
59 print STDERR "Connecting to $user\@$host...\n" if $opt_v;
61 my $sftp = Net::SFTP::Foreign->new( host => $host,
66 die "failed to connect to '$user\@$host'\n(".$sftp->error.")\n"
69 $sftp->put("$tmpdir/$filename", "$filename")
70 or die "failed to upload file (".$sftp->error.")\n";
72 undef $sftp; #$sftp->disconnect;
74 # Auto-approve and close the batch. Some false laziness with manual_approve.
75 my $batchnum = $pay_batch->batchnum;
77 foreach my $cpb ( qsearch('cust_pay_batch', { 'batchnum' => $batchnum } ) ) {
78 $cpb->setfield('paid', $cpb->amount);
79 $error = $cpb->approve($batchnum);
82 $error ||= $pay_batch->set_status('R');
83 die "error closing batch $batchnum: $error\n\n" if $error;
86 print STDERR "Finished!\n" if $opt_v;
90 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
94 freeside-paymentech-upload [ -a ] [ -v ] user batchnum
98 Command line tool to upload a payment batch to the EFT Canada gateway. The
99 batch will be exported to a comma-delimited file and transmitted via SFTP.
100 Use L<freeside-eftca-download> to retrieve the response file.
102 -a: Send all open batches, instead of specifying a batchnum.
106 user: freeside username
108 batchnum: pay_batch primary key