5 use Date::Format qw(time2str);
6 use File::Temp qw(tempdir);
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_v $opt_a );
19 #$Net::SFTP::Foreign::debug = -1;
22 freeside-eftca-download [ -v ] [ -a archivedir ] user\n
26 'tid', # transaction ID
27 'paybatchnum', # reference field
28 'returncode', # status code
30 'paid', # dollars and cents, with decimal
39 my $user = shift or die &HELP_MESSAGE;
42 my $log = FS::Log->new('freeside-eftca-download');
43 log_info( "EFT Canada download started\n" );
46 log_error_and_die( "no such directory: $opt_a\n" )
49 "archive directory $opt_a is not writable by the freeside user\n"
53 #my $tmpdir = File::Temp->newdir();
54 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
56 my $conf = new FS::Conf;
59 if ( $conf->exists('batch-spoolagent') ) {
61 eval { @agents = qsearch('agent', { 'disabled' => '' }); };
62 log_error_and_die("Fatal database error: $@")
68 foreach my $agent (@agents) {
71 if ( $conf->exists('batch-spoolagent') ) {
72 @batchconf = $conf->config('batchconfig-eft_canada', $agent->agentnum, 1);
73 if ( !length($batchconf[0]) ) {
75 "agent '".$agent->agent.
76 "' has no batchconfig-eft_canada setting; skipped.\n"
81 @batchconf = $conf->config('batchconfig-eft_canada');
83 # user, password, transaction code, delay days
84 my $user = $batchconf[0]
85 or log_error_and_die( "no EFT Canada batch username configured\n" );
86 my $pass = $batchconf[1]
87 or log_error_and_die( "no EFT Canada batch password configured\n" );
89 my $host = 'ftp.eftcanada.com';
90 log_info( "Connecting to $user\@$host...\n" );
92 my $sftp = Net::SFTP::Foreign->new( host => $host,
97 log_error_and_die("failed to connect to '$user\@$host'\n(".$sftp->error.")\n")
100 $sftp->setcwd('/Returns');
102 my $files = $sftp->ls('.', wanted => qr/\.txt$/, names_only => 1);
103 log_info_and_die( "Finished: No response files found\n" )
106 FILE: foreach my $filename (@$files) {
107 log_info( "Retrieving $filename\n" );
108 $sftp->get("$filename", "$tmpdir/$filename");
110 log_info( "failed to download $filename\n" );
114 #move to server archive dir
115 $sftp->rename("$filename", "Archive/$filename");
117 log_info( "failed to archive $filename on server\n" );
118 } # process it anyway though
120 #copy to local archive dir
122 log_info( "Copying $tmpdir/$filename to archive dir $opt_a\n" );
123 system 'cp', "$tmpdir/$filename", $opt_a;
124 log_info( "failed to copy $tmpdir/$filename to $opt_a: $@" )
128 open my $fh, "<$tmpdir/$filename";
129 # Some duplication with FS::pay_batch::import_results, but we're really
130 # doing something different here.
131 my $csv = new Text::CSV_XS ( { quote_char => undef, sep_char => '|' } );
133 while (my $line = <$fh>) {
134 next if $line =~ /^\s*$/;
135 $csv->parse($line) or do {
136 log_info( "can't parse $filename: ".$csv->error_input."\n" );
137 next FILE; #parsing errors = reading the wrong kind of file
139 @hash{@fields} = $csv->fields();
140 log_info( "voiding paybatchnum#$hash{paybatchnum}\n" );
141 my $cpb = qsearchs('cust_pay_batch',
142 { paybatchnum => $hash{'paybatchnum'} });
145 "can't find paybatchnum #$hash{paybatchnum} ".
146 "($hash{first} $hash{last}, $hash{paid})\n"
150 my $error = $cpb->decline("Returned payment ($hash{returncode})");
152 log_info( "can't void paybatchnum #$hash{paybatchnum}: $error\n" );
160 log_info( "Finished!\n" );
163 my $log_message = shift;
164 $log->info( $log_message );
165 print STDERR $log_message if $opt_v;
168 sub log_info_and_die {
169 my $log_message = shift;
170 $log->info( $log_message );
174 sub log_error_and_die {
175 my $log_message = shift;
176 $log->error( $log_message );
182 freeside-eftca-download - Retrieve payment batch responses from EFT Canada.
186 freeside-eftca-download [ -v ] [ -a archivedir ] user
190 Command line tool to download returned payment reports from the EFT Canada
191 gateway and void the returned payments. Uses the login and password from
192 'batchconfig-eft_canada'.
196 -a directory: Archive response files in the provided directory.
198 user: freeside username
202 You need to manually SFTP to ftp.eftcanada.com from the freeside account
203 and accept their key before running this script.