RT#33582: RBC return batch processing failure [added comments]
[freeside.git] / FS / FS / pay_batch / RBC.pm
1 package FS::pay_batch::RBC;
2
3 use strict;
4 use vars qw(@ISA %import_info %export_info $name);
5 use Date::Format 'time2str';
6 use FS::Conf;
7
8 my $conf;
9 my ($client_num, $shortname, $longname, $trans_code, $i);
10
11 $name = 'RBC';
12 # Royal Bank of Canada ACH Direct Payments Service
13
14 # Meaning of initial characters in records:
15 # 0 - header row, skipped by begin_condition
16 # 1 - Debit Detail Record (only when subtype is 0)
17 # 2 - Credit Detail Record, we die with a parse error (shouldn't appear in freeside-generated batches)
18 # 3 - Account Trailer Record (appears after Returned items, we skip)
19 # 4 - Client Trailer Record, indicates end of batch in end_condition
20 #
21 # Subtypes (27th char) indicate different kinds of Debit/Credit records
22 # 0 - Credit/Debit Detail Record
23 # 3 - Error Message Record
24 # 4 - Foreign Currency Information Records
25 # We skip all subtypes except 0
26 %import_info = (
27   'filetype'    => 'fixed',
28   'formatre'    => 
29   '^([0134]).{18}(.{4}).{3}(.).{11}(.{19}).{6}(.{30}).{17}(.{9})(.{18}).{6}(.{14}).{23}(.).{9}\r?$',
30   'fields' => [ qw(
31     recordtype
32     batchnum
33     subtype
34     paybatchnum
35     custname
36     bank
37     payinfo
38     paid
39     status
40     ) ],
41   'hook' => sub {
42       my $hash = shift;
43       $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
44       $hash->{'_date'} = time;
45       $hash->{'payinfo'} =~ s/^(\S+).*/$1/; # these often have trailing spaces
46       $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
47   },
48   'approved'    => sub { 
49       my $hash = shift;
50       $hash->{'status'} eq ' '
51   },
52   'declined'    => sub {
53       my $hash = shift;
54       grep { $hash->{'status'} eq $_ } ('E', 'R', 'U', 'T');
55   },
56   'begin_condition' => sub {
57       my $hash = shift;
58       $hash->{recordtype} eq '1'; # Detail Record
59   },
60   'end_hook'    => sub {
61       my( $hash, $total, $line ) = @_;
62       $total = sprintf("%.2f", $total);
63       # We assume here that this is an 'All Records' or 'Input Records'
64       # report.
65       my $batch_total = sprintf("%.2f", substr($line, 59, 18) / 100);
66       return "Our total $total does not match bank total $batch_total!"
67         if $total != $batch_total;
68       '';
69   },
70   'end_condition' => sub {
71       my $hash = shift;
72       $hash->{recordtype} eq '4'; # Client Trailer Record
73   },
74   'skip_condition' => sub {
75       my $hash = shift;
76       $hash->{'recordtype'} eq '3' ||
77         $hash->{'subtype'} ne '0';
78   },
79 );
80
81 %export_info = (
82   init => sub {
83     $conf = shift;
84     ($client_num,
85      $shortname,
86      $longname,
87      $trans_code, 
88      ) = $conf->config("batchconfig-RBC");
89     $i = 1;
90   },
91   header => sub { 
92     my $pay_batch = shift;
93     '$$AAPASTD0152[PROD[NL$$'."\n".
94     '000001'.
95     'A'.
96     'HDR'.
97     sprintf("%10s", $client_num).
98     sprintf("%-30s", $longname).
99     sprintf("%04u", $pay_batch->batchnum).
100     time2str("%Y%j", $pay_batch->download).
101     'CAD'.
102     '1'.
103     ' ' x 87  # filler/reserved fields
104     ;
105   },
106   row => sub {
107     my ($cust_pay_batch, $pay_batch) = @_;
108     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
109     my($bankno, $branch);
110     if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
111       ($bankno, $branch) = ( $1, $2 );
112     } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
113       ($branch, $bankno) = ( $1, $2 );
114     } else {
115       die "invalid branch/routing number '$aba'\n";
116     }
117
118     $i++;
119     sprintf("%06u", $i).
120     'D'.
121     sprintf("%3s",$trans_code).
122     sprintf("%10s",$client_num).
123     ' '.
124     sprintf("%-19s", $cust_pay_batch->paybatchnum).
125     '00'.
126     sprintf("%04s", $bankno).
127     sprintf("%05s", $branch).
128     sprintf("%-18s", $account).
129     ' '.
130     sprintf("%010.0f",$cust_pay_batch->amount*100).
131     '      '.
132     time2str("%Y%j", $pay_batch->download).
133     sprintf("%-30s", $cust_pay_batch->cust_main->first . ' ' .
134                      $cust_pay_batch->cust_main->last).
135     'E'. # English
136     ' '.
137     sprintf("%-15s", $shortname).
138     'CAD'.
139     ' '.
140     'CAN'.
141     '    '.
142     'N' # no customer optional information follows
143     ;
144 # Note: IAT Address Information and Remittance records are not 
145 # supported. This means you probably can't process payments 
146 # destined to U.S. bank accounts.  If you need this feature, contact 
147 # Freeside Internet Services.
148   },
149   footer => sub {
150     my ($pay_batch, $batchcount, $batchtotal) = @_;
151     sprintf("%06u", $i + 1).
152     'Z'.
153     'TRL'.
154     sprintf("%10s", $client_num).
155     '0' x 20 .
156     sprintf("%06u", $batchcount).
157     sprintf("%014.0f", $batchtotal*100).
158     '00' .
159     '000000' . # total number of customer information records
160     ' ' x 84
161     ;
162   },
163 );
164
165 1;
166