- Home /
UnityIAP not restoring purchases on Android
Hey!
We have tried for a long time to get Unity restoring purchases after the player has uninstalled and resinstalled the game. I've added a debug.log into process purchases and it's not even ran after you reinstall the game. I have tracked it all down using device monitor on an Android phone. You can't however buy the same item again after you have already bought it and reinstalled the game, since it is non-consumable.
Any help would be appreciated!
Edit: Added purchaser script
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
// Placing the Purchaser class in the CompleteProject namespace allows it to interact with ScoreManager,
// one of the existing Survival Shooter scripts.
// Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
using SandeepMattepu.Android;
using CodeStage.AntiCheat.ObscuredTypes;
using SandeepMattepu.Testing;
public class Purchaser : MonoBehaviour, IStoreListener
{
private static IStoreController m_StoreController; // The Unity Purchasing system.
private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
public CreateProducts Products;
private List<Product> AllProducts;
void Start()
{
// If we haven't set up the Unity Purchasing reference
if (m_StoreController == null)
{
// Begin to configure our connection to Purchasing
InitializePurchasing();
}
}
public void InitializePurchasing()
{
// If we have already connected to Purchasing ...
if (IsInitialized())
{
// ... we are done here.
return;
}
// Create a builder, first passing in a suite of Unity provided stores.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
/*AllProducts = Products.GetAllProducts();
foreach (Product product in AllProducts)
{
product.productType = getProductType (product.productID);
builder.AddProduct(product.productID, product.productType);
}*/
// Add a product to sell / restore by way of its identifier, associating the general identifier
// with its store-specific identifiers.
//builder.AddProduct("com.jultar.products.blonde", ProductType.Consumable);
// Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
// and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
builder.AddProduct("com.jultar.products.breathore", ProductType.NonConsumable);
builder.AddProduct("com.jultar.products.100c", ProductType.Consumable);
builder.AddProduct("com.jultar.products.laser.blue", ProductType.NonConsumable);
builder.AddProduct("com.jultar.products.operative", ProductType.NonConsumable);
builder.AddProduct("com.jultar.products.pilotar", ProductType.NonConsumable);
builder.AddProduct("com.jultar.products.1000c", ProductType.Consumable);
builder.AddProduct("com.jultar.products.1500c", ProductType.Consumable);
builder.AddProduct("com.jultar.products.250c", ProductType.Consumable);
builder.AddProduct("com.jultar.products.500c", ProductType.Consumable);
builder.AddProduct("com.jultar.products.laser.green", ProductType.NonConsumable);
builder.AddProduct("com.jultar.products.maskera", ProductType.NonConsumable);
builder.AddProduct("com.jultar.products.laser.red", ProductType.NonConsumable);
builder.AddProduct("com.jultar.products.tstactical", ProductType.NonConsumable);
UnityPurchasing.Initialize(this, builder);
}
private ProductType getProductType(string id)
{
switch(id)
{
case "com.jultar.products.2xexp":
return ProductType.Subscription;
case "com.jultar.products.5xexp":
return ProductType.Subscription;
case "com.jultar.products.8xexp":
return ProductType.Subscription;
case "com.jultar.products.1000c":
return ProductType.Consumable;
case "com.jultar.products.1500c":
return ProductType.Consumable;
case "com.jultar.products.100c":
return ProductType.Consumable;
case "com.jultar.products.250c":
return ProductType.Consumable;
case "com.jultar.products.500c":
return ProductType.Consumable;
}
return ProductType.NonConsumable;
}
private bool IsInitialized()
{
// Only say we are initialized if both the Purchasing references are set.
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void BuyItem(string ProductID)
{
// Buy the consumable product using its general identifier. Expect a response either
// through ProcessPurchase or OnPurchaseFailed asynchronously.
BuyProductID(ProductID);
}
void BuyProductID(string productId)
{
// If Purchasing has been initialized ...
if (IsInitialized())
{
// ... look up the Product reference with the general product identifier and the Purchasing
// system's products collection.
UnityEngine.Purchasing.Product product = m_StoreController.products.WithID(productId);
// If the look up found a product for this device's store and that product is ready to be sold ...
if (product != null && product.availableToPurchase)
{
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
// asynchronously.
m_StoreController.InitiatePurchase(product);
}
// Otherwise ...
else if (product == null)
{
Debug.Log("BuyProductID: FAIL. NULL. Not purchasing product, either is not found or is not available for purchase");
}
else if (product.availableToPurchase == false)
{
// ... report the product look-up failure situation
Debug.Log("BuyProductID: FAIL. NOT AVAILABLE. Not purchasing product, either is not found or is not available for purchase");
}
else
{
Debug.Log("BuyProductID: FAIL. ELSE. Not purchasing product, either is not found or is not available for purchase");
}
}
// Otherwise ...
else
{
// ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
// retrying initiailization.
Debug.Log("BuyProductID FAIL. Not initialized.");
//PopupManager.Instance.ShowAlert("Initialization error", "Are you sure you're connected to the internet?");
}
}
// Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
// Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
public void RestorePurchases()
{
// If Purchasing has not yet been set up ...
if (!IsInitialized())
{
// ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
Debug.Log("RestorePurchases FAIL. Not initialized.");
return;
}
// If we are running on an Apple device ...
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer)
{
// ... begin restoring purchases
Debug.Log("RestorePurchases started ...");
// Fetch the Apple store-specific subsystem.
var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
// Begin the asynchronous process of restoring purchases. Expect a confirmation response in
// the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
apple.RestoreTransactions((result) =>
{
// The first phase of restoration. If no more responses are received on ProcessPurchase then
// no purchases are available to be restored.
Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
});
}
// Otherwise ...
else
{
// We are not running on an Apple device. No work is necessary to restore purchases.
Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
}
}
//
// --- IStoreListener
//
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
// Purchasing has succeeded initializing. Collect our Purchasing references.
Debug.Log("OnInitialized: PASS");
// Overall Purchasing system, configured with products for this application.
m_StoreController = controller;
// Store specific subsystem, for accessing device-specific store features.
m_StoreExtensionProvider = extensions;
UpdateSubscriptions ();
}
public void OnInitializeFailed(InitializationFailureReason error)
{
// Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
SMCustomDebug.showDebugMessage ("Processing purchase/Restoring purchase");
if(SMPlayerDataManager.PlayerPurchasedProducts == null)
{
SMCustomDebug.showDebugMessage ("Can't restore/purchase because of the PlayerPurchasedProduct is null");
}
ObscuredBool nonConsumablePurchased = false;
#region Crows Bought
// 1000 C bought
if(args.purchasedProduct.definition.id == "com.jultar.products.1000c")
{
SMPlayerDataManager.PlayerData.numberOfCrowns += 1000;
SMPlayerDataManager.forceSaveData();
}
// 1500 C bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.1500c")
{
SMPlayerDataManager.PlayerData.numberOfCrowns += 1500;
SMPlayerDataManager.forceSaveData();
}
// 100 C bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.100c")
{
SMPlayerDataManager.PlayerData.numberOfCrowns += 100;
SMPlayerDataManager.forceSaveData();
}
// 250 C bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.250c")
{
SMPlayerDataManager.PlayerData.numberOfCrowns += 250;
SMPlayerDataManager.forceSaveData();
}
// 500 C bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.500c")
{
SMPlayerDataManager.PlayerData.numberOfCrowns += 500;
SMPlayerDataManager.forceSaveData();
}
#endregion
#region Laser Bought
// Blue laser bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.laser.blue")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("Blue laser purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseLaser(SandeepMattepu.Laser_Type.BLUE_LASER);
}
// Red laser bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.laser.red")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("Red laser purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseLaser(SandeepMattepu.Laser_Type.RED_LASER);
}
// Green laser bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.laser.green")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("Green laser purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseLaser(SandeepMattepu.Laser_Type.GREEN_LASER);
}
#endregion
#region Helmet bought
// Breathor bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.breathore")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("Breathore helmet purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseHelmet(SandeepMattepu.Helmet_Type.BREATHOR);
}
// Maskera bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.maskera")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("Maskera helmet purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseHelmet(SandeepMattepu.Helmet_Type.MASKERA);
}
// Operative bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.operative")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("Operative helmet purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseHelmet(SandeepMattepu.Helmet_Type.OPERATIVE);
}
// Pilotar bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.pilotar")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("Pilotar helmet purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseHelmet(SandeepMattepu.Helmet_Type.PILOTAR);
}
// TS Tactical bought
else if (args.purchasedProduct.definition.id == "com.jultar.products.tstactical")
{
nonConsumablePurchased = true;
SMCustomDebug.showDebugMessage ("TS tactical helmet purchased");
SMPlayerDataManager.PlayerPurchasedProducts.purchaseHelmet(SandeepMattepu.Helmet_Type.TS_TACTICAL);
}
#endregion
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
if(nonConsumablePurchased)
{
SMPlayerDataManager.saveNonConsumablePurchased ();
}
// Return a flag indicating whether this product has completely been received, or if the application needs
// to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
// saving purchased products to the cloud, and when that save is delayed. fffffffffffffffffffffffffffffffffffff Kaarel can we build apk? check chae ...... keep working okay
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed(UnityEngine.Purchasing.Product product, PurchaseFailureReason failureReason)
{
// A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
// this reason with the user to guide their troubleshooting actions.
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
public void UpdateSubscriptions()
{
if (m_StoreController.products.WithID ("com.jultar.products.2xexp").hasReceipt)
{
SMPlayerDataManager.PlayerPurchasedProducts.purchasedXp (Boost.TwoTimesExp);
}
if (m_StoreController.products.WithID ("com.jultar.products.5xexp").hasReceipt)
{
SMPlayerDataManager.PlayerPurchasedProducts.purchasedXp (Boost.FiveTimesExp);
}
if (m_StoreController.products.WithID ("com.jultar.products.8xexp").hasReceipt)
{
SMPlayerDataManager.PlayerPurchasedProducts.purchasedXp (Boost.EightTimesExp);
}
}
}
Answer by tungnguyendev · Apr 26, 2018 at 07:26 AM
I got same problem because I use latest IAP plugin ver 1.18 , i think that is bug from plugin, try to use old IAP plugin.
Your answer
Follow this Question
Related Questions
IAP Restore purchase on Android "Already Recorded Transaction" 0 Answers
Android IAP: Restore non-consumable purchase after clearing app data 0 Answers
(No) In-App purchases 0 Answers
Apk Build size increase after upgrade to unity 5 1 Answer
Unity IAP - Google Play License Key,Unity IAP Initialization - Android Public key 0 Answers