update: Add Subscription API
							parent
							
								
									01ee547c8e
								
							
						
					
					
						commit
						6ef4d0cdec
					
				|  | @ -22,6 +22,7 @@ namespace Guru | ||||||
|         private const string DefaultCategory = "Store"; |         private const string DefaultCategory = "Store"; | ||||||
| 
 | 
 | ||||||
|         private static bool _showLog; |         private static bool _showLog; | ||||||
|  |         private static string _userId; | ||||||
|          |          | ||||||
|         private ConfigurationBuilder _configBuilder; // 商店配置创建器 |         private ConfigurationBuilder _configBuilder; // 商店配置创建器 | ||||||
|          |          | ||||||
|  | @ -119,8 +120,9 @@ namespace Guru | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// 初始化支付服务 |         /// 初始化支付服务 | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         public virtual void Initialize(bool showLog = false) |         public virtual void Initialize(string userId, bool showLog = false) | ||||||
|         { |         { | ||||||
|  |             _userId = userId; | ||||||
|             _showLog = showLog; |             _showLog = showLog; | ||||||
|             InitPurchasing(); |             InitPurchasing(); | ||||||
|         } |         } | ||||||
|  | @ -131,12 +133,12 @@ namespace Guru | ||||||
|         /// <param name="googlePublicKey"></param> |         /// <param name="googlePublicKey"></param> | ||||||
|         /// <param name="appleRootCert"></param> |         /// <param name="appleRootCert"></param> | ||||||
|         /// <param name="showLog"></param> |         /// <param name="showLog"></param> | ||||||
|         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; |             _googlePublicKey = googlePublicKey; | ||||||
|             _appleRootCert = appleRootCert; |             _appleRootCert = appleRootCert; | ||||||
|             InitModel(); |             InitModel(); | ||||||
|             Initialize(showLog); |             Initialize(userId, showLog); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|  | @ -214,9 +216,12 @@ namespace Guru | ||||||
|             LogI($"--- IAP Initialized Success"); |             LogI($"--- IAP Initialized Success"); | ||||||
|             _storeController = controller; |             _storeController = controller; | ||||||
|             _storeExtensionProvider = extensions; |             _storeExtensionProvider = extensions; | ||||||
|  | 
 | ||||||
|  |             if (string.IsNullOrEmpty(_userId)) _userId = IPMConfig.IPM_UID; | ||||||
|              |              | ||||||
| #if UNITY_IOS | #if UNITY_IOS | ||||||
|             _appleExtensions = extensions.GetExtension<IAppleExtensions>(); |             _appleExtensions = extensions.GetExtension<IAppleExtensions>(); | ||||||
|  |             _appleExtensions.SetApplicationUsername(_userId);  // SetUp UID | ||||||
|             // On Apple platforms we need to handle deferred purchases caused by Apple's Ask to Buy feature. |             // 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. |             // On non-Apple platforms this will have no effect; OnDeferred will never be called. | ||||||
|             _appleExtensions.RegisterPurchaseDeferredListener(item => |             _appleExtensions.RegisterPurchaseDeferredListener(item => | ||||||
|  | @ -225,7 +230,7 @@ namespace Guru | ||||||
|                 OnAppStorePurchaseDeferred?.Invoke(item); |                 OnAppStorePurchaseDeferred?.Invoke(item); | ||||||
|             }); |             }); | ||||||
| #elif UNITY_ANDROID | #elif UNITY_ANDROID | ||||||
|             _configBuilder.Configure<IGooglePlayConfiguration>().SetObfuscatedAccountId(IPMConfig.IPM_UID); |             _configBuilder.Configure<IGooglePlayConfiguration>().SetObfuscatedAccountId(_userId); // SetUp UID | ||||||
|             _googlePlayStoreExtensions = extensions.GetExtension<IGooglePlayStoreExtensions>(); |             _googlePlayStoreExtensions = extensions.GetExtension<IGooglePlayStoreExtensions>(); | ||||||
|             // _googlePlayStoreExtensions.SetObfuscatedAccountId(IPMConfig.IPM_UID); |             // _googlePlayStoreExtensions.SetObfuscatedAccountId(IPMConfig.IPM_UID); | ||||||
|             //添加安装游戏后第一次初试化进行恢复购买的回调 只有安卓才有 |             //添加安装游戏后第一次初试化进行恢复购买的回调 只有安卓才有 | ||||||
|  | @ -1051,6 +1056,10 @@ namespace Guru | ||||||
| 
 | 
 | ||||||
