Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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   @batches = qsearch('pay_batch', \%criteria);
37   die "No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n" 
38     if !@batches;
39 }
40 else {
41   my $batchnum = shift;
42   die &usage if !$batchnum;
43   @batches = qsearchs('pay_batch', { batchnum => $batchnum } );
44   die "Can't find payment batch '$batchnum'\n" if !@batches;
45 }
46
47 my $conf = new FS::Conf;
48 my ($username, $password) = $conf->config('batchconfig-RBC-login');
49
50 $username and $password
51   or die "RBC FTP login not configured. Enter your username and password in 'batchconfig-rbc-login'.\n";
52
53 my $host = 'ftpssl.rbc.com';
54 debug "Connecting to $username\@$host...\n";
55
56 my $date = DateTime->now->strftime('%Y%m%d');
57
58 my $ftp = Net::FTPSSL->new($host,
59                            Timeout => 30,
60                            Debug => ($opt_v ? 1 : 0),
61                            Croak => 1, # rely on auto-rollback when dbh closes
62                           );
63 $ftp->login($username, $password);
64
65 my $tmpdir = tempdir( CLEANUP => 1 );
66
67 foreach my $pay_batch (@batches) {
68   my $batchnum = $pay_batch->batchnum;
69   my $filename = $date . '.' . sprintf('%06d', $batchnum);
70   debug "Exporting batch $batchnum to $filename\n";
71
72   my $text = $pay_batch->export_batch(format => 'RBC');
73   write_file("$tmpdir/$filename", $text);
74
75   debug "Uploading $filename...";
76   $ftp->put("$tmpdir/$filename", $filename);
77   debug "done.\n";
78 }
79
80 debug "Finished.\n";
81
82 =head1 NAME
83
84 freeside-rbc-upload - Transmit a payment batch to RBC via FTP/TLS.
85
86 =head1 SYNOPSIS
87
88   freeside-rbc-upload [ -a [ -p PAYBY ] ] [ -v ] user batchnum
89
90 =head1 DESCRIPTION
91
92 Command line tool to upload a payment batch to the Royal Bank of Canada 
93 ACH service. Use L<freeside-rbc-download> to retrieve the response file.
94 Options:
95
96 -a: Send all open batches, instead of specifying a batchnum.
97
98 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
99
100 -v: Be verbose.
101
102 user: freeside username
103
104 batchnum: pay_batch primary key
105
106 =head1 BUGS
107
108 =head1 SEE ALSO
109
110 L<FS::pay_batch>
111
112 =cut
113
114 1;
115