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();
}
}
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.
@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.
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);
}