Skip to content

Commit

Permalink
[temp] verify multiple subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-dumit committed Sep 15, 2017
1 parent 9d1b446 commit 3c75f8f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
35 changes: 30 additions & 5 deletions SwiftyStoreKit/InAppReceipt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal class InAppReceipt {

// Get receipts info for the product
let receipts = receipt["receipt"]?["in_app"] as? [ReceiptInfo]
let receiptsInfo = filterReceiptsInfo(receipts: receipts, withProductId: productId)
let receiptsInfo = filterReceiptsInfo(receipts: receipts, withProductIds: [productId])
let nonCancelledReceiptsInfo = receiptsInfo.filter { receipt in receipt["cancellation_date"] == nil }

let receiptItems = nonCancelledReceiptsInfo.flatMap { ReceiptItem(receiptInfo: $0) }
Expand All @@ -104,6 +104,7 @@ internal class InAppReceipt {
return .notPurchased
}


/**
* 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.
* - parameter type: .autoRenewable or .nonRenewing(duration)
Expand All @@ -118,11 +119,33 @@ internal class InAppReceipt {
productId: String,
inReceipt receipt: ReceiptInfo,
validUntil date: Date = Date()
) -> VerifySubscriptionResult {
return self.verifyAllSubscriptions(type: type,
productIds: [productId],
inReceipt: receipt,
validUntil: date)
}


/**
* 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.
* - parameter type: .autoRenewable or .nonRenewing(duration)
* - Parameter productIds: the product ids 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
*/
class func verifyAllSubscriptions(
type: SubscriptionType,
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
Expand Down Expand Up @@ -192,7 +215,7 @@ 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 []
Expand All @@ -201,8 +224,10 @@ internal class InAppReceipt {
// Filter receipts with matching product id
let receiptsMatchingProductId = receipts
.filter { (receipt) -> Bool in
let product_id = receipt["product_id"] as? String
return product_id == productId
if let product_id = receipt["product_id"] as? String {
return productIds.contains(product_id)
}
return false
}

return receiptsMatchingProductId
Expand Down
14 changes: 14 additions & 0 deletions SwiftyStoreKit/SwiftyStoreKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,18 @@ extension SwiftyStoreKit {

return InAppReceipt.verifySubscription(type: type, productId: productId, inReceipt: receipt, validUntil: date)
}


/**
* Verify the purchase of all subscriptions (auto-renewable, free or non-renewing) in a receipt. This method extracts all transactions mathing the one of 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 productIds: the product ids of the purchase to verify (should be same family)
* - 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 verifyAllSubscriptions(type: SubscriptionType, productIds: Set<String>, inReceipt receipt: ReceiptInfo, validUntil date: Date = Date()) -> VerifySubscriptionResult {

return InAppReceipt.verifyAllSubscriptions(type: type, productIds: productIds, inReceipt: receipt, validUntil: date)
}
}

0 comments on commit 3c75f8f

Please sign in to comment.