We have a game live on android.We keep getting reports that sometimes players are not getting their coins when purchasing off the store on android.Also google console and unity analytics showing nothing .Following is my script.
public class purchase : MonoBehaviour, IStoreListener
{
private static IStoreController m_StoreController; // The Unity Purchasing system.
private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
public static string trueseriaproduct_money = "trueseriaproduct_money";
public static string trueseriaproduct_research = "trueseriaproduct_research";
public static string trueseriaproduct_all = "trueseriaproduct_all";
public static string trueseriaproduct_2xspeed = "trueseriaproduct_2xspeed";
public string IAll;
public string IMoney;
public string I2x;
public string IRp;
public Text TIAll;
public Text TIMoney;
public Text TI2x;
public Text TIRp;
void Start()
{
if (m_StoreController == null)
{
InitializePurchasing();
}
}
public void InitializePurchasing()
{
if (IsInitialized())
{
return;
}
// Create a builder, first passing in a suite of Unity provided stores.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
// Add a product to sell / restore by way of its identifier, associating the general identifier
// with its store-specific identifiers.
builder.AddProduct(trueseriaproduct_money, ProductType.Consumable);
builder.AddProduct(trueseriaproduct_research, ProductType.Consumable);
// Continue adding the non-consumable product.
builder.AddProduct(trueseriaproduct_2xspeed, ProductType.NonConsumable);
builder.AddProduct(trueseriaproduct_all, ProductType.NonConsumable);
// 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.
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
// Only say we are initialized if both the Purchasing references are set.
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void BuyConsumableMoney()
{
BuyProductID(trueseriaproduct_money);
}
public void BuyConsumableResearch()
{
BuyProductID(trueseriaproduct_research);
}
public void BuyNonConsumableSpeed()
{
BuyProductID(trueseriaproduct_2xspeed);
}
public void BuyNonConsumableAll()
{
BuyProductID(trueseriaproduct_all);
}
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.
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
{
// ... report the product look-up failure situation
Debug.Log("BuyProductID: FAIL. 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.");
}
}
// 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);
}
}
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;
Debug.Log("Available items:");
foreach (var item in controller.products.all)
{
if (item.availableToPurchase)
{
Debug.Log(string.Join(" - ",
new[]
{
item.metadata.localizedTitle,
item.metadata.localizedDescription,
item.metadata.isoCurrencyCode,
item.metadata.localizedPrice.ToString(),
item.metadata.localizedPriceString,
item.transactionID,
item.receipt
}));
}
}
IAll = controller.products.WithID(trueseriaproduct_all).metadata.localizedPriceString;
IMoney = controller.products.WithID(trueseriaproduct_money).metadata.localizedPriceString;
I2x = controller.products.WithID(trueseriaproduct_2xspeed).metadata.localizedPriceString;
IRp = controller.products.WithID(trueseriaproduct_research).metadata.localizedPriceString;
TIAll.text=IAll;
TIMoney.text = IMoney;
TI2x.text = I2x;
TIRp.text = IRp;
}
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)
{
if (String.Equals(args.purchasedProduct.definition.id, trueseriaproduct_money, StringComparison.Ordinal))
{
print("Added money");
money.Amount += 100000000;
}
else if (String.Equals(args.purchasedProduct.definition.id, trueseriaproduct_research, StringComparison.Ordinal))
{
print("Added research point");
Game.ResearchPoint += 100000000;
}
else if (String.Equals(args.purchasedProduct.definition.id, trueseriaproduct_2xspeed, StringComparison.Ordinal))
{
//
print("Double Speed");
}
else if (String.Equals(args.purchasedProduct.definition.id, trueseriaproduct_all, StringComparison.Ordinal))
{
money.Amount += 100000000;
Game.ResearchPoint += 100000000;
fanatic.ft += 100000;
print("All");
}
else
{
print("FAILED");
}
return PurchaseProcessingResult.Complete;
}*/
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
{
bool validPurchase = true; // Presume valid for platforms with no R.V.
var validator = new CrossPlatformValidator(GooglePlayTangle.Data(),
AppleTangle.Data(), Application.bundleIdentifier);
try
{
var result = validator.Validate(e.purchasedProduct.receipt);
Debug.Log("Receipt is valid. Contents:");
foreach (IPurchaseReceipt productReceipt in result)
{
/* Debug.Log(productReceipt.productID);
Debug.Log(productReceipt.purchaseDate);
Debug.Log(productReceipt.transactionID);*/
}
}
catch (IAPSecurityException)
{
// Debug.Log("Invalid receipt, not unlocking content");
validPurchase = false;
}
if (validPurchase)
{
// Unlock the appropriate content here.
if (String.Equals(e.purchasedProduct.definition.id, trueseriaproduct_money, StringComparison.Ordinal))
{
money.Amount += 100000000;
}
else if (String.Equals(e.purchasedProduct.definition.id, trueseriaproduct_research, StringComparison.Ordinal))
{
Game.ResearchPoint += 100000000;
}
else if (String.Equals(e.purchasedProduct.definition.id, trueseriaproduct_2xspeed, StringComparison.Ordinal))
{
//
}
else if (String.Equals(e.purchasedProduct.definition.id, trueseriaproduct_all, StringComparison.Ordinal))
{
money.Amount += 100000000;
Game.ResearchPoint += 100000000;
fanatic.ft += 100000;
}
else
{
}
}
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed(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));
}
}
I appreciate any suggestions on what may be the issue. Thank you.