Hi.
I am in proccess of implementing Unity IAP for google store, all my virtual goods are currency packs (cunsumable item). At stage of testing i checked every product with test purchases on android device and it was ok. Then I started to messing with reciept validation, suddenly one of my products started to throw OnPurchaseFailed with PurchaseFailureReason.Unknown. I thought it was ok, Im gonna fix that later, but then one by one all of the virtual goods started to throw this fail error. I dont know what is the reason of the problem but i suspect that at some points my ProcessPurchase was stoped by some NullRefs (while I tried to parse reciept json) so it didnt return PurchaseProcessingResult.Complete or PurchaseProcessingResult.Pending, maybe it could broke the goods. Unity does not even call ProcessPurchase anymore, fail happens before it.
Please help.
Here is my iap manager class.
using UnityEngine;
using UnityEngine;
using System.Collections;
using UnityEngine.Purchasing;
using System.Text.RegularExpressions;
using UnityEngine.Purchasing.Security;
public class IAP : IStoreListener {
IStoreController controller;
IExtensionProvider extensions;
public IAP () {
ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct("gems_100", ProductType.Consumable);
builder.AddProduct("gems_500", ProductType.Consumable);
builder.AddProduct("gems_1000", ProductType.Consumable);
builder.AddProduct("gems_2500", ProductType.Consumable);
builder.AddProduct("gems_6000", ProductType.Consumable);
UnityPurchasing.Initialize (this, builder);
}
public void initiate_purchase(string product_id)
{
Debug.Log("initiating purchase");
controller.InitiatePurchase(product_id);
}
public void OnInitialized(IStoreController _controller, IExtensionProvider _extensions)
{
controller = _controller;
extensions = _extensions;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
Debug.LogError("OnInitializeFailed " + error.ToString());
}
public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
{
Debug.LogError(p.ToString() + " " + i.definition.id);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
{
bool valid = true;
#if (UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE_OSX) && !UNITY_EDITOR
var validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.bundleIdentifier);
try
{
IPurchaseReceipt[] validation_result = validator.Validate(e.purchasedProduct.receipt);
}
catch (IAPSecurityException)
{
Debug.LogError("Invalid receipt, not unlocking content");
valid = false;
}
#endif
int gems_count = get_gem_count(e.purchasedProduct.definition.id);
if (gems_count > 0 && valid)
{
Game.data.add_gems(gems_count, (success) =>
{
if (success)
controller.ConfirmPendingPurchase(e.purchasedProduct);
});
return PurchaseProcessingResult.Pending;
}
else
{
Debug.Log("invalid purchase");
return PurchaseProcessingResult.Complete;
}
}
int get_gem_count(string product_id)
{
Match number = Regex.Match(product_id, @"\d+");
if (!string.IsNullOrEmpty(number.Value))
{
Debug.Log(number.Value);
return System.Int32.Parse(number.Value);
}
else
{
Debug.LogError("no number found in product id " + product_id);
return 0;
}
}
}