1 package Business::CreditCard;
3 # Jon Orwant, <orwant@media.mit.edu>
5 # Copyright 1995,1996,1997 Jon Orwant. All rights reserved.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
9 # Current maintainer is Ivan Kohler <ivan-business-creditcard@420.am>.
10 # Please don't bother Jon with emails about this module.
15 use vars qw( @ISA $VERSION );
17 @ISA = qw( Exporter );
23 C<Business::CreditCard> - Validate/generate credit card checksums/names
27 use Business::CreditCard;
29 print validate("5276 4400 6542 1319");
30 print cardtype("5276 4400 6542 1319");
31 print generate_last_digit("5276 4400 6542 131");
33 Business::CreditCard is available at a CPAN site near you.
37 These subroutines tell you whether a credit card number is
38 self-consistent -- whether the last digit of the number is a valid
39 checksum for the preceding digits.
41 The validate() subroutine returns 1 if the card number provided passes
42 the checksum test, and 0 otherwise.
44 The cardtype() subroutine returns a string containing the type of
45 card: "MasterCard", "VISA", and so on. My list is not complete;
48 The generate_last_digit() subroutine computes and returns the last
49 digit of the card given the preceding digits. With a 16-digit card,
50 you provide the first 15 digits; the subroutine returns the sixteenth.
52 This module does I<not> tell you whether the number is on an actual
53 card, only whether it might conceivably be on a real card. To verify
54 whether a card is real, or whether it's been stolen, or what its
55 balance is, you need a Merchant ID, which gives you access to credit
56 card databases. The Perl Journal (http://tpj.com/tpj) has
57 a Merchant ID so that I can accept MasterCard and VISA payments; it
58 comes with the little pushbutton/slide-your-card-through device you've
59 seen in restaurants and stores. That device calculates the checksum
60 for you, so I don't actually use this module.
62 These subroutines will also work if you provide the arguments
63 as numbers instead of strings, e.g. C<validate(5276440065421319)>.
69 The Perl Journal and MIT Media Lab
73 Current maintainer is Ivan Kohler <ivan-business-creditcard@420.am>.
74 Please don't bother Jon with emails about this module.
76 Lee Lawrence <LeeL@aspin.co.uk>, Neale Banks <neale@lowendale.com.au> and
77 Max Becker <Max.Becker@firstgate.com> contributed support for additional card
78 types. Lee also contributed a working test.pl.
82 @EXPORT = qw(cardtype validate generate_last_digit);
87 return "Not a credit card" if $number =~ /[^\d\s]/;
91 return "Not a credit card" unless length($number) >= 13 && 0+$number;
93 return "VISA card" if $number =~ /^4\d{12}(\d{3})?$/o;
94 return "MasterCard" if $number =~ /^5[1-5]\d{14}$/o;
95 return "Discover card" if $number =~ /^6011\d{12}$/o;
96 return "American Express card" if $number =~ /^3[47]\d{13}$/o;
97 return "Diner's Club/Carte Blanche"
98 if $number =~ /^3(0[0-5]|[68]\d)\d{11}$/o;
99 return "enRoute" if $number =~ /^2(014|149)\d{11}$/o;
100 return "JCB" if $number =~ /^(3\d{4}|2131|1800)\d{11}$/o;
101 return "BankCard" if $number =~ /^56(10\d\d|022[1-5])\d{10}$/o;
103 if $number =~ /^49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\d{10}(\d{2,3})?$/o
104 || $number =~ /^564182\d{10}(\d{2,3})?$/o
105 || $number =~ /^6(3(33[0-4][0-9])|759[0-9]{2})\d{10}(\d{2,3})?$/o;
107 if $number =~ /^6(3(34[5-9][0-9])|767[0-9]{2})\d{10}(\d{2,3})?$/o;
111 # from http://perl.about.com/compute/perl/library/nosearch/P073000.htm
112 # verified by http://www.beachnet.com/~hstiles/cardtype.html
113 # Card Type Prefix Length
114 # MasterCard 51-55 16
116 # American Express (AMEX) 34, 37 15
117 # Diners Club/Carte Blanche 300-305, 36, 38 14
118 # enRoute 2014, 2149 15
123 # from Neale Banks <neale@lowendale.com.au>
124 # According to a booklet I have from Westpac (an Aussie bank), a card number
125 # starting with 5610 or 56022[1-5] is a BankCard
126 # BankCards have exactly 16 digits.
128 # from "Becker, Max" <Max.Becker@firstgate.com>
129 # It's mostly used in the UK and is either alled "Switch" or "Solo".
130 # Card Type Prefix Length
131 # Switch various 16,18,19
132 # Solo 63, 6767 16,18,19
134 sub generate_last_digit {
136 my ($i, $sum, $weight);
140 for ($i = 0; $i < length($number); $i++) {
141 $weight = substr($number, -1 * ($i + 1), 1) * (2 - ($i % 2));
142 $sum += (($weight < 10) ? $weight : ($weight - 9));
145 return (10 - $sum % 10) % 10;
150 my ($i, $sum, $weight);
152 return 0 if $number =~ /[^\d\s]/;
156 return 0 unless length($number) >= 13 && 0+$number;
158 for ($i = 0; $i < length($number) - 1; $i++) {
159 $weight = substr($number, -1 * ($i + 2), 1) * (2 - ($i % 2));
160 $sum += (($weight < 10) ? $weight : ($weight - 9));
163 return 1 if substr($number, -1) == (10 - $sum % 10) % 10;