diff --git a/Runtime/Code/Model/GuruSDKModel.cs b/Runtime/Code/Model/GuruSDKModel.cs index c4a930b..c65f581 100644 --- a/Runtime/Code/Model/GuruSDKModel.cs +++ b/Runtime/Code/Model/GuruSDKModel.cs @@ -1,5 +1,6 @@ -using UnityEngine.Networking; +using System.Collections.Generic; +using System.Linq; namespace Guru { @@ -30,6 +31,8 @@ namespace Guru public int b_level = 0; public int b_play = 0; public int buy_count = 0; + + public List purchased; //-------------- data --------------- private float _lastSavedTime = 0; @@ -144,6 +147,8 @@ namespace Guru _totalPlayed = new BindableProperty(b_play, OnPlayedChanged); _purchasedCount = new BindableProperty(buy_count, OnPurchasedNumChanged); _uid = new BindableProperty(uid, OnUidChanged); + + purchased = new List(20); } /// @@ -210,10 +215,73 @@ namespace Guru #endregion + #region 订单记录 + + + public bool HasPurchasedProduct(string receipt) + { + if(purchased == null || purchased.Count == 0) return false; + return purchased.Exists(p => p.receipt == receipt); + } + + /// + /// 添加已支付订单 + /// + /// + /// + /// + public void AddReceipt(string receipt, string productName, string productId, bool appleProductIsRestored = false) + { + if (purchased == null) purchased = new List(20); + if (!HasPurchasedProduct(receipt)) + { + purchased.Add(new PurchasedProduct() + { + receipt = receipt, + productName = productName, + productId = productId, + appleProductIsRestored = appleProductIsRestored + }); + Save(); + } + } + + public string[] GetReceipts(string productName) + { + int count = purchased?.Count ?? 0; + if (count == 0) count = 20; + if (purchased == null) purchased = new List(count); + var receipts = new List(count); + receipts.AddRange(from purchasedProduct in purchased where purchasedProduct.productName == productName select purchasedProduct.receipt); + return receipts.ToArray(); + } + + + public string[] GetReceiptsById(string productId) + { + int count = purchased?.Count ?? 0; + if (count == 0) count = 20; + if (purchased == null) purchased = new List(count); + var receipts = new List(count); + receipts.AddRange(from purchasedProduct in purchased where purchasedProduct.productId == productId select purchasedProduct.receipt); + return receipts.ToArray(); + } + + + + #endregion + + } + + [Serializable] + internal class PurchasedProduct + { + public string productName; + public string productId; + public string receipt; + public bool appleProductIsRestored; } - - diff --git a/Runtime/Code/SDK/GuruSDK.IAP.cs b/Runtime/Code/SDK/GuruSDK.IAP.cs index 6422dfd..caf6709 100644 --- a/Runtime/Code/SDK/GuruSDK.IAP.cs +++ b/Runtime/Code/SDK/GuruSDK.IAP.cs @@ -30,6 +30,7 @@ namespace Guru GuruIAP.Instance.OnBuyStart += OnBuyStart; GuruIAP.Instance.OnBuyEnd += OnBuyEnd; GuruIAP.Instance.OnBuyFailed += OnBuyFailed; + GuruIAP.Instance.OnGetProductReceipt += OnGetReceipt; GuruIAP.Instance.InitWithKeys(googleKey, appleRootCerts, IsDebugMode); } @@ -181,5 +182,16 @@ namespace Guru #endregion + #region Receipt + + + private static void OnGetReceipt(string productId, string receipt, bool appleProductIsRestored = false) + { + var productName = GetProductSettingById(productId).ProductName; + Model.AddReceipt(receipt, productName, productId, appleProductIsRestored); + } + + #endregion + } } \ No newline at end of file