Pay
Pay with the card fields, an alternative payment method, a saved payment method, or an authorized Apple Pay payment, and get the result.
checkout.pay(_:presenter:) submits a payment for the session using the card typed into the card fields, an alternative payment method, a saved payment method, or an Apple Pay payment you authorized yourself. The SDK runs 3DS and presents any challenge or redirect on the view controller you pass, then returns the client-side result. To show the Apple Pay sheet as well, use payWithApplePay().
In a setup session, pay saves the payment method instead of charging it.
let result = try await checkout.pay(.card(cardFields), presenter: self)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | PayPaymentRequest | Yes | What to pay with, using one of the cases below. |
presenter | UIViewController | Yes | The view controller that presents the 3DS challenge and redirects. It must be on screen when they open. |
PayPaymentRequest has these cases:
| Case | Description |
|---|---|
.card(PayCardFields, saveForFutureUse: Bool = false, termsAccepted: Bool = false) | The card typed into the SDK's card fields. saveForFutureUse asks to save the card for the customer. When the session's setup_future_usage is on_session, the card is saved anyway. termsAccepted indicates that the customer accepted your terms and conditions on your screen, and Pay.com records that on the payment and the customer. Leave it false unless you asked them. |
.alternative(String, data: [String: String] = [:]) | An alternative payment method, by its type from paymentMethods(). It completes through a redirect or an app switch. data carries the fields the method asks for. |
.savedPaymentMethod(PaySavedPaymentMethod, cvc: PayCVCField? = nil) | A method from savedPaymentMethods(). In a sale or authorization session, the saved method is charged. For a card, pass a PayCVCField with its CVV when requiresCVC(for:) is true. A saved PayPal account or bank account may open the provider's page, as .alternative does. In a setup session, the method becomes the customer's default payment method. |
.applePay(PKPayment, termsAccepted: Bool = false) | A payment your own PKPaymentAuthorizationController authorized, for the request from applePayPaymentRequest(). termsAccepted works as for .card. The Apple Pay sheet doesn't show your terms. |
The drop-in shows no terms and conditions, so it never says the customer accepted them.
Returns
pay returns a PayResult once the payment reaches an outcome the device can see: .succeeded, .processing, .canceled, or .failed. Everything that happens after a request leaves the device comes back as a result, never as a thrown error. For example, declines are .failed(.declined), and a changed total is .failed(.amountChanged).
Errors
pay throws only when a call fails before sending a request:
| Error | When |
|---|---|
invalidCardInput([PayCardFieldKind]) | For .card, the card fields are incomplete or invalid. For .savedPaymentMethod, the saved card requires a CVV and none was passed, or the entered CVV isn't valid. The error lists the invalid fields. |
paymentInProgress | Another payment, or the removal of a saved method, is still running on this checkout. |
unsupported(String) | For .savedPaymentMethod, the method isn't one of this checkout's savedPaymentMethods(). |
How a card payment runs
A card payment, new or saved, runs in these steps.
- The SDK sends the card to Pay.com straight from the card fields. For a saved card, it sends the card's token to Pay.com's vault, with the CVV when there is one. With
clientSideEncryptionon, it sends the card or the CVV encrypted instead. - When the session asks for it, the SDK runs the 3DS Method step in an invisible web view, for up to 10 seconds.
- In any session but a setup session, the SDK checks that the total hasn't changed, then submits the payment. In a setup session, it submits the setup.
- If the issuer asks for a challenge, the challenge opens full screen in a web view on
presenter, with a Cancel button. While it's open, the SDK checks the payment status. The screen closes when the status is final, or afterchallengeTimeout. - The SDK reads the payment status with backoff, for up to 30 seconds after a challenge. If no final status arrives by then, the result is
.processing, never a failure.
When the amount changes
In every session but a setup session, including one whose flow the SDK doesn't recognize, the SDK reads the amount and currency again right before it charges, and compares them with the values shown to the customer in checkout.session (or the request's total for Apple Pay).
- If either changed, nothing is charged.
payreturns.failed(.amountChanged), andcheckout.sessionhas the new amount and currency. Show them, then let the customer pay again. - If they can't be read, nothing is charged either.
payreturns.failedwith that error.
When your server updates the session's amount, call refreshSession() and show checkout.session.amount before the customer pays.
How a redirect runs
An alternative payment method opens its page in an ephemeral ASWebAuthenticationSession, which shares no cookies with Safari and doesn't ask the customer to allow a sign-in. It finishes when the page reaches the checkout's returnURL, if that's a custom scheme, or when your app passes the return URL to PayCheckout.handleReturn(), for example after a bank app switches back. The SDK then reads the payment status for up to 60 seconds. A method the customer confirms somewhere else, with no redirect, gets the same 60 seconds. A saved PayPal account or bank account that sends the customer to its provider runs the same way.
When the customer cancels
If the customer closes the challenge or redirect screen, the SDK keeps checking the status for 5 seconds, so a cancel can still end as .succeeded, .failed, or .processing. With no final status by then, the result is .canceled.
If presenter isn't on screen when a challenge arrives, pay also checks the status for 5 seconds, then returns .failed(.network("the presenting view controller is not on screen")).
Example
import PayCheckout
import PayCore
import PayElements
import UIKit
extension CardViewController {
func payTapped() async {
do throws(PayError) {
let result = try await checkout.pay(
.card(cardFields, saveForFutureUse: saveCardSwitch.isOn),
presenter: self
)
switch result {
case .succeeded(let ids):
showReceipt(ids) // A hint: your server confirms before fulfilling
case .processing(let ids):
showPending(ids) // Your server decides from the webhook or the session
case .canceled:
break // The customer closed the 3DS or redirect screen
case .failed(.declined(let decline)) where decline.maxAttemptsReached:
showMessage("This payment can't be retried.") // Your server needs a new session
case .failed(let error):
showMessage(error.localizedDescription)
}
} catch {
// Nothing was sent.
switch error {
case .invalidCardInput(let fields): highlight(fields)
default: showMessage(error.localizedDescription)
}
}
}
}To try it in the sandbox, use the test cards, for example 4242 4242 4242 4242 with a future expiration date and a CVV such as 123.
Next steps
- Handle the return URL: Finish redirects that come back to your app.
- Refresh the session: Show a new total before the customer pays.
- PayCore:
PayResult,PaymentIDs, andPayError. - Pay with a saved payment method: Charge one of the customer's saved cards, PayPal accounts, or bank accounts.
- Let customers choose a payout method: Pay with a saved method in a setup session.
- Accept payments in your iOS app: The whole flow, from your server to the app.