default to a session cookie instead of setting an explicit timeout, weird timezone...
[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
37   my $extra_sql;
38   $extra_sql = " AND ((payby = 'CHEK' AND type != 'CREDIT') OR (payby != 'CHEK'))" unless FS::pay_batch->can_handle_electronic_refunds('RBC');
39
40   my %hash = (
41     table => 'pay_batch',
42     hashref   => \%criteria,
43     extra_sql => $extra_sql,
44   );
45
46   @batches = qsearch(\%hash);
47   die "No open batches found".($opt_p ? " of type '$opt_p'" : '').".\n" 
48     if !@batches;
49 }
50 else {
51   my $batchnum = shift;
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');
59   }
60 }
61
62 my $conf = new FS::Conf;
63 my ($username, $password) = $conf->config('batchconfig-RBC-login');
64
65 $username and $password
66   or die "RBC FTP login not configured. Enter your username and password in 'batchconfig-rbc-login'.\n";
67
68 my $host = 'ftpssl.rbc.com';
69 debug "Connecting to $username\@$host...\n";
70
71 my $date = DateTime->now->strftime('%Y%m%d');
72
73 my $ftp = Net::FTPSSL->new($host,
74                            Timeout => 30,
75                            Debug => ($opt_v ? 1 : 0),
76                            Croak => 1, # rely on auto-rollback when dbh closes
77                           );
78 $ftp->login($username, $password);
79
80 my $tmpdir = tempdir( CLEANUP => 1 );
81
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";
86
87   my $text = $pay_batch->export_batch(format => 'RBC');
88   unless ($text) {
89     print STDERR "Batch is empty, resolving..." if $opt_v;
90     next;
91   }
92   write_file("$tmpdir/$filename", $text);
93
94   debug "Uploading $filename...";
95   $ftp->put("$tmpdir/$filename", $filename);
96   debug "done.\n";
97 }
98
99 debug "Finished.\n";
100
101 =head1 NAME
102
103 freeside-rbc-upload - Transmit a payment batch to RBC via FTP/TLS.
104
105 =head1 SYNOPSIS
106
107   freeside-rbc-upload [ -a [ -p PAYBY ] ] [ -v ] user batchnum
108
109 =head1 DESCRIPTION
110
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.
113 Options:
114
115 -a: Send all open batches, instead of specifying a batchnum.
116
117 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
118
119 -v: Be verbose.
120
121 user: freeside username
122
123 batchnum: pay_batch primary key
124
125 =head1 BUGS
126
127 =head1 SEE ALSO
128
129 L<FS::pay_batch>
130
131 =cut
132
133 1;
134