Skip to main content
Primer Checkout iOS SDK is currently in beta (v3.0.0-beta.8). The API is subject to change before the stable release.
PrimerCheckoutSession is the owner of a CheckoutComponents session. You hold it (typically as a @StateObject) and wire it into your SwiftUI view hierarchy with the .primerCheckoutSession(_:theme:onCompletion:) modifier. The session builds the SDK, runs client-token initialization, creates the checkout scope, and bridges per-method scopes into observable session objects that the composable views (such as PrimerCardForm and PrimerPaymentMethods) consume from the environment. Any Primer composable view placed under the modifier resolves its session from the environment. The managed modal PrimerCheckout does not use a session: it builds its own checkout and needs neither PrimerCheckoutSession nor the modifier. While the session is .ready, the SDK presents its own follow-up screens (processing, success and failure, plus saved-method management and CVV recapture) in a sheet over your layout. See What the modifier provides.
This is the iOS analog of the Android PrimerCheckoutHost. Where Android wraps your layout in a composable host, iOS wires the session into the environment with a view modifier — child composable views read their per-feature session from EnvironmentValues.

Declaration

Initializer

Phase

The lifecycle phase of the session. Terminal outcomes (success, failure, dismissed) are not delivered here — they are delivered through the modifier’s onCompletion.

Published properties

This is how you read the order the customer is paying for:

Properties

onBeforePaymentCreate uses the BeforePaymentCreateHandler type:
If you set onBeforePaymentCreate, the key you pass to .continuePaymentCreation(withIdempotencyKey:) wins. When your decision continues without a key, the SDK falls back to the idempotencyKey provider.
The SDK calls onBeforePaymentCreate before every payment, including payments with a saved payment method. It waits until you call decisionHandler, and there is no timeout. If you have not answered after 5 seconds, the SDK logs a warning. While it waits, the SDK ignores Back, Cancel and “Choose other payment methods” on its own screens. Your own layout stays interactive, so disable your pay button and payment-method list until you call decisionHandler. If the checkout is closed before you answer, a later decisionHandler call starts no payment.
The two shipping handlers are for Express Checkout. They run while the Apple Pay sheet is open and must return within 20 seconds:
See Express Checkout shipping for the full flow.

Methods

formatAmount(_:)

Formats an amount in minor units for display, so a total you print yourself matches what the SDK prints on its own screens. The currency comes from the client session. Its decimal digits come from the backend and cannot be worked out from PrimerClientSession.currencyCode alone, because some currencies have none and some have three. The locale is the one in PrimerSettings.localeData, which defaults to the device.
Returns nil before the session reaches .ready.
You normally never call start() or cancel() yourself — the .primerCheckoutSession modifier handles the lifecycle. Call refresh() when you need to reload after a client-session update (for example, an amount or currency change).

View modifier

The View.primerCheckoutSession(_:theme:onCompletion:) modifier wires a PrimerCheckoutSession into the SwiftUI environment, bootstraps it on appear, and tears it down on disappear. Apply it once around the Primer composable views you embed inline in your own layout. The managed modal PrimerCheckout does not use it.

What the modifier provides

  1. Environment injection — exposes the PrimerCheckoutSession, the derived PrimerCardFormSession, and the PrimerSelectionSession through EnvironmentValues, so child views like PrimerCardForm and PrimerPaymentMethods resolve their per-feature session automatically.
  2. Lifecycle bootstrapping — calls start() on appear and cancel() on disappear.
  3. Overlay management — once the session is .ready, the modifier presents the SDK’s own follow-up screens in a sheet over your layout: processing, success and failure after any payment you start, the screens of payment methods that need one, the saved-methods list opened by showAll() with its delete confirmation, and the CVV recapture screen that selectVaulted(_:) raises when a saved card needs it. 3DS challenges and web redirects open their own system UI. Do not present a sheet of your own while one of these is up. The customer cannot swipe the SDK’s sheet away while the processing screen is showing or while your onBeforePaymentCreate handler has not yet called decisionHandler. At any other time, swiping the sheet away closes it and returns to your layout without ending the session.
Without the .primerCheckoutSession modifier above them in the hierarchy, composable views such as PrimerCardForm and PrimerPaymentMethods cannot resolve a session and will not function.

Usage

Hold the session as a @StateObject, place the composable views in a ScrollView, and attach the modifier with your completion handler.
The child composable views resolve their per-feature session from the environment — you do not pass a session into them. Behind the scenes the modifier injects PrimerCardFormSession and PrimerSelectionSession, which PrimerCardForm and PrimerPaymentMethods bind to once the session reaches .ready.

Supplying an idempotency key

Provide the declarative idempotencyKey provider at construction, or assign onBeforePaymentCreate for full control over the payment-creation decision. Either may be assigned after .ready — the change applies to the next payment attempt.

See also

PrimerCheckout

The managed modal entry point. It builds its own checkout and does not use a session.

PrimerCardForm

Composable card form that resolves its session from the environment.

PrimerPaymentMethods

Composable payment-method selection list for inline layouts.

PrimerCheckoutState

The terminal outcome delivered to onCompletion.