Treat StoreKit Purchase Cancellation As A Normal Outcome
Someone taps Subscribe, reads the system sheet, and presses Cancel. Showing “Purchase failed” with a retry alert turns a normal decision into an apparent problem.
StoreKit already distinguishes that outcome. Preserve the distinction in your app’s state instead of reducing every non-success path to one error Boolean.
Let the purchase result drive the immediate UI
Apple’s Product.PurchaseResult has separate success, pending, and user-cancelled cases. This coordinator models the immediate purchase interaction:
import Foundation
import StoreKit
enum PurchasePhase: Equatable { case idle, purchasing, pending, complete, failed }
enum PurchaseProblem: Error { case unverified }
@MainActor
final class PurchaseCoordinator {
private(set) var phase: PurchasePhase = .idle
private var isPurchasing = false
func dismissPendingNotice() {
guard !isPurchasing, phase == .pending else { return }
phase = .idle
}
func buy(
using purchase: () async throws -> Product.PurchaseResult,
deliver: (Transaction) async throws -> Void
) async {
guard !isPurchasing && phase != .pending else { return }
isPurchasing = true
defer { isPurchasing = false }
phase = .purchasing
do {
switch try await purchase() {
case .userCancelled: phase = .idle
case .pending: phase = .pending
case .success(let result):
guard case .verified(let transaction) = result else {
throw PurchaseProblem.unverified
}
try await deliver(transaction)
await transaction.finish()
phase = .complete
@unknown default: phase = .failed
}
} catch is CancellationError {
phase = .idle
} catch {
phase = .failed
}
}
}
For a real product, the purchase closure calls try await product.purchase(). The delivery closure belongs to the app’s shared, idempotent transaction processor. Verify the expected product and apply the product-specific delivery rules there.
The separate isPurchasing guard prevents another request from starting while the first async call is active. In a SwiftUI application, expose phase through your existing observable store or view state. This class concentrates on behavior rather than prescribing a second entitlement architecture.
Cancellation should leave existing access alone
.userCancelled returns to idle. It does not remove a subscription the person already owns, log an application failure, or automatically present the purchase sheet again.
A thrown CancellationError is a task-control outcome, not proof that the person pressed Cancel in StoreKit’s sheet. This example also returns the immediate UI to idle, but keeps authoritative access in the entitlement store. A transaction may still need processing through the app’s ongoing listener.
Other errors produce a failed state that can offer a deliberate retry. Use an appropriate user-facing message and a bounded diagnostic category; dumping raw error descriptions is not a purchase UX strategy.
Pending is neither success nor failure
A pending purchase requires further action, such as approval. Stop the spinner and explain that the request is waiting. Do not unlock content merely because the purchase request was accepted for processing.
The coordinator blocks repeated taps while that pending notice remains active. Dismissing the notice returns its local UI to idle; it does not cancel the pending App Store request.
Keep transaction observation alive outside the paywall’s lifetime. When approval arrives, the shared transaction processor must verify and deliver it even if the sheet has been dismissed. The paywall should observe the resulting entitlement change rather than relying on its old button task to resume.
Our entitlements and updates article covers the two StoreKit sequences. Reuse that service rather than starting another listener for every purchase button.
Finish after delivery succeeds
The verified branch awaits delivery before calling finish(). If delivery throws, the code does not mark the transaction finished merely to clear the UI. The transaction processor needs a retry path and must tolerate seeing the same transaction again.
This distinction matters especially for consumables, where delivering twice can duplicate a balance. A non-consumable unlock and a consumable inventory update need different idempotency rules.
Unverified results take a verification-error path. Do not treat the associated unverified transaction as trusted just because the purchase sheet closed successfully.
Test each outcome independently
The fixture injects .userCancelled, .pending, a networking error, and task cancellation. It checks that none deliver content, that pending blocks an immediate duplicate request, and that dismissing the notice restores the local idle state.
The success and verification branches require StoreKit testing with configured products and signed transaction results. A fabricated Boolean called purchaseSucceeded does not test those branches.
September 2026 review: synthetic outcome tests and the iOS 17 typecheck passed. No real purchase was invoked, and verified transaction delivery was not exercised against the StoreKit test environment in this fixture.