|         #region Subscription |         #region Subscription | ||||||
| 
 | 
 | ||||||
|  |          | ||||||
|  |         public static DateTime DefaultSubscriptionDate = new DateTime(1970, 1,1,0,0,0);  | ||||||
|  | 
 | ||||||
|  |          | ||||||
|         private SubscriptionManager GetSubManager(string productName) |         private SubscriptionManager GetSubManager(string productName) | ||||||
|         { |         { | ||||||
|             var product = GetProduct(productName); |             var product = GetProduct(productName); | ||||||
|  | @ -1064,6 +1073,8 @@ namespace Guru | ||||||
| 
 | 
 | ||||||
|         public bool IsSubscriptionFreeTrail(string productName) |         public bool IsSubscriptionFreeTrail(string productName) | ||||||
|         { |         { | ||||||
|  |             if(!IsInitialized) return false; | ||||||
|  |              | ||||||
|             var smgr = GetSubManager(productName); |             var smgr = GetSubManager(productName); | ||||||
|             if (smgr != null) |             if (smgr != null) | ||||||
|             { |             { | ||||||
|  | @ -1075,6 +1086,8 @@ namespace Guru | ||||||
| 
 | 
 | ||||||
|         public bool IsSubscriptionCancelled(string productName) |         public bool IsSubscriptionCancelled(string productName) | ||||||
|         { |         { | ||||||
|  |             if(!IsInitialized) return false; | ||||||
|  |              | ||||||
|             var smgr = GetSubManager(productName); |             var smgr = GetSubManager(productName); | ||||||
|             if (smgr != null) |             if (smgr != null) | ||||||
|             { |             { | ||||||
|  | @ -1085,6 +1098,8 @@ namespace Guru | ||||||
| 
 | 
 | ||||||
|         public bool IsSubscriptionAvailable(string productName) |         public bool IsSubscriptionAvailable(string productName) | ||||||
|         { |         { | ||||||
|  |             if(!IsInitialized) return false; | ||||||
|  |              | ||||||
|             var smgr = GetSubManager(productName); |             var smgr = GetSubManager(productName); | ||||||
|             if (smgr != null) |             if (smgr != null) | ||||||
|             { |             { | ||||||
|  | @ -1096,6 +1111,8 @@ namespace Guru | ||||||
| 
 | 
 | ||||||
|         public bool IsSubscriptionExpired(string productName) |         public bool IsSubscriptionExpired(string productName) | ||||||
|         { |         { | ||||||
|  |             if(!IsInitialized) return false; | ||||||
|  |              | ||||||
|             var smgr = GetSubManager(productName); |             var smgr = GetSubManager(productName); | ||||||
|             if (smgr != null) |             if (smgr != null) | ||||||
|             { |             { | ||||||
|  | @ -1107,6 +1124,8 @@ namespace Guru | ||||||
|          |          | ||||||
|         public bool IsSubscriptionAutoRenewing(string productName) |         public bool IsSubscriptionAutoRenewing(string productName) | ||||||
|         { |         { | ||||||
|  |             if(!IsInitialized) return false; | ||||||
|  |              | ||||||
|             var smgr = GetSubManager(productName); |             var smgr = GetSubManager(productName); | ||||||
|             if (smgr != null) |             if (smgr != null) | ||||||
|             { |             { | ||||||
|  | @ -1123,6 +1142,8 @@ namespace Guru | ||||||
|         /// <returns></returns> |         /// <returns></returns> | ||||||
|         public bool IsSubscriptionIntroductoryPricePeriod(string productName) |         public bool IsSubscriptionIntroductoryPricePeriod(string productName) | ||||||
|         { |         { | ||||||
|  |             if(!IsInitialized) return false; | ||||||
|  |              | ||||||
|             var smgr = GetSubManager(productName); |             var smgr = GetSubManager(productName); | ||||||
|             if (smgr != null) |             if (smgr != null) | ||||||
|             { |             { | ||||||
|  | @ -1130,7 +1151,97 @@ namespace Guru | ||||||
|             } |             } | ||||||
|             return false; |             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 |         #endregion | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -14,9 +14,9 @@ namespace Guru.Sample | ||||||
|             return 1; |             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(); |             InitGameProducts(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue