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:
| Parameter | Type | Required | Description |
|---|---|---|---|
controller | PayCardController | Yes | Holds what's typed. Pass the same controller to every field of one card. |
modifier | Modifier | No | Applied to the field, which fills the width it's given. Default: Modifier. |
style | PayFieldStyle | No | How the field looks. Default: PayFieldStyle(). See Style the card fields. |
placeholder | String | No | Shown while the field is empty. Default: Card number, MM / YY, CVC, or Name on card. |
label | String? | No | Draws 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. |
errorText | String? | No | Text 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.
| Field | Keyboard | What it takes |
|---|---|---|
PayCardNumberField | Numeric | Up to 19 digits, formatted for the brand (4242 4242 4242 4242, or 4, 6, and 5 digits for American Express). |
PayExpiryField | Numeric | A month and a year, shown as MM / YY. A first digit above 1 becomes 0M. |
PayCvcField | Numeric | Up to 4 digits. |
PayNameField | Text, with each word capitalized | Up 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
INVALIDshows its text and border in the style'sinvalidColor. When the customer pays with incomplete or invalid fields,checkout.pay()throwsPayError.InvalidCardInputwith 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.
| Property | Type | Required | Description |
|---|---|---|---|
controller | PayCardController? | Yes | Holds what's typed. Set the same controller on every view of one card. |
style | PayFieldStyle | No | How the field looks. Default: PayFieldStyle(). |
placeholder | String? | No | Shown while the field is empty. Default: null, for the field's own placeholder. |
onFocusChange | ((Boolean) -> Unit)? | No | Called 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
- Style the card fields: Match the fields to your app.
- PayCardController: The controller's state,
onBinChanged, andclear(). - Pay: Pay with the card.