-
Notifications
You must be signed in to change notification settings - Fork 52
/
QrBill.pm
333 lines (230 loc) · 6.83 KB
/
QrBill.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package SL::Helper::QrBill;
use strict;
use warnings;
use Imager;
use Imager::QRCode;
my %Config = (
cross_file => 'image/CH-Kreuz_7mm.png',
out_file => 'out.png',
);
sub new {
my $class = shift;
my $self = bless {}, $class;
$self->_init_check(@_);
$self->_init(@_);
return $self;
}
sub _init {
my $self = shift;
my ($biller_information, $biller_data, $payment_information, $invoice_recipient_data, $ref_nr_data) = @_;
$self->{data}{header} = [
'SPC', # QRType
'0200', # Version
1, # Coding Type
];
$self->{data}{biller_information} = [
$biller_information->{iban},
];
$self->{data}{biller_data} = [
$biller_data->{address_type},
$biller_data->{company},
$biller_data->{address_row1},
$biller_data->{address_row2},
'',
'',
$biller_data->{countrycode},
];
$self->{data}{payment_information} = [
$payment_information->{amount},
$payment_information->{currency},
];
$self->{data}{invoice_recipient_data} = [
$invoice_recipient_data->{address_type},
$invoice_recipient_data->{name},
$invoice_recipient_data->{address_row1},
$invoice_recipient_data->{address_row2},
'',
'',
$invoice_recipient_data->{countrycode},
];
$self->{data}{ref_nr_data} = [
$ref_nr_data->{type},
$ref_nr_data->{ref_number},
];
$self->{data}{additional_information} = [
'',
'EPD', # End Payment Data
];
}
sub _init_check {
my $self = shift;
my ($biller_information, $biller_data, $payment_information, $invoice_recipient_data, $ref_nr_data) = @_;
my $check_re = sub {
my ($href, $elem, $regex) = @_;
defined $href->{$elem} && $href->{$elem} =~ $regex
or die "parameter '$elem' not valid", "\n";
};
$check_re->($biller_information, 'iban', qr{^(?:CH|LI)[0-9a-zA-Z]{19}$});
$check_re->($biller_data, 'address_type', qr{^[KS]$});
$check_re->($biller_data, 'company', qr{^.{1,70}$});
$check_re->($biller_data, 'address_row1', qr{^.{0,70}$});
$check_re->($biller_data, 'address_row2', qr{^.{0,70}$});
$check_re->($biller_data, 'countrycode', qr{^[A-Z]{2}$});
$check_re->($payment_information, 'amount', qr{^(?:(?:0|[1-9][0-9]{0,8})\.[0-9]{2})?$});
$check_re->($payment_information, 'currency', qr{^(?:CHF|EUR)$});
$check_re->($invoice_recipient_data, 'address_type', qr{^[KS]$});
$check_re->($invoice_recipient_data, 'name', qr{^.{1,70}$});
$check_re->($invoice_recipient_data, 'address_row1', qr{^.{0,70}$});
$check_re->($invoice_recipient_data, 'address_row2', qr{^.{0,70}$});
$check_re->($invoice_recipient_data, 'countrycode', qr{^[A-Z]{2}$});
my %ref_nr_regexes = (
QRR => qr{^\d{27}$},
NON => qr{^$},
);
$check_re->($ref_nr_data, 'type', qr{^(?:QRR|SCOR|NON)$});
$check_re->($ref_nr_data, 'ref_number', $ref_nr_regexes{$ref_nr_data->{type}});
}
sub generate {
my $self = shift;
my $out_file = defined $_[0] ? $_[0] : $Config{out_file};
$self->{qrcode} = $self->_qrcode();
$self->{cross} = $self->_cross();
$self->{img} = $self->_plot();
$self->_paste();
$self->_write($out_file);
}
sub _qrcode {
my $self = shift;
return Imager::QRCode->new(
size => 3,
margin => 1,
level => 'M',
);
}
sub _cross {
my $self = shift;
my $cross = Imager->new();
$cross->read(file => $Config{cross_file}) or die $cross->errstr, "\n";
return $cross->scale(xpixels => 27, ypixels => 27, qtype => 'mixing');
}
sub _plot {
my $self = shift;
my @data = (
@{$self->{data}{header}},
@{$self->{data}{biller_information}},
@{$self->{data}{biller_data}},
('') x 7, # for future use
@{$self->{data}{payment_information}},
@{$self->{data}{invoice_recipient_data}},
@{$self->{data}{ref_nr_data}},
@{$self->{data}{additional_information}},
);
foreach (@data) {
s/[\r\n]/ /g;
s/ {2,}/ /g;
s/^\s+//;
s/\s+$//;
}
# CR + LF
my $text = join "\015\012", @data;
return $self->{qrcode}->plot($text);
}
sub _paste {
my $self = shift;
$self->{img}->paste(
src => $self->{cross},
left => ($self->{img}->getwidth / 2) - ($self->{cross}->getwidth / 2),
top => ($self->{img}->getheight / 2) - ($self->{cross}->getheight / 2),
);
}
sub _write {
my $self = shift;
my ($out_file) = @_;
$self->{img}->write(file => $out_file) or die $self->{img}->errstr, "\n";
}
1;
__END__
=encoding utf-8
=head1 NAME
SL::Helper::QrBill - Helper methods for generating Swiss QR-Code
=head1 SYNOPSIS
use SL::Helper::QrBill;
eval {
my $qr_image = SL::Helper::QrBill->new(
\%biller_information,
\%biller_data,
\%payment_information,
\%invoice_recipient_data,
\%ref_nr_data,
);
$qr_image->generate($outfile);
} or do {
local $_ = $@; chomp; my $error = $_;
$::form->error($::locale->text('QR-Image generation failed: ' . $error));
};
=head1 DESCRIPTION
This module generates the Swiss QR-Code with data provided to the constructor.
=head1 METHODS
=head2 C<new>
Creates a new object. Expects five references to hashes as arguments.
The hashes are structured as follows:
=over 4
=item C<%biller_information>
Fields: iban.
=over 4
=item C<iban>
Fixed length; 21 alphanumerical characters, only IBANs with CH- or LI-
country code.
=back
=item C<%biller_data>
Fields: address_type, company, address_row1, address_row2 and countrycode.
=over 4
=item C<address_type>
Fixed length; 1-digit, alphanumerical. 'K' implemented only.
=item C<company>
Maximum of 70 characters, name (surname allowable) or company.
=item C<address_row1>
Maximum of 70 characters, street/nr.
=item C<address_row2>
Maximum of 70 characters, postal code/place.
=item C<countrycode>
2-digit country code according to ISO 3166-1.
=back
=item C<%payment_information>
Fields: amount and currency.
=over 4
=item C<amount>
Decimal, no leading zeroes, maximum of 12 digits (inclusive decimal
separator and places). Only dot as decimal separator is permitted.
=item C<currency>
CHF/EUR.
=back
=item C<%invoice_recipient_data>
Fields: address_type, name, address_row1, address_row2 and countrycode.
=over 4
=item C<address_type>
Fixed length; 1-digit, alphanumerical. 'K' implemented only.
=item C<name>
Maximum of 70 characters, name (surname allowable) or company.
=item C<address_row1>
Maximum of 70 characters, street/nr.
=item C<address_row2>
Maximum of 70 characters, postal code/place.
=item C<countrycode>
2-digit country code according to ISO 3166-1.
=back
=item C<%ref_nr_data>
Fields: type and ref_number.
=over 4
=item C<type>
Maximum of 4 characters, alphanumerical. QRR/SCOR/NON.
=item C<ref_number>
QR-Reference: 27 characters, numerical; without Reference: empty.
=back
=back
=head2 C<generate>
Generates the QR-Code image. Accepts filename of image as argument.
Defaults to C<out.png>.
=head1 AUTHOR
Steven Schubiger E<lt>stsc@refcnt.orgE<gt>
=cut