/** * @see AbstractValidator#onValidate(IValidatable) */ @Override protected void onValidate(IValidatable<String> validatable) { creditCardNumber = validatable.getValue(); try { if (!isLengthAndPrefixCorrect(creditCardNumber)) { error(validatable); } } catch (final NumberFormatException _) { error(validatable); } }
isChecksumCorrect(creditCardNumber)) cardId = isAmericanExpress(creditCardNumber); cardId = isChinaUnionPay(creditCardNumber); cardId = isDinersClubCarteBlanche(creditCardNumber); cardId = isDinersClubInternational(creditCardNumber); cardId = isDinersClubUsAndCanada(creditCardNumber); cardId = isDiscoverCard(creditCardNumber); cardId = isJCB(creditCardNumber); cardId = isLaser(creditCardNumber); cardId = isMaestro(creditCardNumber); cardId = isMastercard(creditCardNumber); cardId = isSolo(creditCardNumber); cardId = isSwitch(creditCardNumber); cardId = isVisa(creditCardNumber); cardId = isVisaElectron(creditCardNumber);
/** * Checks if the credit card number can be determined as a valid number. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number could be determined as a valid number, * else <code>FALSE</code> is returned */ protected boolean isLengthAndPrefixCorrect(String creditCardNumber) { if (creditCardNumber == null) { return false; } // strip spaces and dashes creditCardNumber = creditCardNumber.replaceAll("[ -]", ""); // the length of the credit card number has to be between 12 and 19. // else the number is invalid. if ((creditCardNumber.length() >= 12) && (creditCardNumber.length() <= 19) && isChecksumCorrect(creditCardNumber)) { if ((failOnUnknown == false) || (determineCardId(creditCardNumber) != CreditCard.INVALID)) { return true; } } return false; }
/** * Check if the credit card is a Diners Club International. A Diners Club International number * has to start with the number 36 and has to have a length of 14. The number has to be * validated with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Diners Club * International number. Else <code>FALSE</code> will be returned */ private boolean isDinersClubInternational(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() == 14 && creditCardNumber.startsWith("36") && isChecksumCorrect(creditCardNumber)) { cardId = CreditCardValidator.DINERS_CLUB_INTERNATIONAL; returnValue = true; } return returnValue; }
/** * @see AbstractValidator#onValidate(IValidatable) */ protected void onValidate(IValidatable validatable) { String input = ((String)validatable.getValue()); String numberToCheck = input.replaceAll("[ -]", ""); int nulOffset = '0'; int sum = 0; for (int i = 1; i <= numberToCheck.length(); i++) { int currentDigit = numberToCheck.charAt(numberToCheck.length() - i) - nulOffset; if ((i % 2) == 0) { currentDigit *= 2; currentDigit = currentDigit > 9 ? currentDigit - 9 : currentDigit; sum += currentDigit; } else { sum += currentDigit; } } if (!((sum % 10) == 0)) { error(validatable); } } }
/** * Check if the credit card is an American Express. An American Express number has to start with * 34 or 37 and has to have a length of 15. The number has to be validated with the Luhn * algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid American Express * number. Else <code>FALSE</code> will be returned */ private boolean isAmericanExpress(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() == 15 && (creditCardNumber.startsWith("34") || creditCardNumber.startsWith("37"))) { if (isChecksumCorrect(creditCardNumber)) { cardId = CreditCardValidator.AMERICAN_EXPRESS; returnValue = true; } } return returnValue; }
/** * Checks if the credit card number can be determined as a valid number. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number could be determined as a valid number, * else <code>FALSE</code> is returned */ protected boolean isLengthAndPrefixCorrect(String creditCardNumber) { if (creditCardNumber == null) { return false; } // strip spaces and dashes creditCardNumber = creditCardNumber.replaceAll("[ -]", ""); // the length of the credit card number has to be between 12 and 19. // else the number is invalid. if ((creditCardNumber.length() >= 12) && (creditCardNumber.length() <= 19) && isChecksumCorrect(creditCardNumber)) { if ((failOnUnknown == false) || (determineCardId(creditCardNumber) != CreditCard.INVALID)) { return true; } } return false; }
if (isAmericanExpress(creditCardNumber)) else if (isChinaUnionPay(creditCardNumber)) else if (isDinersClubCarteBlanche(creditCardNumber)) else if (isDinersClubInternational(creditCardNumber)) else if (isDinersClubUsAndCanada(creditCardNumber)) else if (isDiscoverCard(creditCardNumber)) else if (isJCB(creditCardNumber)) else if (isLaser(creditCardNumber)) else if (isMaestro(creditCardNumber)) else if (isMastercard(creditCardNumber)) else if (isSolo(creditCardNumber)) else if (isSwitch(creditCardNumber)) else if (isVisa(creditCardNumber)) else if (isVisaElectron(creditCardNumber)) else if (isUnknown(creditCardNumber))
/** * Check if the credit card is a JCB. A JCB number has to start with a number between 3528 and * 3589 and has to have a length of 16. The number has to be validated with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid JCB number. Else * <code>FALSE</code> will be returned */ private boolean isJCB(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() == 16 && isChecksumCorrect(creditCardNumber)) { int firstFourDigits = Integer.parseInt(creditCardNumber.substring(0, 4)); if (firstFourDigits >= 3528 && firstFourDigits <= 3589) { cardId = CreditCardValidator.JCB; returnValue = true; } } return returnValue; }
isChecksumCorrect(creditCardNumber)) cardId = isAmericanExpress(creditCardNumber); cardId = isChinaUnionPay(creditCardNumber); cardId = isDinersClubCarteBlanche(creditCardNumber); cardId = isDinersClubInternational(creditCardNumber); cardId = isDinersClubUsAndCanada(creditCardNumber); cardId = isDiscoverCard(creditCardNumber); cardId = isJCB(creditCardNumber); cardId = isLaser(creditCardNumber); cardId = isMaestro(creditCardNumber); cardId = isMastercard(creditCardNumber); cardId = isSolo(creditCardNumber); cardId = isSwitch(creditCardNumber); cardId = isVisa(creditCardNumber); cardId = isVisaElectron(creditCardNumber);
/** * Check if the credit card is a Diners Club US & Canada. A Diners Club US & Canada number has * to start with the number 54 or 55 and has to have a length of 16. The number has to be * validated with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Diners Club US & * Canada number. Else <code>FALSE</code> will be returned */ private boolean isDinersClubUsAndCanada(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() == 16 && (creditCardNumber.startsWith("54") || creditCardNumber.startsWith("55")) && isChecksumCorrect(creditCardNumber)) { cardId = CreditCardValidator.DINERS_CLUB_US_AND_CANADA; returnValue = true; } return returnValue; }
/** * Check if the credit card is a Mastercard. A Mastercard number has to start with a number * between 51 and 55 and has to have a length of 16. The number has to be validated with the * Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Mastercard number. * Else <code>FALSE</code> will be returned */ private boolean isMastercard(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() == 16 && isChecksumCorrect(creditCardNumber)) { int firstTwoDigits = Integer.parseInt(creditCardNumber.substring(0, 2)); if (firstTwoDigits >= 51 && firstTwoDigits <= 55) { cardId = CreditCardValidator.MASTERCARD; returnValue = true; } } return returnValue; }
/** * Check if the credit card is a Diners Club Carte Blanche. A Diners Club Carte Blanche number * has to start with a number between 300 and 305 and has to have a length of 14. The number has * to be validated with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Diners Club Carte * Blanche number. Else <code>FALSE</code> will be returned */ private boolean isDinersClubCarteBlanche(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() == 14 && creditCardNumber.startsWith("30")) { int firstDigits = Integer.parseInt(creditCardNumber.substring(0, 3)); if (firstDigits >= 300 && firstDigits <= 305 && isChecksumCorrect(creditCardNumber)) { cardId = CreditCardValidator.DINERS_CLUB_CARTE_BLANCHE; returnValue = true; } } return returnValue; }
/** * Check if the credit card is a Laser. A Laser number has to start with 6304, 6706, 6771 or * 6709 and has to have a length between 16 and 19 digits. The number has to be validated with * the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Laser number. Else * <code>FALSE</code> will be returned */ private boolean isLaser(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() >= 16 && creditCardNumber.length() <= 19 && isChecksumCorrect(creditCardNumber)) { if (creditCardNumber.startsWith("6304") || creditCardNumber.startsWith("6706") || creditCardNumber.startsWith("6771") || creditCardNumber.startsWith("6709")) { cardId = CreditCardValidator.LASER; returnValue = true; } } return returnValue; }
/** * Check if the credit card is a Solo. A Solo number has to start with 6334 or 6767 and has to * have a length of 16, 18 or 19 digits. The number has to be validated with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Solo number. Else * <code>FALSE</code> will be returned */ private boolean isSolo(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if ((creditCardNumber.length() == 16 || creditCardNumber.length() == 18 || creditCardNumber.length() == 19) && isChecksumCorrect(creditCardNumber)) { if (creditCardNumber.startsWith("6334") || creditCardNumber.startsWith("6767")) { cardId = CreditCardValidator.SOLO; returnValue = true; } } return returnValue; }
/** * Check if the credit card is a Maestro. A Maestro number has to start with * 5018,5020,5038,6304,6759,6761 or 6763 and has to have a length between 12 and 19 digits. The * number has to be validated with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Maestro number. Else * <code>FALSE</code> will be returned */ private boolean isMaestro(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() >= 12 && creditCardNumber.length() <= 19 && isChecksumCorrect(creditCardNumber)) { if (creditCardNumber.startsWith("5018") || creditCardNumber.startsWith("5020") || creditCardNumber.startsWith("5038") || creditCardNumber.startsWith("6304") || creditCardNumber.startsWith("6759") || creditCardNumber.startsWith("6761") || creditCardNumber.startsWith("6763")) { cardId = CreditCardValidator.MAESTRO; returnValue = true; } } return returnValue; }
/** * Check if the credit card is a Discover Card. A Discover Card number has to start with 6011, * 622126-622925, 644-649 or 65 and has to have a length of 16. The number has to be validated * with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Discover Card number. * Else <code>FALSE</code> will be returned */ private boolean isDiscoverCard(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if (creditCardNumber.length() == 16 && creditCardNumber.startsWith("6") && isChecksumCorrect(creditCardNumber)) { int firstThreeDigits = Integer.parseInt(creditCardNumber.substring(0, 3)); int firstSixDigits = Integer.parseInt(creditCardNumber.substring(0, 6)); if (creditCardNumber.startsWith("6011") || creditCardNumber.startsWith("65") || (firstThreeDigits >= 644 && firstThreeDigits <= 649) || (firstSixDigits >= 622126 && firstSixDigits <= 622925)) { cardId = CreditCardValidator.DISCOVER_CARD; returnValue = true; } } return returnValue; }
/** * Check if the credit card is a Switch. A Switch number has to start with * 4903,4905,4911,4936,564182,633110,6333 or 6759 and has to have a length of 16, 18 or 19 * digits. The number has to be validated with the Luhn algorithm. * * @param creditCardNumber * the credit card number as a string * @return <code>TRUE</code> if the credit card number seems to be a valid Switch number. Else * <code>FALSE</code> will be returned */ private boolean isSwitch(String creditCardNumber) { cardId = CreditCardValidator.INVALID; boolean returnValue = false; if ((creditCardNumber.length() == 16 || creditCardNumber.length() == 18 || creditCardNumber.length() == 19) && isChecksumCorrect(creditCardNumber)) { if (creditCardNumber.startsWith("4903") || creditCardNumber.startsWith("4905") || creditCardNumber.startsWith("4911") || creditCardNumber.startsWith("4936") || creditCardNumber.startsWith("564182") || creditCardNumber.startsWith("633110") || creditCardNumber.startsWith("6333") || creditCardNumber.startsWith("6759")) { cardId = CreditCardValidator.SWITCH; returnValue = true; } } return returnValue; }