Kaynağa Gözat

Replace PurchaseError and InternalErrorCode with SKError

Andrea Bizzotto 8 yıl önce
ebeveyn
işleme
91dd40562b

+ 16 - 23
SwiftyStoreKit-iOS-Demo/ViewController.swift

@@ -215,29 +215,22 @@ extension ViewController {
             return alertWithTitle("Thank You", message: "Purchase completed")
         case .error(let error):
             print("Purchase Failed: \(error)")
-            switch error {
-                case .failed(let error):
-                    switch error.code {
-                    case .unknown: return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
-                    case .clientInvalid: // client is not allowed to issue the request, etc.
-                        return alertWithTitle("Purchase failed", message: "Not allowed to make the payment")
-                    case .paymentCancelled: // user cancelled the request, etc.
-                        return nil
-                    case .paymentInvalid: // purchase identifier was invalid, etc.
-                        return alertWithTitle("Purchase failed", message: "The purchase identifier was invalid")
-                    case .paymentNotAllowed: // this device is not allowed to make the payment
-                        return alertWithTitle("Purchase failed", message: "The device is not allowed to make the payment")
-                    case .storeProductNotAvailable: // Product is not available in the current storefront
-                        return alertWithTitle("Purchase failed", message: "The product is not available in the current storefront")
-                    case .cloudServicePermissionDenied: // user has not allowed access to cloud service information
-                        return alertWithTitle("Purchase failed", message: "Access to cloud service information is not allowed")
-                    case .cloudServiceNetworkConnectionFailed: // the device could not connect to the nework
-                        return alertWithTitle("Purchase failed", message: "Could not connect to the network")
-                    }
-                case .invalidProductId(let productId):
-                    return alertWithTitle("Purchase failed", message: "\(productId) is not a valid product identifier")
-                case .paymentNotAllowed:
-                    return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
+            switch error.code {
+            case .unknown: return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
+            case .clientInvalid: // client is not allowed to issue the request, etc.
+                return alertWithTitle("Purchase failed", message: "Not allowed to make the payment")
+            case .paymentCancelled: // user cancelled the request, etc.
+                return nil
+            case .paymentInvalid: // purchase identifier was invalid, etc.
+                return alertWithTitle("Purchase failed", message: "The purchase identifier was invalid")
+            case .paymentNotAllowed: // this device is not allowed to make the payment
+                return alertWithTitle("Purchase failed", message: "The device is not allowed to make the payment")
+            case .storeProductNotAvailable: // Product is not available in the current storefront
+                return alertWithTitle("Purchase failed", message: "The product is not available in the current storefront")
+            case .cloudServicePermissionDenied: // user has not allowed access to cloud service information
+                return alertWithTitle("Purchase failed", message: "Access to cloud service information is not allowed")
+            case .cloudServiceNetworkConnectionFailed: // the device could not connect to the nework
+                return alertWithTitle("Purchase failed", message: "Could not connect to the network")
             }
         }
     }

+ 2 - 9
SwiftyStoreKit/SwiftyStoreKit+Types.swift

@@ -54,23 +54,16 @@ public struct RetrieveResults {
     public let error: Error?
 }
 
-// Purchase error types
-public enum PurchaseError {
-    case failed(error: SKError)
-    case invalidProductId(productId: String)
-    case paymentNotAllowed
-}
-
 // Purchase result
 public enum PurchaseResult {
     case success(product: Product)
-    case error(error: PurchaseError)
+    case error(error: SKError)
 }
 
 // Restore purchase results
 public struct RestoreResults {
     public let restoredProducts: [Product]
-    public let restoreFailedProducts: [(Swift.Error, String?)]
+    public let restoreFailedProducts: [(SKError, String?)]
 }
 
 // MARK: Receipt verification

+ 14 - 14
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -32,11 +32,6 @@ public class SwiftyStoreKit {
     
     private var receiptRefreshRequest: InAppReceiptRefreshRequest?
     
-    private enum InternalErrorCode: Int {
-        case restoredPurchaseWhenPurchasing = 0
-        case purchasedWhenRestoringPurchase = 1
-    }
-    
     init(productsInfoController: ProductsInfoController = ProductsInfoController(),
          paymentQueueController: PaymentQueueController = PaymentQueueController(paymentQueue: SKPaymentQueue.default())) {
         
@@ -61,10 +56,12 @@ public class SwiftyStoreKit {
                     self.purchase(product: product, atomically: atomically, applicationUsername: applicationUsername, completion: completion)
                 }
                 else if let error = result.error {
-                    completion(.error(error: .failed(error: SKError(_nsError: error as NSError))))
+                    completion(.error(error: SKError(_nsError: error as NSError)))
                 }
                 else if let invalidProductId = result.invalidProductIDs.first {
-                    completion(.error(error: .invalidProductId(productId: invalidProductId)))
+                    let userInfo = [ NSLocalizedDescriptionKey: "Invalid product id: \(invalidProductId)" ]
+                    let error = NSError(domain: SKErrorDomain, code: SKError.storeProductNotAvailable.rawValue, userInfo: userInfo)
+                    completion(.error(error: SKError(_nsError: error)))
                 }
             }
         }
@@ -111,7 +108,8 @@ public class SwiftyStoreKit {
     // MARK: private methods
     private func purchase(product: SKProduct, atomically: Bool, applicationUsername: String = "", completion: @escaping (PurchaseResult) -> ()) {
         guard SwiftyStoreKit.canMakePayments else {
-            completion(.error(error: .paymentNotAllowed))
+            let error = NSError(domain: SKErrorDomain, code: SKError.paymentNotAllowed.rawValue, userInfo: nil)
+            completion(.error(error: SKError(_nsError: error)))
             return
         }
         
@@ -126,19 +124,20 @@ public class SwiftyStoreKit {
         case .purchased(let product):
             return .success(product: product)
         case .failed(let error):
-            return .error(error: .failed(error: error))
+            return .error(error: error)
         case .restored(let product):
-            return .error(error: .failed(error: SKError(_nsError:  storeInternalError(code: InternalErrorCode.restoredPurchaseWhenPurchasing.rawValue, description: "Cannot restore product \(product.productId) from purchase path"))))
+            return .error(error: storeInternalError(description: "Cannot restore product \(product.productId) from purchase path"))
         }
     }
     
     private func processRestoreResults(_ results: [TransactionResult]) -> RestoreResults {
         var restoredProducts: [Product] = []
-        var restoreFailedProducts: [(Swift.Error, String?)] = []
+        var restoreFailedProducts: [(SKError, String?)] = []
         for result in results {
             switch result {
             case .purchased(let product):
-                restoreFailedProducts.append((storeInternalError(code: InternalErrorCode.purchasedWhenRestoringPurchase.rawValue, description: "Cannot purchase product \(product.productId) from restore purchases path"), product.productId))
+                let error = storeInternalError(description: "Cannot purchase product \(product.productId) from restore purchases path")
+                restoreFailedProducts.append((error, product.productId))
             case .failed(let error):
                 restoreFailedProducts.append((error, nil))
             case .restored(let product):
@@ -148,8 +147,9 @@ public class SwiftyStoreKit {
         return RestoreResults(restoredProducts: restoredProducts, restoreFailedProducts: restoreFailedProducts)
     }
     
-    private func storeInternalError(code: Int = 0, description: String = "") -> NSError {
-        return NSError(domain: "SwiftyStoreKit", code: code, userInfo: [ NSLocalizedDescriptionKey: description ])
+    private func storeInternalError(code: SKError.Code = SKError.unknown, description: String = "") -> SKError {
+        let error = NSError(domain: SKErrorDomain, code: code.rawValue, userInfo: [ NSLocalizedDescriptionKey: description ])
+        return SKError(_nsError: error)
     }
 }