Check If non-consumable is still valid at start-up

Hello,
I’m trying to implement a non-consumable receipt validation at the start-up of the game, to check if the non-consumable is still valid. By valid, I mean the user still has access the content and the entitlement is still active. This is for Google Play only.

From the Debug.Logs (“Purchase 1” to “Purchase 8”), I’ve realized that, no matter if non-consumable is still valid or not, CrossPlatformValidator returns the IAPSecurityException.
If the non-consumable is still valid, there is also an “Already recorded transaction” log from Unity.

Can you help how to solve this?
thanks,

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        Debug.Log("OnInitialized: PASS");
        m_StoreController = controller;
        m_StoreExtensionProvider = extensions;
        CheckIfOwned();
    }

    void CheckIfOwned()
    {
        Debug.Log("Purchase 1");
        foreach (var item in m_StoreController.products.all)
        {
            var validator = new CrossPlatformValidator(GooglePlayTangle.Data(),
                                           AppleTangle.Data(),
                                           Application.identifier);
            Debug.Log("Purchase 2");
            try
            {
                Debug.Log("Purchase 3");
                // On Google Play, result has a single product ID.
                // On Apple stores, receipts contain multiple products.
                var result = validator.Validate(item.receipt);
                // For informational purposes, we list the receipt(s)
                //Debug.Log("CustomMsg: Receipt is valid. Contents:");
                Debug.Log("Purchase 4");
                foreach (IPurchaseReceipt productReceipt in result)
                {
                    ////Debug.Log("CustomMsg: " + productReceipt.productID);
                    ////Debug.Log("CustomMsg: " + productReceipt.purchaseDate);
                    ////Debug.Log("CustomMsg: " + productReceipt.transactionID);
                    Debug.Log("Purchase 5");
                    if (String.Equals(productReceipt.productID, removeAds, StringComparison.Ordinal))
                    {
                        RemoveAdsValid(true);
                        Debug.Log("Purchase 6");
                    }
                    else
                    {
                        RemoveAdsValid(false);
                        Debug.Log("Purchase 7");

                    }
                }
            }
            catch (IAPSecurityException)
            {
                Debug.Log("Purchase 8");
                //Debug.Log("CustomMsg: Invalid receipt, not unlocking content");
                //validPurchase = false;
                //DefaultConfiguration();
            }

        }

    }

In your loop, you need to check if the item.receipt is not null first.

I’ve added that, and as expected, it says item.receipt is null. But why is it null? There’s only one product, and that’s purchased. So shouldn’t it return the receipt?

What type of product? Non-consumable receipts should be non-null. Ensure you are using IAP 2.2.6

It’s Non-consumable.
IAP version is 2.2.2. I’ll update it and let you know if it is solved.

Make sure to create a full project backup before upgrading.

I did, thanks for reminding.
I also remembered why I don’t have the latest version. I couldn’t manage to update it and I just gave up as 2.2.2 was working for me anyway.

I gave it another try.
In the package manager, I don’t see any update (2.2.2 looks as if it’s the latest version)
6763771--781189--upload_2021-1-25_18-29-15.png

In Window-Unity IAP- IAP Updates,
I see that there’s an update. When I click on Install, nothing happens…

6763771--781192--upload_2021-1-25_18-30-33.png

Help, please :slight_smile:

The Install button should start the installation process. Ensure you don’t have any compiler messages, your project needs to be error free first. Since you are working with a backup now, you can remove IAP first as an option too. Just delete it from /Assets/Plugins/UnityPurchasing. After you delete it, ensure your game still compiles. You’ll need to comment out or remove any code causing errors. Then try the update again. Also, compare to a new empty project, and install 2.2.6 to confirm. What Editor version are you using?

I had to delete /Assets/Plugins/UnityPurchasing folder than install 2.2.6 .
The script seems to be working OK now but I have to test a bit more to be sure.
Thanks for the help.

1 Like