您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

293 行
9.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using SUISS.Core;
  5. using SUISS.Storage;
  6. using UnityEngine;
  7. namespace CIG
  8. {
  9. public sealed class IAPValidationManager
  10. {
  11. public IAPValidationManager(CIGWebService webService)
  12. {
  13. this._webService = webService;
  14. this._webService.PullRequestCompleted += this.OnWebServicesPullRequestCompleted;
  15. this.DeserializePurchases(this._unverifiedPurchases, "UnverifiedPurchases");
  16. this.DeserializePurchases(this._untrackedPurchases, "UntrackedPurchases");
  17. }
  18. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  19. public event Action<string, string, bool> purchaseVerifiedEvent;
  20. public void AddPurchase(string productID, string transactionID, string receipt)
  21. {
  22. IAPValidationManager.ValidationPurchase item = new IAPValidationManager.ValidationPurchase(productID, transactionID, receipt, this._webService.GeneratePurchaseReceiptParameters());
  23. this._unverifiedPurchases.Add(item);
  24. this._untrackedPurchases.Add(item);
  25. this.SerializePurchases(this._unverifiedPurchases, "UnverifiedPurchases");
  26. this.SerializePurchases(this._untrackedPurchases, "UntrackedPurchases");
  27. this.ValidatePurchases();
  28. }
  29. public void ValidatePurchases()
  30. {
  31. if (this._unverifiedPurchases.Count > 0)
  32. {
  33. IAPValidationManager.ValidationPurchase purchase = this._unverifiedPurchases[0];
  34. UnityEngine.Debug.LogFormat("Verifying purchase {0}", new object[]
  35. {
  36. purchase.ProductID
  37. });
  38. if (this._webService != null)
  39. {
  40. this._webService.WWWWithTimeout(this._webService.ValidatePurchase(purchase.ProductID, purchase.Receipt, purchase.MetaData), delegate(CIGWebService.WWWResponse result)
  41. {
  42. if (!result.TimedOut)
  43. {
  44. if (string.IsNullOrEmpty(result.Error) && !string.IsNullOrEmpty(result.Text))
  45. {
  46. string value = result.Text.Trim();
  47. if ("OK".Equals(value) || "ERROR".Equals(value))
  48. {
  49. UnityEngine.Debug.Log("Purchase verified " + purchase.ProductID);
  50. this._unverifiedPurchases.Remove(purchase);
  51. this.SerializePurchases(this._unverifiedPurchases, "UnverifiedPurchases");
  52. bool flag = "OK".Equals(value);
  53. if (flag)
  54. {
  55. SingletonMonobehaviour<CIGWebService>.Instance.Properties["is_paying_user"] = true.ToString();
  56. }
  57. if (this.purchaseVerifiedEvent != null)
  58. {
  59. this.purchaseVerifiedEvent(purchase.ProductID, purchase.TransactionID, flag);
  60. }
  61. this.ValidatePurchases();
  62. }
  63. else
  64. {
  65. UnityEngine.Debug.LogError("Verify purchase request unknown result: " + result.Text);
  66. }
  67. }
  68. else
  69. {
  70. UnityEngine.Debug.LogError("Verify purchase request error: " + result.Text);
  71. }
  72. }
  73. else
  74. {
  75. UnityEngine.Debug.LogError("Verify purchase request error, request timedout.");
  76. }
  77. }, 5f);
  78. }
  79. else
  80. {
  81. UnityEngine.Debug.LogError("IAPValidationManagere._webService is null!");
  82. }
  83. }
  84. }
  85. private void OnWebServicesPullRequestCompleted()
  86. {
  87. Dictionary<string, string> properties = SingletonMonobehaviour<CIGWebService>.Instance.Properties;
  88. if (properties.ContainsKey("verified_iaps"))
  89. {
  90. bool flag = false;
  91. List<string> list = new List<string>(properties["verified_iaps"].Split(new char[]
  92. {
  93. ','
  94. }));
  95. int count = this._untrackedPurchases.Count;
  96. for (int i = count - 1; i >= 0; i--)
  97. {
  98. IAPValidationManager.ValidationPurchase validationPurchase = this._untrackedPurchases[i];
  99. string transactionID = validationPurchase.TransactionID;
  100. if (list.Contains(transactionID))
  101. {
  102. string productID = validationPurchase.ProductID;
  103. CIG3StoreProduct cig3StoreProduct = SingletonMonobehaviour<CIGStoreManager>.Instance.Store.FindProduct(productID);
  104. if (cig3StoreProduct != null)
  105. {
  106. }
  107. else
  108. {
  109. UnityEngine.Debug.LogErrorFormat("[Adjust Analytics] Product with productId={0} not found, verification not sent to Adjust.", new object[]
  110. {
  111. productID
  112. });
  113. }
  114. this._untrackedPurchases.RemoveAt(i);
  115. flag = true;
  116. }
  117. }
  118. if (flag)
  119. {
  120. this.SerializePurchases(this._untrackedPurchases, "UntrackedPurchases");
  121. }
  122. }
  123. }
  124. private void SerializePurchases(List<IAPValidationManager.ValidationPurchase> purchases, string serializationKey)
  125. {
  126. List<object> list = new List<object>();
  127. for (int i = 0; i < purchases.Count; i++)
  128. {
  129. list.Add(purchases[i].Serialize());
  130. }
  131. if (!this.storage.ContainsKey(serializationKey))
  132. {
  133. this.storage.Add(serializationKey, null);
  134. }
  135. this.storage[serializationKey] = list;
  136. Storage.SaveLifecycle(StorageLifecycle.Purchases, false);
  137. }
  138. private void DeserializePurchases(List<IAPValidationManager.ValidationPurchase> purchases, string serializationKey)
  139. {
  140. purchases.Clear();
  141. if (this.storage.ContainsKey(serializationKey))
  142. {
  143. List<object> list = (List<object>)this.storage[serializationKey];
  144. int count = list.Count;
  145. for (int i = 0; i < count; i++)
  146. {
  147. purchases.Add(new IAPValidationManager.ValidationPurchase((Dictionary<string, object>)list[i]));
  148. }
  149. }
  150. }
  151. private Dictionary<string, object> storage
  152. {
  153. get
  154. {
  155. return Storage.Get(StorageLifecycle.Purchases).GetDictionary("IAPValidationStorage");
  156. }
  157. }
  158. private const string UNVERIFIED_PURCHASES_KEY = "UnverifiedPurchases";
  159. private const string UntrackedPurchasesKey = "UntrackedPurchases";
  160. private const StorageLifecycle VALIDATION_LIFECYCLE = StorageLifecycle.Purchases;
  161. private const string IsPayingUserServerKey = "is_paying_user";
  162. private const string VerifiedIAPsKey = "verified_iaps";
  163. private CIGWebService _webService;
  164. private List<IAPValidationManager.ValidationPurchase> _unverifiedPurchases = new List<IAPValidationManager.ValidationPurchase>();
  165. private List<IAPValidationManager.ValidationPurchase> _untrackedPurchases = new List<IAPValidationManager.ValidationPurchase>();
  166. private class ValidationPurchase
  167. {
  168. public ValidationPurchase(string productID, string transactionID, string receipt, Dictionary<string, string> metaData)
  169. {
  170. this.TransactionID = transactionID;
  171. this.ProductID = productID;
  172. this.Receipt = receipt;
  173. this.MetaData = metaData;
  174. }
  175. public ValidationPurchase(Dictionary<string, object> storageDictionary)
  176. {
  177. string empty = string.Empty;
  178. this.Receipt = empty;
  179. this.TransactionID = empty;
  180. if (storageDictionary.ContainsKey("TransactionID"))
  181. {
  182. this.TransactionID = (string)storageDictionary["TransactionID"];
  183. }
  184. if (storageDictionary.ContainsKey("ProductID"))
  185. {
  186. this.ProductID = (string)storageDictionary["ProductID"];
  187. }
  188. if (storageDictionary.ContainsKey("Receipt"))
  189. {
  190. this.Receipt = (string)storageDictionary["Receipt"];
  191. }
  192. if (storageDictionary.ContainsKey("MetaData"))
  193. {
  194. this.MetaData = this.LoadDictionary<string>(storageDictionary, "MetaData");
  195. }
  196. else
  197. {
  198. this.MetaData = new Dictionary<string, string>();
  199. }
  200. }
  201. public override string ToString()
  202. {
  203. return string.Concat(new string[]
  204. {
  205. "[PurchaseEntry: TransactionID=",
  206. this.TransactionID,
  207. ", productID=",
  208. this.ProductID,
  209. "]"
  210. });
  211. }
  212. public string TransactionID { get; private set; }
  213. public string ProductID { get; private set; }
  214. public string Receipt { get; private set; }
  215. public Dictionary<string, string> MetaData { get; private set; }
  216. private void SaveDictionary<T>(Dictionary<string, object> storage, string key, Dictionary<string, T> values)
  217. {
  218. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  219. foreach (KeyValuePair<string, T> keyValuePair in values)
  220. {
  221. dictionary.Add(keyValuePair.Key, keyValuePair.Value);
  222. }
  223. storage[key] = dictionary;
  224. }
  225. private Dictionary<string, TField> LoadDictionary<TField>(Dictionary<string, object> storage, string key)
  226. {
  227. if (!storage.ContainsKey(key))
  228. {
  229. return new Dictionary<string, TField>();
  230. }
  231. if (!(storage[key] is Dictionary<string, object>))
  232. {
  233. throw new InvalidOperationException(string.Format("Found {0} at storage[{1}]. Expecting {2}", storage[key].GetType().Name, key, typeof(Dictionary<string, object>).Name));
  234. }
  235. Dictionary<string, TField> dictionary = new Dictionary<string, TField>();
  236. Dictionary<string, object> dictionary2 = (Dictionary<string, object>)storage[key];
  237. foreach (KeyValuePair<string, object> keyValuePair in dictionary2)
  238. {
  239. if (!(dictionary2[keyValuePair.Key] is TField))
  240. {
  241. throw new InvalidOperationException(string.Format("Found {0} at storage[{1}]. Expecting {2}", storage[key].GetType().Name, key, typeof(TField).Name));
  242. }
  243. dictionary.Add(keyValuePair.Key, (TField)((object)dictionary2[keyValuePair.Key]));
  244. }
  245. return dictionary;
  246. }
  247. public Dictionary<string, object> Serialize()
  248. {
  249. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  250. dictionary["TransactionID"] = this.TransactionID;
  251. dictionary["ProductID"] = this.ProductID;
  252. dictionary["Receipt"] = this.Receipt;
  253. this.SaveDictionary<string>(dictionary, "MetaData", this.MetaData);
  254. return dictionary;
  255. }
  256. private const string ProductIDKey = "ProductID";
  257. private const string TransactionIDKey = "TransactionID";
  258. private const string ReceiptKey = "Receipt";
  259. private const string MetaDataKey = "MetaData";
  260. }
  261. }
  262. }