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, such as a card, a PayPal account, or a bank account. savedPaymentMethods() lists the saved methods of the customer the session was created for (its customer_reference_id), and pay charges the one they pick. checkout.requiresCVC(for:) tells you when to ask for a card's CVV.
let method = checkout.savedPaymentMethods()[0]
let cvcField = PayCVCField()
cvcField.brand = method.card?.cardBrand ?? .unknown
// Show cvcField when checkout.requiresCVC(for: method) is true.
let result = try await checkout.pay(.savedPaymentMethod(method, cvc: cvcField), presenter: self)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(for:), and 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, as PaySavedPaymentMethod values, 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.
A method's type indicates the payment method type, for example card or paypal. 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. 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
requiresCVC(for:) is true when the session checks CVVs and Pay.com doesn't hold one for the card. Then show a PayCVCField for the customer to type it into. Set the field's brand to the card's cardBrand, so it expects the right length (4 digits for American Express, 3 for other brands).
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
method | PaySavedPaymentMethod | Yes | One of this checkout's savedPaymentMethods(). |
Returns
A Bool. It's always false in a setup session, and for a saved method that isn't a card.
Pay
Call pay(.savedPaymentMethod(method, cvc: cvcField), presenter:). It returns a PayResult.
- A card. The SDK sends the saved card to Pay.com's vault, with the CVV when there is one. The payment then runs as it does for a card typed into the card fields, including 3DS, any challenge on
presenter, and the amount check.- When
requiresCVC(for:)isfalse,cvcis 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
clientSideEncryptionon, the SDK encrypts it on the device first. Your app never reads it.
- When
- Another saved method, such as a PayPal account or a bank account. Pay.com charges it the same way as in Pay.com.js. It may open the provider's page on
presenter, as an alternative payment method does, and the result can be.processing. Anycvcyou pass is ignored.
Errors
pay throws only when a call fails before sending a request:
| Error | When |
|---|---|
invalidCardInput([.cvc]) | For a card, requiresCVC(for:) is true and no field was passed, or the CVV in the field isn't valid for the card's brand. |
unsupported | The method isn't one of this checkout's savedPaymentMethods(). |
paymentInProgress | Another payment, or the removal of a saved method, is still running on this checkout. |
Declines and the other failures come back as .failed, as for a new card.
Example
import PayCheckout
import PayCore
import PayElements
import UIKit
final class SavedMethodViewController: UIViewController {
var checkout: PayCheckout!
let cvcField = PayCVCField()
func select(_ method: PaySavedPaymentMethod) {
cvcField.clear()
cvcField.brand = method.card?.cardBrand ?? .unknown
cvcField.isHidden = !checkout.requiresCVC(for: method)
}
func pay(with method: PaySavedPaymentMethod) async {
do throws(PayError) {
switch try await checkout.pay(.savedPaymentMethod(method, cvc: cvcField), presenter: self) {
case .succeeded:
await confirmOnServer() // Your server retrieves the session before it fulfills
case .failed(let error):
showMessage(error.localizedDescription)
case .processing:
showPending() // Your server learns the outcome from the webhook
case .canceled:
break
}
} catch {
showMessage(error.localizedDescription) // For example, the CVV is missing
}
}
}Next steps
- Pay: The other ways to pay, and how card payments and redirects run.
- Card fields: The CVV field's look and validation.
- Remove a saved payment method: Let the customer delete one.