projects
/
Business-CreditCard.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
9421e4d
)
add Isracard validation, RT13643
author
levinse
<levinse>
Thu, 14 Jul 2011 04:22:28 +0000
(
04:22
+0000)
committer
levinse
<levinse>
Thu, 14 Jul 2011 04:22:28 +0000
(
04:22
+0000)
CreditCard.pm
patch
|
blob
|
history
t/validation.t
[new file with mode: 0644]
patch
|
blob
diff --git
a/CreditCard.pm
b/CreditCard.pm
index
4993f4e
..
df40315
100644
(file)
--- a/
CreditCard.pm
+++ b/
CreditCard.pm
@@
-219,7
+219,7
@@
sub cardtype {
sub generate_last_digit {
my ($number) = @_;
sub generate_last_digit {
my ($number) = @_;
- #XXX doesn't work for Isracard, should die
+ die "invalid operation" if length($number) == 8 || length($number) == 9;
my ($i, $sum, $weight);
my ($i, $sum, $weight);
@@
-242,8
+242,14
@@
sub validate {
$number =~ s/\D//g;
$number =~ s/\D//g;
- return 1 if $number =~ /^[\dx]{8,9}$/; #XXX Isracard does not use LUHN,
- # validation not yet implemented
+ if ( $number =~ /^[\dx]{8,9}$/ ) { # Isracard
+ $number = "0$number" if length($number) == 8;
+ for($i=1;$i<length($number);$i++){
+ $sum += substr($number,9-$i,1) * $i;
+ }
+ return 1 if $sum%11 == 0;
+ return 0;
+ }
return 0 unless length($number) >= 13 && 0+$number;
return 0 unless length($number) >= 13 && 0+$number;
diff --git a/t/validation.t
b/t/validation.t
new file mode 100644
(file)
index 0000000..
953e4bc
--- /dev/null
+++ b/
t/validation.t
@@ -0,0
+1,20
@@
+
+use Test::More tests => 1;
+use Business::CreditCard;
+
+ok( test_card_validation() );
+
+sub test_card_validation {
+ my %test_table=(
+ '10830529' => 'Isracard',
+ '010830529' => 'Isracard',
+ );
+ while( my ($k, $v)=each(%test_table) ){
+ if(!validate($k)){
+ warn "Card $k - should be a valid $v but validation failed\n";
+ return;
+ }
+ }
+ return 1;
+}
+