147 lines
4.3 KiB
C#
147 lines
4.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Purchasing;
|
|
using UnityEngine.Purchasing.Extension;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Samples.Purchasing.AppleAppStore.RetrievingProductReceipt
|
|
{
|
|
[RequireComponent(typeof(UserWarningAppleAppStore))]
|
|
public class RetrievingProductReceipt : MonoBehaviour, IDetailedStoreListener
|
|
{
|
|
IStoreController m_StoreController;
|
|
IAppleExtensions m_AppleExtensions;
|
|
|
|
public string goldProductId = "com.mycompany.mygame.gold1";
|
|
public ProductType goldType = ProductType.Consumable;
|
|
|
|
public Text goldCountText;
|
|
|
|
int m_GoldCount;
|
|
|
|
void Start()
|
|
{
|
|
InitializePurchasing();
|
|
UpdateWarningMessage();
|
|
}
|
|
|
|
void InitializePurchasing()
|
|
{
|
|
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
|
|
|
|
builder.AddProduct(goldProductId, goldType);
|
|
|
|
UnityPurchasing.Initialize(this, builder);
|
|
}
|
|
|
|
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
|
|
{
|
|
Debug.Log("In-App Purchasing successfully initialized");
|
|
|
|
m_StoreController = controller;
|
|
m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
|
|
m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferredPurchase);
|
|
|
|
SetupAskToBuy();
|
|
|
|
UpdateUI();
|
|
}
|
|
|
|
void SetupAskToBuy()
|
|
{
|
|
if (Application.platform == RuntimePlatform.IPhonePlayer)
|
|
{
|
|
// Only applies to Sandbox testing
|
|
m_AppleExtensions.simulateAskToBuy = true;
|
|
}
|
|
}
|
|
|
|
public void BuyGold()
|
|
{
|
|
m_StoreController.InitiatePurchase(goldProductId);
|
|
}
|
|
|
|
void OnDeferredPurchase(Product product)
|
|
{
|
|
Debug.Log($"Purchase of {product.definition.id} is deferred");
|
|
UpdateUI();
|
|
}
|
|
|
|
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
|
|
{
|
|
var product = args.purchasedProduct;
|
|
|
|
Debug.Log($"Processing Purchase: {product.definition.id}");
|
|
|
|
if (Application.platform == RuntimePlatform.IPhonePlayer ||
|
|
Application.platform == RuntimePlatform.tvOS)
|
|
{
|
|
var receipt = m_AppleExtensions.GetTransactionReceiptForProduct(product);
|
|
Debug.Log($"Product receipt for deferred purchase: {receipt}");
|
|
|
|
// Send transaction receipt to server for validation
|
|
}
|
|
|
|
UnlockContent(product);
|
|
|
|
return PurchaseProcessingResult.Complete;
|
|
}
|
|
|
|
void UnlockContent(Product product)
|
|
{
|
|
Debug.Log($"Unlock Content: {product.definition.id}");
|
|
|
|
if (product.definition.id == goldProductId)
|
|
{
|
|
AddGold();
|
|
}
|
|
|
|
UpdateUI();
|
|
}
|
|
|
|
void AddGold()
|
|
{
|
|
m_GoldCount++;
|
|
}
|
|
|
|
void UpdateUI()
|
|
{
|
|
goldCountText.text = $"Your Gold: {m_GoldCount}";
|
|
}
|
|
|
|
public void OnInitializeFailed(InitializationFailureReason error)
|
|
{
|
|
OnInitializeFailed(error, null);
|
|
}
|
|
|
|
public void OnInitializeFailed(InitializationFailureReason error, string message)
|
|
{
|
|
var errorMessage = $"Purchasing failed to initialize. Reason: {error}.";
|
|
|
|
if (message != null)
|
|
{
|
|
errorMessage += $" More details: {message}";
|
|
}
|
|
|
|
Debug.Log(errorMessage);
|
|
}
|
|
|
|
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
|
|
{
|
|
Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
|
|
}
|
|
|
|
public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
|
|
{
|
|
Debug.Log($"Purchase failed - Product: '{product.definition.id}'," +
|
|
$" Purchase failure reason: {failureDescription.reason}," +
|
|
$" Purchase failure details: {failureDescription.message}");
|
|
}
|
|
|
|
void UpdateWarningMessage()
|
|
{
|
|
GetComponent<UserWarningAppleAppStore>().UpdateWarningText();
|
|
}
|
|
}
|
|
}
|