Reverted menu-left-example.png back to original and cleaned up menu-top-example to...
[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   unless ($text) {
74     print STDERR "Batch is empty, resolving..." if $opt_v;
75     next;
76   }
77   write_file("$tmpdir/$filename", $text);
78
79   debug "Uploading $filename...";
80   $ftp->put("$tmpdir/$filename", $filename);
81   debug "done.\n";
82 }
83
84 debug "Finished.\n";
85
86 =head1 NAME
87
88 freeside-rbc-upload - Transmit a payment batch to RBC via FTP/TLS.
89
90 =head1 SYNOPSIS
91
92   freeside-rbc-upload [ -a [ -p PAYBY ] ] [ -v ] user batchnum
93
94 =head1 DESCRIPTION
95
96 Command line tool to upload a payment batch to the Royal Bank of Canada 
97 ACH service. Use L<freeside-rbc-download> to retrieve the response file.
98 Options:
99
100 -a: Send all open batches, instead of specifying a batchnum.
101
102 -p PAYBY: With -a, limit to batches of that payment type, e.g. -p CARD.
103
104 -v: Be verbose.
105
106 user: freeside username
107
108 batchnum: pay_batch primary key
109
110 =head1 BUGS
111
112 =head1 SEE ALSO
113
114 L<FS::pay_batch>
115
116 =cut
117
118 1;
119