Use this page with AI

Copy this into your coding assistant and add your request.

Read https://openiap.dev/docs/features/purchase and https://openiap.dev/llms.txt. Follow the reading instructions, detailed reference, and linked guides relevant to my task before making changes.
Inspect my existing project and reuse its framework and conventions. Ask me for missing product decisions. Implement the requested behavior and run the applicable checks.
Show the working result, the commands and actual test results, and any remaining limitations. Keep your explanation brief.

My request: [describe what customers should be able to do]
See an example request →

Purchase

Handle in-app purchases with proper transaction management. This guide covers the complete purchase flow from setup to completion.

Purchase Flow Overview#

A complete purchase flow follows these steps:

  1. Setup Listeners - Register callbacks before any purchase
  2. Fetch Products - Get available products from the store
  3. Request Purchase - Initiate the purchase UI
  4. Handle Listener Callback - Receive success or error from listener
  5. Verify Purchase - Validate with your backend or IAPKit
  6. Finish Transaction - Complete the transaction

Setup Purchase Listeners#

Register purchase listeners before initializing the store connection or making purchase requests. These listeners handle successful purchases and errors.

swift
import OpenIAP
import SwiftUI

@MainActor
class PurchaseManager: ObservableObject {
    private let iapStore = OpenIapStore()

    init() {
        setupListeners()
    }

    private func setupListeners() {
        // 1. Setup purchase success callback
        iapStore.onPurchaseSuccess = { [weak self] purchase in
            print("Purchase received: \(purchase.productId)")
            Task {
                await self?.handlePurchase(purchase)
            }
        }

        // 2. Setup error callback
        iapStore.onPurchaseError = { [weak self] error in
            print("Purchase error: \(error.localizedDescription)")
            self?.handlePurchaseError(error)
        }

        // 3. Initialize connection
        Task {
            do {
                try await iapStore.initConnection()
                guard iapStore.isConnected else {
                    print("Store connection failed")
                    return
                }
                print("Store connection established")
            } catch {
                print("Failed to connect: \(error.localizedDescription)")
            }
        }
    }
}

Request Purchase#

After setting up listeners, you can request purchases. The purchase request triggers the native store UI (App Store / Google Play).

Consumable / Non-Consumable Products#

swift
import OpenIAP

@MainActor
func purchaseProduct(productId: String) async {
    let iapStore = OpenIapStore()

    do {
        // Request purchase - result delivered to onPurchaseSuccess
        _ = try await iapStore.requestPurchase(
            sku: productId,
            type: .inApp,  // .inApp for consumables/non-consumables
            autoFinish: false  // We'll finish manually after verification
        )
    } catch {
        print("Purchase request failed: \(error.localizedDescription)")
    }
}

// Example usage
await purchaseProduct(productId: "com.app.coins_100")

Verify the purchase#

Always verify purchases with a trusted verifier. Client-side store state alone can be bypassed. Validation compares the verifiers (your backend, a verification service, or StoreKit on the device), shows how the purchase flow confirms each purchase through your backend, and covers verifyPurchaseWithProvider for every SDK. For verification on your own server (JWS for iOS, the Google Play Developer API for Android), see the server verification references.

Finish Transaction#

Always finish transactions after verification. This step is critical - unfinished transactions cause issues on both platforms.

Transaction Types#

TypeisConsumableBehavior
ConsumabletrueProduct can be purchased again (coins, gems, etc.)
Non-ConsumablefalseOne-time purchase, cannot be bought again (premium unlock)
SubscriptionfalseRecurring purchase, managed by the store
swift
import OpenIAP

// Complete purchase flow
@MainActor
func handlePurchase(_ purchase: PurchaseIOS) async {
    let iapStore = OpenIapStore()

    // 1. Verify on server
    let isValid = await verifyIOSPurchase(purchase)
    guard isValid else {
        print("Invalid purchase")
        return
    }

    // 2. Grant the product to user
    await grantProductToUser(productId: purchase.productId)

    // 3. Finish the transaction (CRITICAL: Android auto-refunds after 3 days!)
    // - isConsumable: true = consume (can buy again)
    // - isConsumable: false = acknowledge only (one-time purchase)
    let isConsumable = purchase.productId.contains("consumable")
    do {
        try await iapStore.finishTransaction(purchase: purchase, isConsumable: isConsumable)
        print("Transaction finished successfully")
    } catch {
        print("Failed to finish transaction: \(error.localizedDescription)")
    }
}

Complete Example#

Here's a complete implementation combining all steps:

swift
import OpenIAP
import SwiftUI

@MainActor
class PurchaseManager: ObservableObject {
    static let shared = PurchaseManager()

    @Published var products: [ProductIOS] = []
    @Published var isProcessing = false

    private let iapStore = OpenIapStore()

    init() {
        setupListeners()
        Task {
            do {
                try await iapStore.initConnection()
                guard iapStore.isConnected else {
                    print("Store connection failed")
                    return
                }
                try await iapStore.fetchProducts(
                    skus: ["com.app.premium", "com.app.coins_100"],
                    type: .inApp
                )
                products = iapStore.iosProducts
            } catch {
                print("Failed to fetch products: \(error.localizedDescription)")
            }
        }
    }

    private func setupListeners() {
        iapStore.onPurchaseSuccess = { [weak self] purchase in
            guard let iosPurchase = purchase.asIOS() else { return }
            Task { @MainActor in
                await self?.handlePurchase(iosPurchase)
            }
        }

        iapStore.onPurchaseError = { [weak self] error in
            Task { @MainActor in
                self?.isProcessing = false
                print("Purchase error: \(error.localizedDescription)")
            }
        }
    }

    func purchase(_ productId: String) async {
        isProcessing = true
        do {
            _ = try await iapStore.requestPurchase(
                sku: productId,
                type: .inApp,
                autoFinish: false
            )
        } catch {
            isProcessing = false
            print("Purchase request failed: \(error.localizedDescription)")
        }
    }

    private func handlePurchase(_ purchase: PurchaseIOS) async {
        defer { isProcessing = false }

        // Step 1: Verify
        let isValid = await verifyIOSPurchase(purchase)
        guard isValid else {
            print("Verification failed")
            return
        }

        // Step 2: Grant product
        await grantProductToUser(productId: purchase.productId)

        // Step 3: Finish
        do {
            let isConsumable = purchase.productId.contains("coins")
            try await iapStore.finishTransaction(
                purchase: purchase,
                isConsumable: isConsumable
            )
            print("Purchase completed!")
        } catch {
            print("Failed to finish: \(error.localizedDescription)")
        }
    }
}

Troubleshooting#

Common Issues#

IssueCauseSolution
Purchase replays on launchTransaction not finishedCall finishTransaction() after verification
Android purchase refundedNot acknowledged within 3 daysFinish transaction immediately after verification
Cannot repurchase consumableNot consumedPass isConsumable: true to finishTransaction()
Listener not calledListener set up after purchaseAlways set up listeners before any purchase request

Handling Pending Purchases#

Check for pending (unfinished) purchases on app launch to complete interrupted transactions:

swift
@MainActor
func checkPendingPurchases() async {
    let iapStore = OpenIapStore()

    do {
        try await iapStore.getAvailablePurchases()

        for purchase in iapStore.availablePurchases {
            // Process each pending purchase
            if let iosPurchase = purchase.asIOS() {
                await handlePurchase(iosPurchase)
            }
        }
    } catch {
        print("Failed to get pending purchases: \(error.localizedDescription)")
    }
}

Native References#