RT# 82988 - Fixed so only formats that can handle electronic refunds can download...
[freeside.git] / FS / bin / freeside-rbc-upload
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use DateTime;
6 use Net::FTPSSL;
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);
11 use FS::pay_batch;
12 use FS::Conf;
13
14 use vars qw( $opt_a $opt_v $opt_p );
15 getopts('avp:');
16
17 sub usage { "
18   Usage:
19     freeside-rbc-upload [ -v ] user batchnum
20     freeside-rbc-upload -a [ -p payby ] [ -v ] user\n
21 " }
22
23 sub debug {
24   print STDERR $_[0] if $opt_v;
25 }
26
27 my $user = shift or die &usage;
28 adminsuidsetup $user;
29
30 my @batches; 
31
32 # copied from freeside-paymentech-upload, obviously
33 if($opt_a) {
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" 
39     if !@batches;
40 }
41 else {
42   my $batchnum = shift;
43   die &usage if !$batchnum;
44   @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
45   die "Can't find payment batch '$batchnum'\n" if !@batches;
46 }
47
48 my $conf = new FS::Conf;
49 my ($username, $password) = $conf->config('batchconfig-RBC-login');
50
51 $username and $password
52   or die "RBC FTP login not configured. Enter your username and password in 'batchconfig-rbc-login'.\n";
53
54 my $host = 'ftpssl.rbc.com';
55 debug "Connecting to $username\@$host...\n";
56
57 my $date = DateTime->now->strftime('%Y%m%d');
58
59 my $ftp = Net::FTPSSL->new($host,
60                            Timeout => 30,
61                            Debug => ($opt_v ? 1 : 0),
62                            Croak => 1, # rely on auto-rollback when dbh closes
63                           );
64 $ftp->login($username, $password);
65
66 my $tmpdir = tempdir( CLEANUP => 1 );
67
68 foreach my $pay_batch (@batches) {
69   my $batchnum = $pay_batch->batchnum;
70   my $filename = $date . '.' . sprintf('%06d', $batchnum);
71   debug "Exporting batch $batchnum to $filename\n";
72
73   my $text = $pay_batch->export_batch(format => 'RBC');
74   unless ($text) {
75     print STDERR "Batch is empty, resolving..." if $opt_v;
76     next;
77   }
78   write_file("$tmpdir/$filename", $text);
79
80   debug "Uploading $filename...";
81   $ftp->put("$tmpdir/$filename", $filename);
82   debug "done.\n";
83 }
84
85 debug "Finished.\n";
86
87 =head1 NAME
88
89 freeside-rbc-upload - Transmit a payment batch to RBC via FTP/TLS.
90
91 =head1 SYNOPSIS
92
93   freeside-rbc-upload [ -a [ -p PAYBY ] ] [ -v ] user batchnum
94
95 =head1 DESCRIPTION
96
97 Command line tool to upload a payment batch to the Royal Bank of Canada 
98 ACH service. Use L<freeside-rbc-download> to retrieve the response file.
99 Options:
100
101 -a: Send all open batches, instead of specifying a batchnum.
102
103 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
104
105 -v: Be verbose.
106
107 user: freeside username
108
109 batchnum: pay_batch primary key
110
111 =head1 BUGS
112
113 =head1 SEE ALSO
114
115 L<FS::pay_batch>
116
117 =cut
118
119 1;
120