blib/lib/Business/OnlinePayment/PayPal.pm

filter: Statementstmt 96.7% 88 / 91 Branchbran 78.9% 30 / 38 Conditioncond 60.0% 24 / 40 MC/DCmcdc 13.6% 3 / 22 Subroutinesub 84.6% 11 / 13 Podpod 100% 8 / 8 totaltotal 77.3% 164 / 212 CC 36 SCAR 22.9
CC
36
Cov
84%
get_request_data
20.4
parse_expiration
9.2
get_remap_fields
8.1
CRAP 9.8
↑ Summary
1 #
2 # $Id: PayPal.pm,v 1.5 2007/02/16 04:48:34 plobbes Exp $
3  
4 package Business::OnlinePayment::PayPal;
5  
6 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use base qw(Business::OnlinePayment);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Business::PayPal::API qw(DirectPayments);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.11';
13 1 $VERSION = eval $VERSION;
14  
15 =head1 NAME
16  
17 Business::OnlinePayment::PayPal - PayPal backend for Business::OnlinePayment
18  
19 =head1 SYNOPSIS
20  
21 use Business::OnlinePayment;
22
23 my $tx = Business::OnlinePayment->new(
24 "PayPal",
25 "Username" => "my_api1.domain.tld",
26 "Password" => "Xdkis9k3jDFk39fj29sD9", ## supplied by PayPal
27 "Signature" => "f7d03YCpEjIF3s9Dk23F2...", ## supplied by PayPal
28 );
29
30 $tx->content(
31 action => "Normal Authorization",
32 amount => "19.95",
33 type => "Visa",
34 card_number => "4111111111111111",
35 expiration => "01/10",
36 cvv2 => "123",
37 name => "John Doe",
38 address => "123 My Street",
39 city => "Chicago",
40 state => "IL",
41 zip => "61443",
42 IPAddress => "10.0.0.1",
43 );
44
45 $tx->test_transaction(1);
46 $tx->submit;
47
48 if ( $tx->is_success ) {
49 print(
50 "SUCCESS:\n",
51 " CorrelationID: ", $tx->correlationid, "\n",
52 " auth: ", $tx->authorization, "\n",
53 " AVS code: ", $tx->avs_code, "\n",
54 " CVV2 code: ", $tx->cvv2_code, "\n",
55 );
56 }
57 else {
58 print(
59 "ERROR: ", $tx->error_message, "\n"
60 );
61 }
62  
63 =head1 DESCRIPTION
64  
65 Business::OnlinePayment::PayPal is a plugin for using PayPal as a
66 payment processor backend with the Business::OnlinePayment API.
67 Specifically, this module uses PayPal's 'DoDirectPayment' operation
68 which utilizes the 'DoDirectPaymentRequest' message type.
69  
70 This module does not do any checks to be sure that all the required
71 fields/arguments/attributes/values, per PayPal's WSDL/XSD, have been
72 provided. In general, PayPal's service will catch errors and return
73 relevant information. However when requests do not meet the minimum
74 message format/structure requirements or if the request contains
75 information not supported by the 'DoDirectPaymentRequest' very generic
76 errors (i.e. PPBaseException) may be sent to STDERR by underlying
77 modules and our response data structure may be completely empty.
78  
79 Anyone using this module or any modules that talk to PayPal should
80 familiarize themselves with the information available at PayPal's
81 integration center. See the L</SEE ALSO> section for links to useful
82 reference material.
83  
84 =head1 METHODS
85  
86 The following methods exist for use with this module.
87  
88 =head2 Convenience methods
89  
90 =over 4
91  
92 =item authorization()
93  
94 Provides access to the TransactionID returned in the PayPal results.
95 This method is part of the Business::OnlinePayment "standard" API.
96  
97 =item transactionid()
98  
99 This method is an alias for the L</authorization()> method.
100  
101 =item correlationid()
102  
103 Provides access to the CorrelationID returned in the PayPal results.
104  
105 =item order_number()
106  
107 This method is an alias for the L</correlationid()> method. It is
108 provided for compatibility with the PayflowPro backend.
109  
110 =item server_response()
111  
112 Provides access, via a hashref, to the results hash returned in the
113 Business::PayPal::API results object returned by
114 DoDirectPaymentRequest. This method is part of the
115 Business::OnlinePayment "standard" API.
116  
117 =item result_code()
118  
119 Returns "" or the first ErrorCode returned from
120 DoDirectPaymentRequest. This method is part of the
121 Business::OnlinePayment "standard" API.
122  
123 =item avs_code()
124  
125 Returns the AVSCode returned from DoDirectPaymentRequest.
126  
127 =item cvv2_code()
128  
129 Returns the CVV2Code returned from DoDirectPaymentRequest.
130  
131 =item is_success()
132  
133 Returns 1 or 0 on success or failure of DoDirectPaymentRequest. This
134 method is part of the Business::OnlinePayment "standard" API.
135  
136 =item error_message()
137  
138 Returns a string containing an error message, if any. This method is
139 part of the Business::OnlinePayment "standard" API.
140  
141 =back
142  
143 =head2 set_defaults()
144  
145 Creates accessor methods L</avs_code()>, L</correlationid()>,
146 L</cvv2_code()> and __map_fields_data (see L</get_request_data>).
147  
148 =cut
149  
150 9 ▶sub set_defaults {
Subroutine set_defaults: called CC=1 SCAR=0.0
Pod documented
151 9 my $self = shift;
152  
153 9 $self->build_subs(qw(avs_code correlationid cvv2_code __map_fields_data));
154  
155 9 $self->__map_fields_data(
156 {
157 PaymentAction => "action",
158 OrderTotal => "amount", # Payment Detail
159  
160 # Credit Card
161 CreditCardType => "type",
162 CreditCardNumber => "card_number",
163 CVV2 => undef,
164  
165 # Card Owner / Payer Name
166 Payer => "email",
167 FirstName => "name",
168 LastName => undef,
169  
170 # Payer Address
171 Street1 => "address",
172 Street2 => undef,
173 CityName => "city",
174 StateOrProvince => "state",
175 Country => "country",
176 PostalCode => "zip",
177 }
178 );
179 }
180  
181 0 ▶sub transactionid { shift()->authorization(@_); }
Subroutine transactionid: not called CC=1 SCAR=6.9
Pod documented
182  
183 0 ▶sub order_number { shift()->correlationid(@_); }
Subroutine order_number: not called CC=1 SCAR=6.9
Pod documented
184  
185 =head2 get_credentials()
186  
187 Get the credential information for Business::PayPal::API that was
188 provided to Business::OnlinePayment::new(). The supported arguments
189 are:
190  
191 =over 4
192  
193 =item * Username Password PKCS12File PKCS12Password
194  
195 =item * Username Password CertFile KeyFile
196  
197 =item * Username Password Signature
198  
199 =back
200  
201 Business::OnlinePayment::PayPal does not currently map arguments to
202 new() from (standard?) names to the PayPal backend specific name. For
203 example, if the argument "login" were passed to new() the module could
204 potentially try to identify that and map that to "Username".
205  
206 NOTE: This requirement/capability seems to be more of a
207 Business::OnlinePayment issue than a backend issue and it isn't clear
208 if behavior like this is needed in this module so I will wait for user
209 feedback to determine if we need/want to implement this.
210  
211 =cut
212  
213 2 ▶sub get_credentials {
Subroutine get_credentials: called CC=5 SCAR=16.1
Pod documented
214 2 my $self = shift;
215  
216 2 my %credentials;
217 2 my @cred_vars = (
218 [qw(PKCS12File PKCS12Password)],
219 [qw(CertFile KeyFile)], [qw(Signature)],
220 );
221  
222 2 foreach my $aref (@cred_vars) {
223 6 my $need = 0;
224 6 my @vars = ( qw(Username Password), @$aref );
225  
226 6 foreach my $var (@vars) {
227  
228 # HACK: Business::OnlinePayment makes method lower case
229 22 my $method = lc($var);
230 22 ▶ if ( $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 7 15 22
231 7 $credentials{$var} = $self->$method;
232 }
233 else {
234 15 $need++;
235 }
236 }
237  
238 6 ▶ if ($need) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need) { } 5 1 6
239 5 undef %credentials;
240 }
241 else {
242 1 last;
243 }
244 }
245 2 return %credentials;
246 }
247  
248 =head2 get_request_data()
249  
250 Return a hash %data with all the data from content() that we will try
251 to use in our request to PayPal. Tasks performed:
252  
253 =over 4
254  
255 =item *
256  
257 Remove unsupported values from our hash (i.e. description fax login
258 password phone).
259  
260 =item *
261  
262 Translate the value in "action" if necessary, from
263 Business::OnlinePayment names to names used by PayPal. Translations
264 used are:
265  
266 "normal authorization" => "Sale"
267 "authorization only" => "Authorization"
268 "void" => "None"
269  
270 =item *
271  
272 Translate the value in "type" if necessary, from
273 Business::OnlinePayment names to names used by PayPal. See
274 L</normalize_creditcardtype()> for details.
275  
276 =item *
277  
278 If necessary, separate ExpMonth and ExpYear values from the single
279 "standard" Business::OnlinePayment "expiration" field. See
280 L</parse_expiration()> for details.
281  
282 =item *
283  
284 Call get_remap_fields to map content() into the %data that we will
285 pass to PayPal. All fields not "mapped" will be passed AS-IS. The
286 mapping used is (map hashref stored in __map_fields_data()):
287  
288 PaymentAction => "action"
289 # Payment Detail
290 OrderTotal => "amount"
291 # Credit Card
292 CreditCardType => "type"
293 CreditCardNumber => "card_number"
294 CVV2 => undef
295 # Card Owner / Payer Name
296 Payer => "email"
297 FirstName => "name"
298 LastName => undef
299 # Payer Address
300 Street1 => "address"
301 Street2 => undef
302 CityName => "city"
303 StateOrProvince => "state"
304 Country => "country"
305 PostalCode => "zip"
306  
307 NOTE: an 'undef' on the right hand side means that field will be
308 looked for as the mixed-case name specified on the left and also as an
309 all lower-case name).
310  
311 =back
312  
313 =cut
314  
315 1 ▶sub get_request_data {
Subroutine get_request_data: called CC=13 SCAR=30.2
Pod documented
316 1 my $self = shift;
317 1 my %content = $self->content;
318  
319 1 ▶ return () unless (%content);
Branch: 1 branch
BranchTrueFalseTotal
unless %content 0 1 1
320  
321 # remove some unsupported content
322 # others? description, invoice_number, customer_id
323 1 delete @content{qw(description fax login password phone)};
324  
325 # action: map "standard" names to supported as needed
326 1 ▶ if ( $content{action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content{"action"}) 1 0 1
327 1 my $act = lc( $content{action} );
328 1 my %actions = (
329 "normal authorization" => "Sale",
330 "authorization only" => "Authorization",
331 "void" => "None",
332 );
333 1 ▶ $content{action} = $actions{$act} || $content{action};
Condition33%1/3
$actions{$act} || $content{'action'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$actions{$act} || $content{'action'}
$actions{$act} $content{'action'}
Truth table33%1/3
A || B
A= $actions{$act}B= $content{'action'}
ABresult
1X1
011
000
334 }
335  
336 # type: translate to supported CreditCardType values
337 1 ▶ if ( $content{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content{"type"}) 1 0 1
338 1 my $type = $content{type};
339 1 ▶ $content{type} = $self->normalize_creditcardtype($type) || $type;
Condition33%1/3
$self->normalize_creditcardtype($type) || $type
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->normalize_creditcardtype($type) || $type
$self->normalize_creditcardtype($type) $type
Truth table33%1/3
A || B
A= $self->normalize_creditcardtype($type)B= $type
ABresult
1X1
011
000
340 }
341  
342 # expiration: need separate month and year values
343 1 ▶ if ( $content{expiration}
Branch: 1 branch
BranchTrueFalseTotal
if ($content{"expiration"} and not $content{"ExpMonth"} && $content{"ExpYear"}) 1 0 1
Condition33%2/6
$content{"expiration"} and not $content{"ExpMonth"} && $content{"ExpYear"}
!ll&&!rl&&r
001
$content{'ExpMonth'} && $content{'ExpYear'}
!ll&&!rl&&r
100
MC/DC0%0/3
$content{"expiration"} and not $content{"ExpMonth"} && $content{"ExpYear"}
$content{"expiration"} $content{'ExpMonth'} $content{'ExpYear'}
Truth table25%1/4
A and not(B && C)
A= $content{"expiration"}B= $content{'ExpMonth'}C= $content{'ExpYear'}
ABCresult
0XX0
1110
10X1
1101
344 and ( !$content{ExpMonth} or !$content{ExpYear} ) )
345 {
346 1 my $exp = $content{expiration};
347 1 delete $content{expiration};
348  
349 # we only set ExpMonth/ExpYear if they aren't already set
350 1 my ( $y, $m ) = $self->parse_expiration($exp);
351 1 ▶ if ( $m and !$content{ExpMonth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m and not $content{"ExpMonth"}) 1 0 1
Condition33%1/3
$m and not $content{"ExpMonth"}
!ll&&!rl&&r
001
MC/DC0%0/2
$m and not $content{"ExpMonth"}
$m not $content{"ExpMonth"}
Truth table33%1/3
A and not(B)
A= $mB= not $content{"ExpMonth"}
ABresult
0X0
100
111
352 1 $content{ExpMonth} = $m;
353 }
354 1 ▶ if ( $y and !$content{ExpYear} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y and not $content{"ExpYear"}) 1 0 1
Condition33%1/3
$y and not $content{"ExpYear"}
!ll&&!rl&&r
001
MC/DC0%0/2
$y and not $content{"ExpYear"}
$y not $content{"ExpYear"}
Truth table33%1/3
A and not(B)
A= $yB= not $content{"ExpYear"}
ABresult
0X0
100
111
355 1 $content{ExpYear} = $y;
356 }
357 }
358  
359 1 my %data = $self->get_remap_fields(
360 content => \%content,
361 map => $self->__map_fields_data,
362 );
363 1 return %data;
364 }
365  
366 =head2 submit()
367  
368 Method that overrides the superclass stub. This method performs the
369 following tasks:
370  
371 =over 4
372  
373 =item *
374  
375 Get credentials to be used for authentication with PayPal by calling
376 L</get_credentials()>.
377  
378 =item *
379  
380 Get request data to be passed to PayPal by calling
381 L</get_request_data()>.
382  
383 =item *
384  
385 Connect to PayPal and perform a DirectPaymentRequest. The request
386 will be run in test mode (i.e. go to PayPal's "sandbox") if
387 test_transaction() returns true. NOTE: I believe PayPal automatically
388 does AVS checking if possible.
389  
390 =item *
391  
392 Store the entire response in server_response().
393  
394 =item *
395  
396 Set result_code() to "" or the first ErrorCode in Errors (if present).
397  
398 =item *
399  
400 Set avs_code() to the response AVSCode.
401  
402 =item *
403  
404 Set cvv2_code() to the response CVV2Code.
405  
406 =item *
407  
408 Set is_success() to 1 or 0, indicating if the transaction was
409 successful or not.
410  
411 =item *
412  
413 On success, set authorization() with the value of TransactionID. On
414 failure, set error_message() with a string containing all ErrorCode
415 and LongMessage values joined together.
416  
417 =back
418  
419 =cut
420  
421 sub submit {
422 my $self = shift;
423  
424 my %credentials = $self->get_credentials;
425 my %request = $self->get_request_data;
426  
427 my $pp =
428 Business::PayPal::API->new( %credentials,
429 sandbox => $self->test_transaction, );
430  
431 my %resp = $pp->DoDirectPaymentRequest(%request);
432  
433 $self->server_response( \%resp );
434 $self->result_code( $resp{Errors} ? $resp{Errors}->[0]->{ErrorCode} : "" );
435 $self->avs_code( $resp{AVSCode} );
436 $self->cvv2_code( $resp{CVV2Code} );
437  
438 if ( $resp{Ack} and $resp{Ack} eq "Success" ) {
439 $self->is_success(1);
440 $self->authorization( $resp{TransactionID} );
441 $self->correlationid( $resp{CorrelationID} );
442 }
443 else {
444 $self->is_success(0);
445 }
446  
447 if ( $resp{Errors} and @{ $resp{Errors} } ) {
448 my $error = join( "; ",
449 map { $_->{ErrorCode} . ": " . $_->{LongMessage} }
450 @{ $resp{Errors} } );
451 $self->error_message($error);
452 }
453  
454 return $self->is_success;
455 }
456  
457 =head2 get_remap_fields()
458  
459 Options:
460 content => $href (default: { $self->content } )
461 map => $href (default: { } )
462  
463 Combines some of the functionality of get_fields and remap_fields for
464 convenience and also extends/alters their behavior. Unlike
465 Business::OnlinePayment::remap_fields, this doesn't modify content(),
466 and can therefore be called more than once. Also, unlike
467 Business::OnlinePayment::get_fields in 3.x, this doesn't exclude
468 fields content with a value of undef.
469  
470 =cut
471  
472 2 ▶sub get_remap_fields {
Subroutine get_remap_fields: called CC=8 SCAR=20.9
Pod documented
473 2 my ( $self, %opt ) = @_;
474  
475 2 ▶ my $content = $opt{content} || { $self->content };
Condition50%1/2
$opt{'content'} || {$self->content}
l!l
20
MC/DC0%0/1
$opt{'content'} || {$self->content}
$opt{'content'}
Truth table50%1/2
A
A= $opt{'content'}
Aresult
11
00
476 2 ▶ my $map = $opt{map} || {};
Condition50%1/2
$opt{'map'} || {}
l!l
20
MC/DC0%0/1
$opt{'map'} || {}
$opt{'map'}
Truth table50%1/2
A
A= $opt{'map'}
Aresult
11
00
477 2 my %data;
478  
479 2 ▶ while ( my ( $to, $from ) = each %$map ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$map) 17 2 19
480 17 my $tolc = lc($to);
481 17 my $v;
482 17 ▶ if ( defined $from ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 13 4 17
483 13 $v = $content->{$from};
484 13 delete $content->{$from};
485 }
486 17 ▶ $v ||= $content->{$to} || $content->{$tolc};
Condition83%5/6
$v ||= $content->{$to} || $content->{$tolc}
l!l&&r!l&&!r
5210
$content->{$to} || $content->{$tolc}
l!l&&r!l&&!r
0210
MC/DC0%0/3
$v ||= $content->{$to} || $content->{$tolc}
$v $content->{$to} $content->{$tolc}
Truth table0%0/4
A ||= B || C
A= $vB= $content->{$to}C= $content->{$tolc}
ABCresult
1XX1
01X1
0011
0000
487 17 delete @$content{ $to, $tolc };
488  
489 17 ▶ if ( defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 7 10 17
490 7 $data{$to} = $v;
491 }
492 }
493  
494 2 %data = ( %$content, %data );
495  
496 2 return %data;
497 }
498  
499 =head2 normalize_creditcardtype()
500  
501 Attempt to normalize the credit card type to names supported by
502 PayPal. If the module is unable to identify the given type it leaves
503 the value AS-IS and leaves it to PayPal to do what it can with the
504 data given. Supported card types are:
505  
506 Visa | MasterCard | Discover | Amex
507  
508 Translations used are:
509  
510 /^vis/i => "Visa"
511 /^mas/i => "MasterCard"
512 /^ame/i => "Amex"
513 /^dis/i => "Discover"
514  
515 =cut
516  
517 6 ▶sub normalize_creditcardtype {
Subroutine normalize_creditcardtype: called CC=5 SCAR=16.1
Pod documented
518 6 my ( $self, $cctype ) = @_;
519  
520 6 ▶ if ( $cctype =~ /^vis/i ) { $cctype = "Visa"; }
Branch: 4 branches
BranchTrueFalseTotal
if ($cctype =~ /^vis/i) { } 2 4 6
elsif ($cctype =~ /^mas/i) { } 1 3 4
elsif ($cctype =~ /^ame/i) { } 1 2 3
elsif ($cctype =~ /^dis/i) { } 1 1 2
521 1 elsif ( $cctype =~ /^mas/i ) { $cctype = "MasterCard"; }
522 1 elsif ( $cctype =~ /^ame/i ) { $cctype = "Amex"; }
523 1 elsif ( $cctype =~ /^dis/i ) { $cctype = "Discover"; }
524 else {
525  
526 # Credit Card type '$cctype' not known
527 }
528 6 return ($cctype);
529 }
530  
531 =head2 parse_expiration()
532  
533 Business::OnlinePayment documents the use of a single expiration or
534 exp_date value. However PayPal requires separate values for both the
535 month and year. There are multiple formates that expiration dates are
536 often specified in so, we try to our best to handle them all.
537  
538 The following formats are supported:
539  
540 YYYY[.-]MM, YYYY[.-]M, YY[-/]M, YY[.-]MM
541 MM[-/]YYYY, M[-/]YYYY, M[-/]YY, MM/YY, MMYY
542  
543 NOTE: this method is based on the parse_exp method found in
544 L<Business::OnlinePayment::InternetSecure|Business::OnlinePayment::InternetSecure>.
545  
546 If an unrecognized format is encountered this method it will return an
547 empty list and leave it to PayPal to do what it can with the data
548 given. To avoid having this module attempt to parse 'expiration'
549 explicitly set ExpMonth and ExpYear in content().
550  
551 =cut
552  
553 9 ▶sub parse_expiration {
Subroutine parse_expiration: called CC=9 SCAR=22.2
Pod documented
554 9 my ( $self, $exp ) = @_;
555 9 my ( $y, $m );
556  
557 9 ▶ return () unless ($exp);
Branch: 1 branch
BranchTrueFalseTotal
unless $exp 0 9 9
558  
559 9 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($exp =~ /^(\d{4})[.-](\d{1,2})$/ or $exp =~ m[^(\d\d)[-/](\d)$] or $exp =~ /^(\d\d)[.-](\d\d)$/) { } 4 5 9
elsif ($exp =~ m[^(\d{1,2})[-/](\d{4})$] or $exp =~ m[^(\d)[-/](\d\d)$] or $exp =~ m[^(\d\d)/?(\d\d)$]) { } 5 0 5
Condition100%6/6
$exp =~ /^(\d{4})[.-](\d{1,2})$/ or $exp =~ m[^(\d\d)[-/](\d)$] or $exp =~ /^(\d\d)[.-](\d\d)$/
l!l&&r!l&&!r
315
$exp =~ /^(\d{4})[.-](\d{1,2})$/ or $exp =~ m[^(\d\d)[-/](\d)$]
l!l&&r!l&&!r
216
MC/DC100%3/3
$exp =~ /^(\d{4})[.-](\d{1,2})$/ or $exp =~ m[^(\d\d)[-/](\d)$] or $exp =~ /^(\d\d)[.-](\d\d)$/
$exp =~ /^(\d{4})[.-](\d{1,2})$/ $exp =~ m[^(\d\d)[-/](\d)$] $exp =~ /^(\d\d)[.-](\d\d)$/
Truth table100%4/4
A or B or C
A= $exp =~ /^(\d{4})[.-](\d{1,2})$/B= $exp =~ m[^(\d\d)[-/](\d)$]C= $exp =~ /^(\d\d)[.-](\d\d)$/
ABCresult
1XX1
01X1
0011
0000
560 $exp =~ /^(\d{4})[.-](\d{1,2})$/ || # YYYY[.-]MM or YYYY[.-]M
561 $exp =~ /^(\d\d)[-\/](\d)$/ || # YY[-/]M
562 $exp =~ /^(\d\d)[.-](\d\d)$/
563 ) # YY[.-]MM
564 {
565 4 ( $y, $m ) = ( $1, $2 );
566 }
567 ▶ elsif (
Condition83%5/6
$exp =~ m[^(\d{1,2})[-/](\d{4})$] or $exp =~ m[^(\d)[-/](\d\d)$] or $exp =~ m[^(\d\d)/?(\d\d)$]
l!l&&r!l&&!r
410
$exp =~ m[^(\d{1,2})[-/](\d{4})$] or $exp =~ m[^(\d)[-/](\d\d)$]
l!l&&r!l&&!r
311
MC/DC0%0/3
$exp =~ m[^(\d{1,2})[-/](\d{4})$] or $exp =~ m[^(\d)[-/](\d\d)$] or $exp =~ m[^(\d\d)/?(\d\d)$]
$exp =~ m[^(\d{1,2})[-/](\d{4})$] $exp =~ m[^(\d)[-/](\d\d)$] $exp =~ m[^(\d\d)/?(\d\d)$]
Truth table75%3/4
A or B or C
A= $exp =~ m[^(\d{1,2})[-/](\d{4})$]B= $exp =~ m[^(\d)[-/](\d\d)$]C= $exp =~ m[^(\d\d)/?(\d\d)$]
ABCresult
1XX1
01X1
0011
0000
568 $exp =~ /^(\d{1,2})[-\/](\d{4})$/ || # MM[-/]YYYY or M[-/]YYYY
569 $exp =~ /^(\d)[-\/](\d\d)$/ || # M[-/]YY
570 $exp =~ /^(\d\d)\/?(\d\d)$/
571 ) # MM/YY or MMYY
572 {
573 5 ( $y, $m ) = ( $2, $1 );
574 }
575 else {
576 0 return (); # unable to parse expiration date '$exp'
577 }
578  
579 # HACK: add the current century - 1
580 9 ▶ if ( $y < 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 100) 4 5 9
581 4 $y += int( ( ( localtime(time) )[5] + 1900 ) / 100 ) * 100;
582 }
583  
584 9 return ( $y, sprintf( "%02.0f", $m ) );
585 }
586  
587 1 1;
588  
589 __END__
↑ Summary