spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / carts / chap7 / 3 current pageTo page 2To page 3To page 4
[next]

Usable Shopping Carts, Chapter 7: CC Validation and Verification

Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Validating Credit Card Numbers with the Luhn Formula

[The following is the conclusion of our series of excerpts from the glasshaus title, Usable Shopping Carts.]

The question of whether or not a credit card number represents an active credit or deposit account, and that this account has a sufficient line of credit or balance to cover a sale can only be answered by a clearinghouse or card processor with access to a banking system of which the card's backer is also a participant. However, because issuers of cards follow certain rules when creating card numbers, it is possible to verify whether a given number is accurate or couldn't possibly be a number of the stated type. We can use this information to check a number given to us by a customer and so catch any errors the customer might have made in typing it into the form before we submit it for authorisation.

Each type of card always has a certain number of digits and begins with a given prefix or range of prefixes. The following table provides the prefixes and lengths for the five most commonly used credit cards:

CARD TYPE

PREFIX(es)

LENGTH(s)

MasterCard

51-55

16

Visa

4

13 or 16

American Express

34 or 37

15

Discover

6011

16

Diners Club / Carte Blanche

300-305, 36, or 38

14

In addition, the number itself can be subjected to a mathematical test (of the sort generally referred to generally as a checksum) which it must pass in order to be legitimate. This test is known as the Luhn formula. It is somewhat tedious to perform by hand, but it's not terribly difficult to write a script to automate the task. These are the steps required to use it:

  1. Double the value of every other digit starting with the next-to-rightmost digit.

  2. If any of the resulting values has more than two digits, then its digits must be added together to produce a single digit.

  3. Add the sum of all the digits not doubled in step 1 to the sum of all the digits resulting in step 2.

  4. If the result is exactly divisible by 10 (that is, if the result ends in a zero), then the number is valid--providing of course that it's of the correct length and bears a correct prefix for that type of card--and can now be submitted for authorisation of a sale.

For example, suppose we're given the number 2323-2005-7766-3554.

2

3

2

3

2

0

0

5

7

7

6

6

3

5

5

4

*2

-

*2

-

*2

-

*2

-

*2

-

*2

-

*2

-

*2

-

4

3

4

3

4

0

0

5

14

7

12

6

6

5

10

4

4

3

4

3

4

0

0

5

1+4=5

7

1+2=3

6

6

5

1+0=1

4

SUM:

4+3+4+3+4+0+0+5+5+7+3+6+6+5+1+4=70.

70 mod 10 = 0.

Of course this number obviously isn't a valid one for any of the types shown due to the prefix, but it serves to illustrate the method used. Now let's put this all together into a workable validation routine. We'll be checking only MasterCard, Visa and American Express numbers in the example, but you should be able to use the information from the table above to extend the validation scripts to cover the additional card types listed, and more are available on the Web (see the References section at the end of the book).


home / programming / carts / chap7 / 3 current pageTo page 2To page 3To page 4
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: January 2, 2003
Revised: January 2, 2003

URL: http://webreference.com/programming/carts/chap7/3/