1  /**
     2   * com.sekati.validate.CreditCardValidation
     3   * @version 1.0.1
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9   import com.sekati.crypt.Luhn;
    10   import com.sekati.utils.StringUtils;
    11  
    12  /**
    13   * Credit Card Validation class
    14   * @see {@link com.sekati.crypt.Luhn}
    15   */
    16  class com.sekati.validate.CreditCardValidation {
    17  
    18  	private static var DEFAULT_ENCODE_DIGITS_SHOWN:Number = 4;
    19  	private static var DEFAULT_ENCODE_CHARACTER:String    = "*";
    20  	private static var MINIMUM_CARD_LENGTH:Number         = 13;
    21  	private static var MAXIMUM_CARD_LENGTH:Number         = 16;
    22  
    23  	/**
    24  	 * validate a credit card expiration date
    25  	 * @param nMonth (Number)
    26  	 * @param nYear (Number)
    27  	 * @return Boolean
    28  	 * {@code Usage:
    29  	 * 	var isValidDate:Boolean = CreditCardValidation.isValidExDate(11,2010);
    30  	 * }
    31  	 */
    32  	public static function isValidExDate (nMonth:Number, nYear:Number):Boolean {
    33  		var objDate:Date = new Date();
    34  		var nCurrentMonth:Number = objDate.getMonth() + 1;
    35  		var nCurrentYear:Number  = objDate.getFullYear();
    36  		if((nYear > nCurrentYear) || (nYear == nCurrentYear && nMonth >= nCurrentMonth)) {
    37  			return true;
    38  		}
    39  		return false;
    40  	}
    41  
    42  	/**
    43  	 * validate a credit card number as much as possible before submitting for approval
    44  	 * @param strNumber (String) credit card number as string
    45  	 * @return Boolean
    46  	 * {@code var isValidNumber:Boolean = CreditCardValidation("1234567890123456"); }
    47  	 */
    48  	public static function isValidNumber (strNumber:String):Boolean {
    49  		var ccNumber:String = StringUtils.toNumeric(strNumber);
    50  		if(ccNumber.length > 0 && !isNaN(ccNumber) && (ccNumber.length >= MINIMUM_CARD_LENGTH && ccNumber.length <= MAXIMUM_CARD_LENGTH)) {
    51  			return Luhn.mod10(ccNumber);
    52  		}
    53  		return false;
    54  	}
    55  
    56  	/**
    57  	 * Encode a credit card number as a string and encode all digits except the last nDigitsShown
    58  	 * @param strNumber (String) credit card number as string
    59  	 * @param nDigitsShown (Number) display this many digits at the end of the card number for security purposes
    60  	 * @param strCharacter (String) optional encoding character to use instead of default '*'
    61  	 * @return String
    62  	 * {@code Usage:
    63  	 * trace(CreditCardValidation.EncodeNumber("1234567890123456")); // ************3456
    64  	 * trace(CreditCardValidation.EncodeNumber("1234567890123456", 5, "x"));  // xxxxxxxxxxx23456
    65  	 * }
    66  	 */
    67  	public static function EncodeNumber(strNumber:String, nDigitsShown:Number, strCharacter:String):String {
    68  		var strEncoded:String = "";
    69  		nDigitsShown = (nDigitsShown != undefined && nDigitsShown != null) ? nDigitsShown : DEFAULT_ENCODE_DIGITS_SHOWN;
    70  		strCharacter = (strCharacter != undefined && strCharacter != null) ? strCharacter : DEFAULT_ENCODE_CHARACTER;
    71  		for(var i:Number=0; i < strNumber.length - nDigitsShown; i++) {
    72  			strEncoded += strCharacter;
    73  		}
    74  		strEncoded += strNumber.slice(-nDigitsShown);
    75  		return strEncoded;
    76  	}
    77  	
    78  	private function CreditCardValidation(){
    79  	}
    80  }