using System;
using UnityEngine.Events;
using UnityEngine.Purchasing.Extension;
using UnityEngine.UI;
namespace UnityEngine.Purchasing
{
///
/// A GUI component for exposing the current price and allow purchasing of In-App Purchases. Exposes configurable
/// elements through the Inspector.
///
///
[AddComponentMenu("In-App Purchasing/IAP Button")]
[HelpURL("https://docs.unity3d.com/Packages/com.unity.purchasing@latest")]
public class CodelessIAPButton : BaseIAPButton
{
///
/// Type of event fired after a successful fetching the product information from the store.
///
[Serializable]
public class OnProductFetchedEvent : UnityEvent
{
}
///
/// Type of event fired after a successful purchase of a product.
///
[Serializable]
public class OnPurchaseCompletedEvent : UnityEvent
{
}
///
/// Type of event fired after a failed purchase of a product.
///
[Serializable]
public class OnPurchaseFailedEvent : UnityEvent
{
}
///
/// Type of event fired after a restore transactions was completed.
///
[Serializable]
public class OnTransactionsRestoredEvent : UnityEvent
{
}
///
/// Which product identifier to represent. Note this is not a store-specific identifier.
///
[HideInInspector]
public string productId;
///
/// The type of this button, can be either a purchase or a restore button.
///
[Tooltip("The type of this button, can be either a purchase or a restore button.")]
public CodelessButtonType buttonType = CodelessButtonType.Purchase;
///
/// Consume the product immediately after a successful purchase.
///
[Tooltip("Consume the product immediately after a successful purchase.")]
public bool consumePurchase = true;
///
/// Event fired after a restore transactions.
///
[Tooltip("Event fired after a restore transactions.")]
public OnTransactionsRestoredEvent onTransactionsRestored;
///
/// Event fired after a successful purchase of this product.
///
[Tooltip("Event fired after a successful purchase of this product.")]
public OnPurchaseCompletedEvent onPurchaseComplete;
///
/// Event fired after a failed purchase of this product.
///
[Tooltip("Event fired after a failed purchase of this product.")]
public OnPurchaseFailedEvent onPurchaseFailed;
///
/// Event fired after a successful fetching the product information from the store.
///
[Tooltip("Event fired after a successful fetching the product information from the store.")]
public OnProductFetchedEvent onProductFetched;
[Tooltip("Button that triggers purchase.")]
public Button button;
internal override string GetProductId()
{
return productId;
}
internal override bool IsAPurchaseButton()
{
return buttonType == CodelessButtonType.Purchase;
}
protected override bool IsARestoreButton()
{
return buttonType == CodelessButtonType.Restore;
}
protected override bool ShouldConsumePurchase()
{
return consumePurchase;
}
protected override void OnTransactionsRestored(bool success, string error)
{
onTransactionsRestored?.Invoke(success, error);
}
protected override void OnPurchaseComplete(Product purchasedProduct)
{
onPurchaseComplete?.Invoke(purchasedProduct);
}
///
/// Invoked on a failed purchase of the product associated with this button
///
/// The which failed to purchase
/// Information to help developers recover from this failure
public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
{
onPurchaseFailed?.Invoke(product, failureDescription);
}
protected override Button GetPurchaseButton()
{
return button;
}
protected override void AddButtonToCodelessListener()
{
CodelessIAPStoreListener.Instance.AddButton(this);
}
protected override void RemoveButtonToCodelessListener()
{
CodelessIAPStoreListener.Instance.RemoveButton(this);
}
internal override void OnInitCompleted()
{
var product = CodelessIAPStoreListener.Instance.GetProduct(productId);
if (product != null)
{
onProductFetched.Invoke(product);
}
}
///
/// Invoke to process a successful purchase of the product associated with this button.
///
/// The successful PurchaseEventArgs for the purchase event.
/// The result of the successful purchase
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
return ProcessPurchaseInternal(args);
}
}
}