LA Flash and Flex Developer
17 Jul
By no means did I write this, if you do a search for Luhn Check in google you will come with a million results. However, most of them are Java implementations, JavaScript, ASP, JSP, but never ActionScript. Below is a version I converted from Javascript into Actionscript 2.0. This Luhn Check validates the users credit card numbers and makes sure A) there are enough for the given card, and B) matches the number pattern of a given card. This example only checks Visa, MasterCard, American Express, and Diners Club.
BY NO MEANS IS THIS THE ONLY CHECK YOU NEED TO DO TO PROCESS AN ORDER.
You must use a valid payment gateway that does proper server side credit card validation. The purpose of this script is to reduce traffic to the server by weeding out the invalid credit card numbers. If the client side tests are successful the application then has to send the Credit Card information to the server for proper Credit Card Validation and order processing.
lass com.scottgmorgan.utils.CreditCardValidation {
/**
* checks for datein the format MM/YY or MM/YYYY against the current date
* @param month a number representing the month to be validated against.
* @param year a number representing the year to be validated against.
*/
public function isValidExpDate(month:Number,year:Number):Boolean {
var result:Boolean = true;
var now:Date = new Date();
var nowMonth:Number = now.getMonth() + 1;
var nowYear:Number = now.getFullYear();
if ((nowYear > year) || ((nowYear == year ) && (nowMonth > month))){
result = false;
}
return result;
}
/**
* checks for valid credit card format using the Luhn check and known digit
* about various cards
* @ccType a string indicating the entered credit card name
* @ccNum a string indicating the credit card number being used.
*/
public function isValidCreditCardNumber(ccType:String,ccNum:String):Boolean {
var result:Boolean = true;
if (ccNum.length>0){
if (isNaN(ccNum)){
result = false;
}
if (result){
if (!luhnCheck(ccNum) || !validateCCNum(ccType,ccNum)){
result = false;
}
}
}
return result;
}
private function luhnCheck(str:String){
var result:Boolean = true;
var sum:Number = 0;
var mul:Number = 1;
var strLen:Number = str.length;
for (var i:Number = 0; i < strLen; i++){
var digit:String = str.substring(strLen-i-1,strLen-i);
var tproduct:Number = parseInt(digit ,10)*mul;
if (tproduct >= 10){
sum += (tproduct % 10) + 1;
} else {
sum += tproduct;
}
if (mul == 1){
mul++;
} else {
mul--;
}
}
if ((sum % 10) != 0){
result = false;
}
return result;
}
private function validateCCNum(cardType:String,cardNum:String):Boolean{
var result:Boolean = false;
cardType = cardType.toUpperCase();
var cardLen:Number = cardNum.length;
var firstdig:String = cardNum.substring(0,1);
var seconddig:String = cardNum.substring(1,2);
var first4digs:String = cardNum.substring(0,4);
switch (cardType){
case "VISA":
result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
break;
case "AMEX":
var validNums = "47";
result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
break;
case "MASTERCARD":
var validNums = "12345";
result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
break;
case "DISCOVER":
result = (cardLen == 16) && (first4digs == "6011");
break;
case "DINERS":
var validNums = "068";
result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
break;
}
return result;
}
}
And here is the sample code that instantiates this bad boy:
var ccValidator = new CreditCardValidation();
var valid = ccValidator.isValidCreditCardNumber('visa', '4111111111111111');
trace('card is valid: ' + valid);
Pretty straightforward. I have hardcoded the values in the method call above but in the real world this would be puuled from a textfield or a combo box for the card type.
As a side note, if you ever want to get around the validation for Visa simply enter ‘4111111111111111′ (1 “4″, and 15 “1’s”). Unfortunatly, this number will not get you past the real creditcard validation that occurs on the server or at the payment gateway.