AuricSystemsInternational / creditcard-validator / CreditCardValidator

This library allows validation of a creditcard number potentially limiting by accepted card types. For example, if your business only accepts Mastercard and Visa you can limit the valid cards to those types.

Definition


type CardType
    = AM
    | DS
    | MC
    | VI
    | DC
    | UK

Card types supported by this library are: American Express (AM), Discover (DS), Diners Club (DC), Mastercard (MC) and VISA (VI).


type alias CardTypeInfo =
{ name : String
, cardType : CardType
, bins : List Range
, valid_lengths : List Basics.Int 
}


type alias CreditCardNumber =
String


type alias ValidationResult =
{ card_types : List (Maybe CardTypeInfo)
, valid : Basics.Bool
, luhn_valid : Basics.Bool
, length_valid : Basics.Bool
, cardTypeValid : Basics.Bool 
}

A card number can match multiple card types. Overall, a number is valid when it's valid according to luhn algorithm with valid length and type.


type Range

Represents possible ranges for a creditcard number. It can be a string or a range with starting and ending digits

Validation

validate : String -> List CardType -> ValidationResult

Find whether a creditcard number is valid. You can limit what card types are eligible for the validation attempt.

Helpers

mopToCardType : String -> CardType

Converts from string MOP (Method of Payment) to CardType

validCardLength : CardTypeInfo -> CreditCardNumber -> Basics.Bool

Finds whether card length is one of the valid lengths for the type

findNumberInBetweenRange : CreditCardNumber -> String -> String -> Basics.Bool

allCardTypes : List CardTypeInfo

List of CardTypeInfo for all supported types

cardTypeByBinRange : CreditCardNumber -> List CardTypeInfo -> Maybe CardTypeInfo

Find card info whose ranges contain a pattern that the card number matches

numberInStartsWithRange : String -> String -> Basics.Bool

cardMatchesRange : CreditCardNumber -> List Range -> Basics.Bool

filterByCardTypes : List CardTypeInfo -> List CardType -> List CardTypeInfo

toCleanCCNumber : String -> CreditCardNumber