1 package FS::pay_batch::RBC;
4 use vars qw(@ISA %import_info %export_info $name);
5 use Date::Format 'time2str';
11 my ($client_num, $shortname, $longname, $trans_code, $testmode, $i, $declined, $totaloffset);
14 # Royal Bank of Canada ACH Direct Payments Service
16 # Meaning of initial characters in records:
17 # 0 - header row, skipped by begin_condition
18 # 1 - Debit Detail Record (only when subtype is 0)
19 # 2 - Credit Detail Record, we die with a parse error (shouldn't appear in freeside-generated batches)
20 # 3 - Account Trailer Record (appears after Returned items, we skip)
21 # 4 - Client Trailer Record, indicates end of batch in end_condition
23 # Subtypes (27th char) indicate different kinds of Debit/Credit records
24 # 0 - Credit/Debit Detail Record
25 # 3 - Error Message Record
26 # 4 - Foreign Currency Information Records
27 # We skip all subtypes except 0
29 # additional info available at https://www.rbcroyalbank.com/ach/cid-213166.html
31 'filetype' => 'fixed',
32 #this only really applies to Debit Detail, but we otherwise only need first char
34 '^(.).{3}(.{10}).{5}(.{4}).{3}(.).{11}(.{19}).{6}(.{30}).{17}(.{9})(.{18}).{6}(.{14}).{23}(.).{9}\r?$',
48 # pull client_num from config and check it against what's in the batch
49 state $clientnum ||= do {
50 my $conf = FS::Conf->new;
51 my @config = $conf->config("batchconfig-RBC");
56 $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
57 $hash->{'_date'} = time;
58 $hash->{'payinfo'} =~ s/^(\S+).*/$1/; # these often have trailing spaces
59 $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
61 if ( $clientnum and $hash->{clientnum} ne $clientnum ) {
62 die "RBC client number in batch (".$hash->{clientnum}.") does not ".
63 "match configuration.\n";
69 $hash->{'status'} eq ' '
73 my $status = $hash->{'status'};
76 $message = 'Reversed payment';
77 } elsif ($status eq 'R') {
78 $message = 'Rejected payment';
79 } elsif ($status eq 'U') {
80 $message = 'Returned payment';
81 } elsif ($status eq 'T') {
86 $hash->{'error_message'} = $message;
87 $declined->{$hash->{'paybatchnum'}} = 1;
90 'begin_condition' => sub {
93 if ($hash->{recordtype} eq '1') {
97 # Credit Detail Record, will immediately trigger end condition & error
98 } elsif ($hash->{recordtype} eq '2') {
105 my( $hash, $total, $line ) = @_;
106 return "Can't process Credit Detail Record, aborting import"
107 if ($hash->{'recordtype'} eq '2');
108 $total += $totaloffset;
109 $total = sprintf("%.2f", $total);
110 # We assume here that this is an 'All Records' or 'Input Records' report.
111 my $batch_total = sprintf("%.2f", substr($line, 59, 18) / 100);
112 return "Our total $total does not match bank total $batch_total!"
113 if $total != $batch_total;
116 'end_condition' => sub {
118 return ($hash->{recordtype} eq '4') # Client Trailer Record
119 || ($hash->{recordtype} eq '2'); # Credit Detail Record, will throw error in end_hook
121 'skip_condition' => sub {
123 #we already declined it this run, no takebacks
124 if ($declined->{$hash->{'paybatchnum'}}) {
125 #file counts this as part of total, but we skip
126 $totaloffset += sprintf("%.2f", $hash->{'paid'} / 100 )
127 if $hash->{'status'} eq ' '; #false laziness with 'approved' above
130 #skipping W for now (maybe it should be declined?)
131 if ($hash->{'status'} eq 'W') {
132 #file counts this as part of total, but we skip
133 $totaloffset += sprintf("%.2f", $hash->{'paid'} / 100 );
137 ($hash->{'recordtype'} eq '3') || #Account Trailer Record, concludes returned items
138 ($hash->{'subtype'} ne '0'); #error messages, etc, too late to apply to previous entry
150 ) = $conf->config("batchconfig-RBC");
151 $testmode = '' unless $testmode eq 'TEST';
155 my $pay_batch = shift;
156 my $mode = $testmode ? 'TEST' : 'PROD';
157 my $filenum = $testmode ? 'TEST' : sprintf("%04u", $pay_batch->batchnum);
158 '$$AAPASTD0152['.$mode.'[NL$$'."\n".
162 sprintf("%10s", $client_num).
163 sprintf("%-30s", $longname).
165 time2str("%Y%j", $pay_batch->download).
168 ' ' x 87 # filler/reserved fields
172 my ($cust_pay_batch, $pay_batch) = @_;
173 my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
174 my($bankno, $branch);
175 if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
176 ($bankno, $branch) = ( $1, $2 );
177 } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
178 ($branch, $bankno) = ( $1, $2 );
180 die "invalid branch/routing number '$aba'\n";
186 sprintf("%3s",$trans_code).
187 sprintf("%10s",$client_num).
189 sprintf("%-19s", $cust_pay_batch->paybatchnum).
191 sprintf("%04s", $bankno).
192 sprintf("%05s", $branch).
193 sprintf("%-18s", $account).
195 sprintf("%010.0f",$cust_pay_batch->amount*100).
197 time2str("%Y%j", time + 86400).
198 sprintf("%-30.30s", encode('utf8', $cust_pay_batch->cust_main->first . ' ' .
199 $cust_pay_batch->cust_main->last)).
202 sprintf("%-15s", $shortname).
207 'N' # no customer optional information follows
209 # Note: IAT Address Information and Remittance records are not
210 # supported. This means you probably can't process payments
211 # destined to U.S. bank accounts. If you need this feature, contact
212 # Freeside Internet Services.
215 my ($pay_batch, $batchcount, $batchtotal) = @_;
216 sprintf("%06u", $i + 1).
219 sprintf("%10s", $client_num).
221 sprintf("%06u", $batchcount).
222 sprintf("%014.0f", $batchtotal*100).
224 '000000' . # total number of customer information records