Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an Auto-Renewable Subscription is still valid

I would like to check the Auto Renewable Subscription status whenever I open the app.

This is to make sure that the user is still subscribed to the service. How do I achieve this?

Any thoughts? Thank you

P.S.: I am using SwiftyStoreKit

like image 341
JayVDiyk Avatar asked Apr 14 '17 09:04

JayVDiyk


People also ask

How do I check my sandbox subscription?

You can choose a subscription renewal rate for each Sandbox Apple ID account in App Store Connect. For a complete list of subscription durations within the sandbox environment and more information, see Test in-app purchases.

Do Apple subscriptions automatically renew?

They automatically renew at the end of their duration until the user chooses to cancel. Subscriptions are available on iOS, iPadOS, macOS, watchOS, and tvOS.


3 Answers

Here is several ways to do receipt validation to check is user granted to subscription. Here is two ways of doing it correctly:

  1. Do receipt validation locally as it is written here.
  2. Do receipt validation remotely as it is written here. It is mentioned that receipt should not be sent to App Store within an app. Short summary:

    • Your app sends receipt to your backend.
    • Your backend sends receipt to Apple backend for validation.
    • Your backend gets response from the apple.
    • Your backend sends result back to your app is receipt valid or invalid.

In both ways you will get list of in-app purchases. It will contain expired subscriptions as well. You would need to go through all subscriptions and check expiration dates. If it is still valid you must grant user with subscription.

As I understand you are using SwiftyStoreKit and here is open task for local receipt validation.

like image 149
Ramis Avatar answered Oct 19 '22 19:10

Ramis


You can check with this function. its works with swift4

func receiptValidation() {
let SUBSCRIPTION_SECRET = "yourpasswordift"
let receiptPath = Bundle.main.appStoreReceiptURL?.path
if FileManager.default.fileExists(atPath: receiptPath!){
    var receiptData:NSData?
    do{
        receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
    }
    catch{
        print("ERROR: " + error.localizedDescription)
    }
    //let receiptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)

    print(base64encodedReceipt!)


    let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]

    guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
    do {
        let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)
        let validationURLString = "https://sandbox.itunes.apple.com/verifyReceipt"  // this works but as noted above it's best to use your own trusted server
        guard let validationURL = URL(string: validationURLString) else { print("the validation url could not be created, unlikely error"); return }
        let session = URLSession(configuration: URLSessionConfiguration.default)
        var request = URLRequest(url: validationURL)
        request.httpMethod = "POST"
        request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
        let task = session.uploadTask(with: request, from: requestData) { (data, response, error) in
            if let data = data , error == nil {
                do {
                    let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
                    print("success. here is the json representation of the app receipt: \(appReceiptJSON)")
                    // if you are using your server this will be a json representation of whatever your server provided
                } catch let error as NSError {
                    print("json serialization failed with error: \(error)")
                }
            } else {
                print("the upload task returned an error: \(error)")
            }
        }
        task.resume()
    } catch let error as NSError {
        print("json serialization failed with error: \(error)")
    }



}
}
like image 20
Yasin Aktimur Avatar answered Oct 19 '22 19:10

Yasin Aktimur


I wanted to provide an alternative solution that uses the RevenueCat SDK for those who still stumble upon this question.

AppDelegate.swift

Configure the RevenueCat Purchases SDK with your api key an optional user identifier.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    Purchases.configure(withAPIKey: "<...>", appUserID: "<...>")

    ...

    return true
}

Subscription status function

The function below checks the PurchaserInfo to see if the user still has an active "entitlement" (or you can check for an active product ID directly).

func subscriptionStatus(completion: @escaping (Bool)-> Void) {
    Purchases.shared.purchaserInfo { (info, error) in

        // Check if the purchaserInfo contains the pro feature ID you configured
        completion(info?.activeEntitlements.contains("pro_feature_ID") ?? false)

        // Alternatively, you can directly check if there is a specific product ID
        // that is active.
        // completion(info?.activeSubscriptions.contains("product_ID") ?? false)
    }
}

Getting subscription status

You can call the above function as often as needed, since the result is cached by the Purchases SDK it will return synchronously in most cases and not require a network request.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    subscriptionStatus { (subscribed) in
        if subscribed {
            // Show that great pro content
        }
    }
}

If you're using SwiftyStoreKit, the RevenueCat syntax is fairly similar and there is a migration guide available to help switch over.

like image 3
enc_life Avatar answered Oct 19 '22 18:10

enc_life