Browse Source

Add getDistinctPurchaseIds method to retrieve all product identifiers from receipt

Olmrzklv 5 years ago
parent
commit
2f6fcbd8ce
2 changed files with 56 additions and 0 deletions
  1. 42 0
      SwiftyStoreKit/InAppReceipt.swift
  2. 14 0
      SwiftyStoreKit/SwiftyStoreKit.swift

+ 42 - 0
SwiftyStoreKit/InAppReceipt.swift

@@ -168,6 +168,39 @@ internal class InAppReceipt {
             return .expired(expiryDate: firstExpiryDateItemPair.0, items: sortedReceiptItems)
         }
     }
+    
+    /**
+     *  Get the distintc product identifiers from receipt.
+     *
+     *  This Method extracts all product identifiers. (Including cancelled ones).
+     *  - Note: You can use this method to get all unique product identifiers from receipt.
+     *  - Parameter type: .autoRenewable or .nonRenewing.
+     *  - Parameter receipt: The receipt to use for looking up the product identifiers.
+     *  - return: Either Set<String> or nil.
+     */
+    class func getDistinctPurchaseIds(
+        ofType type: SubscriptionType,
+        inReceipt receipt: ReceiptInfo
+    ) -> Set<String>? {
+        
+        // Get receipts array from receipt
+        guard let receipts = getReceipts(for: type, inReceipt: receipt) else {
+            return nil
+        }
+        
+        #if swift(>=4.1)
+            let receiptIds = receipts.compactMap { ReceiptItem(receiptInfo: $0)?.productId }
+        #else
+            let receiptIds = receipts.flatMap { ReceiptItem(receiptInfo: $0)?.productId }
+        #endif
+        
+        if receiptIds.count == 0 {
+            return nil
+        }
+        
+        let distintcIds = Set.init(receiptIds)
+        return distintcIds
+    }
 
     private class func expiryDatesAndItems(receiptItems: [ReceiptItem], duration: TimeInterval?) -> [(Date, ReceiptItem)] {
 
@@ -194,6 +227,15 @@ internal class InAppReceipt {
             #endif
         }
     }
+    
+    private class func getReceipts(for subscriptionType: SubscriptionType, inReceipt receipt: ReceiptInfo) -> [ReceiptInfo]? {
+        switch subscriptionType {
+        case .autoRenewable:
+            return receipt["latest_receipt_info"] as? [ReceiptInfo]
+        case .nonRenewing:
+            return getInAppReceipts(receipt: receipt)
+        }
+    }
 
     private class func getReceiptsAndDuration(for subscriptionType: SubscriptionType, inReceipt receipt: ReceiptInfo) -> ([ReceiptInfo]?, TimeInterval?) {
         switch subscriptionType {

+ 14 - 0
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -311,4 +311,18 @@ extension SwiftyStoreKit {
 
         return InAppReceipt.verifySubscriptions(ofType: type, productIds: productIds, inReceipt: receipt, validUntil: date)
     }
+    
+    /**
+     *  Get the distintc product identifiers from receipt.
+     *
+     *  This Method extracts all product identifiers. (Including cancelled ones).
+     *  - Note: You can use this method to get all unique product identifiers from receipt.
+     *  - Parameter type: .autoRenewable or .nonRenewing.
+     *  - Parameter receipt: The receipt to use for looking upproduct identifiers.
+     *  - return: Either Set<String> or nil.
+     */
+    public class func getDistinctPurchaseIds(ofType type: SubscriptionType = .autoRenewable, inReceipt receipt: ReceiptInfo) -> Set<String>? {
+        
+        return InAppReceipt.getDistinctPurchaseIds(ofType: type, inReceipt: receipt)
+    }
 }