335 lines
11 KiB
C#
335 lines
11 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Viveport;
|
|
using Viveport.Core;
|
|
|
|
public class ViveportDemo_IAP : MonoBehaviour
|
|
{
|
|
public class Item
|
|
{
|
|
public string ticket = "test_id";
|
|
|
|
public string[] items;
|
|
|
|
public string subscription_ticket = "unity_test_subscriptionId";
|
|
}
|
|
|
|
private class Result : IAPurchase.IAPurchaseListener
|
|
{
|
|
public Item mItem = new Item();
|
|
|
|
public override void OnSuccess(string pchCurrencyName)
|
|
{
|
|
bIsReady_Done = true;
|
|
Viveport.Core.Logger.Log("[OnSuccess] pchCurrencyName=" + pchCurrencyName);
|
|
}
|
|
|
|
public override void OnRequestSuccess(string pchPurchaseId)
|
|
{
|
|
mItem.ticket = pchPurchaseId;
|
|
Viveport.Core.Logger.Log("[OnRequestSuccess] pchPurchaseId=" + pchPurchaseId + ",mItem.ticket=" + mItem.ticket);
|
|
}
|
|
|
|
public override void OnPurchaseSuccess(string pchPurchaseId)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnPurchaseSuccess] pchPurchaseId=" + pchPurchaseId);
|
|
if (mItem.ticket == pchPurchaseId)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnPurchaseSuccess] give items to user");
|
|
}
|
|
}
|
|
|
|
public override void OnQuerySuccess(IAPurchase.QueryResponse response)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnQuerySuccess] purchaseId=" + response.purchase_id + ",status=" + response.status);
|
|
}
|
|
|
|
public override void OnQuerySuccess(IAPurchase.QueryListResponse response)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnQueryListSuccess] total=" + response.total + ", from=" + response.from + ", to=" + response.to);
|
|
foreach (IAPurchase.QueryResponse2 purchase in response.purchaseList)
|
|
{
|
|
Viveport.Core.Logger.Log("purchase_id=" + purchase.purchase_id + ", user_data=" + purchase.user_data + ", price=" + purchase.price + ", currency=" + purchase.currency + ", paid_timestamp=" + purchase.paid_timestamp);
|
|
}
|
|
}
|
|
|
|
public override void OnBalanceSuccess(string pchBalance)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnBalanceSuccess] pchBalance=" + pchBalance);
|
|
}
|
|
|
|
public override void OnRequestSubscriptionSuccess(string pchSubscriptionId)
|
|
{
|
|
mItem.subscription_ticket = pchSubscriptionId;
|
|
Viveport.Core.Logger.Log("[OnRequestSubscriptionSuccess] pchSubscriptionId=" + pchSubscriptionId + ",mItem.subscription_ticket=" + mItem.subscription_ticket);
|
|
}
|
|
|
|
public override void OnRequestSubscriptionWithPlanIDSuccess(string pchSubscriptionId)
|
|
{
|
|
mItem.subscription_ticket = pchSubscriptionId;
|
|
Viveport.Core.Logger.Log("[OnRequestSubscriptionWithPlanIDSuccess] pchSubscriptionId=" + pchSubscriptionId + ",mItem.subscription_ticket=" + mItem.subscription_ticket);
|
|
}
|
|
|
|
public override void OnSubscribeSuccess(string pchSubscriptionId)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnSubscribeSuccess] pchSubscriptionId=" + pchSubscriptionId);
|
|
if (mItem.subscription_ticket == pchSubscriptionId)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnSubscribeSuccess] give virtual items to user");
|
|
}
|
|
}
|
|
|
|
public override void OnQuerySubscriptionSuccess(IAPurchase.Subscription[] subscriptionlist)
|
|
{
|
|
int num = subscriptionlist.Length;
|
|
Viveport.Core.Logger.Log("[OnQuerySubscriptionSuccess] subscriptionlist size =" + num);
|
|
if (num <= 0)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnQuerySubscriptionSuccess] subscriptionlist[" + i + "].status =" + subscriptionlist[i].status + ", subscriptionlist[" + i + "].plan_id = " + subscriptionlist[i].plan_id);
|
|
if (subscriptionlist[i].plan_id == "pID" && subscriptionlist[i].status == "ACTIVE")
|
|
{
|
|
bIsDuplicatedSubscription = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void OnQuerySubscriptionListSuccess(IAPurchase.Subscription[] subscriptionlist)
|
|
{
|
|
int num = subscriptionlist.Length;
|
|
Viveport.Core.Logger.Log("[OnQuerySubscriptionListSuccess] subscriptionlist size =" + num);
|
|
if (num <= 0)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnQuerySubscriptionListSuccess] subscriptionlist[" + i + "].status =" + subscriptionlist[i].status + ", subscriptionlist[" + i + "].plan_id = " + subscriptionlist[i].plan_id);
|
|
if (subscriptionlist[i].plan_id == "pID" && subscriptionlist[i].status == "ACTIVE")
|
|
{
|
|
bIsDuplicatedSubscription = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void OnCancelSubscriptionSuccess(bool bCanceled)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnCancelSubscriptionSuccess] bCanceled=" + bCanceled);
|
|
}
|
|
|
|
public override void OnFailure(int nCode, string pchMessage)
|
|
{
|
|
Viveport.Core.Logger.Log("[OnFailed] " + nCode + ", " + pchMessage);
|
|
}
|
|
}
|
|
|
|
private int nWidth = 80;
|
|
|
|
private int nHeight = 40;
|
|
|
|
private int nXStart = 10;
|
|
|
|
private int nYStart = 40;
|
|
|
|
private static string IAP_APP_TEST_ID = "app_VIVEPORT_ID";
|
|
|
|
private static string IAP_APP_TEST_KEY = "app_API_Key";
|
|
|
|
private Result mListener;
|
|
|
|
private static bool bIsDuplicatedSubscription;
|
|
|
|
private static bool bInit_Done;
|
|
|
|
private static bool bIsReady_Done;
|
|
|
|
private static bool bShutdown_Done;
|
|
|
|
private Text winText;
|
|
|
|
private void Start()
|
|
{
|
|
Viveport.Core.Logger.Log("mListener: " + mListener);
|
|
mListener = new Result();
|
|
Viveport.Core.Logger.Log("mListener end: " + mListener);
|
|
Viveport.Core.Logger.Log("Version: " + Api.Version());
|
|
Viveport.Core.Logger.Log("UserId: " + User.GetUserId());
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
GUIStyle style = new GUIStyle("button");
|
|
GUI.Label(new Rect(15f, 5f, 100f, 20f), "TOP API");
|
|
GUI.Label(new Rect(15f, 65f, 800f, 20f), "==========================================================================================================================================");
|
|
GUI.Label(new Rect(15f, 80f, 100f, 20f), "IAP - Purchase");
|
|
GUI.Label(new Rect(15f, 145f, 800f, 20f), "==========================================================================================================================================");
|
|
GUI.Label(new Rect(15f, 165f, 200f, 20f), "IAP - Purchase With User Data");
|
|
GUI.Label(new Rect(15f, 235f, 800f, 20f), "==========================================================================================================================================");
|
|
GUI.Label(new Rect(15f, 260f, 120f, 20f), "IAP - Subscription");
|
|
GUI.Label(new Rect(15f, 380f, 800f, 20f), "==========================================================================================================================================");
|
|
GUI.Label(new Rect(15f, 400f, 120f, 20f), "IAP - Optional API");
|
|
if ((!bInit_Done && !bIsReady_Done) || bShutdown_Done)
|
|
{
|
|
GUI.contentColor = Color.white;
|
|
}
|
|
else
|
|
{
|
|
GUI.contentColor = Color.gray;
|
|
}
|
|
if (GUI.Button(new Rect(nXStart, nYStart - 15, nWidth, nHeight), "1.Init", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Init");
|
|
Api.Init(InitStatusHandler, IAP_APP_TEST_ID);
|
|
}
|
|
if (bInit_Done && !bIsReady_Done)
|
|
{
|
|
GUI.contentColor = Color.white;
|
|
}
|
|
else
|
|
{
|
|
GUI.contentColor = Color.gray;
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + nWidth + 15, nYStart - 15, nWidth, nHeight), "2.IsReady", style))
|
|
{
|
|
Viveport.Core.Logger.Log("IsReady");
|
|
IAPurchase.IsReady(mListener, IAP_APP_TEST_KEY);
|
|
}
|
|
if (!bInit_Done || bShutdown_Done)
|
|
{
|
|
GUI.contentColor = Color.grey;
|
|
}
|
|
else
|
|
{
|
|
GUI.contentColor = Color.white;
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + 2 * nWidth + 30, nYStart - 15, nWidth + 20, nHeight), "3.Shutdown", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Shutdown");
|
|
Api.Shutdown(ShutdownStatusHandler);
|
|
}
|
|
if (bInit_Done && bIsReady_Done && !bShutdown_Done)
|
|
{
|
|
GUI.contentColor = Color.white;
|
|
}
|
|
else
|
|
{
|
|
GUI.contentColor = Color.grey;
|
|
}
|
|
if (GUI.Button(new Rect(nXStart, nYStart + 60, nWidth + 20, nHeight), "1.1.Request", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Request");
|
|
mListener.mItem.items = new string[3];
|
|
mListener.mItem.items[0] = "sword";
|
|
mListener.mItem.items[1] = "knife";
|
|
mListener.mItem.items[2] = "medicine";
|
|
IAPurchase.Request(mListener, "1");
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + nWidth + 40, nYStart + 60, nWidth + 20, nHeight), "1.2.Purchase", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Purchase mListener.mItem.ticket=" + mListener.mItem.ticket);
|
|
IAPurchase.Purchase(mListener, mListener.mItem.ticket);
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + 2 * nWidth + 80, nYStart + 60, nWidth + 20, nHeight), "2.Query", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Query");
|
|
IAPurchase.Query(mListener, mListener.mItem.ticket);
|
|
}
|
|
if (bInit_Done && bIsReady_Done && !bShutdown_Done)
|
|
{
|
|
GUI.contentColor = Color.white;
|
|
}
|
|
else
|
|
{
|
|
GUI.contentColor = Color.grey;
|
|
}
|
|
if (GUI.Button(new Rect(nXStart, nYStart + nHeight + 110, nWidth + 90, nHeight), "1.1.RequestWithUserData", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Request");
|
|
IAPurchase.Request(mListener, "1", "Knife");
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + nWidth + 105, nYStart + nHeight + 110, nWidth + 20, nHeight), "1.2.Purchase", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Purchase mListener.mItem.ticket=" + mListener.mItem.ticket);
|
|
IAPurchase.Purchase(mListener, mListener.mItem.ticket);
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + 2 * nWidth + 140, nYStart + nHeight + 110, nWidth + 80, nHeight), "2.QueryPurchaseList", style))
|
|
{
|
|
Viveport.Core.Logger.Log("QueryPurchaseList");
|
|
IAPurchase.Query(mListener);
|
|
}
|
|
if (bInit_Done && bIsReady_Done && !bShutdown_Done)
|
|
{
|
|
GUI.contentColor = Color.white;
|
|
}
|
|
else
|
|
{
|
|
GUI.contentColor = Color.grey;
|
|
}
|
|
if (GUI.Button(new Rect(nXStart, nYStart + 2 * nHeight + 165, nWidth + 160, nHeight), "1.1-1.RequestSubscription ", style))
|
|
{
|
|
Viveport.Core.Logger.Log("RequestSubscription");
|
|
IAPurchase.RequestSubscription(mListener, "1", "month", 1, "day", 2, 3, "pID");
|
|
}
|
|
if (GUI.Button(new Rect(nXStart, nYStart + 3 * nHeight + 175, nWidth + 160, nHeight), "1.1-2.RequestSubscriptionWithPlanID", style))
|
|
{
|
|
Viveport.Core.Logger.Log("RequestSubscriptionWithPlanID");
|
|
IAPurchase.RequestSubscriptionWithPlanID(mListener, "pID");
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + nWidth + 180, nYStart + 2 * nHeight + 190, nWidth + 20, nHeight), "1.2.Subscribe", style))
|
|
{
|
|
Viveport.Core.Logger.Log("Subscribe");
|
|
IAPurchase.Subscribe(mListener, mListener.mItem.subscription_ticket);
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + 2 * nWidth + 218, nYStart + 2 * nHeight + 165, nWidth + 90, nHeight), "2.1.QuerySubscription", style))
|
|
{
|
|
Viveport.Core.Logger.Log("QuerySubscription");
|
|
IAPurchase.QuerySubscription(mListener, mListener.mItem.subscription_ticket);
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + 2 * nWidth + 218, nYStart + 3 * nHeight + 175, nWidth + 105, nHeight), "2.2.QuerySubscriptionList", style))
|
|
{
|
|
Viveport.Core.Logger.Log("QuerySubscriptionList");
|
|
IAPurchase.QuerySubscriptionList(mListener);
|
|
}
|
|
if (GUI.Button(new Rect(nXStart + 3 * nWidth + 345, nYStart + 2 * nHeight + 190, nWidth + 80, nHeight), "3.CancelSubscription", style))
|
|
{
|
|
Viveport.Core.Logger.Log("CancelSubscription");
|
|
IAPurchase.CancelSubscription(mListener, mListener.mItem.subscription_ticket);
|
|
}
|
|
if (bInit_Done && bIsReady_Done && !bShutdown_Done)
|
|
{
|
|
GUI.contentColor = Color.white;
|
|
}
|
|
else
|
|
{
|
|
GUI.contentColor = Color.gray;
|
|
}
|
|
if (GUI.Button(new Rect(nXStart, nYStart + 2 * nHeight + 305, nWidth + 20, nHeight), "GetBalance", style))
|
|
{
|
|
Viveport.Core.Logger.Log("GetBalance");
|
|
IAPurchase.GetBalance(mListener);
|
|
}
|
|
}
|
|
|
|
private static void InitStatusHandler(int nResult)
|
|
{
|
|
bInit_Done = true;
|
|
bShutdown_Done = false;
|
|
Viveport.Core.Logger.Log("InitStatusHandler: " + nResult);
|
|
}
|
|
|
|
private static void ShutdownStatusHandler(int nResult)
|
|
{
|
|
bShutdown_Done = true;
|
|
bInit_Done = false;
|
|
bIsReady_Done = false;
|
|
Viveport.Core.Logger.Log("ShutdownStatusHandler: " + nResult);
|
|
}
|
|
}
|