Documentation
¶
Overview ¶
Package creditcard validates credit card numbers, expiration dates and CVC codes for Laravel for Go, a Go port of laravel-validation-rules/credit-card.
Upstream: https://github.com/laravel-validation-rules/credit-card
Example ¶
The README quick start. CI runs this example.
card, err := creditcard.Parse("4111 0000 0000 0008")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(card.Brand(), card.Brand().Funding(), card)
Output: Visa credit Visa card, 16 digits
Index ¶
- Constants
- func ValidCVC(value any) bool
- func ValidCVCFor(number, cvc any) bool
- func ValidCVCLength(value any, lengths ...int) bool
- func ValidExpiration(year, month any, now time.Time) bool
- func ValidExpirationDate(value, format string, now time.Time) bool
- type Brand
- type BrandSpec
- type Card
- type Checksum
- type Error
- type Funding
- type Rule
- func CVC() Rule
- func CVCFor(number any) Rule
- func CVCFrom(path string) Rule
- func ExpirationDate(phpFormat string) Rule
- func ExpirationMonth() Rule
- func ExpirationMonthFor(year any) Rule
- func ExpirationMonthFrom(path string) Rule
- func ExpirationYear() Rule
- func ExpirationYearFor(month any) Rule
- func ExpirationYearFrom(path string) Rule
- func Number() Rule
Examples ¶
Constants ¶
const ( // KeyCardInvalid is PHP's MSG_CARD_INVALID key. KeyCardInvalid = "validation.credit_card.card_invalid" // KeyPatternInvalid is PHP's unused MSG_CARD_PATTER_INVALID key. KeyPatternInvalid = "validation.credit_card.card_pattern_invalid" // KeyLengthInvalid is PHP's MSG_CARD_LENGTH_INVALID key. KeyLengthInvalid = "validation.credit_card.card_length_invalid" // KeyChecksumInvalid is PHP's MSG_CARD_CHECKSUM_INVALID key. KeyChecksumInvalid = "validation.credit_card.card_checksum_invalid" // KeyCVCInvalid is PHP's MSG_CARD_CVC_INVALID key. KeyCVCInvalid = "validation.credit_card.card_cvc_invalid" // KeyExpirationYearInvalid is PHP's MSG_CARD_EXPIRATION_YEAR_INVALID key. KeyExpirationYearInvalid = "validation.credit_card.card_expiration_year_invalid" // KeyExpirationMonthInvalid is PHP's MSG_CARD_EXPIRATION_MONTH_INVALID key. KeyExpirationMonthInvalid = "validation.credit_card.card_expiration_month_invalid" // KeyExpirationDateInvalid is PHP's MSG_CARD_EXPIRATION_DATE_INVALID key. KeyExpirationDateInvalid = "validation.credit_card.card_expiration_date_invalid" // KeyExpirationDateFormatInvalid is PHP's MSG_CARD_EXPIRATION_DATE_FORMAT_INVALID key. KeyExpirationDateFormatInvalid = "validation.credit_card.card_expiration_date_format_invalid" )
const Version = "0.1.0"
Version is the version of this module.
Variables ¶
This section is empty.
Functions ¶
func ValidCVC ¶
ValidCVC checks for three or four digits, like PHP's CVC length helper. For example, "012" is valid.
func ValidCVCFor ¶
ValidCVCFor checks the CVC against a valid card, like PHP's CardCvc. For example, an American Express number requires a four-digit CVC.
func ValidCVCLength ¶
ValidCVCLength checks against explicit lengths like PHP's isValidCvcLength. For example, ValidCVCLength("0123", 4) succeeds.
func ValidExpiration ¶
ValidExpiration reports whether a year and month are current or in the future as of now. It corresponds to PHP's CardExpirationDateValidator::validate.
Types ¶
type Brand ¶
type Brand struct {
// contains filtered or unexported fields
}
Brand is an immutable PHP card model definition. Look up Visa with BrandNamed("visa").
func BrandNamed ¶
BrandNamed looks up a PHP card model by short name, for example "amex".
func Brands ¶
func Brands() []Brand
Brands returns the PHP card models in recognition order, beginning with Dankort.
func Detect ¶
Detect recognizes the PHP card brand from a number's prefix. For example, Detect("411100") recognizes Visa before its length and checksum are checked.
func (Brand) DisplayName ¶
DisplayName returns the PHP model's display name, such as "Visa".
func (Brand) Parse ¶
Parse validates a number against this PHP card model, for example Visa's length, Luhn checksum and prefix requirements.
type BrandSpec ¶
type BrandSpec struct {
Name, DisplayName string
Funding Funding
Pattern string
Lengths, CVCLengths []int
Checksum Checksum
}
BrandSpec describes a custom PHP-style card model. A Pattern such as ^77 lets the model match numbers beginning with 77.
type Card ¶
type Card struct {
// contains filtered or unexported fields
}
Card is an immutable validation result, corresponding to a PHP card model. It holds the brand and number length.
func Parse ¶
Parse detects the brand and validates a card number, like PHP's Factory::makeFromNumber. For example, Parse("4111 1111 1111 1111") returns a Visa.
type Error ¶
type Error string
Error is a redacted validation category corresponding to a PHP card exception.
const ( // ErrCardNotFound corresponds to PHP's card-not-found exception. ErrCardNotFound Error = "card_not_found" // ErrInvalidType indicates that PHP's string-or-integer contract was not met. ErrInvalidType Error = "invalid_type" // ErrEmpty corresponds to PHP's empty-card exception. ErrEmpty Error = "empty" // ErrCharacters corresponds to PHP's card-characters exception. ErrCharacters Error = "characters" // ErrLength corresponds to PHP's card-length exception. ErrLength Error = "length" // ErrChecksum corresponds to PHP's card-checksum exception. ErrChecksum Error = "checksum" // ErrPattern corresponds to PHP's card-pattern exception. ErrPattern Error = "pattern" // ErrFunding indicates an invalid PHP card product type. ErrFunding Error = "type" // ErrName indicates a missing PHP card name. ErrName Error = "name" // ErrCVCConfig indicates invalid PHP card CVC lengths. ErrCVCConfig Error = "cvc_config" )
type Rule ¶
type Rule struct {
// contains filtered or unexported fields
}
Rule is a Laravel for Go validator corresponding to a PHP card rule. Use constructors like Number() and ExpirationDate("m/y").
func CVCFor ¶
CVCFor implements PHP CardCvc with an explicit number. Only its validated brand is retained.
func ExpirationDate ¶
ExpirationDate implements PHP CardExpirationDate using a PHP format such as "m/y".
func ExpirationMonth ¶
func ExpirationMonth() Rule
ExpirationMonth implements PHP CardExpirationMonth with the sibling expiration_year field.
func ExpirationMonthFor ¶
ExpirationMonthFor implements PHP CardExpirationMonth with an explicit year, such as "28".
func ExpirationMonthFrom ¶
ExpirationMonthFrom implements PHP CardExpirationMonth with a relative field path such as "year".
func ExpirationYear ¶
func ExpirationYear() Rule
ExpirationYear implements PHP CardExpirationYear with the sibling expiration_month field.
func ExpirationYearFor ¶
ExpirationYearFor implements PHP CardExpirationYear with an explicit month, such as 12.
func ExpirationYearFrom ¶
ExpirationYearFrom implements PHP CardExpirationYear with a relative field path such as "month".
func Number ¶
func Number() Rule
Number implements the PHP CardNumber rule as a Laravel for Go rule.
func (Rule) Message ¶
Message returns a rule with one message for every failure, analogous to replacing the PHP rule's translation text.
func (Rule) MessageFor ¶
MessageFor returns a rule with a message for one PHP translation key. For example, Number().MessageFor(KeyLengthInvalid, "Check the number of digits.").