Skip to Content
Docs

Pay with a saved payment method

In a sale or authorization session, list the customer's saved methods, ask for a saved card's CVV when needed, and charge the one they pick.

In a sale or authorization session, the customer can pay with one of their saved payment methods. checkout.savedPaymentMethods() lists the saved methods of the customer the session was created for (its customer_reference_id), and checkout.pay() charges the one they pick. For a saved card, checkout.requiresCvc() tells you when to ask for its CVV.

val method = checkout.savedPaymentMethods().first()
val cvc = PayCvcController().apply { brand = method.card?.brand }
// Show PaySavedCardCvcField(cvc, …) when checkout.requiresCvc(method) is true.
val result = checkout.pay(PayPaymentRequest.SavedPaymentMethod(method, cvc), activity)

The drop-in shows the customer's saved methods for you, in a carousel. To show them on your own screen instead, use savedPaymentMethods(), requiresCvc(), and checkout.pay().

List the saved methods

In a sale or authorization session, savedPaymentMethods() returns the customer's saved cards that haven't expired and their other saved methods the session can pay, such as PayPal accounts and bank accounts, in the order the API sends them. Passkeys, Click to Pay, Apple Pay, and Google Pay aren't listed. Some saved methods can be paid only in a sale session, so an authorization session doesn't list them.

Each one is a PaySavedPaymentMethod. A card's card has the brand, the last 4 digits, the expiration date, and, when Pay.com knows it, the issuing bank's name. Whether a card has expired is checked against the device's clock when the checkout was created. A bank account's bankAccount has the bank's name and the last 4 digits, and a PayPal account has its email.

Ask for the CVV

checkout.requiresCvc(method) is true for a saved card when the session checks CVVs and Pay.com doesn't hold one for the card. Then show a CVV field for the customer to type it into, backed by a PayCvcController from pay-elements:

  • In Compose: PaySavedCardCvcField, a Material 3 outlined field.
  • Without Compose: PaySavedCardCvcView, in the card fields' boxed style.

Set the controller's brand to the card's brand, so the field expects the right length (4 digits for American Express, 3 for other brands, and 3 or 4 when the brand is unknown). The digits stay in pay-elements. Your app sees only the controller's state.

Parameters

ParameterTypeRequiredDescription
methodPaySavedPaymentMethodYesOne of this checkout's savedPaymentMethods().

Returns

A Boolean. It's always false in a setup session, and for a saved method that isn't a card.

The CVV field

PaySavedCardCvcField takes:

ParameterTypeRequiredDescription
controllerPayCvcControllerYesHolds what's typed.
labelStringYesThe field's floating label, for example CVV.
placeholderStringYesShown while the field is focused and empty. Pass "" for none.
isErrorBooleanYesShows the field in the theme's error colors. Your app decides when, from the controller's state.
supportingTextString?YesText under the field, in the error color while isError is true. Pass null for none.
modifierModifierNoApplied to the field, which fills the width it's given. Default: Modifier.

PaySavedCardCvcView draws the field for layouts without Compose, and shows nothing until you set its controller:

PropertyTypeRequiredDescription
controllerPayCvcController?YesHolds what's typed.
stylePayFieldStyleNoHow the field looks. Default: PayFieldStyle(). See Style the card fields.
placeholderString?NoShown while the field is empty. Default: null, for CVC.
onFocusChange((Boolean) -> Unit)?NoCalled with true when the field gains focus and false when it loses it. Default: null.

Both fields take digits only, from a number keyboard, up to the length the brand allows. Like the card fields, they show no copy or cut toolbar and never write to the clipboard. Neither offers autofill, since a CVV the device saved may belong to another card. The View's accessibility label is "Security code".

Pay

Pay with PayPaymentRequest.SavedPaymentMethod(method, cvc) through checkout.pay(), which returns a PayResult.

  • A saved card. The SDK sends it to Pay.com's vault, with the CVV when there is one. The payment then runs the same way as a card entered in the card fields, including 3DS, any challenge over your activity, and the amount check.
  • Another saved method. Pay.com pays it the same way as in Pay.com.js. It may open the provider's page, as a redirect payment method does, so handle the return URL. The payment can end as PayResult.Processing, for your server to decide.

When requiresCvc(method) is false, cvc is optional. A field left empty sends nothing, and a CVV typed into it must be valid. The CVV goes only to Pay.com's vault. With clientSideEncryption on, the SDK encrypts it on the device first. Your app never reads it.

Parameters

PayPaymentRequest.SavedPaymentMethod takes:

ParameterTypeRequiredDescription
methodPaySavedPaymentMethodYesThe method the customer picked, from this checkout's savedPaymentMethods().
cvcCvcSource?NoThe PayCvcController the customer typed the card's CVV into. Required when requiresCvc(method) is true, and not used for a method that isn't a card. Default: null.

Errors

pay() throws a PayError only when a call fails before sending a request:

ErrorWhen
PayError.InvalidCardInput(fields)requiresCvc(method) is true and no cvc was passed, or the CVV entered isn't valid for the card's brand. fields is [CVC].
PayError.Unsupported(detail)The method isn't one of this checkout's savedPaymentMethods().
PayError.PaymentInProgressAnother payment, or a removal, is running on this checkout.

Declines and the other failures come back as PayResult.Failed(error), as for a new card. See Pay.

Example

@Composable
fun SavedMethodPicker(checkout: PayCheckout, onPay: (PayPaymentRequest) -> Unit) {
    val saved = remember(checkout) { checkout.savedPaymentMethods() }
    var selected by remember { mutableStateOf(saved.firstOrNull { it.isDefault } ?: saved.firstOrNull()) }
    val cvc = remember { PayCvcController().apply { brand = selected?.card?.brand } }
    val cvcState by cvc.state.collectAsState()

    Column(Modifier.selectableGroup(), verticalArrangement = Arrangement.spacedBy(8.dp)) {
        saved.forEach { method ->
            Row(
                Modifier
                    .fillMaxWidth()
                    .selectable(
                        selected = method == selected,
                        onClick = {
                            selected = method
                            cvc.clear()
                            cvc.brand = method.card?.brand
                        },
                        role = Role.RadioButton,
                    )
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                RadioButton(selected = method == selected, onClick = null)
                Text(method.label(), Modifier.padding(start = 16.dp))
            }
        }
        val method = selected
        if (method != null && checkout.requiresCvc(method)) {
            val invalid = cvcState.status == PayFieldStatus.INVALID
            PaySavedCardCvcField(
                cvc,
                label = "CVV",
                placeholder = "",
                isError = invalid,
                supportingText = "Check the CVV".takeIf { invalid },
            )
        }
        Button(onClick = { method?.let { onPay(PayPaymentRequest.SavedPaymentMethod(it, cvc)) } }, enabled = method != null) {
            Text("Pay")
        }
    }
}

fun PaySavedPaymentMethod.label(): String {
    card?.let { return "${it.brand ?: "Card"} •••• ${it.last4.orEmpty()}" }
    bankAccount?.let { return "${it.bankName ?: "Bank account"} •••• ${it.last4.orEmpty()}" }
    return email ?: type
}

In the activity, pay with the request, as for any other payment:

private fun pay(request: PayPaymentRequest) {
    lifecycleScope.launch {
        val result = try {
            checkout.pay(request, this@CheckoutActivity)
        } catch (e: PayError) {
            return@launch showMessage(e) // For example, the CVV is missing. Nothing was sent
        }
        when (result) {
            is PayResult.Succeeded -> showReceipt(result.ids)  // A hint: your server confirms first
            is PayResult.Processing -> showPending(result.ids) // Your server decides
            PayResult.Canceled -> Unit                         // The customer can try again
            is PayResult.Failed -> showMessage(result.error)
        }
    }
}

PayResult is a hint for your UI. Fulfill an order only after your server retrieves the session with Retrieve a payment session or receives the signed payment_session.completed webhook.

Next steps

Was this page helpful?