82 lines
1.9 KiB
C#
82 lines
1.9 KiB
C#
using Oculus.Platform.Models;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Oculus.Platform.Samples.VrBoardGame
|
|
{
|
|
public class IAPManager : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private GameController m_gameController;
|
|
|
|
[SerializeField]
|
|
private Text m_priceText;
|
|
|
|
private const string CONSUMABLE_1 = "PowerballPack1";
|
|
|
|
private void Start()
|
|
{
|
|
FetchProductPrices();
|
|
FetchPurchasedProducts();
|
|
}
|
|
|
|
public void FetchProductPrices()
|
|
{
|
|
string[] skus = new string[1] { "PowerballPack1" };
|
|
IAP.GetProductsBySKU(skus).OnComplete(GetProductsBySKUCallback);
|
|
}
|
|
|
|
private void GetProductsBySKUCallback(Message<ProductList> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
return;
|
|
}
|
|
foreach (Product product in msg.GetProductList())
|
|
{
|
|
Debug.LogFormat("Product: sku:{0} name:{1} price:{2}", product.Sku, product.Name, product.FormattedPrice);
|
|
if (product.Sku == "PowerballPack1")
|
|
{
|
|
m_priceText.text = product.FormattedPrice;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void FetchPurchasedProducts()
|
|
{
|
|
IAP.GetViewerPurchases().OnComplete(GetViewerPurchasesCallback);
|
|
}
|
|
|
|
private void GetViewerPurchasesCallback(Message<PurchaseList> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
return;
|
|
}
|
|
foreach (Purchase purchase in msg.GetPurchaseList())
|
|
{
|
|
Debug.LogFormat("Purchased: sku:{0} granttime:{1} id:{2}", purchase.Sku, purchase.GrantTime, purchase.ID);
|
|
}
|
|
}
|
|
|
|
public void BuyPowerBallsPressed()
|
|
{
|
|
IAP.LaunchCheckoutFlow("PowerballPack1").OnComplete(LaunchCheckoutFlowCallback);
|
|
}
|
|
|
|
private void LaunchCheckoutFlowCallback(Message<Purchase> msg)
|
|
{
|
|
if (msg.IsError)
|
|
{
|
|
PlatformManager.TerminateWithError(msg);
|
|
return;
|
|
}
|
|
Purchase purchase = msg.GetPurchase();
|
|
Debug.Log("purchased " + purchase.Sku);
|
|
m_gameController.AddPowerballs(3u);
|
|
}
|
|
}
|
|
}
|