Skip to Content
Docs

Let customers choose a payout method

In a payout setup session, list the customer's saved payout methods, let them keep one or add a new one, and pay out to their choice from your server.

In a payout setup session, the customer chooses where their payouts go, using either a saved payout method or a new one. checkout.savedPaymentMethods() lists the saved ones, and pay records the choice. Your server then reads the choice and creates the payout. The drop-in shows this choice for you. For the same flow on the web, see Let recipients choose a payout method.

Your server creates the session with Create a setup session, passing the customer's customer_reference_id and payout: true, and gives the app its client_secret. In the checkout you start with it:

  • session.isPayout is true, and session.kind is .setup.
  • paymentMethods() lists the payout methods the customer can add.
  • savedPaymentMethods() lists their saved methods that payouts can go to, for example cards that can receive payouts, bank accounts, and PayPal.
let saved = checkout.savedPaymentMethods()
let result = try await checkout.pay(.savedPaymentMethod(saved[0]), presenter: self)

Returns

savedPaymentMethods() returns an array of PaySavedPaymentMethod, in the order the API sends them. Entries without an ID or a token, and repeated IDs, are left out. What it holds depends on the session:

SessionsavedPaymentMethods()
A payout setup sessionThe customer's saved methods that payouts can go to.
Another setup sessionThe customer's saved payment methods.
A sale or authorization sessionThe customer's saved cards that haven't expired, and their other saved methods the session can pay, such as PayPal accounts and bank accounts. See Pay with a saved payment method.

It reads the session the checkout loaded, so it doesn't throw. A method you remove leaves the list.

Record the choice

How you record the choice depends on whether the customer keeps a saved method or adds a new one.

  • A saved method. Call pay(.savedPaymentMethod(method), presenter:). The method becomes the customer's default payment method.
  • A new method. Pay with the card fields (.card) or a redirect method (.alternative). The SDK saves it, and it completes the setup session.

When the choice is recorded, pay returns .succeeded, with the method's ID in paymentMethod when it's known. Use that ID for your UI only.

Errors

pay throws only when a call fails before sending a request, and returns other failures as .failed.

ErrorWhen
unsupportedThrown by pay(.savedPaymentMethod(_:)) when the method isn't one of this checkout's savedPaymentMethods(). Nothing was sent.
requestRejected, network, and the other request errorsReturned as .failed when Pay.com refuses the choice or can't be reached.

Confirm the choice and pay out

Confirm the choice on your server, then pay out to it:

The customer choseYour server reads
A new methodThe setup session's payment_method, with Retrieve a setup session or from the setup_session.completed webhook.
A saved methodThe customer's default_source, with Retrieve a customer.

Then create the payout with that payment method as its destination.

PayResult is a hint for your UI. Pay out only to the method your server reads from Pay.com, never to an ID the app sends.

In a setup session without payout, the same calls list the customer's saved payment methods and make the one they choose their default.

Example

import PayCheckout
import PayCore
import UIKit

extension PayoutMethodViewController {
    func title(of method: PaySavedPaymentMethod) -> String {
        if let card = method.card {
            return "\(card.brand ?? "Card") •••• \(card.last4 ?? "")"
        }
        if let bank = method.bankAccount {
            return "\(bank.bankName ?? "Bank account") •••• \(bank.last4 ?? "")"
        }
        return method.email ?? method.type
    }

    func use(_ method: PaySavedPaymentMethod) async {
        do throws(PayError) {
            switch try await checkout.pay(.savedPaymentMethod(method), presenter: self) {
            case .succeeded:
                await tellServerChoiceMade()   // Your server reads default_source, then pays out
            case .failed(let error):
                showMessage(error.localizedDescription)
            case .processing, .canceled:
                break
            }
        } catch {
            showMessage(error.localizedDescription)   // Not a saved method of this session
        }
    }
}

Next steps

Was this page helpful?