1 package Business::CreditCard;
3 # Business::CreditCard.pm
5 # Jon Orwant, <orwant@media.mit.edu>
8 # 17 Jan 97 - 0.21 released.
9 # short numbers and numbers with letters are no longer kosher.
10 # 1 Feb 2001 - 0.22 released, new maintainer, MakeMaker installation
11 # 3 May 2001 - 0.23 released, silly bug in test.pl
13 # Copyright 1995,1996,1997 Jon Orwant. All rights reserved.
14 # This program is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
17 # Version 0.23. Module list status is "Rdpf."
22 use vars qw( @ISA $VERSION );
24 @ISA = qw( Exporter );
30 C<Business::CreditCard> - Validate/generate credit card checksums/names
34 use Business::CreditCard;
36 print validate("5276 4400 6542 1319");
37 print cardtype("5276 4400 6542 1319");
38 print generate_last_digit("5276 4400 6542 131");
40 Business::CreditCard is available at a CPAN site near you.
44 These subroutines tell you whether a credit card number is
45 self-consistent -- whether the last digit of the number is a valid
46 checksum for the preceding digits.
48 The validate() subroutine returns 1 if the card number provided passes
49 the checksum test, and 0 otherwise.
51 The cardtype() subroutine returns a string containing the type of
52 card: "MasterCard", "VISA", and so on. My list is not complete;
55 The generate_last_digit() subroutine computes and returns the last
56 digit of the card given the preceding digits. With a 16-digit card,
57 you provide the first 15 digits; the subroutine returns the sixteenth.
59 This module does I<not> tell you whether the number is on an actual
60 card, only whether it might conceivably be on a real card. To verify
61 whether a card is real, or whether it's been stolen, or what its
62 balance is, you need a Merchant ID, which gives you access to credit
63 card databases. The Perl Journal (http://tpj.com/tpj) has
64 a Merchant ID so that I can accept MasterCard and VISA payments; it
65 comes with the little pushbutton/slide-your-card-through device you've
66 seen in restaurants and stores. That device calculates the checksum
67 for you, so I don't actually use this module.
69 These subroutines will also work if you provide the arguments
70 as numbers instead of strings, e.g. C<validate(5276440065421319)>.
76 The Perl Journal and MIT Media Lab
80 Current maintainer is Ivan Kohler <ivan-business-creditcard@420.am>.
81 Please don't bother Jon with emails about this module.
85 @EXPORT = qw(cardtype validate generate_last_digit);
90 return "Not a credit card" if $number =~ /[^\d\s]/;
94 return "Not a credit card" unless length($number) >= 13 && 0+$number;
96 return "VISA card" if substr($number,0,1) == "4";
97 return "MasterCard" if substr($number,0,1) == "5";
98 return "Discover card" if substr($number,0,1) == "6";
99 return "American Express card" if substr($number,0,2) == "37";
100 return "Diner's Club, Transmedia, or other dining/entertainment card" if substr($number,0,1) == "3";
104 sub generate_last_digit {
106 my ($i, $sum, $weight);
110 for ($i = 0; $i < length($number); $i++) {
111 $weight = substr($number, -1 * ($i + 1), 1) * (2 - ($i % 2));
112 $sum += (($weight < 10) ? $weight : ($weight - 9));
115 return (10 - $sum % 10) % 10;
120 my ($i, $sum, $weight);
122 return 0 if $number =~ /[^\d\s]/;
126 return 0 unless length($number) >= 13 && 0+$number;
128 for ($i = 0; $i < length($number) - 1; $i++) {
129 $weight = substr($number, -1 * ($i + 2), 1) * (2 - ($i % 2));
130 $sum += (($weight < 10) ? $weight : ($weight - 9));
133 return 1 if substr($number, -1) == (10 - $sum % 10) % 10;