blib/lib/Data/FormValidator/Constraints/CreditCard.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 28 / 28 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 5 / 5 totaltotal 96.9% 128 / 132 CC 18 SCAR 12.2
CC
18
Cov
98%
__ANON__
7.0
__ANON__
5.0
_match_cc_expiry_month
4.0
CRAP 3.4
↑ Summary
1 package Data::FormValidator::Constraints::CreditCard;
2  
3 ###############################################################################
4 # Required inclusions.
5 ###############################################################################
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Business::CreditCard qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 ###############################################################################
11 # Version number.
12 ###############################################################################
13 2 our $VERSION = '0.05';
14  
15 ###############################################################################
16 # Allow our methods to be exported.
17 ###############################################################################
18 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use vars qw( @EXPORT_OK %EXPORT_TAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 @EXPORT_OK = qw(
22 FV_cc_number
23 FV_cc_type
24 FV_cc_expiry
25 FV_cc_expiry_month
26 FV_cc_expiry_year
27 );
28 2 %EXPORT_TAGS = (
29 'all' => [@EXPORT_OK],
30 );
31  
32 ###############################################################################
33 # Subroutine: FV_cc_number()
34 ###############################################################################
35 # Creates a constraint closure that returns true if the constrained value
36 # appears to be a valid credit card number.
37 #
38 # NOTE: "appears to be a valid credit card number" ONLY means that the number
39 # appears to be valid and has passed the checksum test; -NO- tests have been
40 # performed to verify that this is actually a real/valid credit card number.
41 ###############################################################################
42 4 ▶sub FV_cc_number {
Subroutine FV_cc_number: called CC=1 SCAR=0.0
Pod documented
43 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 4 my $dfv = shift;
45 4 my $val = $dfv->get_current_constraint_value();
46 4 return Business::CreditCard::validate($val);
47 4 };
48 }
49  
50 ###############################################################################
51 # Subroutine: FV_cc_type(@set)
52 ###############################################################################
53 # Creates a constraint closure that returns true if the constrained value
54 # appears to be a credit card of one of the types listed in the given '@set'.
55 # The '@set' can be provided as either a list of scalars (which are compared
56 # using the 'eq' operator), or as a list of regular expressions.
57 #
58 # For more information on the actual card types that can be checked for, please
59 # refer to the information for the 'cardtype()' method in
60 # 'Business::CreditCard'.
61 ###############################################################################
62 5 ▶sub FV_cc_type {
Subroutine FV_cc_type: called CC=1 SCAR=0.0
Pod documented
63 5 my (@set) = @_;
64 5 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
65 5 my $dfv = shift;
66 5 my $val = $dfv->get_current_constraint_value();
67 5 my $type = Business::CreditCard::cardtype($val);
68 5 foreach my $elem (@set) {
69 7 ▶ if (ref($elem) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elem eq 'Regexp') { } 3 4 7
70 3 ▶ return 1 if ($type =~ $elem);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /$elem/ 2 1 3
71 }
72 else {
73 4 ▶ return 1 if ($type eq $elem);
Branch: 1 branch
BranchTrueFalseTotal
if $type eq $elem 2 2 4
74 }
75 }
76 1 return;
77 }
78 5 }
79  
80 ###############################################################################
81 # Subroutine: FV_cc_expiry()
82 ###############################################################################
83 # Creates a constraint closure that returns true if the constrained value
84 # appears to be a valid credit card expiry date; correct integer values for
85 # year/month, with the date not being in the past.
86 #
87 # Accepted formats include "MM/YY" and "MM/YYYY".
88 #
89 # NOTE: use of this method requires that the full credit card expiry date be
90 # present in a single field; no facilities are provided for gathering the
91 # month/year data from two separate fields.
92 ###############################################################################
93 15 ▶sub FV_cc_expiry {
Subroutine FV_cc_expiry: called CC=1 SCAR=0.0
Pod documented
94 14 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
95 14 my $dfv = shift;
96 14 my $val = $dfv->get_current_constraint_value();
97 14 my ($month, $year) = split('/', $val);
98 14 ▶ return if ((!defined $month) or (!defined $year));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $month and defined $year 1 13 14
Condition66%2/3
defined $month and defined $year
!ll&&!rl&&r
0113
MC/DC50%1/2
defined $month and defined $year
defined $month defined $year
Truth table66%2/3
A and B
A= defined $monthB= defined $year
ABresult
0X0
100
111
99 # verify each field individually
100 13 ▶ return if (!_match_cc_expiry_month($month));
Branch: 1 branch
BranchTrueFalseTotal
unless _match_cc_expiry_month($month) 6 7 13
101 7 ▶ return if (!_match_cc_expiry_year($year));
Branch: 1 branch
BranchTrueFalseTotal
unless _match_cc_expiry_year($year) 4 3 7
102 # verify that date is not in the past
103 3 my @now = localtime();
104 3 $year = _windowize_year($year);
105 3 ▶ return if ($year == ($now[5]+1900) and $month <= ($now[4]+1));
Branch: 1 branch
BranchTrueFalseTotal
if $year == $now[5] + 1900 and $month <= $now[4] + 1 1 2 3
Condition66%2/3
$year == $now[5] + 1900 and $month <= $now[4] + 1
!ll&&!rl&&r
201
MC/DC50%1/2
$year == $now[5] + 1900 and $month <= $now[4] + 1
$year == $now[5] + 1900 $month <= $now[4] + 1
Truth table66%2/3
A and B
A= $year == $now[5] + 1900B= $month <= $now[4] + 1
ABresult
0X0
100
111
106 # looks good!
107 2 return "$month/$year";
108 }
109 15 }
110  
111 12 ▶sub _windowize_year {
Subroutine _windowize_year: called CC=3 SCAR=11.0
112 12 my $year = shift;
113 12 ▶ if ($year < 1900) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year < 1900) 5 7 12
114 5 ▶ $year += ($year < 70) ? 2000 : 1900;
Branch: 1 branch
BranchTrueFalseTotal
$year < 70 ? : 3 2 5
115 }
116 12 return $year;
117 }
118  
119 24 ▶sub _match_cc_expiry_month {
Subroutine _match_cc_expiry_month: called CC=4 SCAR=13.9
120 24 my $val = shift;
121 24 ▶ return if ($val =~ /\D/); # only contain numerics
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\D/ 3 21 24
122 21 ▶ return if ($val < 1); # can't be <1
Branch: 1 branch
BranchTrueFalseTotal
if $val < 1 4 17 21
123 17 ▶ return if ($val > 12); # can't be >12
Branch: 1 branch
BranchTrueFalseTotal
if $val > 12 4 13 17
124 13 return $val;
125 }
126  
127 13 ▶sub _match_cc_expiry_year {
Subroutine _match_cc_expiry_year: called CC=3 SCAR=11.0
128 13 my $val = shift;
129 13 my $now = (localtime)[5] + 1900;
130 13 ▶ return if ($val =~ /\D/); # only contain numerics
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\D/ 4 9 13
131 9 $val = _windowize_year($val);
132 9 ▶ return if ($val < $now); # can't be before this year
Branch: 1 branch
BranchTrueFalseTotal
if $val < $now 4 5 9
133 5 return $val;
134 }
135  
136 ###############################################################################
137 # Subroutine: FV_cc_expiry_month()
138 ###############################################################################
139 # Creates a constraint closure that returns true if the constrained value
140 # appears to be a valid credit card expiry month; an integer in the range of
141 # "1-12".
142 ###############################################################################
143 12 ▶sub FV_cc_expiry_month {
Subroutine FV_cc_expiry_month: called CC=1 SCAR=0.0
Pod documented
144 11 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 11 my $dfv = shift;
146 11 my $val = $dfv->get_current_constraint_value();
147 11 return _match_cc_expiry_month($val);
148 }
149 12 }
150  
151 ###############################################################################
152 # Subroutine: FV_cc_expiry_year()
153 ###############################################################################
154 # Creates a constraint closure that returns true if the constrained value
155 # appears to be a valid credit card expiry year; an integer value for a year,
156 # not in the past.
157 #
158 # Expiry years can be provided as either "YY" or "YYYY". When using the
159 # two-digit "YY" format, the year is considered to be part of the sliding
160 # window 1970-2069.
161 ###############################################################################
162 6 ▶sub FV_cc_expiry_year {
Subroutine FV_cc_expiry_year: called CC=1 SCAR=0.0
Pod documented
163 6 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
164 6 my $dfv = shift;
165 6 my $val = $dfv->get_current_constraint_value();
166 6 return _match_cc_expiry_year($val);
167 }
168 6 }
169  
170 2 1;
171  
172 =for stopwords MM YY YYYY checksum
173  
174 =head1 NAME
175  
176 Data::FormValidator::Constraints::CreditCard - Data constraints, using Business::CreditCard
177  
178 =head1 SYNOPSIS
179  
180 use Data::FormValidator::Constraints::CreditCard qw(:all);
181  
182 constraint_methods => {
183 cc_number => [
184 # number is syntactically valid
185 FV_cc_number(),
186  
187 # verify type, by value
188 FV_cc_type(qw(Visa MasterCard)),
189  
190 # verify type, by regex
191 FV_cc_type(qr/visa|mastercard/i),
192 ],
193  
194 # expiry month is within valid range
195 cc_exp_mon => FV_cc_expiry_month(),
196  
197 # expiry year is not in the past
198 cc_exp_year => FV_cc_expiry_year(),
199  
200 # full expiry date is not in the past
201 cc_expiry => FV_cc_expiry(),
202 },
203  
204 =head1 DESCRIPTION
205  
206 C<Data::FormValidator::Constraints::CreditCard> provides several methods that
207 can be used to generate constraint closures for use with C<Data::FormValidator>
208 for the purpose of validating credit card numbers and expiry dates, using
209 C<Business::CreditCard>.
210  
211 =head1 METHODS
212  
213 =over
214  
215 =item FV_cc_number()
216  
217 Creates a constraint closure that returns true if the constrained value
218 appears to be a valid credit card number.
219  
220 NOTE: "appears to be a valid credit card number" ONLY means that the number
221 appears to be valid and has passed the checksum test; -NO- tests have been
222 performed to verify that this is actually a real/valid credit card number.
223  
224 =item FV_cc_type(@set)
225  
226 Creates a constraint closure that returns true if the constrained value
227 appears to be a credit card of one of the types listed in the given
228 C<@set>. The C<@set> can be provided as either a list of scalars (which are
229 compared using the C<eq> operator), or as a list of regular expressions.
230  
231 For more information on the actual card types that can be checked for,
232 please refer to the information for the C<cardtype()> method in
233 C<Business::CreditCard>.
234  
235 =item FV_cc_expiry()
236  
237 Creates a constraint closure that returns true if the constrained value
238 appears to be a valid credit card expiry date; correct integer values for
239 year/month, with the date not being in the past.
240  
241 Accepted formats include "MM/YY" and "MM/YYYY".
242  
243 NOTE: use of this method requires that the full credit card expiry date be
244 present in a single field; no facilities are provided for gathering the
245 month/year data from two separate fields.
246  
247 =item FV_cc_expiry_month()
248  
249 Creates a constraint closure that returns true if the constrained value
250 appears to be a valid credit card expiry month; an integer in the range of
251 "1-12".
252  
253 =item FV_cc_expiry_year()
254  
255 Creates a constraint closure that returns true if the constrained value
256 appears to be a valid credit card expiry year; an integer value for a year,
257 not in the past.
258  
259 Expiry years can be provided as either "YY" or "YYYY". When using the
260 two-digit "YY" format, the year is considered to be part of the sliding
261 window 1970-2069.
262  
263 =back
264  
265 =head1 AUTHOR
266  
267 Graham TerMarsch (cpan@howlingfrog.com)
268  
269 =head1 COPYRIGHT
270  
271 Copyright (C) 2008, Graham TerMarsch. All Rights Reserved.
272  
273 This is free software; you can redistribute it and/or modify it under the same
274 license as Perl itself.
275  
276 =head1 SEE ALSO
277  
278 L<Data::FormValidator>,
279 L<Business::CreditCard>.
280  
281 =cut
↑ Summary