7 use File::Temp qw(tempdir);
8 use File::Slurp 'write_file';
9 use FS::UID qw(adminsuidsetup dbh);
10 use FS::Record qw(qsearch qsearchs);
14 use vars qw( $opt_a $opt_v $opt_p );
19 freeside-rbc-upload [ -v ] user batchnum
20 freeside-rbc-upload -a [ -p payby ] [ -v ] user\n
24 print STDERR $_[0] if $opt_v;
27 my $user = shift or die &usage;
32 # copied from freeside-paymentech-upload, obviously
34 my %criteria = (status => 'O');
35 $criteria{'payby'} = uc($opt_p) if $opt_p;
38 $extra_sql = " AND ((payby = 'CHEK' AND type != 'CREDIT') OR (payby != 'CHEK'))" unless FS::pay_batch->can_handle_electronic_refunds('RBC');
42 hashref => \%criteria,
43 extra_sql => $extra_sql,
46 @batches = qsearch(\%hash);
47 die "No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n"
52 die &usage if !$batchnum;
53 @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
54 die "Can't find payment batch '$batchnum'\n" if !@batches;
55 if ($batches[0]->type eq "CREDIT") {
56 warn "running credit\n";
57 die( "Batch number $batchnum is a credit (batch refund) batch, and this format can not handle batch refunds.\n" )
58 unless FS::pay_batch->can_handle_electronic_refunds('RBC');
62 my $conf = new FS::Conf;
63 my ($username, $password) = $conf->config('batchconfig-RBC-login');
65 $username and $password
66 or die "RBC FTP login not configured. Enter your username and password in 'batchconfig-rbc-login'.\n";
68 my $host = 'ftpssl.rbc.com';
69 debug "Connecting to $username\@$host...\n";
71 my $date = DateTime->now->strftime('%Y%m%d');
73 my $ftp = Net::FTPSSL->new($host,
75 Debug => ($opt_v ? 1 : 0),
76 Croak => 1, # rely on auto-rollback when dbh closes
78 $ftp->login($username, $password);
80 my $tmpdir = tempdir( CLEANUP => 1 );
82 foreach my $pay_batch (@batches) {
83 my $batchnum = $pay_batch->batchnum;
84 my $filename = $date . '.' . sprintf('%06d', $batchnum);
85 debug "Exporting batch $batchnum to $filename\n";
87 my $text = $pay_batch->export_batch(format => 'RBC');
89 print STDERR "Batch is empty, resolving..." if $opt_v;
92 write_file("$tmpdir/$filename", $text);
94 debug "Uploading $filename...";
95 $ftp->put("$tmpdir/$filename", $filename);
103 freeside-rbc-upload - Transmit a payment batch to RBC via FTP/TLS.
107 freeside-rbc-upload [ -a [ -p PAYBY ] ] [ -v ] user batchnum
111 Command line tool to upload a payment batch to the Royal Bank of Canada
112 ACH service. Use L<freeside-rbc-download> to retrieve the response file.
115 -a: Send all open batches, instead of specifying a batchnum.
117 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
121 user: freeside username
123 batchnum: pay_batch primary key