Skip to Content
Docs

Let customers choose a payout method

List the saved methods a setup session can use, such as the payout methods of the customer being paid, and make the one they pick their default.

In a payout setup session, the customer being paid picks either a previously saved method or a new one to receive payouts. checkout.savedPaymentMethods() lists their saved payout methods. When they pick one, pay with PayPaymentRequest.SavedPaymentMethod through checkout.pay(). The method becomes their default, and your server pays out to it with Create a payout.

Your server creates the session with Create a setup session and payout: true, as in Let recipients choose a payout method. In the app, checkout.session.isPayout is true for it. Other setup sessions work the same way, with the customer's saved payment methods. The customer can also remove a saved method, and the drop-in shows all of this for you. To charge a saved method in a sale or authorization session instead, see Pay with a saved payment method.

val saved: List<PaySavedPaymentMethod> = checkout.savedPaymentMethods()
val result = checkout.pay(PayPaymentRequest.SavedPaymentMethod(saved.first()), activity)

Parameters

savedPaymentMethods() takes no parameters. PayPaymentRequest.SavedPaymentMethod takes the following parameters:

ParameterTypeRequiredDescription
methodPaySavedPaymentMethodYesThe method the customer picked, from this checkout's savedPaymentMethods().
cvcCvcSource?NoA saved card's CVV, when you charge the card in a sale or authorization session. Not used in a setup session. Default: null.

Returns

savedPaymentMethods() returns the customer's saved methods this session can use, as a list of PaySavedPaymentMethod, in the API's order. In a payout session, they're the methods the customer can be paid to. In another setup session, they're the customer's saved payment methods. In a sale or authorization session, they're the customer's saved cards that haven't expired and their other saved methods the session can pay. See Pay with a saved payment method. In other sessions, the list is empty. It reads what PayCheckout.create() loaded, so it doesn't call Pay.com.

Each method has its type, whether it's the customer's default (isDefault), and, depending on its type, the card, bankAccount, or email details to show in your list.

In a setup session, paying with a saved method makes it the customer's default payment method, and returns PayResult.Succeeded with the session's ID in ids.sessionId and the method's id in ids.paymentMethod. Nothing else is set.

The result is a hint for your UI. Before you pay out, your server confirms the customer's choice instead of trusting an ID from the app. For a saved method, it retrieves the customer with Retrieve a customer and uses its default_source. For a new method, it retrieves the session with Retrieve a setup session, or waits for the setup_session.completed webhook, and uses its payment_method. Then it creates the payout with that method as the destination.

Errors

pay throws a PayError for a SavedPaymentMethod request when it sent nothing:

ErrorWhen
PayError.Unsupported(detail)The method isn't one of this checkout's savedPaymentMethods().
PayError.PaymentInProgressAnother payment, or a removal, is running on this checkout.

Other failures come back as PayResult.Failed(error):

ErrorWhen
PayError.SessionExpiredThe session expired, or Pay.com stopped accepting its client_secret.
PayError.RequestRejected(error)Pay.com refused the change, 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.

Add a new method

To let the customer add a payout method instead, pay as you would in a payment session, using the card fields or a method from checkout.paymentMethods(). Paying with a new method saves it and completes the setup session.

Example

@Composable
fun PayoutMethodPicker(checkout: PayCheckout, onUse: (PaySavedPaymentMethod) -> Unit) {
    val saved = remember(checkout) { checkout.savedPaymentMethods() }
    var selected by remember { mutableStateOf(saved.firstOrNull { it.isDefault } ?: saved.firstOrNull()) }

    Column(Modifier.selectableGroup()) {
        saved.forEach { method ->
            Row(
                Modifier
                    .fillMaxWidth()
                    .selectable(selected = method == selected, onClick = { selected = method }, role = Role.RadioButton)
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                RadioButton(selected = method == selected, onClick = null)
                Text(method.label(), Modifier.padding(start = 16.dp))
            }
        }
        Button(onClick = { selected?.let(onUse) }, enabled = selected != null) {
            Text("Use payment method")
        }
    }
}

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

In the activity, pay with the method the customer picked, then tell your server:

private fun use(method: PaySavedPaymentMethod) {
    lifecycleScope.launch {
        val result = try {
            checkout.pay(PayPaymentRequest.SavedPaymentMethod(method), this@PayoutActivity)
        } catch (e: PayError) {
            return@launch // A payment or a removal is already running
        }
        when (result) {
            is PayResult.Succeeded -> shopApi.payoutMethodChosen() // Your server reads the customer's default_source
            is PayResult.Failed -> showMessage("We couldn't use this method. Please try again.")
            else -> Unit
        }
    }
}

Next steps

Was this page helpful?