| name | axiom-in-app-purchases |
| description | Use when implementing in-app purchases, StoreKit 2, subscriptions, or transaction handling - testing-first workflow with .storekit configuration, StoreManager architecture, transaction verification, subscription management, and restore purchases for consumables, non-consumables, and auto-renewable subscriptions |
| license | MIT |
| metadata | {"version":"1.0"} |
StoreKit 2 In-App Purchase Implementation
Purpose: Guide robust, testable in-app purchase implementation
StoreKit Version: StoreKit 2
iOS Version: iOS 15+ (iOS 18.4+ for latest features)
Xcode: Xcode 13+ (Xcode 16+ recommended)
Context: WWDC 2025-241, 2025-249, 2023-10013, 2021-10114
When to Use This Skill
✅ Use this skill when:
- Implementing any in-app purchase functionality (new or existing)
- Adding consumable products (coins, hints, boosts)
- Adding non-consumable products (premium features, level packs)
- Adding auto-renewable subscriptions (monthly/annual plans)
- Debugging purchase failures, missing transactions, or restore issues
- Setting up StoreKit testing configuration
- Implementing subscription status tracking
- Adding promotional offers or introductory offers
- Server-side receipt validation
- Family Sharing support
❌ Do NOT use this skill for:
- StoreKit 1 (legacy API) - this skill focuses on StoreKit 2
- App Store Connect product configuration (separate documentation)
- Pricing strategy or business model decisions
⚠️ Already Wrote Code Before Creating .storekit Config?
If you wrote purchase code before creating .storekit configuration, you have three options:
Option A: Delete and Start Over (Strongly Recommended)
Delete all IAP code and follow the testing-first workflow below. This reinforces correct habits and ensures you experience the full benefit of .storekit-first development.
Why this is best:
- Validates that you understand the workflow
- Catches product ID issues you might have missed
- Builds muscle memory for future IAP implementations
- Takes only 15-30 minutes for experienced developers
Option B: Create .storekit Config Now (Acceptable with Caution)
Create the .storekit file now with your existing product IDs. Test everything works locally. Document in your PR that you tested in sandbox first.
Trade-offs:
- ✅ Keeps working code
- ✅ Adds local testing capability
- ❌ Misses product ID validation benefit
- ❌ Reinforces testing-after pattern
- ❌ Requires extra vigilance in code review
If choosing this path: Create .storekit immediately, verify locally, and commit a note explaining the approach.
Option C: Skip .storekit Entirely (Not Recommended)
Commit without .storekit configuration, test only in sandbox.
Why this is problematic:
- Teammates can't test purchases locally
- No validation of product IDs before runtime
- Harder iteration (requires App Store Connect)
- Missing documentation of product structure
Bottom line: Choose Option A if possible, Option B if pragmatic, never Option C.
Core Philosophy: Testing-First Workflow
Best Practice: Create and test StoreKit configuration BEFORE writing production purchase code.
Why .storekit-First Matters
The recommended workflow is to create .storekit configuration before writing any purchase code. This isn't arbitrary - it provides concrete benefits:
Immediate product ID validation:
- Typos caught in Xcode, not at runtime
- Product configuration visible in project
- No App Store Connect dependency for testing
Faster iteration:
- Test purchases in simulator instantly
- No network requests during development
- Accelerated subscription renewal for testing
Team benefits:
- Anyone can test purchase flows locally
- Product catalog documented in code
- Code review includes purchase testing
Common objections addressed:
❓ "I already tested in sandbox" - Sandbox testing is valuable but comes later. Local testing with .storekit is faster and enables true TDD.
❓ "My code works" - Working code is great! Adding .storekit makes it easier for teammates to verify and maintain.
❓ "I've done this before" - Experience is valuable. The .storekit-first workflow makes experienced developers even more productive.
❓ "Time pressure" - Creating .storekit takes 10-15 minutes. The time saved in iteration pays back immediately.
The Recommended Workflow
StoreKit Config → Local Testing → Production Code → Unit Tests → Sandbox Testing
↓ ↓ ↓ ↓ ↓
.storekit Test purchases StoreManager Mock store Integration test
Why this order helps:
- StoreKit Config First: Defines products without App Store Connect dependency
- Local Testing: Validates product IDs and purchase flows immediately
- Production Code: Implements against validated product configuration
- Unit Tests: Verifies business logic with mocked store responses
- Sandbox Testing: Final validation in App Store environment
Benefits of following this workflow:
- Product IDs validated before writing code
- Faster development iteration
- Easier team collaboration
- Better test coverage
Mandatory Checklist
Before marking IAP implementation complete, ALL items must be verified:
Phase 1: Testing Foundation
Phase 2: Architecture
Phase 3: Purchase Flow
Phase 4: Subscription Management (if applicable)
Phase 5: Restore & Sync
Phase 6: Error Handling
Phase 7: Testing & Validation
Step 1: Create StoreKit Configuration (FIRST!)
DO THIS BEFORE WRITING ANY PURCHASE CODE.
Create Configuration File
- Xcode → File → New → File → StoreKit Configuration File
- Save as:
Products.storekit (or your app name)
- Add to target: ✅ (include in app bundle for testing)
Add Products
Click "+" and add each product type:
Consumable
Product ID: com.yourapp.coins_100
Reference Name: 100 Coins
Price: $0.99
Non-Consumable
Product ID: com.yourapp.premium
Reference Name: Premium Upgrade
Price: $4.99
Auto-Renewable Subscription
Product ID: com.yourapp.pro_monthly
Reference Name: Pro Monthly
Price: $9.99/month
Subscription Group ID: pro_tier
Test Immediately
- Run app in simulator
- Scheme → Edit Scheme → Run → Options
- StoreKit Configuration: Select
Products.storekit
- Verify: Products load, purchases complete, transactions appear
Step 2: Implement StoreManager Architecture
Required Pattern: Centralized StoreManager
All purchase logic must go through a single StoreManager. No scattered Product.purchase() calls throughout app.
import StoreKit
@MainActor
final class StoreManager: ObservableObject {
@Published private(set) var products: [Product] = []
@Published private(set) var purchasedProductIDs: Set<String> = []
private let productIDs = [
"com.yourapp.coins_100",
"com.yourapp.premium",
"com.yourapp.pro_monthly"
]
private var transactionListener: Task<Void, Never>?
init() {
transactionListener = listenForTransactions()
Task {
await loadProducts()
await updatePurchasedProducts()
}
}
deinit {
transactionListener?.cancel()
}
}
Why @MainActor: Published properties must update on main thread for UI binding.
Load Products (At Launch)
extension StoreManager {
func loadProducts() async {
do {
let loadedProducts = try await Product.products(for: productIDs)
self.products = loadedProducts
} catch {
print("Failed to load products: \(error)")
}
}
}
Call from: App.init() or first view's .task modifier
Listen for Transactions (REQUIRED)
extension StoreManager {
func listenForTransactions() -> Task<Void, Never> {
Task.detached { [weak self] in
for await verificationResult in Transaction.updates {
await self?.handleTransaction(verificationResult)
}
}
}
@MainActor
private func handleTransaction(_ result: VerificationResult<Transaction>) async {
guard let transaction = try? result.payloadValue else {
print("Transaction verification failed")
return
}
await grantEntitlement(for: transaction)
await transaction.finish()
await updatePurchasedProducts()
}
}
Why detached: Transaction listener runs independently of view lifecycle
Step 3: Implement Purchase Flow
Purchase with UI Context (iOS 18.2+)
extension StoreManager {
func purchase(_ product: Product, confirmIn scene: UIWindowScene) async throws -> Bool {
let result = try await product.purchase(confirmIn: scene)
switch result {
case .success(let verificationResult):
guard let transaction = try? verificationResult.payloadValue else {
print("Transaction verification failed")
return false
}
await grantEntitlement(for: transaction)
await transaction.finish()
await updatePurchasedProducts()
return true
case .userCancelled:
return false
case .pending:
:
}
}
}
SwiftUI Purchase (Using Environment)
struct ProductRow: View {
let product: Product
@Environment(\.purchase) private var purchase
var body: some View {
Button("Buy \(product.displayPrice)") {
Task {
do {
let result = try await purchase(product)
} catch {
print("Purchase failed: \(error)")
}
}
}
}
}
Set appAccountToken (If Using Backend)
func purchase(
_ product: Product,
confirmIn scene: UIWindowScene,
accountToken: UUID
) async throws -> Bool {
let result = try await product.purchase(
confirmIn: scene,
options: [
.appAccountToken(accountToken)
]
)
}
When to use: When your backend needs to associate purchases with user accounts
Step 4: Verify Transactions (MANDATORY)
Always Use VerificationResult
func handleTransaction(_ result: VerificationResult<Transaction>) async {
switch result {
case .verified(let transaction):
await grantEntitlement(for: transaction)
await transaction.finish()
case .unverified(let transaction, let error):
print("Unverified transaction: \(error)")
await transaction.finish()
}
}
Why verify: Prevents granting entitlements for:
- Fraudulent receipts
- Jailbroken device receipts
- Man-in-the-middle attacks
Check Transaction Fields
func grantEntitlement(for transaction: Transaction) async {
guard transaction.revocationDate == nil else {
print("Transaction was refunded")
await revokeEntitlement(for: transaction.productID)
return
}
switch transaction.productType {
case .consumable:
await addConsumable(productID: transaction.productID)
case .nonConsumable:
await unlockFeature(productID: transaction.productID)
case .autoRenewable:
await activateSubscription(productID: transaction.productID)
default:
break
}
}
Step 5: Track Current Entitlements
Check What User Owns
extension StoreManager {
func updatePurchasedProducts() async {
var purchased: Set<String> = []
for await result in Transaction.currentEntitlements {
guard let transaction = try? result.payloadValue else {
continue
}
if transaction.revocationDate == nil {
purchased.insert(transaction.productID)
}
}
self.purchasedProductIDs = purchased
}
}
Check Specific Product
func isEntitled(to productID: String) async -> Bool {
for await result in Transaction.currentEntitlements(for: productID) {
if let transaction = try? result.payloadValue,
transaction.revocationDate == nil {
return true
}
}
return false
}
Step 6: Implement Subscription Management
Track Subscription Status
extension StoreManager {
func checkSubscriptionStatus(for groupID: String) async -> Product.SubscriptionInfo.Status? {
guard let result = try? await Product.SubscriptionInfo.status(for: groupID),
let status = result.first else {
return nil
}
return status.state
}
}
Handle Subscription States
func updateSubscriptionUI(for status: Product.SubscriptionInfo.Status) {
switch status.state {
case .subscribed:
showSubscribedContent()
case .expired:
showResubscribeOffer()
case .inGracePeriod:
showUpdatePaymentPrompt()
case .inBillingRetryPeriod:
showBillingRetryMessage()
case .revoked:
removeAccess()
@unknown default:
break
}
}
Use StoreKit Views (iOS 17+)
struct SubscriptionView: View {
var body: some View {
SubscriptionStoreView(groupID: "pro_tier") {
VStack {
Image("premium-icon")
Text("Unlock all features")
}
}
.subscriptionStoreControlStyle(.prominentPicker)
}
}
Step 7: Implement Restore Purchases (REQUIRED)
Restore Flow
extension StoreManager {
func restorePurchases() async {
try? await AppStore.sync()
await updatePurchasedProducts()
}
}
UI Button
struct SettingsView: View {
@StateObject private var store = StoreManager()
var body: some View {
Button("Restore Purchases") {
Task {
await store.restorePurchases()
}
}
}
}
App Store Requirement: Apps with IAP must provide restore functionality for non-consumables and subscriptions.
Step 8: Handle Refunds
Listen for Refund Notifications
extension StoreManager {
func listenForTransactions() -> Task<Void, Never> {
Task.detached { [weak self] in
for await verificationResult in Transaction.updates {
await self?.handleTransaction(verificationResult)
}
}
}
@MainActor
private func handleTransaction(_ result: VerificationResult<Transaction>) async {
guard let transaction = try? result.payloadValue else {
return
}
if let revocationDate = transaction.revocationDate {
print("Transaction refunded on \(revocationDate)")
await revokeEntitlement(for: transaction.productID)
} else {
await grantEntitlement(for: transaction)
}
await transaction.finish()
}
}
Step 9: Unit Testing
Mock Store Responses
protocol StoreProtocol {
func products(for ids: [String]) async throws -> [Product]
func purchase(_ product: Product) async throws -> PurchaseResult
}
final class StoreManager: StoreProtocol {
func products(for ids: [String]) async throws -> [Product] {
try await Product.products(for: ids)
}
}
final class MockStore: StoreProtocol {
var mockProducts: [Product] = []
var mockPurchaseResult: PurchaseResult?
func products(for ids: [String]) async throws -> [Product] {
mockProducts
}
func ( : ) -> {
mockPurchaseResult .userCancelled
}
}
Test Purchase Logic
@Test func testSuccessfulPurchase() async {
let mockStore = MockStore()
let manager = StoreManager(store: mockStore)
mockStore.mockPurchaseResult = .success(.verified(mockTransaction))
let result = await manager.purchase(mockProduct)
#expect(result == true)
#expect(manager.purchasedProductIDs.contains("com.app.premium"))
}
@Test func testCancelledPurchase() async {
let mockStore = MockStore()
let manager = StoreManager(store: mockStore)
mockStore.mockPurchaseResult = .userCancelled
let result = await manager.purchase(mockProduct)
#expect(result == false)
#expect(manager.purchasedProductIDs.isEmpty)
}
Common Anti-Patterns (NEVER DO THIS)
❌ No StoreKit Configuration
let products = try await Product.products(for: productIDs)
✅ Correct: Create .storekit file FIRST, test in Xcode, THEN implement.
❌ Code Before .storekit Config
let products = try await Product.products(for: productIDs)
let result = try await product.purchase(confirmIn: scene)
✅ Recommended: Create .storekit config first, then write code.
If you're in this situation: See "Already Wrote Code Before Creating .storekit Config?" section above for your options (A, B, or C).
Why .storekit-first is better:
- Product ID typos caught in Xcode, not at runtime
- Faster iteration without network requests
- Teammates can test locally
- Documents product structure in code
Sandbox testing is valuable - it validates against real App Store infrastructure. But starting with .storekit makes sandbox testing easier because you've already validated product IDs locally.
❌ Scattered Purchase Calls
Button("Buy") {
try await product.purchase()
}
Button("Subscribe") {
try await subscriptionProduct.purchase()
}
✅ Correct: All purchases through centralized StoreManager.
❌ Forgetting to Finish Transactions
func handleTransaction(_ transaction: Transaction) {
grantEntitlement(for: transaction)
}
✅ Correct: ALWAYS call transaction.finish() after granting entitlement.
❌ Not Verifying Transactions
for await transaction in Transaction.all {
grantEntitlement(for: transaction)
}
✅ Correct: Always check VerificationResult before granting.
❌ Ignoring Transaction Listener
func purchase() {
let result = try await product.purchase()
}
✅ Correct: Listen to Transaction.updates for ALL transaction sources.
❌ Not Implementing Restore
✅ Correct: Provide visible "Restore Purchases" button in settings.
Validation
Before marking IAP implementation complete, verify:
Code Inspection
Run these searches to verify compliance:
find . -name "*.storekit"
rg "transaction\.finish\(\)" --type swift
rg "VerificationResult" --type swift
rg "Transaction\.updates" --type swift
rg "AppStore\.sync|Transaction\.all" --type swift
Functional Testing
Sandbox Testing
Resources
WWDC: 2025-241, 2025-249, 2023-10013, 2021-10114
Docs: /storekit, /appstoreserverapi
Skills: axiom-storekit-ref