Skip to Content
Docs

Card fields

Collect a card in Compose fields, or their View versions, that the SDK owns, so your app never reads the card number.

The card fields (PayCardNumberField, PayExpiryField, PayCvcField, and optionally PayNameField) collect card details inside the SDK. Give them the same PayCardController, then pay with PayPaymentRequest.Card(controller) through checkout.pay().

Your app never reads what's typed. It sees the fields' state, the card's brand, and, through the controller's onBinChanged, its first 8 digits. The fields are Jetpack Compose functions in pay-elements (package com.pay.sdk.elements), with View versions for layouts without Compose. To change how they look, see Style the card fields.

To ask for a saved card's CVV at checkout, use PaySavedCardCvcField or PaySavedCardCvcView with a PayCvcController instead. See Pay with a saved payment method.

val card = remember { PayCardController() }

PayCardNumberField(card)
PayExpiryField(card)
PayCvcField(card)
PayNameField(card)

Parameters

Each field takes the same parameters:

ParameterTypeRequiredDescription
controllerPayCardControllerYesHolds what's typed. Pass the same controller to every field of one card.
modifierModifierNoApplied to the field, which fills the width it's given. Default: Modifier.
stylePayFieldStyleNoHow the field looks. Default: PayFieldStyle(). See Style the card fields.
placeholderStringNoShown while the field is empty. Default: Card number, MM / YY, CVC, or Name on card.
labelString?NoDraws a Material 3 outlined field with this floating label, instead of the default boxed field. With a label, the placeholder shows only while the field is focused and empty. Default: null.
errorTextString?NoText displayed under the field when label is set, which also puts the field in its error colors. Default: null.

PayCardNumberField also takes showsBrandLogo: Boolean. It displays the card's brand at the end of the field, matching Pay.com.js (the Visa, Mastercard, American Express, or Discover logo once detected, or a generic card icon). Pass false to hide it. Default: true.

What the fields accept

Each field shows a keyboard for its input and limits what the customer can type.

FieldKeyboardWhat it takes
PayCardNumberFieldNumericUp to 19 digits, formatted for the brand (4242 4242 4242 4242, or 4, 6, and 5 digits for American Express).
PayExpiryFieldNumericA month and a year, shown as MM / YY. A first digit above 1 becomes 0M.
PayCvcFieldNumericUp to 4 digits.
PayNameFieldText, with each word capitalizedUp to 200 characters. Showing it adds the name to the card's state.
  • Autofill. Each field asks the device's autofill service for its corresponding value (card number, expiration date, security code, or cardholder name).
  • No copying. The fields show no copy or cut toolbar and never write to the clipboard. Pasting still works through the keyboard's clipboard suggestions.
  • Invalid input. A field whose status is INVALID shows its text and border in the style's invalidColor. When the customer pays with incomplete or invalid fields, checkout.pay() throws PayError.InvalidCardInput with the fields, and sends nothing.
  • Accessibility. Each field sets a default accessibility label ("Card number", "Expiry date", "Security code", and "Name on card"). The card number's brand logo reads the brand's name, for example "Visa".

View versions

For layouts without Compose, use PayCardNumberView, PayExpiryView, PayCvcView, and PayNameView. Each one draws the matching field, and shows nothing until you set its controller. They have no label or errorText. Draw those in your layout. PayCardNumberView always shows the brand logo.

PropertyTypeRequiredDescription
controllerPayCardController?YesHolds what's typed. Set the same controller on every view of one card.
stylePayFieldStyleNoHow the field looks. Default: PayFieldStyle().
placeholderString?NoShown while the field is empty. Default: null, for the field's own placeholder.
onFocusChange((Boolean) -> Unit)?NoCalled with true when the field gains focus and false when it loses it. Default: null.

Example

@Composable
fun CardForm(onPay: (PayCardController) -> Unit) {
    val card = remember { PayCardController(requiresName = true) }
    val state by card.state.collectAsState()

    Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
        PayCardNumberField(
            card,
            label = "Card number",
            errorText = "Check the card number".takeIf { state.number == PayFieldStatus.INVALID },
        )
        Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {
            PayExpiryField(card, Modifier.weight(1f), label = "Expiration date")
            PayCvcField(card, Modifier.weight(1f), label = "CVV")
        }
        PayNameField(card, label = "Name on card", placeholder = "")
        Button(onClick = { onPay(card) }, enabled = state.isComplete, modifier = Modifier.fillMaxWidth()) {
            Text("Pay")
        }
    }
}

With the View versions, place them in your layout:

<com.pay.sdk.elements.PayCardNumberView
    android:id="@+id/card_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <com.pay.sdk.elements.PayExpiryView
        android:id="@+id/card_expiry"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <com.pay.sdk.elements.PayCvcView
        android:id="@+id/card_cvc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

Then give them one controller, and follow its state:

val card = PayCardController()
listOf(binding.cardNumber, binding.cardExpiry, binding.cardCvc).forEach { it.controller = card }
binding.cardCvc.placeholder = "Security code"

lifecycleScope.launch {
    repeatOnLifecycle(Lifecycle.State.STARTED) {
        card.state.collect { state -> binding.payButton.isEnabled = state.isComplete }
    }
}
binding.payButton.setOnClickListener { pay(PayPaymentRequest.Card(card)) }

Next steps

Was this page helpful?