Skip to content
This repository was archived by the owner on Oct 16, 2025. It is now read-only.

Commit df4e226

Browse files
authored
fix: prevent fetchProducts from returning cached products not in request (#16)
- Change from `getAllProducts()` to use only `fetchedProducts` from current request - Add subscription type validation for `ProductQueryType.subs` queries - Fixes issue where subsequent `fetchProducts` calls would incorrectly include products from previous requests
1 parent 873b7e1 commit df4e226

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Sources/OpenIapModule.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ public final class OpenIapModule: NSObject, OpenIapModuleProtocol {
9090
throw error
9191
}
9292

93+
let fetchedProducts: [StoreKit.Product]
9394
do {
94-
let fetched = try await StoreKit.Product.products(for: params.skus)
95-
for product in fetched {
95+
fetchedProducts = try await StoreKit.Product.products(for: params.skus)
96+
for product in fetchedProducts {
9697
await productManager.addProduct(product)
9798
}
9899
} catch {
@@ -101,11 +102,11 @@ public final class OpenIapModule: NSObject, OpenIapModuleProtocol {
101102
throw purchaseError
102103
}
103104

104-
let storedProducts = await productManager.getAllProducts()
105+
// Only process products that were actually requested, not all cached products
105106
var productEntries: [OpenIAP.Product] = []
106107
var subscriptionEntries: [OpenIAP.ProductSubscription] = []
107108

108-
for product in storedProducts {
109+
for product in fetchedProducts {
109110
productEntries.append(await StoreKitTypesBridge.product(from: product))
110111
if let subscription = await StoreKitTypesBridge.productSubscription(from: product) {
111112
subscriptionEntries.append(subscription)
@@ -114,7 +115,13 @@ public final class OpenIapModule: NSObject, OpenIapModuleProtocol {
114115

115116
switch params.type ?? .all {
116117
case .subs:
117-
return .subscriptions(subscriptionEntries.isEmpty ? nil : subscriptionEntries)
118+
// Only return products that are actually subscriptions
119+
let validSubs = subscriptionEntries.filter { sub in
120+
fetchedProducts.contains { product in
121+
product.id == sub.id && product.subscription != nil
122+
}
123+
}
124+
return .subscriptions(validSubs.isEmpty ? nil : validSubs)
118125
case .inApp:
119126
let inApp = productEntries.compactMap { entry -> OpenIAP.Product? in
120127
guard case let .productIos(value) = entry, value.type == .inApp else { return nil }

0 commit comments

Comments
 (0)