Install the package and configure validation and session exclusions for a Laravel for Go checkout form.
go get github.com/bowerbird-si/laravel-go-credit-card
creditcard.Parse("4111 0000 0000 0008") validates the number and returns a card containing its
brand and digit count. Use card.Brand().Funding() for debit or credit. creditcard.Detect checks
brand recognition alone. To require a particular brand, look it up with creditcard.BrandNamed("visa")
and call its Parse method. The checks run locally and validate format and checksum. The payment
provider verifies the account and authorizes payments.
Validate a form
Keep numbers and security codes in string fields to preserve leading zeroes. This example uses the
standard sibling name card_number. Use CVCFrom("number") when the field has a different name.
type Payment struct {
CardNumber string `json:"card_number"`
CVC string `json:"cvc"`
Expiry string `json:"expiry"`
}
func (f *Payment) Rules() validation.Rules {
return validation.For(f,
rule.Field(&f.CardNumber, rule.Required, creditcard.Number()),
rule.Field(&f.CVC, rule.Required, creditcard.CVC()),
rule.Field(&f.Expiry, rule.Required, creditcard.ExpirationDate("m/y")),
)
}
Import go.laravel.com/validation and go.laravel.com/validation/rule for the form. Add
rule.Required to mandatory fields. Laravel skips these card rules for missing, null and blank
values.
Keep card fields out of old input
Laravel flashes form input to the session on a validation redirect. Set DontFlash on your
application's laravel.Options before accepting a card form.
laravel.Options{
Errors: web.Errors{
DontFlash: []string{"card_number", "cvc"},
},
}
Import go.laravel.com and go.laravel.com/web for this configuration. Use the submitted field paths,
such as card.number and cards.*.cvc for nested forms. The application uses the same exclusions
with web.Back().WithInput().
Expiration checks
ExpirationDate("m/y") expects PHP format tokens. For separate fields, use ExpirationYear()
and ExpirationMonth() with the default sibling paths expiration_year and
expiration_month. The From constructors select other field paths. All three rules use the
Laravel application clock. For direct checks, pass a reference time to ValidExpiration or
ValidExpirationDate.
The card stays valid through its expiration month. A two-digit year in a split field uses the
current century. A formatted date uses the PHP pivot, where 69 means 2069 and 70 means 1970.
Messages
Rules use English by default. Call Message("Check the entered value.") to override a rule, or
MessageFor(creditcard.KeyLengthInvalid, "Check the number of digits.") to replace one failure
message. A template can use Laravel's :attribute placeholder.