RT# 82988 - added check for batch payment upload scripts to make sure they can handle...
[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   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');
50   }
51 }
52
53 my $conf = new FS::Conf;
54 my ($username, $password) = $conf->config('batchconfig-RBC-login');
55
56 $username and $password
57   or die "RBC FTP login not configured. Enter your username and password in 'batchconfig-rbc-login'.\n";
58
59 my $host = 'ftpssl.rbc.com';
60 debug "Connecting to $username\@$host...\n";
61
62 my $date = DateTime->now->strftime('%Y%m%d');
63
64 my $ftp = Net::FTPSSL->new($host,
65                            Timeout => 30,
66                            Debug => ($opt_v ? 1 : 0),
67                            Croak => 1, # rely on auto-rollback when dbh closes
68                           );
69 $ftp->login($username, $password);
70
71 my $tmpdir = tempdir( CLEANUP => 1 );
72
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";
77
78   my $text = $pay_batch->export_batch(format => 'RBC');
79   write_file("$tmpdir/$filename", $text);
80
81   debug "Uploading $filename...";
82   $ftp->put("$tmpdir/$filename", $filename);
83   debug "done.\n";
84 }
85
86 debug "Finished.\n";
87
88 =head1 NAME
89
90 freeside-rbc-upload - Transmit a payment batch to RBC via FTP/TLS.
91
92 =head1 SYNOPSIS
93
94   freeside-rbc-upload [ -a [ -p PAYBY ] ] [ -v ] user batchnum
95
96 =head1 DESCRIPTION
97
98 Command line tool to upload a payment batch to the Royal Bank of Canada 
99 ACH service. Use L<freeside-rbc-download> to retrieve the response file.
100 Options:
101
102 -a: Send all open batches, instead of specifying a batchnum.
103
104 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
105
106 -v: Be verbose.
107
108 user: freeside username
109
110 batchnum: pay_batch primary key
111
112 =head1 BUGS
113
114 =head1 SEE ALSO
115
116 L<FS::pay_batch>
117
118 =cut
119
120 1;
121