Skip to content

Commit e7a3c3e

Browse files
committed
Assert that in app purchases are supported. Show it in UI
1 parent 479aabb commit e7a3c3e

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct InAppPurchasesDebugView: View {
88
private let inAppPurchasesForWPComPlansManager = InAppPurchasesForWPComPlansManager()
99
@State var products: [WPComPlanProduct] = []
1010
@State var entitledProductIDs: [String] = []
11+
@State var inAppPurchasesAreSupported = true
1112

1213
var body: some View {
1314
List {
@@ -35,6 +36,13 @@ struct InAppPurchasesDebugView: View {
3536
Section {
3637
Button("Retry WPCom Synchronization for entitled products") {
3738
retryWPComSynchronizationForPurchasedProducts()
39+
}.disabled(!inAppPurchasesAreSupported || entitledProductIDs.isEmpty)
40+
}
41+
42+
if !inAppPurchasesAreSupported {
43+
Section {
44+
Text("In-App Purchases are not supported for this user")
45+
.foregroundColor(.red)
3846
}
3947
}
4048
}
@@ -46,6 +54,12 @@ struct InAppPurchasesDebugView: View {
4654

4755
private func loadProducts() async {
4856
do {
57+
inAppPurchasesAreSupported = await inAppPurchasesForWPComPlansManager.inAppPurchasesAreSupported()
58+
59+
guard inAppPurchasesAreSupported else {
60+
return
61+
}
62+
4963
self.products = try await inAppPurchasesForWPComPlansManager.fetchProducts()
5064
await loadUserEntitlements()
5165
} catch {

Yosemite/Yosemite/Stores/InAppPurchaseStore.swift

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private extension InAppPurchaseStore {
6262
func loadProducts(completion: @escaping (Result<[StoreKit.Product], Error>) -> Void) {
6363
Task {
6464
do {
65+
try await assertInAppPurchasesAreSupported()
6566
let identifiers = try await getProductIdentifiers()
6667
logInfo("Requesting StoreKit products: \(identifiers)")
6768
let products = try await StoreKit.Product.products(for: identifiers)
@@ -76,18 +77,21 @@ private extension InAppPurchaseStore {
7677

7778
func purchaseProduct(siteID: Int64, productID: String, completion: @escaping (Result<StoreKit.Product.PurchaseResult, Error>) -> Void) {
7879
Task {
79-
guard let product = try await StoreKit.Product.products(for: [productID]).first else {
80-
return completion(.failure(Errors.transactionProductUnknown))
81-
}
80+
do {
81+
try await assertInAppPurchasesAreSupported()
82+
83+
guard let product = try await StoreKit.Product.products(for: [productID]).first else {
84+
return completion(.failure(Errors.transactionProductUnknown))
85+
}
86+
87+
logInfo("Purchasing product \(product.id) for site \(siteID)")
88+
var purchaseOptions: Set<StoreKit.Product.PurchaseOption> = []
89+
if let appAccountToken = AppAccountToken.tokenWithSiteId(siteID) {
90+
logInfo("Generated appAccountToken \(appAccountToken) for site \(siteID)")
91+
purchaseOptions.insert(.appAccountToken(appAccountToken))
92+
}
8293

83-
logInfo("Purchasing product \(product.id) for site \(siteID)")
84-
var purchaseOptions: Set<StoreKit.Product.PurchaseOption> = []
85-
if let appAccountToken = AppAccountToken.tokenWithSiteId(siteID) {
86-
logInfo("Generated appAccountToken \(appAccountToken) for site \(siteID)")
87-
purchaseOptions.insert(.appAccountToken(appAccountToken))
88-
}
8994

90-
do {
9195
logInfo("Purchasing product \(product.id) for site \(siteID) with options \(purchaseOptions)")
9296
let purchaseResult = try await product.purchase(options: purchaseOptions)
9397
if case .success(let result) = purchaseResult {
@@ -98,7 +102,7 @@ private extension InAppPurchaseStore {
98102
}
99103
completion(.success(purchaseResult))
100104
} catch {
101-
logError("Error purchasing product \(product.id) for site \(siteID): \(error)")
105+
logError("Error purchasing product \(productID) for site \(siteID): \(error)")
102106
completion(.failure(error))
103107
}
104108
}
@@ -133,6 +137,8 @@ private extension InAppPurchaseStore {
133137
}
134138

135139
func retryWPComSyncForPurchasedProduct(with id: String) async throws {
140+
try await assertInAppPurchasesAreSupported()
141+
136142
guard let verificationResult = await Transaction.currentEntitlement(for: id) else {
137143
// The user doesn't have a valid entitlement for this product
138144
throw Errors.transactionProductUnknown
@@ -146,6 +152,12 @@ private extension InAppPurchaseStore {
146152
try await handleCompletedTransaction(verificationResult)
147153
}
148154

155+
func assertInAppPurchasesAreSupported() async throws {
156+
guard await inAppPurchasesAreSupported() else {
157+
throw Errors.inAppPurchasesNotSupported
158+
}
159+
}
160+
149161
func submitTransaction(_ transaction: StoreKit.Transaction) async throws {
150162
guard useBackend else {
151163
return
@@ -259,6 +271,7 @@ public extension InAppPurchaseStore {
259271
case transactionProductUnknown
260272
case storefrontUnknown
261273
case missingAppReceipt
274+
case inAppPurchasesNotSupported
262275
}
263276

264277
enum Constants {

0 commit comments

Comments
 (0)