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;
41 my @batchconf = $conf->config('batchconfig-eft_canada');
42 my $username = $batchconf[0] or die "no EFT Canada batch username configured\n";
43 my $password = $batchconf[1] or die "no EFT Canada batch password configured\n";
45 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
49 foreach my $pay_batch (@batches) {
50 my $batchnum = $pay_batch->batchnum;
51 my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
52 print STDERR "Exporting batch $batchnum to $filename...\n" if $opt_v;
53 my $text = $pay_batch->export_batch('eft_canada');
54 open OUT, ">$tmpdir/$filename";
57 push @filenames, $filename;
60 my $host = 'ftp.eftcanada.com';
61 print STDERR "Connecting to $username\@$host...\n" if $opt_v;
63 my $sftp = Net::SFTP::Foreign->new( host => $host,
65 password => $password,
68 die "failed to connect to '$username\@$host'\n(".$sftp->error.")\n"
71 foreach my $filename (@filenames) {
72 $sftp->put("$tmpdir/$filename", "$filename")
73 or die "failed to upload file (".$sftp->error.")\n";
76 $FS::UID::AutoCommit = 0;
77 foreach my $pay_batch (@batches) {
78 # Auto-approve and close the batch. Some false laziness with manual_approve.
79 my $batchnum = $pay_batch->batchnum;
81 foreach my $cpb ( qsearch('cust_pay_batch', { 'batchnum' => $batchnum } ) ) {
82 $cpb->setfield('paid', $cpb->amount);
83 $error = $cpb->approve($batchnum);
86 $error ||= $pay_batch->set_status('R');
87 die "error closing batch $batchnum: $error\n\n" if $error;
91 print STDERR "Finished!\n" if $opt_v;
95 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
99 freeside-paymentech-upload [ -a ] [ -v ] user batchnum
103 Command line tool to upload a payment batch to the EFT Canada gateway. The
104 batch will be exported to a comma-delimited file and transmitted via SFTP.
105 Use L<freeside-eftca-download> to retrieve the response file.
107 -a: Send all open batches, instead of specifying a batchnum.
111 user: freeside username
113 batchnum: pay_batch primary key