Skip to content

Commit 7d0aaa4

Browse files
committed
Improve error handling in the IAP store
1 parent f2da8d3 commit 7d0aaa4

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

Yosemite/Yosemite/Stores/InAppPurchaseStore.swift

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,26 @@ private extension InAppPurchaseStore {
9494

9595
logInfo("Purchasing product \(product.id) for site \(siteID) with options \(purchaseOptions)")
9696
let purchaseResult = try await product.purchase(options: purchaseOptions)
97-
if case .success(let result) = purchaseResult {
98-
logInfo("Purchased product \(product.id) for site \(siteID): \(result)")
99-
try await handleCompletedTransaction(result)
100-
} else {
101-
logError("Ignorning unsuccessful purchase: \(purchaseResult)")
97+
switch purchaseResult {
98+
case .success(let result):
99+
guard case .verified(let transaction) = result else {
100+
// Ignore unverified transactions.
101+
logError("Transaction unverified: \(result)")
102+
throw Errors.unverifiedTransaction
103+
}
104+
logInfo("Purchased product \(product.id) for site \(siteID): \(transaction)")
105+
106+
try await submitTransaction(transaction)
107+
await transaction.finish()
108+
case .userCancelled:
109+
logInfo("User cancelled the purchase flow")
110+
throw Errors.userCancelled
111+
case .pending:
112+
logError("Purchase returned in a pending state, it might succeed in the future")
113+
throw Errors.pending
114+
@unknown default:
115+
logError("Unknown result for purchase: \(purchaseResult)")
116+
throw Errors.unknownResult
102117
}
103118
completion(.success(purchaseResult))
104119
} catch {
@@ -265,12 +280,45 @@ private extension InAppPurchaseStore {
265280
}
266281

267282
public extension InAppPurchaseStore {
268-
enum Errors: Error {
283+
enum Errors: String, Error {
284+
/// The user canceled the IAP flow
285+
case userCancelled
286+
287+
/// The purchase is pending some user action.
288+
///
289+
/// These purchases may succeed in the future, and the resulting `Transaction` will be
290+
/// delivered via `Transaction.updates`
291+
case pending
292+
293+
/// The purchase returned a PurchaseResult value that didn't exist when this was developed
294+
case unknownResult
295+
296+
/// The purchase was successful but the transaction was unverified
297+
///
298+
case unverifiedTransaction
299+
300+
/// The purchase was successful but it's not associated to an account
301+
///
269302
case transactionMissingAppAccountToken
303+
304+
/// The transaction has an associated account but it can't be translated to a site
305+
///
270306
case appAccountTokenMissingSiteIdentifier
307+
308+
/// The transaction is associated with an unknown product
309+
///
271310
case transactionProductUnknown
311+
312+
/// The storefront for the user is unknown, and so we can't know their country code
313+
///
272314
case storefrontUnknown
315+
316+
/// App receipt was missing, even after a refresh
317+
///
273318
case missingAppReceipt
319+
320+
/// In-app purchases are not supported for this user
321+
///
274322
case inAppPurchasesNotSupported
275323
}
276324

0 commit comments

Comments
 (0)