Skip to content

Commit f2da8d3

Browse files
committed
Show an alert in the IAP debug screen when a purchase fails
1 parent 7bbef09 commit f2da8d3

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

WooCommerce/Classes/ViewRelated/InAppPurchases/InAppPurchasesDebugView.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ struct InAppPurchasesDebugView: View {
1010
@State var entitledProductIDs: Set<String> = []
1111
@State var inAppPurchasesAreSupported = true
1212
@State var isPurchasing = false
13+
@State private var purchaseError: PurchaseError? {
14+
didSet {
15+
presentAlert = purchaseError != nil
16+
}
17+
}
18+
@State var presentAlert = false
1319

1420
var body: some View {
1521
List {
@@ -30,11 +36,16 @@ struct InAppPurchasesDebugView: View {
3036
Button(entitledProductIDs.contains(product.id) ? "Entitled: \(product.description)" : product.description) {
3137
Task {
3238
isPurchasing = true
33-
try? await inAppPurchasesForWPComPlansManager.purchaseProduct(with: product.id, for: siteID)
39+
do {
40+
try await inAppPurchasesForWPComPlansManager.purchaseProduct(with: product.id, for: siteID)
41+
} catch {
42+
purchaseError = PurchaseError(error: error)
43+
}
3444
await loadUserEntitlements()
3545
isPurchasing = false
3646
}
3747
}
48+
.alert(isPresented: $presentAlert, error: purchaseError, actions: {})
3849
}
3950
}
4051
}
@@ -102,6 +113,16 @@ struct InAppPurchasesDebugView: View {
102113
}
103114
}
104115

116+
/// Just a silly little wrapper because SwiftUI's `alert(isPresented:error:actions:)` wants a `LocalizedError`
117+
/// but we only have an `Error` coming from `purchaseProduct`.
118+
private struct PurchaseError: LocalizedError {
119+
let error: Error
120+
121+
var errorDescription: String? {
122+
error.localizedDescription
123+
}
124+
}
125+
105126
struct InAppPurchasesDebugView_Previews: PreviewProvider {
106127
static var previews: some View {
107128
InAppPurchasesDebugView(siteID: 0)

0 commit comments

Comments
 (0)