Skip to main content

PaymentCardNumber

This class represents a payment card number and provides comprehensive validation including digit checks, Luhn algorithm verification, and brand-specific length requirements. It automatically extracts metadata such as the Bank Identification Number (BIN), the last four digits, and the card brand during initialization. Additionally, it includes a property for generating a masked version of the card number for secure display.

Attributes

AttributeTypeDescription
strip_whitespaceClassVar[bool] = TrueDetermines whether leading and trailing whitespace should be removed from the card number string during validation.
min_lengthClassVar[int] = 12The minimum allowed character length for a valid payment card number.
max_lengthClassVar[int] = 19The maximum allowed character length for a valid payment card number.
binstrThe Bank Identification Number (BIN) consisting of the first six digits of the card number.
last4strThe final four digits of the payment card number used for identification and display.
brand[PaymentCardBrand](../v1/types/paymentcardbrand.md?sid=pydantic_v1_types_paymentcardbrand)The detected issuer brand of the payment card based on its BIN and length.

Constructor

Signature

def PaymentCardNumber(
card_number: str
)

Parameters

NameTypeDescription
card_numberstrThe raw payment card number string to be validated and processed.

Signature

def PaymentCardNumber(
card_number: str
)

Parameters

NameTypeDescription
card_numberstrThe raw payment card number string to be validated and parsed

Methods


validate()

@classmethod
def validate(
input_value: str,
_: core_schema.ValidationInfo
) - > [PaymentCardNumber](../v1/types/paymentcardnumber.md?sid=pydantic_v1_types_paymentcardnumber)

Validate the card number and return a PaymentCardNumber instance.

Parameters

NameTypeDescription
input_valuestrThe raw card number string to validate
_core_schema.ValidationInfoPydantic validation context information

Returns

TypeDescription
[PaymentCardNumber](../v1/types/paymentcardnumber.md?sid=pydantic_v1_types_paymentcardnumber)An initialized and validated PaymentCardNumber instance

masked()

@classmethod
def masked() - > str

Mask all but the last 4 digits of the card number.

Returns

TypeDescription
strA masked card number string.

validate_digits()

@classmethod
def validate_digits(
card_number: str
) - > None

Validate that the card number is all digits.

Parameters

NameTypeDescription
card_numberstrThe card number string to check for numeric characters

Returns

TypeDescription
NoneNothing; raises a PydanticCustomError if non-digit characters are found

validate_luhn_check_digit()

@classmethod
def validate_luhn_check_digit(
card_number: str
) - > str

Based on: https://en.wikipedia.org/wiki/Luhn_algorithm.

Parameters

NameTypeDescription
card_numberstrThe card number string to verify using the Luhn algorithm

Returns

TypeDescription
strThe validated card number string if the Luhn check passes

validate_brand()

@classmethod
def validate_brand(
card_number: str
) - > [PaymentCardBrand](../v1/types/paymentcardbrand.md?sid=pydantic_v1_types_paymentcardbrand)

Validate length based on BIN for major brands: https://en.wikipedia.org/wiki/Payment_card_number#Issuer_identification_number_(IIN).

Parameters

NameTypeDescription
card_numberstrThe card number string used to identify the issuer and validate length

Returns

TypeDescription
[PaymentCardBrand](../v1/types/paymentcardbrand.md?sid=pydantic_v1_types_paymentcardbrand)The identified PaymentCardBrand enum value