QDAppStore.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using UnityEngine.Purchasing;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class QDAppStore
  6. {
  7. public static void InitPlatform()
  8. {
  9. IAPManager.Instance.InitializePurchasing();
  10. }
  11. public static void Pay(int buyID, int count, string orderID, long Price)
  12. {
  13. IAPManager.Instance.Pay(buyID, count, orderID, Price);
  14. }
  15. public static void OnEnterGame()
  16. {
  17. IAPManager.Instance.CheckOrderToDo();
  18. }
  19. }
  20. public class IAPManager : SingletonBase<IAPManager>, IStoreListener
  21. {
  22. IStoreController m_StoreController; // The Unity Purchasing system.
  23. private string OrderId
  24. {
  25. get
  26. {
  27. return PlayerPrefs.GetString(OrderIdLocalkey, null);
  28. }
  29. set
  30. {
  31. PlayerPrefs.SetString(OrderIdLocalkey, value);
  32. }
  33. }
  34. private string TransactionId
  35. {
  36. get
  37. {
  38. return PlayerPrefs.GetString(TransactionIdLocalkey, null);
  39. }
  40. set
  41. {
  42. PlayerPrefs.SetString(TransactionIdLocalkey, value);
  43. }
  44. }
  45. private string OrderIdLocalkey
  46. {
  47. get { return RoleDataManager.roleId + "OrderId"; }
  48. }
  49. private string TransactionIdLocalkey
  50. {
  51. get { return RoleDataManager.roleId + "transactionID"; }
  52. }
  53. public void CheckOrderToDo()
  54. {
  55. if (!string.IsNullOrEmpty(this.OrderId) && !string.IsNullOrEmpty(this.TransactionId))
  56. {
  57. IOSRechargeSProxy.IosVerifyOrder(OrderId, this.TransactionId).Coroutine();
  58. }
  59. }
  60. public void Pay(int buyID, int count, string orderID, long Price)
  61. {
  62. Debug.Log($"Pay {buyID}");
  63. if(!string.IsNullOrEmpty(this.OrderId))
  64. {
  65. PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!");
  66. return;
  67. }
  68. this.OrderId = orderID;
  69. m_StoreController.InitiatePurchase(buyID + "");
  70. }
  71. public void OnServerSuccess(string OrderId, string TransactionId)
  72. {
  73. this.OrderId = null;
  74. this.TransactionId = null;
  75. PlayerPrefs.DeleteKey(OrderIdLocalkey);
  76. PlayerPrefs.DeleteKey(TransactionIdLocalkey);
  77. }
  78. public void InitializePurchasing()
  79. {
  80. Debug.Log("InitializePurchasing");
  81. var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  82. var dataArray = ShopCfgArray.Instance.dataArray;
  83. foreach(var shopCfg in dataArray)
  84. {
  85. builder.AddProduct(shopCfg.id + "", ProductType.Consumable);
  86. }
  87. UnityPurchasing.Initialize(this, builder);
  88. }
  89. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  90. {
  91. Debug.Log("In-App Purchasing successfully initialized");
  92. m_StoreController = controller;
  93. EventAgent.DispatchEvent(ConstMessage.ON_PLATFORM_SDK_INITED, true);
  94. }
  95. public void OnInitializeFailed(InitializationFailureReason error)
  96. {
  97. Debug.Log($"In-App Purchasing initialize failed: {error}");
  98. EventAgent.DispatchEvent(ConstMessage.ON_PLATFORM_SDK_INITED, false);
  99. }
  100. public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
  101. {
  102. Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
  103. }
  104. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  105. {
  106. //Retrieve the purchased product
  107. var product = args.purchasedProduct;
  108. Debug.Log($"Purchase Complete - Product: {product.definition.id}");
  109. this.TransactionId = product.transactionID;
  110. IOSRechargeSProxy.IosVerifyOrder(OrderId, product.transactionID).Coroutine();
  111. //We return Complete, informing IAP that the processing on our side is done and the transaction can be closed.
  112. return PurchaseProcessingResult.Complete;
  113. }
  114. }
  115. }