120 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
using Guru;
 | 
						|
using UnityEngine.Purchasing;
 | 
						|
 | 
						|
namespace GuruService
 | 
						|
{
 | 
						|
    public partial class GuruSDKService
 | 
						|
    {
 | 
						|
        private static void AddIAPCallbackListener()
 | 
						|
        {
 | 
						|
            GuruSDK.Callbacks.IAP.OnIAPInitStart += OnIAPInitStart;
 | 
						|
            GuruSDK.Callbacks.IAP.OnIAPInitComplete += OnIAPInitComplete;
 | 
						|
            GuruSDK.Callbacks.IAP.OnPurchaseStart += OnPurchaseStart;
 | 
						|
            GuruSDK.Callbacks.IAP.OnPurchaseEnd += OnPurchaseEnd;
 | 
						|
            GuruSDK.Callbacks.IAP.OnPurchaseFailed += OnPurchaseFailed;
 | 
						|
            GuruSDK.Callbacks.IAP.OnIAPRestored += OnIAPRestored;
 | 
						|
        }
 | 
						|
 | 
						|
        #region callbacks
 | 
						|
 | 
						|
        private static void OnIAPInitStart()
 | 
						|
        {
 | 
						|
            Log.I($"[GuruCallback][OnIAPInitStart]");
 | 
						|
            _serviceImp.OnIAPInitStart();
 | 
						|
        }
 | 
						|
 | 
						|
        private static void OnIAPInitComplete(bool success)
 | 
						|
        {
 | 
						|
            Log.I($"[GuruCallback][OnIAPInitComplete] success:{success}");
 | 
						|
            _serviceImp.OnIAPInitComplete(success);
 | 
						|
        }
 | 
						|
 | 
						|
        private static void OnPurchaseStart(string productName)
 | 
						|
        {
 | 
						|
            Log.I($"[GuruCallback][OnPurchaseStart] productName:{productName}");
 | 
						|
            ShowIAPLoadingView();
 | 
						|
            _serviceImp.OnPurchaseStart(productName);
 | 
						|
        }
 | 
						|
 | 
						|
        private static void OnPurchaseEnd(string productName, bool success)
 | 
						|
        {
 | 
						|
            Log.I($"[GuruCallback][OnPurchaseEnd] productName:{productName}, success:{success}");
 | 
						|
            CloseIAPLoadingView();
 | 
						|
            _serviceImp.OnPurchaseEnd(productName, success);
 | 
						|
        }
 | 
						|
 | 
						|
        private static void OnPurchaseFailed(string productName, string reason)
 | 
						|
        {
 | 
						|
            Log.I($"[GuruCallback][OnPurchaseFailed] productName:{productName}, reason:{reason}");
 | 
						|
            _serviceImp.OnPurchaseFailed(productName, reason);
 | 
						|
        }
 | 
						|
 | 
						|
        private static void OnIAPRestored(bool success, string msg)
 | 
						|
        {
 | 
						|
            Log.I($"[GuruCallback][OnIAPRestored] success:{success}, msg:{msg}");
 | 
						|
            _serviceImp.OnIAPRestored(success, msg);
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 查询接口
 | 
						|
 | 
						|
        public static string GetProductCurrencyCode(string productName)
 | 
						|
        {
 | 
						|
            if (string.IsNullOrEmpty(productName))
 | 
						|
                return Fallback();
 | 
						|
 | 
						|
            var productInfo = GuruSDK.GetProductInfo(productName);
 | 
						|
            if (productInfo == null)
 | 
						|
                return Fallback();
 | 
						|
 | 
						|
            return productInfo.CurrencyCode;
 | 
						|
 | 
						|
            string Fallback()
 | 
						|
            {
 | 
						|
                return "USD";
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        public static string GetProductPriceString(string productName)
 | 
						|
        {
 | 
						|
            if (string.IsNullOrEmpty(productName))
 | 
						|
                return Fallback();
 | 
						|
 | 
						|
            var productInfo = GuruSDK.GetProductInfo(productName);
 | 
						|
            if (productInfo == null)
 | 
						|
                return Fallback();
 | 
						|
 | 
						|
            return productInfo.LocalizedPriceString;
 | 
						|
 | 
						|
            string Fallback()
 | 
						|
            {
 | 
						|
                return "$0";
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        public static double GetProductPrice(string productName)
 | 
						|
        {
 | 
						|
            if (string.IsNullOrEmpty(productName))
 | 
						|
                return Fallback();
 | 
						|
 | 
						|
            var productInfo = GuruSDK.GetProductInfo(productName);
 | 
						|
            if (productInfo == null)
 | 
						|
                return Fallback();
 | 
						|
 | 
						|
            return productInfo.Price;
 | 
						|
 | 
						|
            double Fallback()
 | 
						|
            {
 | 
						|
                return 0.0;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        public static void ShowIAPLoadingView() => _serviceImp.ShowIAPBuyLoadingView();
 | 
						|
        public static void CloseIAPLoadingView() => _serviceImp.CloseIAPLoadingView();
 | 
						|
        public static void IAPBuySuccessTip() => _serviceImp.IAPBuySuccessTip();
 | 
						|
        public static void IAPBuyFailTip() => _serviceImp.IAPBuyFailTip();
 | 
						|
    }
 | 
						|
} |