blib/lib/Business/iDEAL/Adyen.pm

filter: Statementstmt 48.9% 45 / 92 Branchbran 25.0% 7 / 28 Conditioncond 12.5% 3 / 24 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 55.5% 10 / 18 Podpod 100% 5 / 5 totaltotal 39.2% 71 / 181 CC 30 SCAR 33.8
CC
30
Cov
38%
fetch
110.0
check
20.0
_url
12.0
CRAP 29.4
↑ Summary
1 package Business::iDEAL::Adyen;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.02';
13  
14 =pod
15  
16 =head1 NAME
17  
18 Business::iDEAL::Adyen - Backend for iDEAL payments through adyen.com
19  
20 =head1 SYNOPSIS
21  
22 use Business::iDEAL::Adyen;
23
24 # first setup the object
25 my $ideal = Business::iDEAL::Adyen->new({
26 shared_secret => 'your very secure secret',
27 skinCode => 's0m3C0d3',
28 merchantAccount => 'your merchant account',
29 test => 1,
30 });
31
32 # then fetch a list of bank ids and bank codes
33 my $banks = $ideal->banklist();
34
35 # after the user has chosen the bank he/she wants to use
36 # it's time to fetch the redirect URL
37 my $redir = $ideal->fetch({
38 # mandatory fields
39 bank_id => 1000,
40 paymentAmount => 1250,
41
42 # optional fields
43 merchantReference => 'your order ID',
44 currencyCode => 'EUR',
45 shopperLocale => 'nl',
46 shipBeforeDate => '2010-01-01',
47 sessionValidity => '2009-01-01T01:01:01Z',
48 });
49
50 # redirect your user to his/her bank, like
51 print redirect( $redir );
52  
53 After the user has finalized the payment, he/she'll be returned to
54 your website (as defined in the Adyen skin)
55  
56 use Business::iDEAL::Adyen;
57 use CGI qw/:standard/;
58
59 # first setup the objects
60 my $cgi = new CGI;
61 my $ideal = Business::iDEAL::Adyen->new({
62 shared_secret => 'your very secure secret',
63 skinCode =>
64 test => 1,
65 });
66
67 # check user input
68 if( $ideal->check( \%{$cgi->Vars} ) ) {
69
70 # payment succeeded, so you probably want to update your
71 # database with $cgi->param('merchantReference')
72
73 } else {
74  
75 # payment was not successful
76 # $ideal->error() contains what went wrong (most likely the
77 # request has been tampered with and the signature is incorrect)
78
79 }
80  
81  
82 =head1 DESCRIPTION
83  
84 Business::iDEAL::Adyen provides a backend to process iDEAL payments
85 through adyen.com (the non-HPP (Hosted Payment Pages) way).
86  
87 A word of warning to start with (copied verbatim out of Adyen's iDEAL PDF):
88  
89 iDeal API Payments are not enabled by default. If you would like to
90 process iDeal using this method, you can request this through the support
91 channel at L<https://support.adyen.com>.
92  
93 =head2 METHODS
94  
95 =head3 new
96  
97 C<new> creates a new C<Business::iDEAL::Adyen> object.
98  
99 =head4 options
100  
101 =over 5
102  
103 =item B<shared_secret> I<[mandatory]>
104  
105 This option should be the same as the secret entered
106 in the Adyen skin.
107  
108 =item B<skinCode> I<[mandatory]>
109  
110 The code of the skin we're using.
111  
112 =item B<merchantAccount> I<[mandatory]>
113  
114 The merchant account name
115  
116 =item B<test>
117  
118 A boolean value that switches on the use of the test environment.
119  
120 =back
121  
122 =cut
123  
124 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
125 1 my ($class, $args) = @_;
126 1 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
10
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
127 1 ▶ $class->_croak("Options must be a hash reference")
Branch: 1 branch
BranchTrueFalseTotal
if ref $args ne "HASH" 0 1 1
128 if ref($args) ne 'HASH';
129  
130 1 my $self = {};
131 1 bless $self, $class;
132  
133 # initialize the object
134 1 $self->_init($args);
135  
136 1 return $self;
137 }
138  
139 0 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=6.9
140 0 my $self = shift;
141 0 Carp::croak(@_);
142 }
143  
144 0 ▶sub _carp {
Subroutine _carp: not called CC=1 SCAR=6.9
145 0 my $self = shift;
146 0 Carp::carp(@_);
147 }
148  
149 1 ▶sub _init {
Subroutine _init: called CC=4 SCAR=13.9
150 1 my ($self, $args) = @_;
151  
152 # test for mandatory fields
153 1 for(qw/shared_secret skinCode merchantAccount/) {
154 3 ▶ $self->_croak("$_ not set") unless $args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{$_} 0 3 3
155 }
156  
157 # set some defaults and let user override if needed
158 my %options = (
159 ua => LWP::UserAgent->new(
160 agent => __PACKAGE__." v. ".$VERSION,
161 requests_redirectable => '',
162 ),
163 xso => XML::Simple->new(),
164 hmac => Digest::HMAC_SHA1->new(delete $args->{shared_secret}),
165 prod_base_url => 'https://live.adyen.com',
166 test_base_url => 'https://test.adyen.com',
167 banklist_path => '/hpp/idealbanklist.shtml',
168 redirect_path => '/hpp/redirectIdeal.shtml',
169 1 %{ $args }
170 );
171  
172 # map all keys to $self->{_$key} for easy access
173 1 $self->{"_$_"} = $options{$_} for (keys %options);
174 }
175  
176 0 ▶sub _url {
Subroutine _url: not called CC=3 SCAR=24.8
177 0 my ($self, $type) = @_;
178  
179 # determine what path we need and whether that exists or not
180 0 ▶ my $path = $self->{"_".$type."_path"} or
Branch: 1 branch
BranchTrueFalseTotal
my $path = $$self{'_' . $type . '_path'} or $self->_croak("Unknown type '${type}'") 0 0 0
181 $self->_croak("Unknown type '$type'");
182  
183 # return the test or production url based on $type input
184 return ($self->{_test} ? $self->{_test_base_url}
185 0 ▶ : $self->{_prod_base_url}).
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_test'} ? : 0 0 0
186 $path;
187 }
188  
189 0 ▶sub _parse_xml {
Subroutine _parse_xml: not called CC=2 SCAR=17.9
190 0 my ($self, $input) = @_;
191 0 ▶ return unless($input);
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 0 0
192  
193 0 return $self->{_xso}->XMLin($input);
194 }
195  
196 1 ▶sub _sign_req {
Subroutine _sign_req: called CC=9 SCAR=24.2
197 1 my ($self, $args) = @_;
198  
199 1 my $plaintext = '';
200 1 ▶ if($args->{paymentAmount}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'paymentAmount'}) { } 1 0 1
201 # Initial signature (the one we _send_)
202 1 for(qw/paymentAmount currencyCode shipBeforeDate merchantReference
203 skinCode merchantAccount sessionValidity shopperEmail
204 shopperReference allowedMethods blockedMethods/) {
205 $plaintext .= ( defined $self->{"_$_"} )
206 11 ▶ ? $self->{"_$_"} : ( $args->{$_} || "" );
Branch: 1 branch
BranchTrueFalseTotal
defined $$self{"_$_"} ? : 2 9 11
Condition100%2/2
$args->{$_} or ""
l!l
54
MC/DC100%1/1
$args->{$_} or ""
$args->{$_}
Truth table100%2/2
A
A= $args->{$_}
Aresult
11
00
207 }
208 } else {
209 # Second signature (the one we _receive_)
210 0 for(qw/authResult pspReference merchantReference skinCode/) {
211 $plaintext .= ( defined $self->{"_$_"} )
212 0 ▶ ? $self->{"_$_"} : ( $args->{$_} || "" );
Branch: 1 branch
BranchTrueFalseTotal
defined $$self{"_$_"} ? : 0 0 0
Condition0%0/2
$args->{$_} or ""
l!l
00
MC/DC0%0/1
$args->{$_} or ""
$args->{$_}
Truth table0%0/2
A
A= $args->{$_}
Aresult
11
00
213 }
214 }
215  
216 1 $self->{_hmac}->add($plaintext);
217 1 my $b64_digest = $self->{_hmac}->b64digest;
218 1 ▶ $b64_digest .= '=' while (length($b64_digest) % 4);
Branch: 1 branch
BranchTrueFalseTotal
length($b64_digest) % 4 and do { $b64_digest .= '=' } 1 1 2
219  
220 1 return $b64_digest;
221 }
222  
223 =pod
224  
225 =head3 banklist
226  
227 In order to offer all iDEAL banks, you will have to fetch a list
228 with their names and codes. This list is subject to change, so check
229 this often (Adyen recommends "regularly (e.g. once a day)").
230 I'd suggest to always check this before a payment.
231  
232 This method will return an arrayref with the bank_ids and bank_names,
233 or undef in case an error occured (see L<"error">)
234  
235 =cut
236  
237 0 ▶sub banklist {
Subroutine banklist: not called CC=2 SCAR=17.9
Pod documented
238 0 my $self = shift;
239  
240 0 my $res = $self->{_ua}->get($self->_url('banklist'));
241 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
242 0 return $self->_parse_xml($res->decoded_content)->{'bank'};
243 } else {
244 0 $self->{_error} = $res->status_line;
245 0 return undef;
246 }
247 }
248  
249 =pod
250  
251 =head3 fetch
252  
253 After you've retrieved the L<"banklist">, your users may choose the preferred
254 bank. Now you can feed that 'bank_id', together with the other mandatory
255 options to this method.
256  
257 C<fetch> will return an URL to the bank's iDEAL page that the user should
258 be directed to.
259  
260 Some fields are mandatory, while others have somewhat sane defaults and
261 may be skipped.
262  
263 =head4 options
264  
265 =over 5
266  
267 =item B<bank_id> I<[mandatory]>
268  
269 This is the bank ID chose by the user and provided by the L<"banklist">
270 method.
271  
272 =item B<paymentAmount> I<[mandatory]>
273  
274 How much would you like to charge your user? Note that this is in
275 cents, so 12,50 EUR should be inserted as 1250. If a dot or comma
276 is found in this value, it will be stripped. Don't count on this being
277 perfect, so sanitize your own input.
278  
279 =item B<merchantReference>
280  
281 This will normally be set to your order number, or anything that's useful
282 to identify the order with. If not set, a semi-random number is generated.
283  
284 =item B<currencyCode>
285  
286 Defaults to 'EUR', since iDEAL is a Dutch system and we Dutchmen "embraced"
287 the euro.
288  
289 =item B<shopperLocale>
290  
291 Defaults to 'nl'. Again, iDEAL is a Dutch system.
292  
293 =item B<shipBeforeDate>
294  
295 To make matters easy, we set a I<shipBeforeDate> of today + 1 month.
296  
297 =item B<sessionValidity>
298  
299 By default, we set this value to now + 1 hour, UTC. A user should
300 be able to finish his/her transaction within an hour.
301  
302 =back
303  
304 Other options, as described in the Adyen integration manual, like
305 I<shopperEmail>, could be passed in as well, but are completely
306 optional.
307  
308 =cut
309  
310 0 ▶sub fetch {
Subroutine fetch: not called CC=10 SCAR=47.0
Pod documented
311 0 my ($self, $parms) = @_;
312 0 ▶ $parms ||= {};
Condition0%0/2
$parms ||= {}
l!l
00
MC/DC0%0/1
$parms ||= {}
$parms
Truth table0%0/2
A
A= $parms
Aresult
11
00
313 0 ▶ $self->_croak("Parameters must be a hash reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parms eq "HASH" 0 0 0
314 unless ref($parms) eq 'HASH';
315  
316 # check for mandatory fields that we can't generate
317 0 for(qw/bank_id paymentAmount/) {
318 0 ▶ $self->_croak("$_ not set") unless($parms->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $parms->{$_} 0 0 0
319 }
320  
321 # make sure we have the paymentAmount in cents
322 0 $parms->{paymentAmount} =~ s![,\.]!!;
323  
324 # check for mandatory fields that we could generate where needed
325 0 ▶ $parms->{merchantReference} ||= int(rand(1000000)).time();
Condition0%0/3
$parms->{"merchantReference"} ||= int(rand 1000000) . time
l!l&&r!l&&!r
000
MC/DC0%0/2
$parms->{"merchantReference"} ||= int(rand 1000000) . time
$parms->{"merchantReference"} int(rand 1000000) . time
Truth table0%0/3
A ||= B
A= $parms->{"merchantReference"}B= int(rand 1000000) . time
ABresult
1X1
011
000
326 0 ▶ $parms->{currencyCode} ||= 'EUR';
Condition0%0/2
$parms->{"currencyCode"} ||= "EUR"
l!l
00
MC/DC0%0/1
$parms->{"currencyCode"} ||= "EUR"
$parms->{"currencyCode"}
Truth table0%0/2
A
A= $parms->{"currencyCode"}
Aresult
11
00
327 0 ▶ $parms->{shopperLocale} ||= 'nl';
Condition0%0/2
$parms->{"shopperLocale"} ||= "nl"
l!l
00
MC/DC0%0/1
$parms->{"shopperLocale"} ||= "nl"
$parms->{"shopperLocale"}
Truth table0%0/2
A
A= $parms->{"shopperLocale"}
Aresult
11
00
328 0 ▶ $parms->{shipBeforeDate} ||= DateTime->now()
Condition0%0/3
$parms->{"shipBeforeDate"} ||= "DateTime"->now->add("months", 1)->strftime("%F")
l!l&&r!l&&!r
000
MC/DC0%0/2
$parms->{"shipBeforeDate"} ||= "DateTime"->now->add("months", 1)->strftime("%F")
$parms->{"shipBeforeDate"} "DateTime"->now->add("months", 1)->strftime("%F")
Truth table0%0/3
A ||= B
A= $parms->{"shipBeforeDate"}B= "DateTime"->now->add("months", 1)->strftime("%F")
ABresult
1X1
011
000
329 ->add(months => 1)
330 ->strftime("%F");
331 0 ▶ $parms->{sessionValidity} ||= DateTime->now(time_zone => 'UTC')
Condition0%0/3
$parms->{"sessionValidity"} ||= "DateTime"->now("time_zone", "UTC")->add("hours", 1)->strftime("%FT%TZ")
l!l&&r!l&&!r
000
MC/DC0%0/2
$parms->{"sessionValidity"} ||= "DateTime"->now("time_zone", "UTC")->add("hours", 1)->strftime("%FT%TZ")
$parms->{"sessionValidity"} "DateTime"->now("time_zone", "UTC")->add("hours", 1)->strftime("%FT%TZ")
Truth table0%0/3
A ||= B
A= $parms->{"sessionValidity"}B= "DateTime"->now("time_zone", "UTC")->add("hours", 1)->strftime("%FT%TZ")
ABresult
1X1
011
000
332 ->add(hours => 1)
333 ->strftime("%FT%TZ");
334  
335 # set iDEAL settings
336 0 $parms->{skipSelection} = 'true';
337 0 $parms->{brandCode} = 'ideal';
338 0 $parms->{idealIssuerId} = delete $parms->{bank_id};
339  
340 # set globals
341 0 $parms->{skinCode} = $self->{_skinCode};
342 0 $parms->{merchantAccount} = $self->{_merchantAccount};
343  
344 # calculate and set the signature
345 0 $parms->{merchantSig} = $self->_sign_req($parms);
346  
347 # create URL
348 0 my $uri = URI->new( $self->_url('redirect') );
349 0 $uri->query_form( $parms );
350  
351 0 return $uri->as_string;
352 }
353  
354 =pod
355  
356 =head3 check
357  
358 When a user has proceeded the payment throught the bank's website, he/she'll
359 be returned to your website (as specified on the Adyen's skin pages).
360  
361 This method can be called to check whether the payment succeeded or not.
362 I<check> returns true when the payment was authorized and undef in
363 all other cases (see L<"error"> in that case).
364  
365 =cut
366  
367 0 ▶sub check {
Subroutine check: not called CC=4 SCAR=30.0
Pod documented
368 0 my ($self, $args) = @_;
369  
370 0 ▶ if($args->{merchantSig} && $args->{merchantSig} eq $self->_sign_req($args)){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'merchantSig'} and $args->{'merchantSig'} eq $self->_sign_req($args)) { } 0 0 0
Condition0%0/3
$args->{"merchantSig"} and $args->{"merchantSig"} eq $self->_sign_req($args)
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"merchantSig"} and $args->{"merchantSig"} eq $self->_sign_req($args)
$args->{"merchantSig"} $args->{"merchantSig"} eq $self->_sign_req($args)
Truth table0%0/3
A and B
A= $args->{"merchantSig"}B= $args->{"merchantSig"} eq $self->_sign_req($args)
ABresult
0X0
100
111
371 # Signature is ok
372 0 ▶ if($args->{authResult} eq 'AUTHORISED') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'authResult'} eq 'AUTHORISED') { } 0 0 0
373 # Payment is OK
374 0 return 1;
375 } else {
376 # Payment failed
377 0 $self->{_error} = "Payment status: ".$args->{authResult};
378 }
379 } else {
380 # Signature failed
381 0 $self->{_error} = "Merchant Signature was incorrect";
382 }
383 0 return undef;
384 }
385  
386 =pod
387  
388 =head3 error
389  
390 If errors occur (most likely signature related), this method will return
391 the latest error that occured.
392  
393 =cut
394  
395 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
396 0 my $self = shift;
397 0 return $self->{_error};
398 }
399  
400  
401 =pod
402  
403 =head1 SEE ALSO
404  
405 =over 5
406  
407 =item * L<http://www.ideal.nl>
408  
409 =item * L<https://support.adyen.com/index.php>
410  
411 =item * L<http://menno.b10m.net/perl/>
412  
413 =back
414  
415 =head1 BUGS
416  
417 Please report any bugs to L<http://rt.cpan.org/Ticket/Create.html?Queue=Business-iDEAL-Adyen>.
418  
419 =head1 AUTHOR
420  
421 Menno Blom,
422 E<lt>blom@cpan.orgE<gt>,
423 L<http://menno.b10m.net/perl/>
424  
425 =head1 COPYRIGHT AND LICENSE
426  
427 Copyright (C) 2009 by Menno Blom
428  
429 This program is free software; you can redistribute
430 it and/or modify it under the same terms as Perl itself.
431  
432 The full text of the license can be found in the
433 LICENSE file included with this module.
434  
435 =cut
436  
437 1 1;
↑ Summary