8 use List::Util qw(first sum);
9 use List::MoreUtils qw(any);
10 use POSIX qw(strftime);
14 use SL::SEPA::XML::Transaction;
31 $self->{transactions} = [];
32 $self->{src_charset} = 'UTF-8';
35 map { $self->{$_} = $params{$_} if (exists $params{$_}) } qw(src_charset company creditor_id message_id grouped collection);
37 $self->{iconv} = SL::Iconv->new($self->{src_charset}, "UTF-8") || croak "Unsupported source charset $self->{src_charset}.";
39 my $missing_parameter = first { !$self->{$_} } qw(company message_id);
40 croak "Missing parameter: $missing_parameter" if ($missing_parameter);
41 croak "Missing parameter: creditor_id" if !$self->{creditor_id} && $self->{collection};
43 map { $self->{$_} = $self->_replace_special_chars($self->{iconv}->convert($self->{$_})) } qw(company message_id creditor_id);
49 foreach my $transaction (@_) {
50 croak "Expecting hash reference." if (ref $transaction ne 'HASH');
51 push @{ $self->{transactions} }, SL::SEPA::XML::Transaction->new(%{ $transaction }, 'sepa' => $self);
57 sub _replace_special_chars {
73 map { $text =~ s/$_/$special_chars{$_}/g; } keys %special_chars;
82 return sprintf '%.02f', $amount;
85 sub _group_transactions {
93 foreach my $transaction (@{ $self->{transactions} }) {
94 my $key = $self->{grouped} ? join("\t", map { $transaction->get($_) } qw(src_bic src_iban execution_date)) : 'all';
95 $grouped->{groups}->{$key} ||= {
97 'transactions' => [ ],
100 push @{ $grouped->{groups}->{$key}->{transactions} }, $transaction;
102 $grouped->{groups}->{$key}->{sum_amount} += $transaction->{amount};
103 $grouped->{sum_amount} += $transaction->{amount};
109 sub _restricted_identification_sepa1 {
110 my ($self, $string) = @_;
112 $string =~ s/[^A-Za-z0-9\+\?\/\-:\(\)\.,' ]//g;
113 return substr $string, 0, 35;
116 sub _restricted_identification_sepa2 {
117 my ($self, $string) = @_;
119 $string =~ s/[^A-Za-z0-9\+\?\/\-:\(\)\.,']//g;
120 return substr $string, 0, 35;
126 croak "No transactions added yet." if (!@{ $self->{transactions} });
130 my $xml = XML::Writer->new(OUTPUT => \$output,
133 ENCODING => 'utf-8');
136 my $time_zone = strftime "%z", @now;
137 my $now_str = strftime('%Y-%m-%dT%H:%M:%S', @now) . substr($time_zone, 0, 3) . ':' . substr($time_zone, 3, 2);
139 my $is_coll = $self->{collection};
140 my $cd_src = $is_coll ? 'Cdtr' : 'Dbtr';
141 my $cd_dst = $is_coll ? 'Dbtr' : 'Cdtr';
142 my $pain_id = $is_coll ? 'pain.008.002.02' : 'pain.001.002.03';
143 my $pain_elmt = $is_coll ? 'CstmrDrctDbtInitn' : 'CstmrCdtTrfInitn';
144 my @pii_base = (strftime('PII%Y%m%d%H%M%S', @now), rand(1000000000));
146 my $grouped_transactions = $self->_group_transactions();
150 $xml->startTag('Document',
151 'xmlns' => "urn:iso:std:iso:20022:tech:xsd:${pain_id}",
152 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
153 'xsi:schemaLocation' => "urn:iso:std:iso:20022:tech:xsd:${pain_id} ${pain_id}.xsd");
155 $xml->startTag($pain_elmt);
157 $xml->startTag('GrpHdr');
158 $xml->dataElement('MsgId', encode('UTF-8', $self->_restricted_identification_sepa1($self->{message_id})));
159 $xml->dataElement('CreDtTm', $now_str);
160 $xml->dataElement('NbOfTxs', scalar @{ $self->{transactions} });
161 $xml->dataElement('CtrlSum', $self->_format_amount($grouped_transactions->{sum_amount}));
163 $xml->startTag('InitgPty');
164 $xml->dataElement('Nm', encode('UTF-8', substr($self->{company}, 0, 70)));
165 $xml->endTag('InitgPty');
167 $xml->endTag('GrpHdr');
169 foreach my $key (keys %{ $grouped_transactions->{groups} }) {
170 my $transaction_group = $grouped_transactions->{groups}->{$key};
171 my $master_transaction = $transaction_group->{transactions}->[0];
173 $xml->startTag('PmtInf');
174 $xml->dataElement('PmtInfId', sprintf('%s%010d', @pii_base));
176 $xml->dataElement('PmtMtd', $is_coll ? 'DD' : 'TRF');
177 $xml->dataElement('NbOfTxs', scalar @{ $transaction_group->{transactions} });
178 $xml->dataElement('CtrlSum', $self->_format_amount($transaction_group->{sum_amount}));
180 $xml->startTag('PmtTpInf');
181 $xml->startTag('SvcLvl');
182 $xml->dataElement('Cd', 'SEPA');
183 $xml->endTag('SvcLvl');
186 $xml->startTag('LclInstrm');
187 $xml->dataElement('Cd', 'CORE');
188 $xml->endTag('LclInstrm');
189 $xml->dataElement('SeqTp', 'OOFF');
191 $xml->endTag('PmtTpInf');
193 $xml->dataElement($is_coll ? 'ReqdColltnDt' : 'ReqdExctnDt', $master_transaction->get('execution_date'));
194 $xml->startTag($cd_src);
195 $xml->dataElement('Nm', encode('UTF-8', substr($self->{company}, 0, 70)));
196 $xml->endTag($cd_src);
198 $xml->startTag($cd_src . 'Acct');
199 $xml->startTag('Id');
200 $xml->dataElement('IBAN', $master_transaction->get('src_iban', 34));
202 $xml->endTag($cd_src . 'Acct');
204 $xml->startTag($cd_src . 'Agt');
205 $xml->startTag('FinInstnId');
206 $xml->dataElement('BIC', $master_transaction->get('src_bic', 20));
207 $xml->endTag('FinInstnId');
208 $xml->endTag($cd_src . 'Agt');
210 $xml->dataElement('ChrgBr', 'SLEV');
212 foreach my $transaction (@{ $transaction_group->{transactions} }) {
213 $xml->startTag($is_coll ? 'DrctDbtTxInf' : 'CdtTrfTxInf');
215 $xml->startTag('PmtId');
216 $xml->dataElement('EndToEndId', $self->_restricted_identification_sepa1($transaction->get('end_to_end_id')));
217 $xml->endTag('PmtId');
220 $xml->startTag('InstdAmt', 'Ccy' => 'EUR');
221 $xml->characters($self->_format_amount($transaction->{amount}));
222 $xml->endTag('InstdAmt');
224 $xml->startTag('DrctDbtTx');
226 $xml->startTag('MndtRltdInf');
227 $xml->dataElement('MndtId', $self->_restricted_identification_sepa2($transaction->get('mandator_id')));
228 $xml->dataElement('DtOfSgntr', $self->_restricted_identification_sepa2($transaction->get('date_of_signature')));
229 $xml->endTag('MndtRltdInf');
231 $xml->startTag('CdtrSchmeId');
232 $xml->startTag('Id');
233 $xml->startTag('PrvtId');
234 $xml->startTag('Othr');
235 $xml->dataElement('Id', encode('UTF-8', substr($self->{creditor_id}, 0, 35)));
236 $xml->startTag('SchmeNm');
237 $xml->dataElement('Prtry', 'SEPA');
238 $xml->endTag('SchmeNm');
239 $xml->endTag('Othr');
240 $xml->endTag('PrvtId');
242 $xml->endTag('CdtrSchmeId');
244 $xml->endTag('DrctDbtTx');
247 $xml->startTag('Amt');
248 $xml->startTag('InstdAmt', 'Ccy' => 'EUR');
249 $xml->characters($self->_format_amount($transaction->{amount}));
250 $xml->endTag('InstdAmt');
254 $xml->startTag("${cd_dst}Agt");
255 $xml->startTag('FinInstnId');
256 $xml->dataElement('BIC', $transaction->get('dst_bic', 20));
257 $xml->endTag('FinInstnId');
258 $xml->endTag("${cd_dst}Agt");
260 $xml->startTag("${cd_dst}");
261 $xml->dataElement('Nm', $transaction->get('company', 70));
262 $xml->endTag("${cd_dst}");
264 $xml->startTag("${cd_dst}Acct");
265 $xml->startTag('Id');
266 $xml->dataElement('IBAN', $transaction->get('dst_iban', 34));
268 $xml->endTag("${cd_dst}Acct");
270 $xml->startTag('RmtInf');
271 $xml->dataElement('Ustrd', $transaction->get('reference', 140));
272 $xml->endTag('RmtInf');
274 $xml->endTag($is_coll ? 'DrctDbtTxInf' : 'CdtTrfTxInf');
277 $xml->endTag('PmtInf');
280 $xml->endTag($pain_elmt);
281 $xml->endTag('Document');