-
Notifications
You must be signed in to change notification settings - Fork 795
Verify Subscription
Samuel Spencer edited this page Jan 22, 2021
·
1 revision
This can be used to check if a subscription was previously purchased, and whether it is still active or if it's expired.
From Apple - Working with Subscriptions:
keep a record of the date that each piece of content is published. Read the Original Purchase Date and Subscription Expiration Date field from each receipt entry to determine the start and end dates of the subscription.
When one or more subscriptions are found for a given product id, they are returned as a ReceiptItem
array ordered by expiryDate
, with the first one being the newest.
let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "your-shared-secret")
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(
ofType: .autoRenewable, // or .nonRenewing (see below)
productId: productId,
inReceipt: receipt)
switch purchaseResult {
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 \(productId)")
}
case .error(let error):
print("Receipt verification failed: \(error)")
}
}
let purchaseResult = SwiftyStoreKit.verifySubscription(
ofType: .autoRenewable,
productId: "com.musevisions.SwiftyStoreKit.Subscription",
inReceipt: receipt)
// validDuration: time interval in seconds
let purchaseResult = SwiftyStoreKit.verifySubscription(
ofType: .nonRenewing(validDuration: 3600 * 24 * 30),
productId: "com.musevisions.SwiftyStoreKit.Subscription",
inReceipt: receipt)
Notes
- The expiration dates are calculated against the receipt date. This is the date of the last successful call to
verifyReceipt
. - When purchasing subscriptions in sandbox mode, the expiry dates are set just minutes after the purchase date for testing purposes.
The verifySubscription
method can be used together with the purchaseProduct
method to purchase a subscription and check its expiration date, like so:
let productId = "your-product-id"
SwiftyStoreKit.purchaseProduct(productId, atomically: true) { result in
if case .success(let purchase) = result {
// Deliver content from server, then:
if purchase.needsFinishTransaction {
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "your-shared-secret")
SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
if case .success(let receipt) = result {
let purchaseResult = SwiftyStoreKit.verifySubscription(
ofType: .autoRenewable,
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 .notPurchased:
print("This product has never been purchased")
}
} else {
// receipt verification error
}
}
} else {
// purchase error
}
}