123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using UnityEngine.Purchasing;
- using UnityEngine;
- namespace GFGGame
- {
- public class QDAppStore
- {
- public static void InitPlatform()
- {
- IAPManager.Instance.InitializePurchasing();
- }
- public static void Pay(int buyID, int count, string orderID, long Price)
- {
- IAPManager.Instance.Pay(buyID, count, orderID, Price);
- }
- public static void OnEnterGame()
- {
- IAPManager.Instance.CheckOrderToDo();
- }
- }
- public class IAPManager : SingletonBase<IAPManager>, IStoreListener
- {
- IStoreController m_StoreController; // The Unity Purchasing system.
- private string OrderId
- {
- get
- {
- return PlayerPrefs.GetString(OrderIdLocalkey, null);
- }
- set
- {
- PlayerPrefs.SetString(OrderIdLocalkey, value);
- }
- }
- private string TransactionId
- {
- get
- {
- return PlayerPrefs.GetString(TransactionIdLocalkey, null);
- }
- set
- {
- PlayerPrefs.SetString(TransactionIdLocalkey, value);
- }
- }
- private string OrderIdLocalkey
- {
- get { return RoleDataManager.roleId + "OrderId"; }
- }
- private string TransactionIdLocalkey
- {
- get { return RoleDataManager.roleId + "transactionID"; }
- }
- public void CheckOrderToDo()
- {
- if (!string.IsNullOrEmpty(this.OrderId) && !string.IsNullOrEmpty(this.TransactionId))
- {
- IOSRechargeSProxy.IosVerifyOrder(OrderId, this.TransactionId).Coroutine();
- }
- }
- public void Pay(int buyID, int count, string orderID, long Price)
- {
- Debug.Log($"Pay {buyID}");
- if(!string.IsNullOrEmpty(this.OrderId))
- {
- PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!");
- return;
- }
- this.OrderId = orderID;
- m_StoreController.InitiatePurchase(buyID + "");
- }
- public void OnServerSuccess(string OrderId, string TransactionId)
- {
- this.OrderId = null;
- this.TransactionId = null;
- PlayerPrefs.DeleteKey(OrderIdLocalkey);
- PlayerPrefs.DeleteKey(TransactionIdLocalkey);
- }
- public void InitializePurchasing()
- {
- Debug.Log("InitializePurchasing");
- var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
- var dataArray = ShopCfgArray.Instance.dataArray;
- foreach(var shopCfg in dataArray)
- {
- builder.AddProduct(shopCfg.id + "", ProductType.Consumable);
- }
- UnityPurchasing.Initialize(this, builder);
- }
- public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
- {
- Debug.Log("In-App Purchasing successfully initialized");
- m_StoreController = controller;
- EventAgent.DispatchEvent(ConstMessage.ON_PLATFORM_SDK_INITED, true);
- }
- public void OnInitializeFailed(InitializationFailureReason error)
- {
- Debug.Log($"In-App Purchasing initialize failed: {error}");
- EventAgent.DispatchEvent(ConstMessage.ON_PLATFORM_SDK_INITED, false);
- }
- public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
- {
- Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
- }
- public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
- {
- //Retrieve the purchased product
- var product = args.purchasedProduct;
- Debug.Log($"Purchase Complete - Product: {product.definition.id}");
- this.TransactionId = product.transactionID;
- IOSRechargeSProxy.IosVerifyOrder(OrderId, product.transactionID).Coroutine();
- //We return Complete, informing IAP that the processing on our side is done and the transaction can be closed.
- return PurchaseProcessingResult.Complete;
- }
- }
- }
|