瀏覽代碼

Add verifySubscriptions method to check the validity of auto-renewable subscriptions within a subscription group

Andrea Bizzotto 7 年之前
父節點
當前提交
9a77a4a30d
共有 2 個文件被更改,包括 32 次插入18 次删除
  1. 17 16
      SwiftyStoreKit/InAppReceipt.swift
  2. 15 2
      SwiftyStoreKit/SwiftyStoreKit.swift

+ 17 - 16
SwiftyStoreKit/InAppReceipt.swift

@@ -93,7 +93,7 @@ internal class InAppReceipt {
 
         // Get receipts info for the product
         let receipts = getInAppReceipts(receipt: receipt)
-        let filteredReceiptsInfo = filterReceiptsInfo(receipts: receipts, withProductId: productId)
+        let filteredReceiptsInfo = filterReceiptsInfo(receipts: receipts, withProductIds: [productId])
         let nonCancelledReceiptsInfo = filteredReceiptsInfo.filter { receipt in receipt["cancellation_date"] == nil }
 
         let receiptItems = nonCancelledReceiptsInfo.flatMap { ReceiptItem(receiptInfo: $0) }
@@ -105,24 +105,23 @@ internal class InAppReceipt {
     }
 
     /**
-     *  Verify the purchase of a subscription (auto-renewable, free or non-renewing) in a receipt. This method extracts all transactions mathing the given productId and sorts them by date in descending order, then compares the first transaction expiry date against the validUntil value.
+     *  Verify the validity of a set of subscriptions (auto-renewable, free or non-renewing) in a receipt. This method extracts all transactions mathing the given productIds and sorts them by date in descending order, then compares the first transaction expiry date against the validUntil value.
      *  - parameter type: .autoRenewable or .nonRenewing(duration)
-     *  - Parameter productId: the product id of the purchase to verify
-     *  - Parameter inReceipt: the receipt to use for looking up the subscription
-     *  - Parameter validUntil: date to check against the expiry date of the subscription. If nil, no verification
-     *  - Parameter validDuration: the duration of the subscription. Only required for non-renewable subscription.
-     *  - return: either NotPurchased or Purchased / Expired with the expiry date found in the receipt
+     *  - Parameter productIds: the product ids of the subscriptions to verify
+     *  - Parameter inReceipt: the receipt to use for looking up the subscriptions
+     *  - Parameter validUntil: date to check against the expiry date of the subscriptions.
+     *  - return: either notPurchased or purchased / expired with the expiry date found in the receipt
      */
-    class func verifySubscription(
+    class func verifySubscriptions(
         type: SubscriptionType,
-        productId: String,
+        productIds: Set<String>,
         inReceipt receipt: ReceiptInfo,
         validUntil date: Date = Date()
     ) -> VerifySubscriptionResult {
 
         // The values of the latest_receipt and latest_receipt_info keys are useful when checking whether an auto-renewable subscription is currently active. By providing any transaction receipt for the subscription and checking these values, you can get information about the currently-active subscription period. If the receipt being validated is for the latest renewal, the value for latest_receipt is the same as receipt-data (in the request) and the value for latest_receipt_info is the same as receipt.
         let (receipts, duration) = getReceiptsAndDuration(for: type, inReceipt: receipt)
-        let receiptsInfo = filterReceiptsInfo(receipts: receipts, withProductId: productId)
+        let receiptsInfo = filterReceiptsInfo(receipts: receipts, withProductIds: productIds)
         let nonCancelledReceiptsInfo = receiptsInfo.filter { receipt in receipt["cancellation_date"] == nil }
         if nonCancelledReceiptsInfo.count == 0 {
             return .notPurchased
@@ -198,19 +197,21 @@ internal class InAppReceipt {
      *  - Parameter receipts: the receipts array to grab info from
      *  - Parameter productId: the product id
      */
-    private class func filterReceiptsInfo(receipts: [ReceiptInfo]?, withProductId productId: String) -> [ReceiptInfo] {
+    private class func filterReceiptsInfo(receipts: [ReceiptInfo]?, withProductIds productIds: Set<String>) -> [ReceiptInfo] {
 
         guard let receipts = receipts else {
             return []
         }
 
-        // Filter receipts with matching product id
-        let receiptsMatchingProductId = receipts
+        // Filter receipts with matching product ids
+        let receiptsMatchingProductIds = receipts
             .filter { (receipt) -> Bool in
-                let product_id = receipt["product_id"] as? String
-                return product_id == productId
+                if let productId = receipt["product_id"] as? String {
+                    return productIds.contains(productId)
+                }
+                return false
             }
 
-        return receiptsMatchingProductId
+        return receiptsMatchingProductIds
     }
 }

+ 15 - 2
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -262,7 +262,7 @@ extension SwiftyStoreKit {
     }
 
     /**
-     *  Verify the purchase of a subscription (auto-renewable, free or non-renewing) in a receipt. This method extracts all transactions mathing the given productId and sorts them by date in descending order, then compares the first transaction expiry date against the validUntil value.
+     *  Verify the validity of a subscription (auto-renewable, free or non-renewing) in a receipt. This method extracts all transactions mathing the given productId and sorts them by date in descending order, then compares the first transaction expiry date against the validUntil value.
      *  - Parameter type: autoRenewable or nonRenewing
      *  - Parameter productId: the product id of the purchase to verify
      *  - Parameter inReceipt: the receipt to use for looking up the subscription
@@ -271,6 +271,19 @@ extension SwiftyStoreKit {
      */
     public class func verifySubscription(type: SubscriptionType, productId: String, inReceipt receipt: ReceiptInfo, validUntil date: Date = Date()) -> VerifySubscriptionResult {
 
-        return InAppReceipt.verifySubscription(type: type, productId: productId, inReceipt: receipt, validUntil: date)
+        return InAppReceipt.verifySubscriptions(type: type, productIds: [productId], inReceipt: receipt, validUntil: date)
+    }
+    
+    /**
+     *  Verify the validity of a set of auto-renewable subscriptions in a receipt. This method extracts all transactions mathing the given productIds and sorts them by date in descending order, then compares the first transaction expiry date against the validUntil value.
+     *  - Note: you can use this method to check the validity of subscriptions in a subscription group
+     *  - Parameter productIds: the product ids of the subscriptions to verify
+     *  - Parameter inReceipt: the receipt to use for looking up the subscription
+     *  - Parameter validUntil: date to check against the expiry date of the subscription. If nil, no verification
+     *  - return: either .notPurchased or .purchased / .expired with the expiry date found in the receipt
+     */
+    public class func verifySubscriptions(productIds: Set<String>, inReceipt receipt: ReceiptInfo) -> VerifySubscriptionResult {
+
+        return InAppReceipt.verifySubscriptions(type: .autoRenewable, productIds: productIds, inReceipt: receipt)
     }
 }