A java library that utilizes the Luhn algorithm to test for validity of numeric credit card combinations. Additionally, the validation package provides utility classes to check credit card type, expiration date and CVV.
The following are the supported credit card types:
Common types
- VISA, VISA Electron
- American Express
- Mastercard
- Discover
Additional types
- JCB
- Maestro, Maestro UK
- China Union, China T-Union
- Dankort
- Diners Club, Diners Club International
- InstaPayment
- InterPayment
- LankaPay
- MIR
- NPS Pridnestrovie
- RuPay
- Troy
- UATP
- Verve
This library provides issuer-based validators for custom implementations and a TypeChecker utility class that compares the credit card number against common types.
// To utilize individual validators, instantiate the validator
VisaValidator visaValidator = new VisaValidator("4547250466466268");
// Then validate
boolean isValid = visaValidator.validate();
List of utility classes:
└── parseNumber(String creditCardNumber)
├── parseIIN(List creditCardNumberList, int range)
└── CURRENT_YEAR
├── CURRENT_MONTH
├── compareDates(int date, int currentDate)
├── convertDate(String expirationDate)
└── parseDate(StringBuilder expirationDate)
├── parseDate(StringBuilder expirationDate, int startLocation, int endLocation)
└── checkType(String creditCardNumber)
The credit card parser provides static utility methods used to parse credit card number and IIN to compare against the accepted IIN ranges set by issuers.
Consumes the String representation of credit card number and returns the parsed value as a List of Integers.
Usage
List<Integer> creditCardNumberList = new ArrayList<>();
creditCardNumberList = CreditCardParser.parseNumber("4547250466466268");
Consumes the List representation of parsed credit card number and the IIN range. Then returns the IIN value.
Usage
List<Integer> creditCardNumberList = new ArrayList<>();
creditCardNumberList = CreditCardParser.parseNumber("4547250466466268");
int IINRange = 31;
int firstTwo = parseIIN(creditCardNumberList, 2);
if (firstTwo == IINRange) {
hasAllowedIINRange = true;
}
The date checker provides static utility classes and constants to compare the expiration date of the credit card against the current year and month.
A static constant of the current year.
A static constant of the current month.
A static method that consumes the expiration month/year and current month/year then returns a boolean of whether or not the value passes.
boolean isValid = DateChecker.compareDates("11", CURRENT_MONTH);
A static method that consumes the String representation of an expiration date and returns a StringBuilder.
StringBuilder expirationDate = DateChecker.convertDate("11/20");
The date parser provides utility classes to parse the expiration date.
A static method that consumes a StringBuilder representation of the expiration date and returns a standardized StringBuilder value without the backslash.
StringBuilder parsedExpirationDate = DateParser.parseDate(expirationDate);
A static method that consumes a StringBuilder representation of the expiration date, the start and end location to parse out then returns the parsed value as an int.
int month = DateParser.parseDate(expirationDate, 0, 2);
The type checker provides a static method that is utilized to check the credit card number against the common credit card types from the supported credit card type list of this library.
A static method that runs the credit card number through the common credit card type validators and returns the type with a fallback of OTHER.
CreditCardType.checkType("4547250466466268");