usePrimerCardForm gives your components the card form state held by PrimerCardFormProvider: field values, update functions, validation errors, the detected network’s traits and submit(). The card inputs and PrimerCardForm use it internally. Call it yourself for a Pay button, your own fields or your own error messages.
Signature
PrimerCardFormProvider, which sits inside PrimerCheckoutProvider. Without PrimerCardFormProvider it throws an error that starts with usePrimerCardForm must be used within a <PrimerCardFormProvider>.
Every caller below the same provider reads and writes the same state. The options apply only to the caller that passes them.
Options
The hook keeps the latest callbacks in refs, so passing a new function on every render doesn’t cause extra calls.
Returns
Field values
Update functions
When the detected network changes, the SDK reformats the card number for the new network and cuts digits beyond its longest length.
Validation state
Submission state
Card details
Network descriptor
descriptor describes the detected network. The SDK uses it to format the card number and to size and label the CVV field.
Until a network is detected, and when the native SDK has no traits for it,
descriptor holds the default:
Types
UseCardFormOptions, UsePrimerCardFormReturn, CardFormField, CardFormErrors.
When errors show
The native SDK validates the card data. The hook decides which messages to show:- A field’s error appears in
errorsafter the shopper focuses the field and leaves it with a value in it. The error stays, also while the shopper edits the field again, until the value is valid. An empty field shows no error. - After a submit attempt, every card field with an error shows it, empty fields included. This lasts until
reset(). isValiddoesn’t depend on which errors are shown. Gate your Pay button on it.- The messages come from the native SDK.
errorsonly has entries for the four card fields.
markFieldFocused and markFieldBlurred for you. Call them yourself only for fields you build.
How submit works
submit() runs these steps:
- It marks the submit as attempted, so every error shows.
- It stops when the provider isn’t ready, the card form isn’t active, or a submit is already running.
- It sends any pending field changes to the native SDK.
- It stops when
isValidisfalse. - It hands the card to the native SDK, which tokenizes it and creates the payment.
submit() never rejects. Read the result from usePrimerCheckout().paymentOutcome.
The card form is active after you call start() on usePrimerPaymentMethod('PAYMENT_CARD'). Until then the SDK validates nothing and isValid stays false.
Examples
Pay button
error.description is for your logs. Show the shopper your own message. See PrimerError.
Your own field
Build a field withPrimerTextInput and wire it to the hook. Pass the value, the updater, the focus calls and the error:
See also
PrimerCardForm
The card fields in a fixed layout, and
PrimerCardFormProvider.Card inputs
The individual card fields and
PrimerTextInput.Card networks
BinData, the detected network and co-badged selection.usePrimerCheckout
paymentOutcome and the rest of the checkout state.