Start a checkout
Check a session's client_secret, load the session, and get the checkout that lists its payment methods and pays.
PayCheckout(clientSecret:configuration:) starts a checkout for one session. It checks the client_secret, loads the session from Pay.com, and returns the checkout you list payment methods and pay with. The drop-in takes the same checkout. Start one when the customer reaches your payment screen.
Your server creates the session with Create a payment session, or Create a setup session to save a payment method, and gives the app only the client_secret. Never put an API key in the app. The SDK never logs the secret.
PayCheckout runs on the main actor.
let checkout = try await PayCheckout(clientSecret: clientSecret, configuration: .init(returnURL: returnURL))Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
clientSecret | String | Yes | The session's client_secret, from your server. Leading and trailing whitespace is trimmed. |
configuration | PayCheckout.Configuration | Yes | How the checkout runs. Create it with .init(returnURL:) and any of the options below. |
configuration.returnURL | URL | Yes | The URL where redirects return to your app (a custom scheme such as myshop://pay or a universal link). Your server must create the session with return_url set to the same URL, and your app must pass incoming URLs to PayCheckout.handleReturn(). |
configuration.environment | PayEnvironment | No | The Pay.com environment the SDK connects to. Keep the default, .production, for both live and sandbox sessions. See PayEnvironment. |
configuration.challengeTimeout | TimeInterval | No | How long, in seconds, the 3DS challenge screen may stay open before the SDK stops waiting and reads the outcome from the payment status instead. Default: 600. |
configuration.threeDSAppReturnMode | Bool | No | Asks the 3DS challenge page to end with a redirect to returnURL. Default: false. |
configuration.clientSideEncryption | Bool | No | Encrypts the card on the device with PayCSE and sends the encrypted card instead of the card fields. Default: false. Leave it off until Pay.com turns on client-side encryption for your account. With it on, the checkout fetches the encryption keys while it loads the session, and the SDK never sends the raw card. If the card can't be encrypted, pay returns .failed(.encryptionUnavailable). See Client-side encryption on mobile. |
The configuration's properties are all mutable (var) properties, so you can also set them after you create it.
Returns
A PayCheckout for the session, with these properties:
| Property | Type | Description |
|---|---|---|
session | PaySession | The loaded session, including its kind, amount, currency, expiration time, and whether it's a payout session. After your server changes the amount, refreshSession() updates it. |
configuration | PayCheckout.Configuration | The configuration you passed. |
A checkout serves one session. When the client_secret expires (session.expiresAt), Pay.com refuses the checkout's requests with sessionExpired. Get a new client_secret from your server and start a new checkout. PayCheckout has no method to release it. When you're done, drop your reference to it.
Errors
PayCheckout(clientSecret:configuration:) throws a PayError when it can't start the checkout.
| Error | When |
|---|---|
invalidClientSecret | The client_secret is empty after trimming, longer than 8192 bytes, or contains a character outside printable ASCII, spaces included. Its format isn't otherwise checked. Nothing was sent. |
sessionExpired | Pay.com returned HTTP 401. The session expired, or Pay.com rejected the client_secret. |
requestRejected | Pay.com refused the request with another status below 500. |
server | Pay.com failed with a 5xx or returned a 408. |
network | The request didn't reach Pay.com, or the response didn't arrive. |
unexpectedResponse | Pay.com returned a response this SDK version can't read. |
Example
import PayCheckout
import PayCore
import UIKit
final class PaymentViewController: UIViewController {
private var checkout: PayCheckout?
// clientSecret comes from your server, which created the session.
func start(clientSecret: String) async {
do throws(PayError) {
let checkout = try await PayCheckout(
clientSecret: clientSecret,
configuration: .init(returnURL: URL(string: "myshop://pay")!)
)
self.checkout = checkout
showPaymentMethods(checkout.paymentMethods())
} catch {
switch error {
case .sessionExpired:
await restartWithNewSession() // Ask your server for a new client_secret
default:
showMessage(error.localizedDescription)
}
}
}
}Next steps
- List payment methods: Draw your own list of the session's methods.
- Present the drop-in: Show a ready-made payment sheet over the checkout.
- Pay: Pay with the card fields, a redirect method, or a saved method.
- Accept payments in your iOS app: Take a first card payment, from your server to the app.