PrimerCheckout( clientToken: clientToken, scope: { checkoutScope in Task { for await state in checkoutScope.state { switch state { case .initializing: print("Loading checkout...") case .ready(let totalAmount, let currencyCode): print("Ready: \(totalAmount) \(currencyCode)") case .success(let result): print("Success: \(result.payment?.id ?? "")") case .failure(let error): print("Failed: \(error.errorId)") case .dismissed: print("Dismissed") } } } })
The onCompletion callback fires when the checkout reaches a terminal state (success, failure, or dismissed):
Copy
Ask AI
PrimerCheckout( clientToken: clientToken, onCompletion: { state in switch state { case .success(let result): navigateToConfirmation(result: result) case .failure(let error): logError(error) case .dismissed: navigateBack() default: break } })
onCompletion receives the same PrimerCheckoutState enum as the AsyncStream. Use onCompletion when you only need to react to the final result, and AsyncStream when you need to track intermediate states.
Task { let cardFormScope: PrimerCardFormScope = checkoutScope.getPaymentMethodScope(PrimerCardFormScope.self)! for await state in cardFormScope.state { print("Valid: \(state.isValid)") print("Loading: \(state.isLoading)") print("Fields: \(state.displayFields)") if let network = state.selectedNetwork { print("Network: \(network)") } }}
Task { if let applePayScope: PrimerApplePayScope = checkoutScope.getPaymentMethodScope(for: .applePay) { for await state in applePayScope.state { print("Available: \(state.isAvailable)") print("Loading: \(state.isLoading)") } }}
Task { if let klarnaScope: PrimerKlarnaScope = checkoutScope.getPaymentMethodScope(PrimerKlarnaScope.self) { for await state in klarnaScope.state { switch state.step { case .loading: print("Loading Klarna...") case .categorySelection: print("Categories: \(state.categories)") case .viewReady: print("Klarna view ready") case .authorizationStarted: print("Authorizing...") case .awaitingFinalization: print("Awaiting finalization") } } }}
Task { if let achScope: PrimerAchScope = checkoutScope.getPaymentMethodScope(PrimerAchScope.self) { for await state in achScope.state { switch state.step { case .loading: print("Loading...") case .userDetailsCollection: print("Collecting user details") case .bankAccountCollection: print("Collecting bank account") case .mandateAcceptance: print("Mandate: \(state.mandateText ?? "")") case .processing: print("Processing...") } } }}
Web redirect follows a linear flow through redirect and polling:
Copy
Ask AI
Task { if let webRedirectScope: PrimerWebRedirectScope = checkoutScope.getPaymentMethodScope(PrimerWebRedirectScope.self) { for await state in webRedirectScope.state { switch state.status { case .idle: print("Ready to pay") case .loading: print("Preparing payment...") case .redirecting: print("Opening external page...") case .polling: print("Waiting for confirmation...") case .success: print("Payment completed") case .failure(let message): print("Failed: \(message)") } } }}
Form redirect collects user input before completing in an external app:
Copy
Ask AI
Task { if let formScope: PrimerFormRedirectScope = checkoutScope.getPaymentMethodScope(PrimerFormRedirectScope.self) { for await state in formScope.state { switch state.status { case .ready: print("Fields: \(state.fields.count)") print("Can submit: \(state.isSubmitEnabled)") case .submitting: print("Submitting...") case .awaitingExternalCompletion: print("Pending: \(state.pendingMessage ?? "")") case .success: print("Payment completed") case .failure(let message): print("Failed: \(message)") } } }}
QR code auto-initiates payment and polls after displaying the code:
Copy
Ask AI
Task { if let qrScope: PrimerQRCodeScope = checkoutScope.getPaymentMethodScope(PrimerQRCodeScope.self) { for await state in qrScope.state { switch state.status { case .loading: print("Generating QR code...") case .displaying: if let imageData = state.qrCodeImageData { print("QR code ready (\(imageData.count) bytes)") } case .success: print("Payment completed") case .failure(let message): print("Failed: \(message)") } } }}