Search Unity

Bug 'IAPManager' bug

Discussion in 'Unity IAP' started by egor_stratusfera_21, Mar 7, 2023.

  1. egor_stratusfera_21

    egor_stratusfera_21

    Joined:
    Mar 7, 2023
    Posts:
    3
    After I upgraded from Unity 2021 to 2022, I got this error, I already checked the package update, so what's the problem?

    IAPManager.cs(7,42): error CS0535: 'IAPManager' does not implement interface member 'IStoreListener.OnInitializeFailed(InitializationFailureReason, string?)'
     
  2. egor_stratusfera_21

    egor_stratusfera_21

    Joined:
    Mar 7, 2023
    Posts:
    3
    using System;
    using System.Threading.Tasks;
    using UnityEngine;
    using UnityEngine.Purchasing;
    using UnityEngine.UI;

    public class IAPManager : MonoBehaviour, IStoreListener
    {
    [SerializeField] private GameObject btnNoAds;
    [SerializeField] private GameObject btnVip;
    [SerializeField] private GameObject btnVip_afterBuy;
    [SerializeField] private GameObject vipBanner;

    IStoreController m_StoreController;

    private string noads = "com.raybelgames.popit.noads";
    private string vip = "com.raybelgames.popit.vip";

    void Start()
    {
    InitializePurchasing();

    //if (PlayerPrefs.HasKey("firstStart") == false)
    //{
    // PlayerPrefs.SetInt("firstStart", 1);
    // RestoreMyProduct();
    //}

    RestoreVariable();
    }

    void InitializePurchasing()
    {
    var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

    builder.AddProduct(noads, ProductType.NonConsumable);
    builder.AddProduct(vip, ProductType.NonConsumable);

    UnityPurchasing.Initialize(this, builder);
    }

    void RestoreVariable()
    {
    if (PlayerPrefs.HasKey("ads"))
    {
    btnNoAds.SetActive(false);
    }

    if (PlayerPrefs.HasKey("vip"))
    {
    btnVip.SetActive(false);
    btnVip_afterBuy.SetActive(true);
    }

    if (PlayerPrefs.HasKey("ads") && PlayerPrefs.HasKey("vip"))
    {
    vipBanner.SetActive(true);
    }
    }

    public void BuyProduct(string productName)
    {
    m_StoreController.InitiatePurchase(productName);
    }

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    {
    var product = args.purchasedProduct;

    if (product.definition.id == noads)
    {
    Product_NoAds();
    }

    if (product.definition.id == vip)
    {
    Product_VIP();
    }

    Debug.Log($"Purchase Complete - Product: {product.definition.id}");

    return PurchaseProcessingResult.Complete;
    }

    private void Product_NoAds()
    {
    PlayerPrefs.SetInt("ads", 0);
    btnNoAds.SetActive(false);

    if (PlayerPrefs.HasKey("vip"))
    vipBanner.SetActive(true);
    }

    private void Product_VIP()
    {
    PlayerPrefs.SetInt("vip", 0);
    btnVip.SetActive(false);
    btnVip_afterBuy.SetActive(true);

    if (PlayerPrefs.HasKey("ads"))
    vipBanner.SetActive(true);
    }

    public void OnInitializeFailed(InitializationFailureReason error)
    {
    Debug.Log($"In-App Purchasing initialize failed: {error}");
    }

    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    {
    Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
    }

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
    Debug.Log("In-App Purchasing successfully initialized");
    m_StoreController = controller;
    }


    //public void RestoreMyProduct()
    //{
    // if (CodelessIAPStoreListener.Instance.StoreController.products.WithID(noads).hasReceipt)
    // {
    // Product_NoAds();
    // }

    // if (CodelessIAPStoreListener.Instance.StoreController.products.WithID(vip).hasReceipt)
    // {
    // Product_VIP();
    // }
    //}
    }
     
  3. egor_stratusfera_21

    egor_stratusfera_21

    Joined:
    Mar 7, 2023
    Posts:
    3
    The problem was solved by a duplicate of the OnInitialize Failed method, but with the addition of "string message"
     
    BreynartStudios and ruiswang like this.
  4. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    how?
     
  5. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,263
    The most recent Unity IAP version implements some callbacks twice, even though marked as obsolete your IStoreListener class needs to implement all of them.

    Code (CSharp):
    1. void OnInitializeFailed(InitializationFailureReason error);
    2. void OnInitializeFailed(InitializationFailureReason error, string message);
    And if you are using IDetailedStoreListener:

    Code (CSharp):
    1. void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason);
    2. void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription);
     
  6. ruiswang

    ruiswang

    Joined:
    Feb 22, 2023
    Posts:
    6
    thank you! that helped!
     
  7. edinhohazard10chelsea

    edinhohazard10chelsea

    Joined:
    Jul 9, 2023
    Posts:
    4
    Please, Can someone who doesn't assume we are all coders and explains things like a teacher, come and explain his solution here for me to be able to solve this too?
    Like tell us where we are supposed to insert or change the ambiguous thing he has written for only those who know?


     
    edinventa likes this.
  8. edinhohazard10chelsea

    edinhohazard10chelsea

    Joined:
    Jul 9, 2023
    Posts:
    4
    Okay, i have been able to figure it out by watching a youtube video..
    But below is a well-explained, inclusive and much simple way of understanding the solution for people of all backgrounds reading this.

    When you get the problem anywhere in IAP(In-app purchases), do the following

    1. Click on the problem to open up your editor. (Mine is Visual studio. but yours can be different. All that matters is being able to edit the text in the script with the problem)

    2. Now search for where the method fails. Which can be found by copying and searching that script for these words.
    "OnInitializeFailed" ,
    You will realize you will find it looking something like this, in the brackets of highlighted in my picture below
    upload_2023-7-9_14-58-25.png



    3. Now all you are supposed to do, is copy and paste the same code again right under where the closed brackets of the code circled above ends.
    Then you will add the words i have added to where i have circled in below picture. dont forget the comma as a non-coder like me please --->> , string? message


    upload_2023-7-9_15-1-59.png




    This i believe, is a more inclusive answer. Whereby even 8 year olds watching this and facing the problem will understand.. Its very important that we make our explanations on unity community simple for people of all backgrounds, because the art of game-making really fascinates Children.. And you will realize that most adults who are CEOs and founders today in tech, had their "spark" to enter this field by first experimenting with games when they were kids.
    Therefore its imperative that we take time to explain stuff with visuals, so that children reading and trying to solve bugs in their game making assets as unity always cause, do not get discouraged or give up when they see the answers here looking like that of what adult coders with BSCs and MSCs share amongst themselves on stackoverflow.com which is not a community primarily for games like unity community is.

    Lets always try to keep it level for kids and non-coder adults like myself.

    Thank you all
     
    edinventa and moumou101 like this.
  9. moumou101

    moumou101

    Joined:
    Feb 20, 2018
    Posts:
    1
    You Are the Best :)
     
    edinhohazard10chelsea likes this.
  10. billybully

    billybully

    Joined:
    Sep 8, 2021
    Posts:
    1
    Thanks, sir.
    It looks silly but There is apparently no other way.
     
  11. Kazs99

    Kazs99

    Joined:
    Jan 25, 2013
    Posts:
    16
    Thanks, that works!