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
12 # 11 Jun 2001 - 0.24. added enRoute, JCB, BankCard, rewrote with regexes
14 # Copyright 1995,1996,1997 Jon Orwant. All rights reserved.
15 # This program is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
18 # Version 0.24. Module list status is "Rdpf."
23 use vars qw( @ISA $VERSION );
25 @ISA = qw( Exporter );
31 C<Business::CreditCard> - Validate/generate credit card checksums/names
35 use Business::CreditCard;
37 print validate("5276 4400 6542 1319");
38 print cardtype("5276 4400 6542 1319");
39 print generate_last_digit("5276 4400 6542 131");
41 Business::CreditCard is available at a CPAN site near you.
45 These subroutines tell you whether a credit card number is
46 self-consistent -- whether the last digit of the number is a valid
47 checksum for the preceding digits.
49 The validate() subroutine returns 1 if the card number provided passes
50 the checksum test, and 0 otherwise.
52 The cardtype() subroutine returns a string containing the type of
53 card: "MasterCard", "VISA", and so on. My list is not complete;
56 The generate_last_digit() subroutine computes and returns the last
57 digit of the card given the preceding digits. With a 16-digit card,
58 you provide the first 15 digits; the subroutine returns the sixteenth.
60 This module does I<not> tell you whether the number is on an actual
61 card, only whether it might conceivably be on a real card. To verify
62 whether a card is real, or whether it's been stolen, or what its
63 balance is, you need a Merchant ID, which gives you access to credit
64 card databases. The Perl Journal (http://tpj.com/tpj) has
65 a Merchant ID so that I can accept MasterCard and VISA payments; it
66 comes with the little pushbutton/slide-your-card-through device you've
67 seen in restaurants and stores. That device calculates the checksum
68 for you, so I don't actually use this module.
70 These subroutines will also work if you provide the arguments
71 as numbers instead of strings, e.g. C<validate(5276440065421319)>.
77 The Perl Journal and MIT Media Lab
81 Current maintainer is Ivan Kohler <ivan-business-creditcard@420.am>.
82 Please don't bother Jon with emails about this module.
86 @EXPORT = qw(cardtype validate generate_last_digit);
91 return "Not a credit card" if $number =~ /[^\d\s]/;
95 return "Not a credit card" unless length($number) >= 13 && 0+$number;
97 return "VISA card" if $number =~ /^4\d{12}\d{3}?$/;
98 return "MasterCard" if $number =~ /^5[1-5]\d{14}$/;
99 return "Discover card" if $number =~ /^6011\d{12}$/;
100 return "American Express card" if $number =~ /^3[47]\d{13}/;
101 return "Diner's Club/Carte Blanche"
102 if $number =~ /^3(0[0-5]|[68]\d)\d{11}$/;
103 return "enRoute" if $number =~ /^2(014|149)\d{11}$/;
104 return "JCB" if $number =~ /^3\d{15}$/
105 || $number =~ /^(2131|1800)\d{11}$/;
106 return "BankCard" if $number =~ /^56(10\d\d|022[1-5])\d{10}$/;
110 # from http://perl.about.com/compute/perl/library/nosearch/P073000.htm
111 # Card Type Prefix Length
112 # MasterCard 51-55 16
114 # American Express (AMEX) 34, 37 15
115 # Diners Club/Carte Blanche 300-305, 36, 38 14
116 # enRoute 2014, 2149 15
121 # from Neale Banks <neale@lowendale.com.au>
122 # According to a booklet I have from Westpac (an Aussie bank), a card number
123 # starting with 5610 or 56022[1-5] is a BankCard
124 # BankCards have exactly 16 digits.
126 sub generate_last_digit {
128 my ($i, $sum, $weight);
132 for ($i = 0; $i < length($number); $i++) {
133 $weight = substr($number, -1 * ($i + 1), 1) * (2 - ($i % 2));
134 $sum += (($weight < 10) ? $weight : ($weight - 9));
137 return (10 - $sum % 10) % 10;
142 my ($i, $sum, $weight);
144 return 0 if $number =~ /[^\d\s]/;
148 return 0 unless length($number) >= 13 && 0+$number;
150 for ($i = 0; $i < length($number) - 1; $i++) {
151 $weight = substr($number, -1 * ($i + 2), 1) * (2 - ($i % 2));
152 $sum += (($weight < 10) ? $weight : ($weight - 9));
155 return 1 if substr($number, -1) == (10 - $sum % 10) % 10;