From 6ef4d0cdec8ba87de61c701b6e95c6914c77cd0c Mon Sep 17 00:00:00 2001 From: huyufei Date: Tue, 7 May 2024 21:48:47 +0800 Subject: [PATCH] update: Add Subscription API --- .../GuruIAP/Runtime/Code/IAPServiceBase.cs | 119 +++++++++++++++++- Runtime/GuruIAP/~Sample/MyIAPService.cs | 4 +- 2 files changed, 117 insertions(+), 6 deletions(-) diff --git a/Runtime/GuruIAP/Runtime/Code/IAPServiceBase.cs b/Runtime/GuruIAP/Runtime/Code/IAPServiceBase.cs index 0c1d4b3..daf7920 100644 --- a/Runtime/GuruIAP/Runtime/Code/IAPServiceBase.cs +++ b/Runtime/GuruIAP/Runtime/Code/IAPServiceBase.cs @@ -22,6 +22,7 @@ namespace Guru private const string DefaultCategory = "Store"; private static bool _showLog; + private static string _userId; private ConfigurationBuilder _configBuilder; // 商店配置创建器 @@ -119,8 +120,9 @@ namespace Guru /// /// 初始化支付服务 /// - public virtual void Initialize(bool showLog = false) + public virtual void Initialize(string userId, bool showLog = false) { + _userId = userId; _showLog = showLog; InitPurchasing(); } @@ -131,12 +133,12 @@ namespace Guru /// /// /// - public virtual void InitWithKeys(byte[] googlePublicKey, byte[] appleRootCert, bool showLog = false) + public virtual void InitWithKeys(string userId, byte[] googlePublicKey, byte[] appleRootCert, bool showLog = false) { _googlePublicKey = googlePublicKey; _appleRootCert = appleRootCert; InitModel(); - Initialize(showLog); + Initialize(userId, showLog); } /// @@ -214,9 +216,12 @@ namespace Guru LogI($"--- IAP Initialized Success"); _storeController = controller; _storeExtensionProvider = extensions; + + if (string.IsNullOrEmpty(_userId)) _userId = IPMConfig.IPM_UID; #if UNITY_IOS _appleExtensions = extensions.GetExtension(); + _appleExtensions.SetApplicationUsername(_userId); // SetUp UID // On Apple platforms we need to handle deferred purchases caused by Apple's Ask to Buy feature. // On non-Apple platforms this will have no effect; OnDeferred will never be called. _appleExtensions.RegisterPurchaseDeferredListener(item => @@ -225,7 +230,7 @@ namespace Guru OnAppStorePurchaseDeferred?.Invoke(item); }); #elif UNITY_ANDROID - _configBuilder.Configure().SetObfuscatedAccountId(IPMConfig.IPM_UID); + _configBuilder.Configure().SetObfuscatedAccountId(_userId); // SetUp UID _googlePlayStoreExtensions = extensions.GetExtension(); // _googlePlayStoreExtensions.SetObfuscatedAccountId(IPMConfig.IPM_UID); //添加安装游戏后第一次初试化进行恢复购买的回调 只有安卓才有 @@ -1051,6 +1056,10 @@ namespace Guru #region Subscription + + public static DateTime DefaultSubscriptionDate = new DateTime(1970, 1,1,0,0,0); + + private SubscriptionManager GetSubManager(string productName) { var product = GetProduct(productName); @@ -1064,6 +1073,8 @@ namespace Guru public bool IsSubscriptionFreeTrail(string productName) { + if(!IsInitialized) return false; + var smgr = GetSubManager(productName); if (smgr != null) { @@ -1075,6 +1086,8 @@ namespace Guru public bool IsSubscriptionCancelled(string productName) { + if(!IsInitialized) return false; + var smgr = GetSubManager(productName); if (smgr != null) { @@ -1085,6 +1098,8 @@ namespace Guru public bool IsSubscriptionAvailable(string productName) { + if(!IsInitialized) return false; + var smgr = GetSubManager(productName); if (smgr != null) { @@ -1096,6 +1111,8 @@ namespace Guru public bool IsSubscriptionExpired(string productName) { + if(!IsInitialized) return false; + var smgr = GetSubManager(productName); if (smgr != null) { @@ -1107,6 +1124,8 @@ namespace Guru public bool IsSubscriptionAutoRenewing(string productName) { + if(!IsInitialized) return false; + var smgr = GetSubManager(productName); if (smgr != null) { @@ -1123,6 +1142,8 @@ namespace Guru /// public bool IsSubscriptionIntroductoryPricePeriod(string productName) { + if(!IsInitialized) return false; + var smgr = GetSubManager(productName); if (smgr != null) { @@ -1130,7 +1151,97 @@ namespace Guru } return false; } + + + + public DateTime GetSubscriptionExpireDate(string productName) + { + if(!IsInitialized) return DefaultSubscriptionDate; + + var smgr = GetSubManager(productName); + if (smgr != null) + { + return smgr.getSubscriptionInfo()?.getExpireDate() ?? DateTime.Now; + } + return DefaultSubscriptionDate; + } + + + public DateTime GetSubscriptionPurchaseDate(string productName) + { + if(!IsInitialized) return DefaultSubscriptionDate; + + var smgr = GetSubManager(productName); + if (smgr != null) + { + return smgr.getSubscriptionInfo().getPurchaseDate(); + } + return DefaultSubscriptionDate; + } + + + public DateTime GetSubscriptionCancelDate(string productName) + { + if(!IsInitialized) return DefaultSubscriptionDate; + + var smgr = GetSubManager(productName); + if (smgr != null) + { + return smgr.getSubscriptionInfo().getCancelDate(); + } + return DefaultSubscriptionDate; + } + + public TimeSpan GetSubscriptionRemainingTime(string productName) + { + if(!IsInitialized) return TimeSpan.Zero; + + var smgr = GetSubManager(productName); + if (smgr != null) + { + return smgr.getSubscriptionInfo().getRemainingTime(); + } + return TimeSpan.Zero; + } + + public TimeSpan GetSubscriptionIntroductoryPricePeriod(string productName) + { + if(!IsInitialized) return TimeSpan.Zero; + + var smgr = GetSubManager(productName); + if (smgr != null) + { + return smgr.getSubscriptionInfo().getIntroductoryPricePeriod(); + } + return TimeSpan.Zero; + } + + + public TimeSpan GetSubscriptionFreeTrialPeriod(string productName) + { + if(!IsInitialized) return TimeSpan.Zero; + + var smgr = GetSubManager(productName); + if (smgr != null) + { + return smgr.getSubscriptionInfo().getFreeTrialPeriod(); + } + return TimeSpan.Zero; + } + + public string GetSubscriptionInfoJsonString(string productName) + { + if(!IsInitialized) return ""; + + var smgr = GetSubManager(productName); + if (smgr != null) + { + return smgr.getSubscriptionInfo().getSubscriptionInfoJsonString(); + } + return ""; + } + #endregion } diff --git a/Runtime/GuruIAP/~Sample/MyIAPService.cs b/Runtime/GuruIAP/~Sample/MyIAPService.cs index 04eecab..1892fe8 100644 --- a/Runtime/GuruIAP/~Sample/MyIAPService.cs +++ b/Runtime/GuruIAP/~Sample/MyIAPService.cs @@ -14,9 +14,9 @@ namespace Guru.Sample return 1; } - public override void Initialize(bool showLog = false) + public override void Initialize(string uid, bool showLog = false) { - base.Initialize(true); + base.Initialize(uid,true); InitGameProducts(); }