@@ -94,11 +94,23 @@ 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+ case . pending:
111+ logError ( " Purchase returned in a pending state, it might succeed in the future " )
112+ @unknown default :
113+ logError ( " Unknown result for purchase: \( purchaseResult) " )
102114 }
103115 completion ( . success( purchaseResult) )
104116 } catch {
@@ -265,13 +277,67 @@ private extension InAppPurchaseStore {
265277}
266278
267279public extension InAppPurchaseStore {
268- enum Errors : Error {
280+ enum Errors : Error , LocalizedError {
281+ /// The purchase was successful but the transaction was unverified
282+ ///
283+ case unverifiedTransaction
284+
285+ /// The purchase was successful but it's not associated to an account
286+ ///
269287 case transactionMissingAppAccountToken
288+
289+ /// The transaction has an associated account but it can't be translated to a site
290+ ///
270291 case appAccountTokenMissingSiteIdentifier
292+
293+ /// The transaction is associated with an unknown product
294+ ///
271295 case transactionProductUnknown
296+
297+ /// The storefront for the user is unknown, and so we can't know their country code
298+ ///
272299 case storefrontUnknown
300+
301+ /// App receipt was missing, even after a refresh
302+ ///
273303 case missingAppReceipt
304+
305+ /// In-app purchases are not supported for this user
306+ ///
274307 case inAppPurchasesNotSupported
308+
309+ public var errorDescription : String ? {
310+ switch self {
311+ case . unverifiedTransaction:
312+ return NSLocalizedString (
313+ " The purchase transaction couldn't be verified " ,
314+ comment: " Error message used when a purchase was successful but its transaction was unverified " )
315+ case . transactionMissingAppAccountToken:
316+ return NSLocalizedString (
317+ " Purchase transaction missing account information " ,
318+ comment: " Error message used when the purchase transaction doesn't have the right metadata to associate to a specific site " )
319+ case . appAccountTokenMissingSiteIdentifier:
320+ return NSLocalizedString (
321+ " Purchase transaction can't be associated to a site " ,
322+ comment: " Error message used when the purchase transaction doesn't have the right metadata to associate to a specific site " )
323+ case . transactionProductUnknown:
324+ return NSLocalizedString (
325+ " Purchase transaction received for an unknown product " ,
326+ comment: " Error message used when we received a transaction for an unknown product " )
327+ case . storefrontUnknown:
328+ return NSLocalizedString (
329+ " Couldn't determine App Stoure country " ,
330+ comment: " Error message used when we can't determine the user's App Store country " )
331+ case . missingAppReceipt:
332+ return NSLocalizedString (
333+ " Couldn't retrieve app receipt " ,
334+ comment: " Error message used when we can't read the app receipt " )
335+ case . inAppPurchasesNotSupported:
336+ return NSLocalizedString (
337+ " In-app purchases are not supported for this user yet " ,
338+ comment: " Error message used when In-app purchases are not supported for this user/site " )
339+ }
340+ }
275341 }
276342
277343 enum Constants {
0 commit comments