1 package FS::pay_batch::RBC;
4 use vars qw(@ISA %import_info %export_info $name);
5 use Date::Format 'time2str';
12 my ($client_num, $shortname, $longname, $trans_code, $testmode, $i, $declined, $totaloffset);
15 # Royal Bank of Canada ACH Direct Payments Service
17 # Meaning of initial characters in records:
18 # 0 - header row, skipped by begin_condition
19 # 1 - Debit Detail Record (only when subtype is 0)
20 # 2 - Credit Detail Record, we die with a parse error (shouldn't appear in freeside-generated batches)
21 # 3 - Account Trailer Record (appears after Returned items, we skip)
22 # 4 - Client Trailer Record, indicates end of batch in end_condition
24 # Subtypes (27th char) indicate different kinds of Debit/Credit records
25 # 0 - Credit/Debit Detail Record
26 # 3 - Error Message Record
27 # 4 - Foreign Currency Information Records
28 # We skip all subtypes except 0
30 # additional info available at https://www.rbcroyalbank.com/ach/cid-213166.html
32 'filetype' => 'fixed',
33 #this only really applies to Debit Detail, but we otherwise only need first char
35 '^(.).{3}(.{10}).{5}(.{4}).{3}(.).{11}(.{19}).{6}(.{30})(.{2})(.{2})(.{4}).{9}(.{9})(.{18}).{6}(.{14}).{23}(.).{9}\r?$',
52 # pull client_num from config and check it against what's in the batch
53 state $clientnum ||= do {
54 my $conf = FS::Conf->new;
55 my @config = $conf->config("batchconfig-RBC");
60 $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
61 my $paydate = $hash->{'paydate_year'} . $hash->{'paydate_month'} . $hash->{'paydate_day'};
62 $hash->{'_date'} = str2time($paydate, 'local');
63 $hash->{'payinfo'} =~ s/^(\S+).*/$1/; # these often have trailing spaces
64 $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
66 if ( $clientnum and $hash->{clientnum} ne $clientnum ) {
67 die "RBC client number in batch (".$hash->{clientnum}.") does not ".
68 "match configuration.\n";
74 ($hash->{'status'} eq ' ') || ($hash->{'status'} eq 'W');
78 my $status = $hash->{'status'};
81 $message = 'Reversed payment';
82 } elsif ($status eq 'R') {
83 $message = 'Rejected payment';
84 } elsif ($status eq 'U') {
85 $message = 'Returned payment';
86 } elsif ($status eq 'T') {
91 $hash->{'error_message'} = $message;
92 $declined->{$hash->{'paybatchnum'}} = 1;
95 'begin_condition' => sub {
98 if ($hash->{recordtype} eq '1') {
102 # Credit Detail Record, will immediately trigger end condition & error
103 } elsif ($hash->{recordtype} eq '2') {
110 my( $hash, $total, $line ) = @_;
111 return "Can't process Credit Detail Record, aborting import"
112 if ($hash->{'recordtype'} eq '2');
113 $total += $totaloffset;
114 $total = sprintf("%.2f", $total);
115 # We assume here that this is an 'All Records' or 'Input Records' report.
116 my $batch_total = sprintf("%.2f", substr($line, 59, 18) / 100);
117 return "Our total $total does not match bank total $batch_total!"
118 if $total != $batch_total;
121 'end_condition' => sub {
123 return ($hash->{recordtype} eq '4') # Client Trailer Record
124 || ($hash->{recordtype} eq '2'); # Credit Detail Record, will throw error in end_hook
126 'skip_condition' => sub {
128 #we already declined it this run, no takebacks
129 if ($declined->{$hash->{'paybatchnum'}}) {
130 #file counts this as part of total, but we skip
131 $totaloffset += sprintf("%.2f", $hash->{'paid'} / 100 )
132 if $hash->{'status'} eq ' '; #false laziness with 'approved' above
136 ($hash->{'recordtype'} eq '3') || #Account Trailer Record, concludes returned items
137 ($hash->{'subtype'} ne '0'); #error messages, etc, too late to apply to previous entry
149 ) = $conf->config("batchconfig-RBC");
150 $testmode = '' unless $testmode eq 'TEST';
154 my $pay_batch = shift;
155 my $mode = $testmode ? 'TEST' : 'PROD';
156 my $filenum = $testmode ? 'TEST' : sprintf("%04u", $pay_batch->batchnum);
157 my $qualifier = $pay_batch->type eq 'CREDIT' ? 'D' : 'A';
158 '$$AAP'.$qualifier.'STD0152['.$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";
183 ## set custname to business name if business checking or savings account is used otherwise leave as first and last name.
184 my $custname = $cust_pay_batch->cust_main->first . ' ' . $cust_pay_batch->cust_main->last;
185 $custname = $cust_pay_batch->cust_main->company
186 if (($cust_pay_batch->cust_main->paytype eq "Business checking" || $cust_pay_batch->cust_main->paytype eq "Business savings") && $cust_pay_batch->cust_main->company);
190 ## set to D for debit by default, then override to what cust_pay_batch has as payments may not have paycode.
191 my $debitorcredit = 'D';
192 $debitorcredit = $cust_pay_batch->paycode unless !$cust_pay_batch->paycode;
196 sprintf("%3s",$trans_code).
197 sprintf("%10s",$client_num).
199 sprintf("%-19s", $cust_pay_batch->paybatchnum).
201 sprintf("%04s", $bankno).
202 sprintf("%05s", $branch).
203 sprintf("%-18s", $account).
205 sprintf("%010.0f",$cust_pay_batch->amount*100).
207 time2str("%Y%j", time + 86400).
208 sprintf("%-30.30s", encode('utf8', $custname)).
211 sprintf("%-15s", $shortname).
216 'N' # no customer optional information follows
218 # Note: IAT Address Information and Remittance records are not
219 # supported. This means you probably can't process payments
220 # destined to U.S. bank accounts. If you need this feature, contact
221 # Freeside Internet Services.
224 my ($pay_batch, $batchcount, $batchtotal) = @_;
226 my $batch_info = '0' x 20 . sprintf("%06u", $batchcount) . sprintf("%014.0f", $batchtotal*100);
227 $batch_info = sprintf("%06u", $batchcount) . sprintf("%014.0f", $batchtotal*100) . '0' x 20 if ($pay_batch->type eq 'CREDIT');
229 sprintf("%06u", $i + 1).
232 sprintf("%10s", $client_num).
235 '000000' . # total number of customer information records
241 ## this format can handle credit transactions
242 sub can_handle_credits {