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;
15 use vars qw( $opt_v $opt_a );
18 #$Net::SFTP::Foreign::debug = -1;
21 freeside-eftca-download [ -v ] [ -a archivedir ] user\n
25 'tid', # transaction ID
26 'paybatchnum', # reference field
27 'returncode', # status code
29 'paid', # dollars and cents, with decimal
38 my $user = shift or die &HELP_MESSAGE;
42 die "no such directory: $opt_a\n"
44 die "archive directory $opt_a is not writable by the freeside user\n"
48 #my $tmpdir = File::Temp->newdir();
49 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
51 my $conf = new FS::Conf;
54 if ( $conf->exists('batch-spoolagent') ) {
55 @agents = qsearchs('agent', { 'disabled' => '' });
60 foreach my $agent (@agents) {
63 if ( $conf->exists('batch-spoolagent') ) {
64 @batchconf = $conf->config('batchconfig-eft_canada', $agent->agentnum, 1);
65 next unless $batchconf[0];
67 @batchconf = $conf->config('batchconfig-eft_canada');
69 # BIN, terminalID, merchantID, username, password
70 my $user = $batchconf[0] or die "no EFT Canada batch username configured\n";
71 my $pass = $batchconf[1] or die "no EFT Canada batch password configured\n";
73 my $host = 'ftp.eftcanada.com';
74 print STDERR "Connecting to $user\@$host...\n" if $opt_v;
76 my $sftp = Net::SFTP::Foreign->new( host => $host,
81 die "failed to connect to '$user\@$host'\n(".$sftp->error.")\n" if $sftp->error;
83 $sftp->setcwd('/Returns');
85 my $files = $sftp->ls('.', wanted => qr/\.txt$/, names_only => 1);
86 die "no response files found\n" if !@$files;
88 FILE: foreach my $filename (@$files) {
89 print STDERR "Retrieving $filename\n" if $opt_v;
90 $sftp->get("$filename", "$tmpdir/$filename");
92 warn "failed to download $filename\n";
96 #move to server archive dir
97 $sftp->rename("$filename", "Archive/$filename");
99 warn "failed to archive $filename on server\n";
100 } # process it anyway though
102 #copy to local archive dir
104 print STDERR "Copying $tmpdir/$filename to archive dir $opt_a\n"
106 system 'cp', "$tmpdir/$filename", $opt_a;
107 warn "failed to copy $tmpdir/$filename to $opt_a: $@" if $@;
110 open my $fh, "<$tmpdir/$filename";
111 # Some duplication with FS::pay_batch::import_results, but we're really
112 # doing something different here.
113 my $csv = new Text::CSV_XS ( { quote_char => undef, sep_char => '|' } );
115 while (my $line = <$fh>) {
116 next if $line =~ /^\s*$/;
117 $csv->parse($line) or do {
118 warn "can't parse $filename: ".$csv->error_input."\n";
119 next FILE; #parsing errors = reading the wrong kind of file
121 @hash{@fields} = $csv->fields();
122 print STDERR "voiding paybatchnum#$hash{paybatchnum}\n" if $opt_v;
123 my $cpb = qsearchs('cust_pay_batch',
124 { paybatchnum => $hash{'paybatchnum'} });
126 warn "can't find paybatchnum #$hash{paybatchnum} ($hash{first} $hash{last}, $hash{paid})\n";
129 my $error = $cpb->decline("Returned payment ($hash{returncode})");
131 warn "can't void paybatchnum #$hash{paybatchnum}: $error\n";
139 print STDERR "Finished!\n" if $opt_v;
143 freeside-eftca-download - Retrieve payment batch responses from EFT Canada.
147 freeside-eftca-download [ -v ] [ -a archivedir ] user
151 Command line tool to download returned payment reports from the EFT Canada
152 gateway and void the returned payments. Uses the login and password from
153 'batchconfig-eft_canada'.
157 -a directory: Archive response files in the provided directory.
159 user: freeside username
163 You need to manually SFTP to ftp.eftcanada.com from the freeside account
164 and accept their key before running this script.