5 use Date::Format qw(time2str);
6 use File::Temp qw(tempdir); #0.19 for ->newdir() interface, not in 5.10.0
8 use FS::UID qw(adminsuidsetup dbh);
9 use FS::Record qw(qsearch qsearchs);
13 use vars qw( $opt_v $opt_a $opt_f );
16 #$Net::SFTP::Foreign::debug = -1;
19 freeside-rbc-download [ -v ] [ -a archivedir ] [ -f filename ] user\n
23 print STDERR $_[0] if $opt_v;
26 my $user = shift or die &usage;
29 $FS::UID::AutoCommit = 0;
33 die "no such directory: $opt_a\n"
35 die "archive directory $opt_a is not writable by the freeside user\n"
39 my $tmpdir = tempdir( CLEANUP => 1 ); #DIR=>somewhere?
41 my $conf = new FS::Conf;
42 my ($username, $password) = $conf->config('batchconfig-RBC-login');
43 $username and $password
44 or die "RBC FTP login not configured. Enter your username and password in 'batchconfig-rbc-login'.\n";
46 my $host = 'ftpssl.rbc.com';
47 debug "Connecting to $username\@$host...\n";
49 my $ftp = Net::FTPSSL->new($host,
51 Debug => ($opt_v ? 1 : 0),
52 Croak => 1, # rely on auto-rollback when dbh closes
54 $ftp->login($username, $password);
59 # ~/inbound/valid # batches move here while being processed
61 # ~/outbound/XXXX # string of four characters; results arrive here
63 $ftp->cwd('outbound');
64 for my $dir ( $ftp->nlst ) {
65 debug "Entering outbound/$dir\n";
67 FILE: for my $filename ( $ftp->nlst ) {
69 # filenames look like "RPT9999X.111".
70 # 9999 is the four-digit report type
71 # X is "P" for production or "T" for test
72 # 111 is the sequential file number
74 if ( $filename ne $opt_f ) {
75 debug "is not the requested file.\n";
78 # -f can be used to download/process any file, even one that doesn't fit
79 # the naming rule (e.g. those that are already downloaded).
80 } elsif ( $filename =~ /^RPT(\d{4})[PT]\.\d{3}$/ ) {
81 # fallthrough; don't currently reject files based on RPT type, because
82 # our parser should be able to figure it out
88 debug "downloading.\n";
89 $ftp->get($filename, "$tmpdir/$filename");
93 debug "Copying to archive dir $opt_a\n";
94 system 'cp', "$tmpdir/$filename", $opt_a;
95 warn "failed to copy $tmpdir/$filename to $opt_a: $!\n" if $!;
98 debug "Processing batch...";
99 open(my $fh, '<', "$tmpdir/$filename")
100 or die "couldn't read temp file: $!\n";
102 my $error = FS::pay_batch->import_results(
108 die "Processing $filename failed:\n$error\n\n";
122 freeside-rbc-download - Retrieve payment batch responses from RBC.
126 freeside-rbc-download [ -v ] [ -f filename ] [ -a archivedir ] user
130 Command line tool to download payment batch responses from the Royal Bank of
131 Canada ACH service. These files are fixed-width data files containing some
132 combination of valid, returned, or reversed payment records.
134 By default, the script will download any files with names like "RPT9999X.111"
135 where 9999 is a four-digit document type code (like "0900", all records), X is
136 the letter "P" for production or "T" for test mode, and 111 is a counter
137 incremented with each new response file. After the files are downloaded, RBC's
138 server will automatically rename them with the suffix '.downloaded%FTPS' to
139 avoid double-processing them.
144 -f filename: Download a file with a specific name, instead of all files
145 matching the pattern. This can be used to reprocess a specific file.
147 -a directory: Archive the files in the specified directory.
149 user: freeside username