1 package FS::pay_batch::RBC;
4 use vars qw(@ISA %import_info %export_info $name);
5 use Date::Format 'time2str';
10 my ($client_num, $shortname, $longname, $trans_code, $testmode, $i, $declined, $totaloffset);
13 # Royal Bank of Canada ACH Direct Payments Service
15 # Meaning of initial characters in records:
16 # 0 - header row, skipped by begin_condition
17 # 1 - Debit Detail Record (only when subtype is 0)
18 # 2 - Credit Detail Record, we die with a parse error (shouldn't appear in freeside-generated batches)
19 # 3 - Account Trailer Record (appears after Returned items, we skip)
20 # 4 - Client Trailer Record, indicates end of batch in end_condition
22 # Subtypes (27th char) indicate different kinds of Debit/Credit records
23 # 0 - Credit/Debit Detail Record
24 # 3 - Error Message Record
25 # 4 - Foreign Currency Information Records
26 # We skip all subtypes except 0
28 # additional info available at https://www.rbcroyalbank.com/ach/cid-213166.html
30 'filetype' => 'fixed',
31 #this only really applies to Debit Detail, but we otherwise only need first char
33 '^(.).{18}(.{4}).{3}(.).{11}(.{19}).{6}(.{30}).{17}(.{9})(.{18}).{6}(.{14}).{23}(.).{9}\r?$',
47 $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
48 $hash->{'_date'} = time;
49 $hash->{'payinfo'} =~ s/^(\S+).*/$1/; # these often have trailing spaces
50 $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
54 $hash->{'status'} eq ' '
58 my $status = $hash->{'status'};
61 $message = 'Reversed payment';
62 } elsif ($status eq 'R') {
63 $message = 'Rejected payment';
64 } elsif ($status eq 'U') {
65 $message = 'Returned payment';
66 } elsif ($status eq 'T') {
71 $hash->{'error_message'} = $message;
72 $declined->{$hash->{'paybatchnum'}} = 1;
75 'begin_condition' => sub {
78 if ($hash->{recordtype} eq '1') {
82 # Credit Detail Record, will immediately trigger end condition & error
83 } elsif ($hash->{recordtype} eq '2') {
90 my( $hash, $total, $line ) = @_;
91 return "Can't process Credit Detail Record, aborting import"
92 if ($hash->{'recordtype'} eq '2');
93 $totaloffset = sprintf("%.2f", $totaloffset / 100 );
94 $total += $totaloffset;
95 $total = sprintf("%.2f", $total);
96 # We assume here that this is an 'All Records' or 'Input Records' report.
97 my $batch_total = sprintf("%.2f", substr($line, 59, 18) / 100);
98 return "Our total $total does not match bank total $batch_total!"
99 if $total != $batch_total;
102 'end_condition' => sub {
104 return ($hash->{recordtype} eq '4') # Client Trailer Record
105 || ($hash->{recordtype} eq '2'); # Credit Detail Record, will throw error in end_hook
107 'skip_condition' => sub {
109 #we already declined it this run, no takebacks
110 if ($declined->{$hash->{'paybatchnum'}}) {
111 #file counts this as part of total, but we skip
112 $totaloffset += $hash->{'paid'}
113 if $hash->{'status'} eq ' '; #false laziness with 'approved' above
117 ($hash->{'recordtype'} eq '3') || #Account Trailer Record, concludes returned items
118 ($hash->{'subtype'} ne '0'); #error messages, etc, too late to apply to previous entry
130 ) = $conf->config("batchconfig-RBC");
131 $testmode = '' unless $testmode eq 'TEST';
135 my $pay_batch = shift;
136 my $mode = $testmode ? 'TEST' : 'PROD';
137 my $filenum = $testmode ? 'TEST' : sprintf("%04u", $pay_batch->batchnum);
138 '$$AAPASTD0152['.$mode.'[NL$$'."\n".
142 sprintf("%10s", $client_num).
143 sprintf("%-30s", $longname).
145 time2str("%Y%j", $pay_batch->download).
148 ' ' x 87 # filler/reserved fields
152 my ($cust_pay_batch, $pay_batch) = @_;
153 my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
154 my($bankno, $branch);
155 if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
156 ($bankno, $branch) = ( $1, $2 );
157 } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
158 ($branch, $bankno) = ( $1, $2 );
160 die "invalid branch/routing number '$aba'\n";
166 sprintf("%3s",$trans_code).
167 sprintf("%10s",$client_num).
169 sprintf("%-19s", $cust_pay_batch->paybatchnum).
171 sprintf("%04s", $bankno).
172 sprintf("%05s", $branch).
173 sprintf("%-18s", $account).
175 sprintf("%010.0f",$cust_pay_batch->amount*100).
177 time2str("%Y%j", time + 86400).
178 sprintf("%-30.30s", encode('utf8', $cust_pay_batch->cust_main->first . ' ' .
179 $cust_pay_batch->cust_main->last)).
182 sprintf("%-15s", $shortname).
187 'N' # no customer optional information follows
189 # Note: IAT Address Information and Remittance records are not
190 # supported. This means you probably can't process payments
191 # destined to U.S. bank accounts. If you need this feature, contact
192 # Freeside Internet Services.
195 my ($pay_batch, $batchcount, $batchtotal) = @_;
196 sprintf("%06u", $i + 1).
199 sprintf("%10s", $client_num).
201 sprintf("%06u", $batchcount).
202 sprintf("%014.0f", $batchtotal*100).
204 '000000' . # total number of customer information records