Card fields
Create a set of SDK-owned card inputs, place them in your own layout, and follow their validation state.
The card fields are the native SDKs' own inputs, shown in your layout through React Native components. What the customer types stays in native code and goes from there to Pay.com. The card number, expiration date, and CVV never reach JavaScript, so no bundle, debugger, error reporter, or analytics SDK in the app can read them. JavaScript sees only each field's validation state, the card brand, and the first 8 digits.
usePayCardFields() creates a set of fields for the calling component, and releases them when it unmounts. Render PayCardNumberField, PayExpiryField, PayCvcField, and, for the cardholder's name, PayNameField with the same fields. Then pay with { type: 'card', card: fields } in checkout.pay(). To change how the inputs look, see Style the card fields.
const { fields, state } = usePayCardFields()
return (
<>
<PayCardNumberField fields={fields} />
<PayExpiryField fields={fields} />
<PayCvcField fields={fields} />
</>
)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
options.requiresName | boolean | No | Requires the cardholder's name. state.isComplete then waits for a valid PayNameField. It's read once, when the fields are created. Default: false. |
Returns
usePayCardFields() returns the set of fields and its current state.
| Field | Type | Description |
|---|---|---|
fields | PayCardFields | The set of fields. Pass it to each field component, and to checkout.pay(). |
state | PayCardFieldsState | The fields' validation state. The component renders again whenever it changes. |
PayCardFieldsState
state has a status for each field, the card's brand, and whether the set is complete.
| Field | Type | Description |
|---|---|---|
number | PayFieldStatus | The card number's status ('empty', 'incomplete', 'invalid', or 'valid'). |
expiry | PayFieldStatus | The expiration date's status. |
cvc | PayFieldStatus | The CVV's status. |
name | PayFieldStatus | null | The name's status, or null when the set has no name field, because the name isn't required and no PayNameField has been shown. |
brand | PayCardBrand | The card brand detected from the entered number ('visa', 'mastercard', 'amex', 'discover', 'diners', 'jcb', 'unionpay', 'maestro', or 'unknown'). |
isComplete | boolean | true when every required field is valid. Turn on your Pay button with it. |
PayCardFields
fields is a PayCardFields:
| Member | Type | Description |
|---|---|---|
state | PayCardFieldsState | The current state. |
subscribe(listener) | (listener: (state: PayCardFieldsState) => void) => () => void | Calls listener whenever the state changes. Returns the function that stops it. |
onBinChange(listener) | (listener: (bin: string | null) => void) => () => void | Calls listener with the card number's first 8 digits once they're typed, and with null when fewer remain. Returns the function that stops it. |
clear() | () => void | Empties every field. |
release() | () => void | Frees the native fields, and the components showing them go blank. It's safe to call more than once. usePayCardFields() calls it when the component unmounts. |
isReleased | boolean | true once the fields are released. |
requiresName | boolean | Whether the set requires the cardholder's name. |
id | string | Opaque, process-scoped identifier for the native input views. |
Outside a component, create a set with new PayCardFields({ requiresName }). It lives until you call its release().
Props
Each field component shows one input of a set:
| Component | Input |
|---|---|
PayCardNumberField | The card number, formatted as it's typed, with brand detection. The brand's logo appears at the end of the field (Visa, Mastercard, American Express, or Discover, or a generic card icon). |
PayExpiryField | The expiration date, as MM / YY. |
PayCvcField | The CVV. Its length follows the brand typed in the number field. |
PayNameField | The cardholder's name. It's required only when the fields were created with requiresName. |
They all take the same props, PayCardFieldProps:
| Prop | Type | Required | Description |
|---|---|---|---|
fields | PayCardFields | Yes | The set the input belongs to, from usePayCardFields(). |
placeholder | string | No | The input's placeholder. |
enabled | boolean | No | false disables the input on iOS. Default: true. |
onFocusChange | (focused: boolean) => void | No | Called when the input gains or loses focus. |
fieldStyle | PayFieldStyle | No | The input's colors, border, and font. See Style the card fields. |
style | StyleProp<ViewStyle> | No | The input's size and place in your layout. See Layout. |
They also take the other View props except children, for example testID.
On iOS, each input of a set is a single native view, so show each input in one place at a time. Showing the same input of a set in a second place moves it there.
Example
In the sandbox, type a test card such as 4242 4242 4242 4242, with any future expiration date and a CVV such as 123.
import { useEffect } from 'react'
import { Button, StyleSheet, Text, View } from 'react-native'
import {
PayCardNumberField,
PayCvcField,
PayExpiryField,
PayNameField,
usePayCardFields,
type PayCardFields,
} from '@pay-com/react-native'
export function CardForm({ onPay }: { onPay: (fields: PayCardFields) => void }) {
const { fields, state } = usePayCardFields({ requiresName: true })
// For example, to look the card's BIN up on your server. Unsubscribes on unmount.
useEffect(() => fields.onBinChange((bin) => console.log('BIN', bin)), [fields])
return (
<View style={styles.form}>
<PayNameField fields={fields} placeholder="Name on card" />
<PayCardNumberField fields={fields} placeholder="Card number" />
<View style={styles.row}>
<PayExpiryField fields={fields} placeholder="MM / YY" style={styles.half} />
<PayCvcField fields={fields} placeholder="CVV" style={styles.half} />
</View>
{state.number === 'invalid' ? <Text>Check the card number.</Text> : null}
<Button title="Pay" disabled={!state.isComplete} onPress={() => onPay(fields)} />
<Button title="Clear" onPress={() => fields.clear()} />
</View>
)
}
const styles = StyleSheet.create({
form: { gap: 12 },
row: { flexDirection: 'row', gap: 12 },
half: { flex: 1 },
})Next steps
- Style the card fields: Change the inputs' colors, border, font, and size.
- Saved card CVV: The input for a saved card's CVV at checkout.
- checkout.pay(): Pay with the fields, and what it rejects with when they're incomplete.
- Present the drop-in: Let the drop-in show its own card form instead.