How do I check if a user is still subscribed with IAP Unity?

I have already done the code for the user to pay for a subscription but I want to check if the user is still subscribed in another script and another scene. How can I do this?

I have tried Subscription Product support. But, I can’t seem to understand how to use it after the transaction has already happened in another scene.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;

public class checkSubscription : SubscriptionManager {
    // Start is called before the first frame update
    void Start(){
        SubscriptionInfo("product_ID");
    }

    void SubscriptionInfo(string productId) {
        bool x = isSubscribed();
    }
}

Hello,

You can find an example of this in the Unity IAP Sample: 02 Buying Subscription
This is an extract from that sample:

Once IAP has been initialized, you will need to obtain the product from your store controller:

Product subscriptionProduct = IStoreController.products.WithID(subscriptionProductId);

Then you will want to use the SubscriptionManager to get the SubscriptionInfo which has the isSubscribed function:

        bool IsSubscribedTo(Product subscription)
        {
            // If the product doesn't have a receipt, then it wasn't purchased and the user is therefore not subscribed.
            if (subscription.receipt == null)
            {
                return false;
            }

            //The intro_json parameter is optional and is only used for the App Store to get introductory information.
            var subscriptionManager = new SubscriptionManager(subscription, null);

            // The SubscriptionInfo contains all of the information about the subscription.
            // Find out more: https://docs.unity3d.com/Packages/com.unity.purchasing@3.1/manual/UnityIAPSubscriptionProducts.html
            var info = subscriptionManager.getSubscriptionInfo();

            return info.isSubscribed() == Result.True;
        }

@Yannick_D It is giving me the error that in IStoreController.products.WithID(subscriptionProductId); products does not exist.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;

public class checkSubscription : SubscriptionManager {

    private Product subscriptionProduct;

    public void Start() {
        subscriptionProduct = new IStoreController.products.WithID("com.corysparks.motivication.1yearSub");
    }

    public bool IsSubscribedTo(Product subscription) {
        // If the product doesn't have a receipt, then it wasn't purchased and the user is therefore not subscribed.
        if (subscription.receipt == null) {
            return false;
        }

        //The intro_json parameter is optional and is only used for the App Store to get introductory information.
        var subscriptionManager = new SubscriptionManager(subscription, null);

        // The SubscriptionInfo contains all of the information about the subscription.
        // Find out more: https://docs.unity3d.com/Packages/com.unity.purchasing@3.1/manual/UnityIAPSubscriptionProducts.html
        var info = subscriptionManager.getSubscriptionInfo();

        return info.isSubscribed() == Result.True;
    }
}

This is my code, it is also telling me that SubscriptionManager does not contain a constructor that takes 0 arguments.

You should replace the IStoreController with the one that was returned from the OnInitialized callback

Your checkSubscription class doesn’t seem to need to inherit from SubscriptionManager:

public class checkSubscription {

I use codeless IAP to initialize, does this make a difference?

@Yannick_D So I think I am very close, this is what I have

using UnityEngine;
using UnityEngine.Purchasing;

public class checkSubscription : MonoBehaviour, IStoreListener {

    private Product subscriptionProduct1;
    private Product subscriptionProduct2;
    private IStoreController controller;
    private IExtensionProvider extensions;

    string[] productList = { "product1", "product2" };

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions) {
        this.controller = controller;
        this.extensions = extensions;
    }

    public void Start() {
        subscriptionProduct1 = controller.products.WithID(productList[0]);
        subscriptionProduct2 = controller.products.WithID(productList[1]);

        if(IsSubscribedTo(subscriptionProduct1) == true || IsSubscribedTo(subscriptionProduct2) == true) {
            Debug.Log("subbed");
            PlayerPrefs.SetInt("IsSubbed", 1);
        } else {
            Debug.Log("no subbed");
            PlayerPrefs.SetInt("IsSubbed", 0);
        }
    }

    public bool IsSubscribedTo(Product subscription) {
        // If the product doesn't have a receipt, then it wasn't purchased and the user is therefore not subscribed.
        if (subscription.receipt == null) {
            return false;
        }

        //The intro_json parameter is optional and is only used for the App Store to get introductory information.
        var subscriptionManager = new SubscriptionManager(subscription, null);

        // The SubscriptionInfo contains all of the information about the subscription.
        var info = subscriptionManager.getSubscriptionInfo();

        return info.isSubscribed() == Result.True;
    }

    public void OnInitializeFailed(InitializationFailureReason error) {
        throw new System.NotImplementedException();
    }

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent) {
        throw new System.NotImplementedException();
    }

    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason) {
        throw new System.NotImplementedException();
    }
}

But I am getting the error ```
NullReferenceException: Object reference not set to an instance of an object
checkSubscription.Start () (at Assets/Scripts/checkSubscription.cs:19)

@corysparks You cannot access controller.products from Start(), you must wait after initialization in OnInitialized. Try moving your Start() code there.

@Baroni1 @Yannick_D

So I got most of it, I use Codeless IAP to create the products in the IAP Catalog & automatically initialize. But, when I go to Initialize it is logging Failed to Init: NoProductsAvailable to the console. When I have 2 products.

public void Start() {
        var module = StandardPurchasingModule.Instance();
        ConfigurationBuilder builder = ConfigurationBuilder.Instance(module);
        UnityPurchasing.Initialize(this, builder);
    }
public void OnInitializeFailed(InitializationFailureReason error) {
        Debug.Log("Failed to Init: " + error);
    }

@Baroni1 @Yannick_D

Never mind, figured it out. Thank you guys for your help!

public void OnInitialized(IStoreController controller, IExtensionProvider extensions) {
        Debug.Log("Initializing");
        this.controller = controller;
        this.extensions = extensions;

        subscriptionProduct1 = controller.products.WithID(productList[0]);
        subscriptionProduct2 = controller.products.WithID(productList[1]);

        if (IsSubscribedTo(subscriptionProduct1) == true || IsSubscribedTo(subscriptionProduct2) == true) {
            Debug.Log("subbed");
            PlayerPrefs.SetInt("IsSubbed", 1);
        } else {
            Debug.Log("not subbed");
            PlayerPrefs.SetInt("IsSubbed", 0);
        }
    }

    public void Start() {
        ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
        IAPConfigurationHelper.PopulateConfigurationBuilder(ref builder, ProductCatalog.LoadDefaultCatalog());
        Debug.Log(builder.products.Count);
        UnityPurchasing.Initialize(this, builder);
    }

This thread is now closed. Feel free to reach out via a new thread if you encounter further issues.
Thanks!