QDAppStoreManager.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using UnityEngine.Purchasing;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class QDAppStoreManager
  6. {
  7. public static void Init()
  8. {
  9. QDManager.isInited = true;
  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 IAppleExtensions m_AppleExtensions;
  24. private bool canMakePayments;
  25. private string OrderId;
  26. private string OrderIdLocal
  27. {
  28. get
  29. {
  30. return PlayerPrefs.GetString(OrderIdLocalkey, null);
  31. }
  32. set
  33. {
  34. PlayerPrefs.SetString(OrderIdLocalkey, value);
  35. }
  36. }
  37. private string TransactionId
  38. {
  39. get
  40. {
  41. return PlayerPrefs.GetString(TransactionIdLocalkey, null);
  42. }
  43. set
  44. {
  45. PlayerPrefs.SetString(TransactionIdLocalkey, value);
  46. }
  47. }
  48. private string OrderIdLocalkey
  49. {
  50. get { return RoleDataManager.roleId + "OrderId"; }
  51. }
  52. private string TransactionIdLocalkey
  53. {
  54. get { return RoleDataManager.roleId + "transactionID"; }
  55. }
  56. public void CheckOrderToDo()
  57. {
  58. if (!string.IsNullOrEmpty(this.OrderIdLocal) && !string.IsNullOrEmpty(this.TransactionId))
  59. {
  60. IOSRechargeSProxy.IosVerifyOrder(OrderIdLocal, this.TransactionId).Coroutine();
  61. }
  62. }
  63. public void Pay(int buyID, int count, string orderID, long Price)
  64. {
  65. Debug.Log($"Pay {buyID}");
  66. if(!canMakePayments)
  67. {
  68. PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。");
  69. return;
  70. }
  71. if(!string.IsNullOrEmpty(this.OrderIdLocal))
  72. {
  73. PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!");
  74. CheckOrderToDo();
  75. return;
  76. }
  77. if(!string.IsNullOrEmpty(this.OrderId))
  78. {
  79. PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!");
  80. return;
  81. }
  82. ViewManager.Show<ModalStatusView>("");
  83. this.OrderId = orderID;
  84. m_StoreController.InitiatePurchase(buyID + "");
  85. }
  86. public void OnServerSuccess(string OrderId, string TransactionId)
  87. {
  88. this.OrderId = null;
  89. this.OrderIdLocal = null;
  90. this.TransactionId = null;
  91. PlayerPrefs.DeleteKey(OrderIdLocalkey);
  92. PlayerPrefs.DeleteKey(TransactionIdLocalkey);
  93. }
  94. public void InitializePurchasing()
  95. {
  96. Debug.Log("InitializePurchasing");
  97. var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  98. canMakePayments = builder.Configure<IAppleConfiguration>().canMakePayments;
  99. builder.Configure<IAppleConfiguration>().SetApplePromotionalPurchaseInterceptorCallback(OnPromotionalPurchase);
  100. var dataArray = ShopCfgArray.Instance.dataArray;
  101. foreach(var shopCfg in dataArray)
  102. {
  103. if(shopCfg.costType == CostType.RMB)
  104. {
  105. builder.AddProduct(shopCfg.id + "", ProductType.Consumable);
  106. }
  107. }
  108. UnityPurchasing.Initialize(this, builder);
  109. }
  110. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  111. {
  112. Debug.Log("In-App Purchasing successfully initialized");
  113. m_StoreController = controller;
  114. m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
  115. EventAgent.DispatchEvent(ConstMessage.ON_PLATFORM_SDK_INITED, true);
  116. // 在 Apple 平台上,我们需要处理由 Apple 的"购买前先询问"功能导致的延期购买。
  117. //在非 Apple 平台上,这将不起作用;永远不会调用 OnDeferred。
  118. m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
  119. }
  120. public void OnInitializeFailed(InitializationFailureReason error)
  121. {
  122. Debug.Log($"In-App Purchasing initialize failed: {error}");
  123. EventAgent.DispatchEvent(ConstMessage.ON_PLATFORM_SDK_INITED, false);
  124. }
  125. public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
  126. {
  127. Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
  128. this.OrderId = null;
  129. ViewManager.Hide<ModalStatusView>();
  130. switch (failureReason)
  131. {
  132. case PurchaseFailureReason.PurchasingUnavailable:
  133. PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。");
  134. break;
  135. case PurchaseFailureReason.ExistingPurchasePending:
  136. PromptController.Instance.ShowFloatTextPrompt("现购待定,请稍后再试。");
  137. break;
  138. case PurchaseFailureReason.ProductUnavailable:
  139. PromptController.Instance.ShowFloatTextPrompt("该商品不可购买,请稍后再试。");
  140. break;
  141. case PurchaseFailureReason.SignatureInvalid:
  142. PromptController.Instance.ShowFloatTextPrompt("验证失败,请稍后再试。");
  143. break;
  144. case PurchaseFailureReason.UserCancelled:
  145. break;
  146. case PurchaseFailureReason.PaymentDeclined:
  147. PromptController.Instance.ShowFloatTextPrompt("此次购买被拒绝,请稍后再试。");
  148. break;
  149. case PurchaseFailureReason.DuplicateTransaction:
  150. PromptController.Instance.ShowFloatTextPrompt("重复的交易,请稍后再试。");
  151. break;
  152. default:
  153. PromptController.Instance.ShowFloatTextPrompt("购买失败,请稍后再试!");
  154. break;
  155. }
  156. }
  157. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  158. {
  159. ViewManager.Hide<ModalStatusView>();
  160. //Retrieve the purchased product
  161. var product = args.purchasedProduct;
  162. Debug.Log($"Purchase Complete - Product: {product.definition.id}");
  163. this.OrderIdLocal = this.OrderId;
  164. this.TransactionId = product.transactionID;
  165. GameGlobal.maxShowOrderTime = 300000;
  166. IOSRechargeSProxy.IosVerifyOrder(OrderIdLocal, product.transactionID).Coroutine();
  167. //We return Complete, informing IAP that the processing on our side is done and the transaction can be closed.
  168. return PurchaseProcessingResult.Complete;
  169. }
  170. /// &lt;summary>
  171. /// 特定于 iOS。
  172. /// 未成年人要求购买并提交给家长批准时,
  173. /// 此函数将作为 Apple 的"购买前先询问"功能的一部分
  174. /// 接受调用。
  175. ///
  176. /// 购买被批准或拒绝时,将触发正常的购买
  177. /// 事件。
  178. /// &lt;/summary>
  179. /// &lt;param name="item">Item.&lt;/param>
  180. private void OnDeferred(Product item)
  181. {
  182. Debug.Log("Purchase deferred: " + item.definition.id);
  183. PromptController.Instance.ShowFloatTextPrompt("未成年人购买需要经过家长批准才能继续!");
  184. }
  185. private void OnPromotionalPurchase(Product item)
  186. {
  187. Debug.Log("Attempted promotional purchase: " + item.definition.id);
  188. // 已检测到推荐性购买。
  189. // 通过呈现家长控制门等方式处理此事件。
  190. //此处,仅出于演示目的,我们将等待五秒钟
  191. // 再继续购买。
  192. //StartCoroutine(ContinuePromotionalPurchases());
  193. }
  194. }
  195. }