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);
14 use vars qw( $opt_a $opt_v );
17 #$Net::SFTP::Foreign::debug = -1;
21 freeside-eftca-upload [ -v ] user batchnum
22 freeside-eftca-upload -a [ -v ] user\n
25 my $user = shift or die &HELP_MESSAGE;
28 my $log = FS::Log->new('freeside-eftca-upload');
29 log_info( "EFT Canada upload started\n" );
36 my %criteria= ( 'status' => 'O', 'payby' => 'CHEK' );
37 $criteria{'type'} = 'DEBIT' unless FS::pay_batch->can_handle_electronic_refunds('eft_canada');
40 @batches = qsearch('pay_batch', \%criteria)
42 log_error_and_die ("Fatal database error: $@")
45 log_info_and_die( "Finished: No open batches found.\n" )
50 die &HELP_MESSAGE if !$batchnum;
53 eval { @batches = qsearchs('pay_batch', { batchnum => $batchnum } ); };
54 log_error_and_die("Fatal database error: $@")
57 log_error_and_die( "Can't find payment batch '$batchnum'\n" ) if !@batches;
60 my $conf = new FS::Conf;
62 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
64 foreach my $pay_batch (@batches) {
66 my $batchnum = $pay_batch->batchnum;
67 my $filename = time2str('%Y%m%d', time) . '-' . sprintf('%06d.csv',$batchnum);
68 log_info( "Exporting batch $batchnum to $filename...\n" );
69 my $text = $pay_batch->export_batch(format => 'eft_canada');
71 log_info( "Batch is empty, resolving..." );
74 open OUT, ">$tmpdir/$filename";
78 my @batchconf = $conf->config('batchconfig-eft_canada', $pay_batch->agentnum);
79 my $user = $batchconf[0]
80 or log_error_and_die( "no EFT Canada batch username configured\n" );
81 my $pass = $batchconf[1]
82 or log_error_and_die( "no EFT Canada batch password configured\n" );
84 my $host = 'ftp.eftcanada.com';
85 log_info( "Connecting to $user\@$host...\n" );
87 my $sftp = Net::SFTP::Foreign->new( host => $host,
92 log_error_and_die("failed to connect to '$user\@$host'\n(".$sftp->error.")\n")
95 $sftp->put("$tmpdir/$filename", "$filename")
96 or log_error_and_die( "failed to upload file (".$sftp->error.")\n" );
98 undef $sftp; #$sftp->disconnect;
100 # Auto-approve and close the batch. Some false laziness with manual_approve.
101 my $batchnum = $pay_batch->batchnum;
106 foreach my $cpb ( qsearch('cust_pay_batch', { 'batchnum' => $batchnum } )) {
107 $cpb->setfield('paid', $cpb->amount);
108 $error = $cpb->approve($batchnum);
112 log_error_and_die("Fatal database error: $@")
115 $error ||= $pay_batch->set_status('R');
116 log_error_and_die( "error closing batch $batchnum: $error\n\n" )
120 log_info( "Finished!\n" );
123 my $log_message = shift;
124 $log->info( $log_message );
125 print STDERR $log_message if $opt_v;
128 sub log_info_and_die {
129 my $log_message = shift;
130 $log->info( $log_message );
134 sub log_error_and_die {
135 my $log_message = shift;
136 $log->error( $log_message );
142 freeside-eftca-upload - Transmit a payment batch to EFT Canada via SFTP.
146 freeside-paymentech-upload [ -a ] [ -v ] user batchnum
150 Command line tool to upload a payment batch to the EFT Canada gateway. The
151 batch will be exported to a comma-delimited file and transmitted via SFTP.
152 Use L<freeside-eftca-download> to retrieve the response file.
154 -a: Send all open batches, instead of specifying a batchnum.
158 user: freeside username
160 batchnum: pay_batch primary key