Skip to content

aadams26/creditcardvalidator

Repository files navigation

Build Status

Credit Card Validator

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.

Supported card types

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

Type Validators

This library provides issuer-based validators for custom implementations and a TypeChecker utility class that compares the credit card number against common types.


Issuer-based validator usage

// To utilize individual validators, instantiate the validator
VisaValidator visaValidator = new VisaValidator("4547250466466268");

// Then validate
boolean isValid = visaValidator.validate();

Utility classes


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)


# CreditCardParser

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.

parseNumber(String creditCardNumber)

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");

parseIIN(List creditCardNumberList, int range)

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;
}

# DateChecker

The date checker provides static utility classes and constants to compare the expiration date of the credit card against the current year and month.

CURRENT_YEAR

A static constant of the current year.

CURRENT_MONTH

A static constant of the current month.

compareDates(int date, int currentDate)

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);

convertDate(String expirationDate)

A static method that consumes the String representation of an expiration date and returns a StringBuilder.

StringBuilder expirationDate = DateChecker.convertDate("11/20");

# DateParser

The date parser provides utility classes to parse the expiration date.

parseDate(StringBuilder expirationDate)

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);

parseDate(StringBuilder expirationDate, int startLocation, int endLocation)

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);

# TypeChecker

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.

checkType(String creditCardNumber)

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");

About

A java library to validate credit card information.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages