Skip to Content
Docs

Remove a saved payment method

Delete one of the customer's saved methods, and drop it from the checkout's list.

checkout.removeSavedPaymentMethod() deletes one of checkout.savedPaymentMethods() from the customer's saved methods, for example when they tap Remove next to it in your list. It works in any session that lists the method, at checkout as well as in a setup session. Pay.com checks that the method belongs to the session's customer. The drop-in offers it with allowsRemovingSavedPaymentMethods.

The SDK doesn't ask the customer to confirm. Ask them before you call it. It's a suspend function, so call it from a coroutine.

checkout.removeSavedPaymentMethod(method)

Parameters

ParameterTypeRequiredDescription
methodPaySavedPaymentMethodYesThe method to delete, from this checkout's savedPaymentMethods().

Returns

Nothing. When it returns, Pay.com has deleted the method, and savedPaymentMethods() no longer lists it.

Errors

removeSavedPaymentMethod throws a PayError when the method wasn't deleted. The method then stays in savedPaymentMethods().

ErrorWhen
PayError.Unsupported(detail)The method isn't one of this checkout's savedPaymentMethods(). Nothing was sent.
PayError.PaymentInProgressA payment or another removal is running on this checkout. Nothing was sent.
PayError.SessionExpiredThe session expired, or Pay.com stopped accepting its client_secret.
PayError.RequestRejected(error)Pay.com refused to delete it, for example for a method it no longer has.
PayError.Server(error)Pay.com failed or timed out.
PayError.Network(detail)The request didn't reach Pay.com, or the response didn't arrive.

Example

class SavedMethodsViewModel(private val checkout: PayCheckout) : ViewModel() {
    private val _saved = MutableStateFlow(checkout.savedPaymentMethods())
    val saved: StateFlow<List<PaySavedPaymentMethod>> = _saved.asStateFlow()

    private val _message = MutableStateFlow<String?>(null)
    val message: StateFlow<String?> = _message.asStateFlow()

    // Call it after the customer confirms, for example in your own "Remove this card?" dialog.
    fun remove(method: PaySavedPaymentMethod) {
        viewModelScope.launch {
            try {
                checkout.removeSavedPaymentMethod(method)
                _saved.value = checkout.savedPaymentMethods() // The list without it
            } catch (e: PayError) {
                _message.value = "We couldn't remove it. Please try again."
            }
        }
    }
}

Next steps

Was this page helpful?