Error with IAP and player prefs on Android

Hello everybody,

If my in app purchase is bought, i put a player pref to 1 and i know that the player already bought it. So the next time he want to go to the second part of the game (which is unlocked with the IAP), the game don’t ask to bought it and the player can access to the second part immediately.

So, i ask if the item has a receipt, if it has i put the player pref at 1. But i don’t success to do it with a lot of tries.
My last try is the next one, i used Sample IAP Project.

If i want to test it with an open test on Google play Console, i get this message “You already ownm this item” at eaxh time but the player pref is not changed cause i can’t access to the second part, the game propose to bought this item again and again.

Can you help me?

Thank you

public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
MyDebug("OnInitialized: PASS");
m_StoreController = controller;
m_StoreExtensionProvider = extensions;
m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
m_GoogleExtensions = extensions.GetExtension<IGooglePlayStoreExtensions>();
m_GoogleExtensions?.SetDeferredPurchaseListener(OnPurchaseDeferred);
Dictionary<string, string> dict = m_AppleExtensions.GetIntroductoryPriceDictionary();
foreach (UnityEngine.Purchasing.Product item in controller.products.all)
{
if (item.definition.id == part2Unlock)
{
if (item.hasReceipt == true)
{
PlayerPrefs.SetInt("part2Unlocked", 1);
part2Purchase = PlayerPrefs.GetInt("part2Unlocked");
}
// set the price text for the app upgrad
break;
}
}
}

How are you debugging? I don’t see any Debug.Log statements. You should have them at the beginning of the foreach and before and after every if statement. Is this a consumable or non-consumable product? Please provide specific steps to reproduce. Since you can reproduce, go through the steps yourself and provide the adb logcat output which will contain your Debug.Log output too. Also include your updated code with the Debug.Log statements so we can correlate the logs. The Sample IAP Project actually writes the debug output to the screen, may I ask why you removed that? It’s much easier than capturing the logs during development. Before you release your game, then remove the debug UI. https://discussions.unity.com/t/699654

Ok thank you for your answer, i was unfamiliar with this method.

Is it useful to post the results of the device logs? If not i will add Dedub.Log and create a release on the google play console which it take few days to be accept. I will post the results.

Yes, post the logs you have. And no, don’t create a new release. If you’ve already been testing, then just directly sideload the updated APK file directly to your phone via USB. Or just Build and Run from Unity after you’ve added the additional Debug.Log statements.

Ok so i changed my script like this:

public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
//MyDebug("OnInitialized: PASS");
Debug.Log("MESSAGE: OnInitialized: PASS");
m_StoreController = controller;
m_StoreExtensionProvider = extensions;
m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
m_GoogleExtensions = extensions.GetExtension<IGooglePlayStoreExtensions>();
m_GoogleExtensions?.SetDeferredPurchaseListener(OnPurchaseDeferred);
Dictionary<string, string> dict = m_AppleExtensions.GetIntroductoryPriceDictionary();
foreach (UnityEngine.Purchasing.Product item in controller.products.all)
{
Debug.Log("MESSAGE: for each START");

if (item.definition.id == part2Unlock)
{
Debug.Log("MESSAGE: item = part2unlock");
if (item.hasReceipt == true)
{
Debug.Log("MESSAGE: has receipt = true");
PlayerPrefs.SetInt("part2Unlocked", 1);
part2Purchase = PlayerPrefs.GetInt("part2Unlocked");
}

break;
}
}
}

And none of this Debug are visibles, but there are visibles in the unity console.

If you are saying you used “adb logcat” on an Android device and didn’t see these logs, that means this code didn’t run. Add additional debug statements to determine what code does run.

Yes “OnInitialized” is not called

On adb logcat i get only this:

07-19 19:38:18.540 24613 29811 I Unity : Starting Initialized…

07-19 19:38:18.540 24613 29811 I Unity : Part2script:InitializePurchasing()

07-19 19:38:18.540 24613 29811 I Unity : Part2script:Start()

Who means that this code is correctly run but nothing else of my IAP script:

public void InitializePurchasing()
    {
        if (IsInitialized())
        {
            return;
        }

        var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

        builder.AddProduct(part2Unlock, ProductType.NonConsumable);

        MyDebug("Starting Initialized...");
        UnityPurchasing.Initialize(this, builder);

    }

Did i miss something to call “OnInitialized” on my device?

Thank you again

Sorry I don’t quite follow. Your original post mentioned you were able to make purchases but had trouble saving to PlayerPrefs. Now initialization is not working at all? Have you published to the store in a test track? Are you a registered as a tester?

The game is avalaible on the store in production since december. It’s my first game. I used iap codeless at first and it worked good if the player want to bought it. But if he delete the app and install it again, he can’t access to the second part cause the player pref is 0, so he try to bought it again and he get this message “You already own this item”. He can’t change the player prefs and access to the second part in any way.

So i started to use Sample IAP Project to check the receipt with OnInitialized. But i can see that OnIntialized is not call so i can’t check the receipt. Yes i’m registered as a tester.

You created a new project and created a new application on Google developer dashboard, correct? Are you downloading from Google Play as a tester on your device after accepting the tester invite?

The game is avalaible on the google play store. I have some feedback from customers who can’t access to the second part (which is locked by an iap) if they delete the game or change their phones. I tried and it’s true. I want to upgrade my game to solve this iap issue.

So on the same project, the same app and with the same IAP. I need to access to the receipts, but i didn’t success with the codeless IAP, so i use the exemple project “Sample IAP Project” to try. The problem is the same.

Now we know that OnInitialized is not call.

Yes i download each new version of my game (i tried lot of different build with to solve this iap issue) with open test link given by google play console.

Thank you for your help

Yes, I understand the issue that you are trying to solve. I had suggested that you create an entirely new app on Google Play with the Sample IAP Project, separate from your game. This way, you can test new features privately and without breaking your existing game. When you have done this, send me a private message and I’ll provide my tester email address and I’ll take a look too.

Ok thank you i try this but i get another troubles that i have to deal with this new project, for example i don’t success to enter the google key of the new iap in the player settings unity iap, i have an error message. I try to turn off analytics, unlog my account, do the updates, close unity, and it didn’t change anything.

I did a perfect copy cut of the sample IAP project in my app, the iap id it’s the only changement. I do this to see if On initialized is call but no, i have a SKU error in the device log.

Do you want to test the app with this modifications?

No, not a “perfect copy”, I meant a brand new app. Once you do that, I would be happy to test. You don’t need the Google key that you mention for testing, just bypass that step. And I would not know which SKU is throwing the error since you didn’t share that information.