Skip to content

Instantly share code, notes, and snippets.

@phesch
Last active December 12, 2016 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phesch/52935927d2802043e6d9a803a0586e0d to your computer and use it in GitHub Desktop.
Save phesch/52935927d2802043e6d9a803a0586e0d to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.Purchasing;
public class MyIAPManager : IStoreListener
{
public MyIAPManager()
{
var builder - ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct("remove_ads_endless", ProductType.NonConsumable);
UnityPurchasing.Initialize (this, builder);
}
}
public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
{
this.controller = controller;
this.extensions = extensions;
}
//what are you even trying to DO here??????
extensions.GetExtension<IAppleExtensions> ().RestoreTransactions (result => {
if (result) {
} else {
}
});
}
public void OnInitializeFailed (InitializationFailureReason error)
{
}
public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e)
{
bool validPurchase = true;
#if UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE_OSX
// Prepare the validator with the secrets we prepared in the Editor
// obfuscation window.
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); //remove_ads_endless? or leave as?
Debug.Log(productReceipt.purchaseDate); // Go through Purchase Receipts?
Debug.Log(productReceipt.transactionID); // Go through Purchase Receipts?
}
}
catch (IAPSecurityException)
{
Debug.Log("Invalid receipt, not unlocking content");
validPurchase = false;
}
#endif
if (validPurchase)
{
// Unlock Appropriate content here. // Set AdsRemoved = True here?
{
#if UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE_OSX
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
// Get a reference to IAppleConfiguration during IAP initialization.
var appleConfig = builder.Configure<IAppleConfiguration>();
var receiptData = System.Convert.FromBase64String(appleConfig.appReceipt);
AppleReceipt receipt = new AppleValidator(AppleTangle.Data()).Validate(receiptData);
Debug.Log(receipt.bundleID);
Debug.Log(receipt.receiptCreationDate);
foreach (AppleInAppPurchaseReceipt productReceipt in receipt.inAppPurchaseReceipts)
{
Debug.Log(productReceipt.transactionIdentifier);
Debug.Log(productReceipt.productIdentifier);
}
#endif
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed (Product i, PurchaseFailureReason p)
{
if (p == PurchaseFailureReason.PurchasingUnavailable)
{
}
}
foreach (var product in controller.products.all)
{
Debug.Log (product.metadata.localizedTitle);
Debug.Log (product.metadata.localizedDescription);
Debug.Log (product.metadata.localizedPriceString);
}
public void OnPurchaseClicked(string remove_ads_endless)
{
controller.InitiatePurchase(remove_ads_endless);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment