|  | @@ -25,6 +25,8 @@ namespace GFGGame
 | 
	
		
			
				|  |  |      public class IAPManager : SingletonBase<IAPManager>, IStoreListener
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          IStoreController m_StoreController; // The Unity Purchasing system.
 | 
	
		
			
				|  |  | +        private IAppleExtensions m_AppleExtensions;
 | 
	
		
			
				|  |  | +        private bool canMakePayments;
 | 
	
		
			
				|  |  |          private string OrderId;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          private string OrderIdLocal
 | 
	
	
		
			
				|  | @@ -71,6 +73,12 @@ namespace GFGGame
 | 
	
		
			
				|  |  |          public void Pay(int buyID, int count, string orderID, long Price)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              Debug.Log($"Pay {buyID}");
 | 
	
		
			
				|  |  | +            if(!canMakePayments)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。");
 | 
	
		
			
				|  |  | +                return;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              if(!string.IsNullOrEmpty(this.OrderIdLocal))
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  |                  PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!");
 | 
	
	
		
			
				|  | @@ -101,11 +109,15 @@ namespace GFGGame
 | 
	
		
			
				|  |  |              Debug.Log("InitializePurchasing");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +            canMakePayments = builder.Configure<IAppleConfiguration>().canMakePayments;
 | 
	
		
			
				|  |  | +            builder.Configure<IAppleConfiguration>().SetApplePromotionalPurchaseInterceptorCallback(OnPromotionalPurchase);
 | 
	
		
			
				|  |  |              var dataArray = ShopCfgArray.Instance.dataArray;
 | 
	
		
			
				|  |  |              foreach(var shopCfg in dataArray)
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  | -                builder.AddProduct(shopCfg.id + "", ProductType.Consumable);
 | 
	
		
			
				|  |  | +                if(shopCfg.costType == CostType.RMB)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    builder.AddProduct(shopCfg.id + "", ProductType.Consumable);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              UnityPurchasing.Initialize(this, builder);
 | 
	
	
		
			
				|  | @@ -115,7 +127,12 @@ namespace GFGGame
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              Debug.Log("In-App Purchasing successfully initialized");
 | 
	
		
			
				|  |  |              m_StoreController = controller;
 | 
	
		
			
				|  |  | +            m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
 | 
	
		
			
				|  |  |              EventAgent.DispatchEvent(ConstMessage.ON_PLATFORM_SDK_INITED, true);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            // 在 Apple 平台上,我们需要处理由 Apple 的"购买前先询问"功能导致的延期购买。
 | 
	
		
			
				|  |  | +            //在非 Apple 平台上,这将不起作用;永远不会调用 OnDeferred。
 | 
	
		
			
				|  |  | +            m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          public void OnInitializeFailed(InitializationFailureReason error)
 | 
	
	
		
			
				|  | @@ -129,6 +146,35 @@ namespace GFGGame
 | 
	
		
			
				|  |  |              Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              this.OrderId = null;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            switch (failureReason)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                case PurchaseFailureReason.PurchasingUnavailable:
 | 
	
		
			
				|  |  | +                    PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。");
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case PurchaseFailureReason.ExistingPurchasePending:
 | 
	
		
			
				|  |  | +                    PromptController.Instance.ShowFloatTextPrompt("现购待定,请稍后再试。");
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case PurchaseFailureReason.ProductUnavailable:
 | 
	
		
			
				|  |  | +                    PromptController.Instance.ShowFloatTextPrompt("该商品不可购买,请稍后再试。");
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case PurchaseFailureReason.SignatureInvalid:
 | 
	
		
			
				|  |  | +                    PromptController.Instance.ShowFloatTextPrompt("验证失败,请稍后再试。");
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case PurchaseFailureReason.UserCancelled:
 | 
	
		
			
				|  |  | +                    
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case PurchaseFailureReason.PaymentDeclined:
 | 
	
		
			
				|  |  | +                    PromptController.Instance.ShowFloatTextPrompt("此次购买被拒绝,请稍后再试。");
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case PurchaseFailureReason.DuplicateTransaction:
 | 
	
		
			
				|  |  | +                    PromptController.Instance.ShowFloatTextPrompt("重复的交易,请稍后再试。");
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                default:
 | 
	
		
			
				|  |  | +                    PromptController.Instance.ShowFloatTextPrompt("购买失败,请稍后再试!");
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
 | 
	
	
		
			
				|  | @@ -143,5 +189,31 @@ namespace GFGGame
 | 
	
		
			
				|  |  |              //We return Complete, informing IAP that the processing on our side is done and the transaction can be closed.
 | 
	
		
			
				|  |  |              return PurchaseProcessingResult.Complete;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// 特定于 iOS。
 | 
	
		
			
				|  |  | +        /// 未成年人要求购买并提交给家长批准时,
 | 
	
		
			
				|  |  | +        /// 此函数将作为 Apple 的"购买前先询问"功能的一部分
 | 
	
		
			
				|  |  | +        /// 接受调用。
 | 
	
		
			
				|  |  | +        ///
 | 
	
		
			
				|  |  | +        /// 购买被批准或拒绝时,将触发正常的购买
 | 
	
		
			
				|  |  | +        /// 事件。
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="item">Item.</param>
 | 
	
		
			
				|  |  | +        private void OnDeferred(Product item)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            Debug.Log("Purchase deferred: " + item.definition.id);
 | 
	
		
			
				|  |  | +            PromptController.Instance.ShowFloatTextPrompt("未成年人购买需要经过家长批准才能继续!");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        private void OnPromotionalPurchase(Product item)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            Debug.Log("Attempted promotional purchase: " + item.definition.id);
 | 
	
		
			
				|  |  | +            // 已检测到推荐性购买。
 | 
	
		
			
				|  |  | +            // 通过呈现家长控制门等方式处理此事件。
 | 
	
		
			
				|  |  | +            //此处,仅出于演示目的,我们将等待五秒钟
 | 
	
		
			
				|  |  | +            // 再继续购买。
 | 
	
		
			
				|  |  | +            //StartCoroutine(ContinuePromotionalPurchases());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |