Browse Source

Merge pull request #331 from bizz84/verify-purchase-subscription-improve-logging

Improve logging for verifyPurchase, verifySubscription methods
Andrea Bizzotto 7 năm trước cách đây
mục cha
commit
e607b084f1
2 tập tin đã thay đổi với 22 bổ sung20 xóa
  1. 11 9
      README.md
  2. 11 11
      SwiftyStoreKit-iOS-Demo/ViewController.swift

+ 11 - 9
README.md

@@ -354,16 +354,17 @@ let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "
 SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
     switch result {
     case .success(let receipt):
+        let productId = "com.musevisions.SwiftyStoreKit.Purchase1"
         // Verify the purchase of Consumable or NonConsumable
         let purchaseResult = SwiftyStoreKit.verifyPurchase(
-            productId: "com.musevisions.SwiftyStoreKit.Purchase1",
+            productId: productId,
             inReceipt: receipt)
             
         switch purchaseResult {
         case .purchased(let receiptItem):
-            print("Product is purchased: \(receiptItem)")
+            print("\(productId) is purchased: \(receiptItem)")
         case .notPurchased:
-            print("The user has never purchased this product")
+            print("The user has never purchased \(productId)")
         }
     case .error(let error):
         print("Receipt verification failed: \(error)")
@@ -388,19 +389,20 @@ let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "
 SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
     switch result {
     case .success(let receipt):
+        let productId = "com.musevisions.SwiftyStoreKit.Subscription"
         // Verify the purchase of a Subscription
         let purchaseResult = SwiftyStoreKit.verifySubscription(
             type: .autoRenewable, // or .nonRenewing (see below)
-            productId: "com.musevisions.SwiftyStoreKit.Subscription",
+            productId: productId,
             inReceipt: receipt)
             
         switch purchaseResult {
-        case .purchased(let expiryDate, let receiptItems):
-            print("Product is valid until \(expiryDate)")
-        case .expired(let expiryDate, let receiptItems):
-            print("Product is expired since \(expiryDate)")
+        case .purchased(let expiryDate, let items):
+            print("\(productId) is valid until \(expiryDate)\n\(items)\n")
+        case .expired(let expiryDate, let items):
+            print("\(productId) is expired since \(expiryDate)\n\(items)\n")
         case .notPurchased:
-            print("The user has never purchased this product")
+            print("The user has never purchased \(productId)")
         }
 
     case .error(let error):

+ 11 - 11
SwiftyStoreKit-iOS-Demo/ViewController.swift

@@ -192,7 +192,7 @@ class ViewController: UIViewController {
                         inReceipt: receipt,
                         validUntil: Date()
                     )
-                    self.showAlert(self.alertForVerifySubscription(purchaseResult))
+                    self.showAlert(self.alertForVerifySubscription(purchaseResult, productId: productId))
                 case .nonRenewingPurchase:
                     let purchaseResult = SwiftyStoreKit.verifySubscription(
                         type: .nonRenewing(validDuration: 60),
@@ -200,13 +200,13 @@ class ViewController: UIViewController {
                         inReceipt: receipt,
                         validUntil: Date()
                     )
-                    self.showAlert(self.alertForVerifySubscription(purchaseResult))
+                    self.showAlert(self.alertForVerifySubscription(purchaseResult, productId: productId))
                 default:
                     let purchaseResult = SwiftyStoreKit.verifyPurchase(
                         productId: productId,
                         inReceipt: receipt
                     )
-                    self.showAlert(self.alertForVerifyPurchase(purchaseResult))
+                    self.showAlert(self.alertForVerifyPurchase(purchaseResult, productId: productId))
                 }
 
             case .error:
@@ -257,7 +257,7 @@ extension ViewController {
         switch result {
         case .success(let purchase):
             print("Purchase Success: \(purchase.productId)")
-            return alertWithTitle("Thank You", message: "Purchase completed")
+            return nil
         case .error(let error):
             print("Purchase Failed: \(error)")
             switch error.code {
@@ -315,29 +315,29 @@ extension ViewController {
         }
     }
 
-    func alertForVerifySubscription(_ result: VerifySubscriptionResult) -> UIAlertController {
+    func alertForVerifySubscription(_ result: VerifySubscriptionResult, productId: String) -> UIAlertController {
 
         switch result {
         case .purchased(let expiryDate, let items):
-            print("Product is valid until \(expiryDate) \n items \(items)")
+            print("\(productId) is valid until \(expiryDate)\n\(items)\n")
             return alertWithTitle("Product is purchased", message: "Product is valid until \(expiryDate)")
         case .expired(let expiryDate, let items):
-            print("Product is expired since \(expiryDate) \n items \(items)")
+            print("\(productId) is expired since \(expiryDate)\n\(items)\n")
             return alertWithTitle("Product expired", message: "Product is expired since \(expiryDate)")
         case .notPurchased:
-            print("This product has never been purchased")
+            print("\(productId) has never been purchased")
             return alertWithTitle("Not purchased", message: "This product has never been purchased")
         }
     }
 
-    func alertForVerifyPurchase(_ result: VerifyPurchaseResult) -> UIAlertController {
+    func alertForVerifyPurchase(_ result: VerifyPurchaseResult, productId: String) -> UIAlertController {
 
         switch result {
         case .purchased:
-            print("Product is purchased")
+            print("\(productId) is purchased")
             return alertWithTitle("Product is purchased", message: "Product will not expire")
         case .notPurchased:
-            print("This product has never been purchased")
+            print("\(productId) has never been purchased")
             return alertWithTitle("Not purchased", message: "This product has never been purchased")
         }
     }