Tuesday, August 16, 2022
HomeGame Developmentin app buy - unity IAP scripting not initialized on android

in app buy – unity IAP scripting not initialized on android


I’m attempting so as to add a In App Buy in unity for android however after I push the button that launch the code I get this error: BuyProductID FAIL. Not initialized.

There is no such thing as a extra error on logcat console. I’ve a closed check revealed on play console and set on IAP unity service.

the method get the InitializePurchasing methodology however not the OnInitialized or OnInitializedFailed strategies.

right here is my code:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.Buying;
utilizing System;

public class IAPScript : MonoBehaviour, IStoreListener
{
public static IAPScript occasion;
personal static IStoreController m_StoreController;         
personal static IExtensionProvider m_StoreExtensionProvider; 


///  HERE THE STRINGS. NO CAPS

public static string buy50lifes = "50lifes";

void Begin()
{
    // If we've not arrange the Unity Buying reference
    if (m_StoreController == null)
    {
        // Start to configure our connection to Buying
        InitializePurchasing();
    }
}

personal void Awake()
{
if (occasion == null)
{
  occasion = this;
}
else
{
  Destroy(this);
}
}

public void InitializePurchasing() 
{
    // If we have now already linked to Buying ...
    if (IsInitialized())
    {
        // ... we're accomplished right here.
        return;
    }

    

    // Create a builder, first passing in a collection of Unity supplied shops.
    var builder = ConfigurationBuilder.Occasion(StandardPurchasingModule.Occasion());

    // Add a product to promote / restore by means of its identifier, associating the overall identifier
    // with its store-specific identifiers.
    builder.AddProduct(buy50lifes, ProductType.Consumable);     ///PUT PRODUCTS HERE <<<<<<<<<<<<<<<<<<<
    // Proceed including the non-consumable product.
    //builder.AddProduct(, ProductType.NonConsumable);

    // Kick off the rest of the set-up with an asynchrounous name, passing the configuration 
    // and this class' occasion. Anticipate a response both in OnInitialized or OnInitializeFailed.
    UnityPurchasing.Initialize(this, builder);
}


public bool IsInitialized()
{
    // Solely say we're initialized if each the Buying references are set.
    return m_StoreController != null && m_StoreExtensionProvider != null;
}


public void BuyConsumable() ///CREATE VOID FOR BUYING PRODUCTS<<<<<<<<<<<<<<<<
{
    BuyProductID(buy50lifes);
}

public String GetProducePriceFromStore(string id)
{
    if(m_StoreController!=null&& m_StoreController.merchandise!=null)
        return m_StoreController.merchandise.WithID(id).metadata.localizedPriceString;
    else 
        return "";
}


void BuyProductID(string productId)
{
    // If Buying has been initialized ...
    if (IsInitialized())
    {
        // ... search for the Product reference with the overall product identifier and the Buying 
        // system's merchandise assortment.
        Product product = m_StoreController.merchandise.WithID(productId);

        // If the search for discovered a product for this machine's retailer and that product is able to be offered ... 
        if (product != null && product.availableToPurchase)
        {
            Debug.Log(string.Format("Buying product asychronously: '{0}'", product.definition.id));
            // ... purchase the product. Anticipate a response both by way of ProcessPurchase or OnPurchaseFailed 
            // asynchronously.
            m_StoreController.InitiatePurchase(product);
        }
        // In any other case ...
        else
        {
            // ... report the product look-up failure scenario  
            Debug.Log("BuyProductID: FAIL. Not buying product, both isn't discovered or isn't out there for buy");
        }
    }
    // In any other case ...
    else
    {
        // ... report the very fact Buying has not succeeded initializing but. Contemplate ready longer or 
        // retrying initiailization.
        Debug.Log("BuyProductID FAIL. Not initialized.");
    }
}


// Restore purchases beforehand made by this buyer. Some platforms routinely restore purchases, like Google. 
// Apple at present requires express buy restoration for IAP, conditionally displaying a password immediate.
public void RestorePurchases()
{
    // If Buying has not but been arrange ...
    if (!IsInitialized())
    {
        // ... report the scenario and cease restoring. Contemplate both ready longer, or retrying initialization.
        Debug.Log("RestorePurchases FAIL. Not initialized.");
        return;
    }

    // If we're operating on an Apple machine ... 
    if (Utility.platform == RuntimePlatform.IPhonePlayer || 
        Utility.platform == RuntimePlatform.OSXPlayer)
    {
        // ... start restoring purchases
        Debug.Log("RestorePurchases began ...");

        // Fetch the Apple store-specific subsystem.
        var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
        // Start the asynchronous strategy of restoring purchases. Anticipate a affirmation response in 
        // the Motion<bool> under, and ProcessPurchase if there are beforehand bought merchandise to revive.
        apple.RestoreTransactions((end result) => {
            // The primary part of restoration. If no extra responses are obtained on ProcessPurchase then 
            // no purchases can be found to be restored.
            Debug.Log("RestorePurchases persevering with: " + end result + ". If no additional messages, no purchases out there to revive.");
        });
    }
    // In any other case ...
    else
    {
        // We aren't operating on an Apple machine. No work is critical to revive purchases.
        Debug.Log("RestorePurchases FAIL. Not supported on this platform. Present = " + Utility.platform);
    }
}


//  
// --- IStoreListener
//

public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
    // Buying has succeeded initializing. Acquire our Buying references.
    Debug.Log("OnInitialized: PASS");

    // General Buying system, configured with merchandise for this software.
    m_StoreController = controller;
    // Retailer particular subsystem, for accessing device-specific retailer options.
    m_StoreExtensionProvider = extensions;
}


public void OnInitializeFailed(InitializationFailureReason error)
{
    // Buying set-up has not succeeded. Examine error for motive. Contemplate sharing this motive with the consumer.
    Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
}


public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) 
{
// A consumable product has been bought by this consumer.
if (String.Equals(args.purchasedProduct.definition.id, buy50lifes, StringComparison.Ordinal))                                 ///PRODUCTS<<<<<<<<<<<<<<<<<<
{
    Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));


}
else
{
    Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}

    // Return a flag indicating whether or not this product has utterly been obtained, or if the appliance wants 
    // to be reminded of this buy at subsequent app launch. Use PurchaseProcessingResult.Pending when nonetheless 
    // saving bought merchandise to the cloud, and when that save is delayed. 
    return PurchaseProcessingResult.Full;
}


public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
    // A product buy try didn't succeed. Examine failureReason for extra element. Contemplate sharing 
    // this motive with the consumer to information their troubleshooting actions.
    Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments