using System; using UnityEngine; using UnityEngine.Purchasing; using UnityEngine.Purchasing.Extension; using UnityEngine.UI; namespace Samples.Purchasing.GooglePlay.RestoringTransactions { [RequireComponent(typeof(UserWarningGooglePlayStore))] public class RestoringTransactions : MonoBehaviour, IDetailedStoreListener { IStoreController m_StoreController; IGooglePlayStoreExtensions m_GooglePlayStoreExtensions; public string noAdsProductId = "com.mycompany.mygame.no_ads"; public Text hasNoAdsText; public Text restoreStatusText; void Start() { InitializePurchasing(); UpdateWarningMessage(); } void InitializePurchasing() { var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct(noAdsProductId, ProductType.NonConsumable); UnityPurchasing.Initialize(this, builder); } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { Debug.Log("In-App Purchasing successfully initialized"); m_StoreController = controller; m_GooglePlayStoreExtensions = extensions.GetExtension(); UpdateUI(); } public void Restore() { m_GooglePlayStoreExtensions.RestoreTransactions(OnRestore); } void OnRestore(bool success, string error) { var restoreMessage = ""; if (success) { // This does not mean anything was restored, // merely that the restoration process succeeded. restoreMessage = "Restore Successful"; } else { // Restoration failed. restoreMessage = $"Restore Failed with error: {error}"; } Debug.Log(restoreMessage); restoreStatusText.text = restoreMessage; } public void BuyNoAds() { m_StoreController.InitiatePurchase(noAdsProductId); } public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) { var product = args.purchasedProduct; Debug.Log($"Processing Purchase: {product.definition.id}"); UpdateUI(); return PurchaseProcessingResult.Complete; } void UpdateUI() { hasNoAdsText.text = HasNoAds() ? "No ads will be shown" : "Ads will be shown"; } bool HasNoAds() { var noAdsProduct = m_StoreController.products.WithID(noAdsProductId); return noAdsProduct != null && noAdsProduct.hasReceipt; } 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().UpdateWarningText(); } } }