Add link to Neil Bowers' review of CC check modules
[Business-CreditCard.git] / CreditCard.pm
index 4993f4e..d657e42 100644 (file)
@@ -131,12 +131,24 @@ types.  Lee also contributed a working test.pl.  Alexandr Ciornii
 
 Copyright (C) 1995,1996,1997 Jon Orwant
 Copyright (C) 2001-2006 Ivan Kohler
-Copyright (C) 2007-2011 Freeside Internet Services, Inc.
+Copyright (C) 2007-2012 Freeside Internet Services, Inc.
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself, either Perl version 5.8.8 or,
 at your option, any later version of Perl 5 you may have available.
 
+=head1 BUGS
+
+(paraphrasing Neil Bowers) We export all functions by default.  It would be
+better to let the user decide which functions to import.  And validate() is
+a bit of a generic name.
+
+The question is, after almost 2 decades with this interface (inherited from
+the original author, who probably never expected it to live half this long),
+how to change things to behave in a more modern fashion without breaking
+existing code?  "use Business::CreditCard <some_minimum_version>" turns it off?
+Explicitly ask to turn it off and list that in the SYNOPSIS?
+
 =head1 SEE ALSO
 
 L<Business::CreditCard::Object> is a wrapper around Business::CreditCard
@@ -146,6 +158,9 @@ Business::CreditCard distribution is welcome.
 L<Business::OnlinePayment> is a framework for processing online payments
 including modules for various payment gateways.
 
+http://neilb.org/reviews/luhn.html is an excellent overview of similar modules
+providing credit card number verification (LUHN checking).
+
 =cut
 
 @EXPORT = qw(cardtype validate generate_last_digit);
@@ -219,7 +234,7 @@ sub cardtype {
 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);
 
@@ -242,8 +257,14 @@ sub validate {
 
     $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;