QDAppStore.cs 4.7 KB

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