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]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:
- Setup Listeners - Register callbacks before any purchase
- Fetch Products - Get available products from the store
- Request Purchase - Initiate the purchase UI
- Handle Listener Callback - Receive success or error from listener
- Verify Purchase - Validate with your backend or IAPKit
- 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.
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#
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#
| Type | isConsumable | Behavior |
|---|---|---|
| Consumable | true | Product can be purchased again (coins, gems, etc.) |
| Non-Consumable | false | One-time purchase, cannot be bought again (premium unlock) |
| Subscription | false | Recurring purchase, managed by the store |
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:
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#
| Issue | Cause | Solution |
|---|---|---|
| Purchase replays on launch | Transaction not finished | Call finishTransaction() after verification |
| Android purchase refunded | Not acknowledged within 3 days | Finish transaction immediately after verification |
| Cannot repurchase consumable | Not consumed | Pass isConsumable: true to finishTransaction() |
| Listener not called | Listener set up after purchase | Always set up listeners before any purchase request |
Handling Pending Purchases#
Check for pending (unfinished) purchases on app launch to complete interrupted transactions:
@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#
- Apple · In-App Purchase overview
- Apple · StoreKit 2 In-App Purchase
- Apple · StoreKit Transaction
- Google · Integrate the Google Play Billing Library
- Google · BillingClient reference