A Web Application commonly involves transactions which require processing of Credit Card information.
Implementation of basic credit card validation ensures that the final overhead involving the actual verification of credit card numbers is reduced to a great extent. In this white paper we have elaborated one such credit card validation algorithm, LUHN Algorithm, and presented a JAVA code to implement it.
Credit Card Validation
This article outlines procedures and algorithm for verifying the validity of credit card numbers.
Most credit card numbers are encoded with a ‘Check Digit’. A check digit is a digit added to a number either at the beginning or at the end and is used to validate the authenticity of the number. Over here, a simple algorithm has been applied to the other digits which yield the check digit. This algorithm is used to validate the credit card number before it is sent for Debit Authorization.
The following table outlines the algorithm that is used for some of the credit card providers:-
Card Type | Prefix | Length | Check Digit Algorithm |
MasterCard | 51-55 | 16 | mod 10 |
VISA | 4 | 13, 16 | mod 10 |
AMEX | 34, 37 | 15 | mod 10 |
Diners Club/Carte Blanche | 300-305, 36, 38 | 14 | mod 10 |
Discover | 6011 | 16 | mod 10 |
JCB | 3 | 16 | mod 10 |
JCB | 2131, 1800 | 15 | mod 10 |
LUHN Algorithm (mod 10)
LUHN Algorithm is used to provide basic validation of credit card numbers. The steps are as follows:
- Double the value of alternate digits of the credit card number beginning with the second digit from the right (the first right hand digit is the check digit).
- Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.
- The total obtained in Step 2 must be a number ending in zero (30, 40, 50, etc.) for the account number to be validated.
For example:
As an illustration, if the credit card number is 49927398716, it will be validated as follows:
1. Double every alternate digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
2. Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
3. Take the sum modulo 10. 70 mod 10 = 0; the account number is valid.
The JAVA Code to implement the algorithm is attached below:
public static boolean validateCreditCard(String creditCardType,
String creditCardNo, String year, String month) {
boolean isValidCard = false;
int[] number = { };
// if the CC type is enRoute
if (creditCardType.equalsIgnoreCase("enRoute")) {
if ((creditCardNo.length() == 15) &&
(creditCardNo.startsWith("2014") ||
creditCardNo.startsWith("2149"))) {
isValidCard = true;
}
} else {
// The checkCreditCard method returns true if the card number and
// the type is
// metting the basic criteria
if (checkCreditCardType(creditCardNo, creditCardType, year, month)) {
char[] ccNumArray = creditCardNo.toCharArray();
int[] intArray = Utility.toIntArray(ccNumArray);
number = reverseintArray(intArray);
int len = number.length;
int sum = 0;
for (int i = 0; i < len; i++) {
int num = number[i];
if (i == 0) {
sum += num;
continue;
}
if ((i % 2) != 0) {
num *= 2;
sum += ((num / 10) + (num % 10));
} else {
sum += num;
}
}
if ((sum % 10) == 0) {
isValidCard = true;
} else {
isValidCard = false;
}
} else {
isValidCard = false;
}
}
return isValidCard;
}
public static boolean checkCreditCardType(String ccNum, String ccType,
String year, String month) {
boolean isValid = false;
if (ccType.equalsIgnoreCase("amex") ||
ccType.equalsIgnoreCase("american express") ||
ccType.equalsIgnoreCase("americanexpress")) {
ccType = "amex";
}
if (isCcExpired(year, month)) {
return false;
}
if (ccType.equalsIgnoreCase("visa") &&
((ccNum.length() == 16) || (ccNum.length() == 13)) &&
ccNum.startsWith("4")) {
isValid = true;
} else if ((ccType.equalsIgnoreCase("amex")) && (ccNum.length() == 15) &&
(ccNum.startsWith("34") || ccNum.startsWith("37"))) {
isValid = true;
} else if ((ccType.equalsIgnoreCase("MASTERCARD")) &&
(ccNum.length() == 16) &&
(ccNum.startsWith("51") || ccNum.startsWith("52") ||
ccNum.startsWith("53") || ccNum.startsWith("54") ||
ccNum.startsWith("55"))) {
isValid = true;
} else if ((ccType.equalsIgnoreCase("Discover")) &&
(ccNum.length() == 16) && (ccNum.startsWith("6011"))) {
isValid = true;
} else if ((ccType.equalsIgnoreCase("JCB")) &&
(((ccNum.length() == 16) && ccNum.startsWith("3")) ||
((ccNum.length() == 15) &&
(ccNum.startsWith("2131") || ccNum.startsWith("1800"))))) {
isValid = true;
} else if ((ccType.equalsIgnoreCase("Diners Club") ||
ccType.equalsIgnoreCase("Carte Blanche")) &&
(ccNum.length() == 14) &&
(ccNum.startsWith("36") || ccNum.startsWith("38") ||
ccNum.startsWith("300") || ccNum.startsWith("301") ||
ccNum.startsWith("302") || ccNum.startsWith("303") ||
ccNum.startsWith("304") || ccNum.startsWith("305"))) {
isValid = true;
}
return isValid;
}
public static int[] reverseintArray(int[] intArray) {
int size = intArray.length;
int[] retArray = new int[size];
for (int i = 0; i < size; i++) {
retArray[size - i - 1] = intArray[i];
}
return retArray;
}
0 comments:
Post a Comment