how do i check if the user canceled or expired

i use playerprefs to set the complete state, how do i check if the user canceled or expired like

info.nonsubscription(). so I can reset the PlayerPrefs back. thanks

public void OnPurchaseComplete(Product product)

{

if (product.definition.id == id)

{
PlayerPrefs.SetInt(“iap”, 1);

}

}

You can check for Product.hasReceipt. For subscriptions and if it still has a receipt, you can use SubscriptionManager and call getSubscriptionInfo https://docs.unity3d.com/Packages/com.unity.purchasing@4.4/api/UnityEngine.Purchasing.SubscriptionManager.html You can also look at the Subscription examples that are shipped with IAP in Package Manager.

thank。

and I use this code below which is your simple iap project. but the. product.hasReceipt is no show(apple device) or still false.
why??

async void Start()

{
InitializePurchasing();

UnityEngine.Purchasing.Product product = m_StoreController.products.WithID(“1”);

myText2.text = “1” + product.hasReceipt.ToString() ;
}

Are you testing on TestFlight on your iPhone/iPad and have made a successful purchase? And your productID is actually just “1”? Please show a screenshot of your Apple dashboard showing this product as available for sale. Ensure to follow the steps here https://docs.unity3d.com/Manual/UnityIAPAppleConfiguration.html

“1” is my product, and i test this in sandbox user , and i successful purchase, it need to test in testfight?

Screenshot please. And yes. Did you make a successful purchase?

ok, i found the problem, if I put it in the start it’s not showing.
but i use a button to exculte the product.hasReceipt it show. i don’ know why…

@nathanlin You need to wait until IAP finishes initializing, so in OnInitialized or later. I saw your logs for a second and it shows “DuplicateTransaction” so you’ve had a purchase already. If possible, please answer all questions directly and provide the requested screenshots. If the user actively cancels the purchase, then OnPurchaseFailed will be called with reason code “User Cancelled”

Hi. I use to delete the app and switch to another sandbox account, but the hasReceipt product I bought still return true.
For subscriptions is product.hasrecipe no suit for check if the user canceled or expired ?

If the new user bought the product then hasReceipt should be true. Is the product actually canceled or expired? Which one? If this is a subscription, you’ll want to use the SubscriptionManager, as mentioned earlier. Please show the code you are currently using to check the receipt.

What I mean is that hasReceipt still returns true if I switch to another account on this phone or reinstall the app without the subscription being cancelled or expired.

Is this normal?

IEnumerator waiuntilInit()
    {
         InitializePurchasing();
        yield return new WaitForSeconds(1.0f);
        UnityEngine.Purchasing.Product product = m_StoreController.products.WithID("productID");

        if (product != null)
        {
            if (product.hasReceipt )
            {
                Iapmanger.singleton.payEd();
            }
            else
            {
                Iapmanger.singleton.noPay();
            }
        }
        else
        {
            Iapmanger.singleton.noPay();
        }
    }

How are you switching accounts? Is ProcessPurchase being triggered during IAP initialization? That would mean the product is restored. What does SubscriptionManager say about the product status? https://docs.unity3d.com/Manual/UnityIAPRestoringTransactions.html And you shouldn’t be using waitforseconds, instead you should use the IsInitialized method in the Sample project https://discussions.unity.com/t/700293/6

To really get the current subscription state at all times, you would need to use receipt validation and receive App Store notifications for cancel/expire/refund events. Look for App Store notifications and Google RTDN.

Small advertising note: I offer a serverless solution including an Unity example project:

i switched the sandbox tester account in AppStore,then test the hasRecipes it’s still be true.

like he say it need to use receipt validation and receive App Store notifications for cancel/expire/refund events…

and “use the IsInitialized method” like this?

IEnumerator waiuntilInit()
{
   Initialize();
   yield return new WaitUntil( () => IsInitialized());
   dosomeThing();
}

I don’t know where you get these methods from, but you should really use the OnInitialized callback from Unity IAP, not write the methods below each other like that, and do not use a WaitForSeconds coroutine.

thanks. i. get this from the Sample project https://discussions.unity.com/t/700293/6

No, the sample project does not use waitforseconds. Please use SubscriptionManager. Test with a new subscription and confirm hasReceipt = false.

HI, i use the sample-iap-project, and i have test everything ok in appstore ,

but in the google store internal testing, I write the correct product ID and add the tester in license testing. but the paying page does not show up when I click the purchase button.

do you know why?

8477507--1126883--QQ截图20220930012506.png

If this is a subscription, it must be auto-renewing. If you are using the Sample IAP Project, the Debug.Log output is written to the UI, very handy for debugging. Please share any errors you might be seeing. Otherwise, please provide the device logs (which will include your Debug.Log statements) https://discussions.unity.com/t/699654 .

You are aware that it’s trivial for a user to modify the contents of PlayerPrefs, right?