Card field types (pay-elements)
Reference for the card fields' controller and the state it reports, from each field's status to the card's brand.
pay-elements provides Jetpack Compose card fields along with View versions for layouts without Compose. Its package is com.pay.sdk.elements, and it brings in pay-core.
| API | Description |
|---|---|
PayCardNumberField, PayExpiryField, PayCvcField, PayNameField | The Compose fields. |
PayCardNumberView, PayExpiryView, PayCvcView, PayNameView | Their View versions. They extend PayFieldView, an AbstractComposeView with the controller, style, placeholder, and onFocusChange properties. |
PayFieldStyle | How the fields look. |
PayCardController | Holds what was typed into one card's fields. |
PaySavedCardCvcField, PaySavedCardCvcView | A saved card's CVV field, in Compose and as a View. |
PayCvcController | Holds what was typed into a saved card's CVV field. |
The SDK owns what is typed into the fields. The values stay inside pay-elements, and only the checkout reads them, when it pays. Your app sees the fields' state and, through onBinChanged, the card's first 8 digits.
PayCardController
Holds what was typed into one card's fields. Pass the same controller to each field, then pay with PayPaymentRequest.Card(controller) through checkout.pay(). Printing it shows PayCardController(<redacted>).
val card = PayCardController(requiresName = false)| Member | Type | Description |
|---|---|---|
requiresName | Boolean | Whether the name field must be filled. With true and no PayNameField, the card is never complete, and pay throws InvalidCardInput with NAME. Default: false. |
state | StateFlow<PayCardFieldsState> | The state of every field. |
onBinChanged | ((String?) -> Unit)? | Called with the first 8 digits once they're typed, and with null when fewer remain, clear() included. |
clear() | Empties every field. |
PayCvcController
Holds what was typed into a saved card's CVV field, PaySavedCardCvcField or PaySavedCardCvcView. Pay with PayPaymentRequest.SavedPaymentMethod(method, cvc = controller) through checkout.pay(). Printing it shows PayCvcController(<redacted>).
val cvc = PayCvcController().apply { brand = method.card?.brand }| Member | Type | Description |
|---|---|---|
brand | String? | The saved card's brand as the API sends it, from method.card?.brand, for example visa or american_express. The CVV has 4 digits for American Express, 3 for the other brands, and 3 or 4 when the brand is null or unknown. Default: null. |
state | StateFlow<PayCvcState> | The field's status, a PayFieldStatus for brand, and isEmpty, true while nothing is typed. |
clear() | Empties the field. |
Card state
PayCardFieldsState, PayFieldStatus, and PayCardBrand are in pay-core, package com.pay.sdk.core.card. state is a PayCardFieldsState:
| Property | Type | Description |
|---|---|---|
number, expiry, cvc | PayFieldStatus | The status of each field. |
name | PayFieldStatus? | The name field's status, or null when there's no name field and requiresName is false. |
brand | PayCardBrand | The brand detected from the first digits. |
isComplete | Boolean | true when every required field is valid. Enable your pay button with it. |
PayFieldStatus is EMPTY, INCOMPLETE (still being typed), INVALID, or VALID.
PayCardBrand is VISA, MASTERCARD, AMEX, DISCOVER, DINERS, JCB, UNIONPAY, MAESTRO, or UNKNOWN.
How the fields validate
Each field is VALID when its value meets these rules.
| Field | VALID when |
|---|---|
| Card number | It passes the Luhn check with a valid length for its brand (13, 16, or 19 digits for Visa, 16 for Mastercard, 15 for American Express, 14 to 19 for Diners Club, 16 to 19 for Discover, JCB, and UnionPay, and 12 to 19 for Maestro and unknown brands). A number that fails the check at 16 digits or more, or at its brand's longest length, is INVALID. |
| Expiration date | It has a month from 01 to 12 and a year, and isn't in the past or more than 20 years ahead. Any other month is INVALID as soon as it's typed. |
| CVV | It has 4 digits for American Express, 3 for the other brands, and 3 or 4 when the brand is unknown. |
| Name | It has a letter and at most 200 characters, after trimming. An empty name is fine unless requiresName is true. |