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;
36 $criteria{'type'} = 'DEBIT' unless FS::pay_batch->can_handle_electronic_refunds('RBC');
37 @batches = qsearch('pay_batch', \%criteria);
38 die "No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n"
43 die &usage if !$batchnum;
44 @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
45 die "Can't find payment batch '$batchnum'\n" if !@batches;
46 if ($batches[0]->type eq "CREDIT") {
47 warn "running credit\n";
48 log_and_die( "Batch number $batchnum is a credit (batch refund) batch, and this format can not handle batch refunds.\n" )
49 unless FS::pay_batch->can_handle_electronic_refunds('RBC');
53 my $conf = new FS::Conf;
54 my ($username, $password) = $conf->config('batchconfig-RBC-login');
56 $username and $password
57 or die "RBC FTP login not configured. Enter your username and password in 'batchconfig-rbc-login'.\n";
59 my $host = 'ftpssl.rbc.com';
60 debug "Connecting to $username\@$host...\n";
62 my $date = DateTime->now->strftime('%Y%m%d');
64 my $ftp = Net::FTPSSL->new($host,
66 Debug => ($opt_v ? 1 : 0),
67 Croak => 1, # rely on auto-rollback when dbh closes
69 $ftp->login($username, $password);
71 my $tmpdir = tempdir( CLEANUP => 1 );
73 foreach my $pay_batch (@batches) {
74 my $batchnum = $pay_batch->batchnum;
75 my $filename = $date . '.' . sprintf('%06d', $batchnum);
76 debug "Exporting batch $batchnum to $filename\n";
78 my $text = $pay_batch->export_batch(format => 'RBC');
80 print STDERR "Batch is empty, resolving..." if $opt_v;
83 write_file("$tmpdir/$filename", $text);
85 debug "Uploading $filename...";
86 $ftp->put("$tmpdir/$filename", $filename);
94 freeside-rbc-upload - Transmit a payment batch to RBC via FTP/TLS.
98 freeside-rbc-upload [ -a [ -p PAYBY ] ] [ -v ] user batchnum
102 Command line tool to upload a payment batch to the Royal Bank of Canada
103 ACH service. Use L<freeside-rbc-download> to retrieve the response file.
106 -a: Send all open batches, instead of specifying a batchnum.
108 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
112 user: freeside username
114 batchnum: pay_batch primary key