No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UDPImpl.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using UnityEngine.Purchasing.Extension;
  5. using UnityEngine.Purchasing.MiniJSON;
  6. namespace UnityEngine.Purchasing
  7. {
  8. internal class UDPImpl : JSONStore, IUDPExtensions
  9. {
  10. private INativeUDPStore m_Bindings;
  11. private object m_UserInfo = null; //UDP UserInfo via Reflection
  12. private string m_LastInitError;
  13. private const string k_Unknown = "Unknown"; // From PurchaseFailureReason
  14. private bool m_Initialized = false;
  15. private const int PURCHASE_PENDING_CODE = -303;
  16. private Action<Product> m_DeferredCallback;
  17. private const string k_Errorcode = "errorCode"; // From UDP sdk purchase pending state
  18. public void SetNativeStore(INativeUDPStore nativeUdpStore)
  19. {
  20. m_Bindings = nativeUdpStore;
  21. }
  22. #region IStore
  23. /// <summary>
  24. /// This function only save the IStoreCallback.
  25. /// The initialization of UDP is put into <see cref="RetrieveProducts"/>,
  26. /// because in <see cref="PurchasingManager"/>, <see cref="Initialize"/> and <see cref="RetrieveProducts"/>
  27. /// will be called together.
  28. /// However, for UDP, <see cref="RetrieveProducts"/> must been done AFTER <see cref="Initialize"/> succeeds.
  29. /// If the <see cref="Initialize"/> involves login, it may take a long time and <see cref="RetrieveProducts"/>
  30. /// will fail.
  31. /// </summary>
  32. /// <param name="callback"></param>
  33. public override void Initialize(IStoreCallback callback)
  34. {
  35. unity = callback;
  36. }
  37. public override void RetrieveProducts(ReadOnlyCollection<ProductDefinition> products)
  38. {
  39. void retrieveCallback(bool success, string json)
  40. {
  41. if (success && !string.IsNullOrEmpty(json))
  42. {
  43. OnProductsRetrieved(json);
  44. }
  45. else
  46. {
  47. m_Logger.LogIAPWarning("RetrieveProducts failed: " + json);
  48. }
  49. }
  50. if (!m_Initialized)
  51. {
  52. m_Bindings.Initialize((success, message) =>
  53. {
  54. // Clear the message and userInfo when an Initialized is called successfully.
  55. m_LastInitError = "";
  56. m_UserInfo = null;
  57. if (success)
  58. {
  59. if (!string.IsNullOrEmpty(message))
  60. {
  61. var dic = message.HashtableFromJson();
  62. if (dic.ContainsKey("Channel"))
  63. {
  64. var udpUserInfo = UserInfoInterface.GetClassType();
  65. if (udpUserInfo != null)
  66. {
  67. m_UserInfo = Activator.CreateInstance(udpUserInfo);
  68. DictionaryToStringProperty(dic, m_UserInfo);
  69. }
  70. }
  71. }
  72. m_Initialized = true;
  73. // IStoreCallback do not have initialize success callback.
  74. m_Bindings.RetrieveProducts(products, retrieveCallback);
  75. }
  76. else
  77. {
  78. m_LastInitError = message;
  79. unity.OnSetupFailed(InitializationFailureReason.AppNotKnown, null);
  80. }
  81. });
  82. }
  83. else
  84. {
  85. m_Bindings.RetrieveProducts(products, retrieveCallback);
  86. }
  87. }
  88. public override void Purchase(ProductDefinition product, string developerPayload)
  89. {
  90. m_Bindings.Purchase(product.storeSpecificId, (success, message) =>
  91. {
  92. var dic = message.HashtableFromJson();
  93. if (success)
  94. {
  95. var transactionId = dic.GetString("GameOrderId");
  96. var storeSpecificId = dic.GetString("ProductId");
  97. if (!string.IsNullOrEmpty(transactionId))
  98. {
  99. dic["transactionId"] = transactionId;
  100. }
  101. if (!string.IsNullOrEmpty(storeSpecificId))
  102. {
  103. dic["storeSpecificId"] = storeSpecificId;
  104. }
  105. if (!product.storeSpecificId.Equals(storeSpecificId))
  106. {
  107. m_Logger.LogFormat(LogType.Error,
  108. "UDPImpl received mismatching product Id for purchase. Expected {0}, received {1}",
  109. product.storeSpecificId, storeSpecificId);
  110. }
  111. var data = dic.toJson();
  112. unity.OnPurchaseSucceeded(product.storeSpecificId, data, transactionId);
  113. }
  114. else
  115. {
  116. if (dic.ContainsKey(k_Errorcode) && Convert.ToInt32(dic[k_Errorcode]) == PURCHASE_PENDING_CODE)
  117. {
  118. if (null != m_DeferredCallback)
  119. {
  120. OnPurchaseDeferred(product.storeSpecificId);
  121. }
  122. return;
  123. }
  124. var reason = (PurchaseFailureReason)Enum.Parse(typeof(PurchaseFailureReason),
  125. k_Unknown);
  126. var reasonString = reason.ToString();
  127. var errDic = new Dictionary<string, object> { ["error"] = reasonString };
  128. if (dic.ContainsKey("purchaseInfo"))
  129. {
  130. errDic["purchaseInfo"] = dic["purchaseInfo"];
  131. }
  132. var errData = errDic.toJson();
  133. var pfd = new PurchaseFailureDescription(product.storeSpecificId, reason, message);
  134. unity.OnPurchaseFailed(pfd);
  135. }
  136. }, developerPayload);
  137. }
  138. private void OnPurchaseDeferred(string productId)
  139. {
  140. if (null == productId || productId == "")
  141. {
  142. return;
  143. }
  144. var product = unity.products?.WithStoreSpecificID(productId);
  145. if (null != product)
  146. {
  147. m_DeferredCallback(product);
  148. }
  149. }
  150. public override void FinishTransaction(ProductDefinition product, string transactionId)
  151. {
  152. if (product == null || transactionId == null)
  153. {
  154. return;
  155. }
  156. if (product.type == ProductType.Consumable)
  157. {
  158. m_Bindings.FinishTransaction(product, transactionId);
  159. }
  160. }
  161. #endregion
  162. #region extension
  163. public string GetLastInitializationError()
  164. {
  165. return m_LastInitError;
  166. }
  167. public object GetUserInfo()
  168. {
  169. return m_UserInfo;
  170. }
  171. public void RegisterPurchaseDeferredListener(Action<Product> callback)
  172. {
  173. m_DeferredCallback = callback;
  174. }
  175. public void EnableDebugLog(bool enable)
  176. {
  177. var storeServiceInfo = StoreServiceInterface.GetClassType();
  178. if (storeServiceInfo != null)
  179. {
  180. var enableDebugLogging = StoreServiceInterface.GetEnableDebugLoggingMethod();
  181. enableDebugLogging.Invoke(null, new object[] { enable });
  182. }
  183. else
  184. {
  185. Debug.LogError("Failed to access StoreService for UDP, cannot enable logging");
  186. }
  187. }
  188. #endregion
  189. #region helper functions
  190. private static void DictionaryToStringProperty(Dictionary<string, object> dic, object info)
  191. {
  192. var properties = info.GetType().GetProperties();
  193. foreach (var property in properties)
  194. {
  195. if (property.PropertyType == typeof(string))
  196. {
  197. property.SetValue(info, dic.GetString(property.Name), null);
  198. }
  199. }
  200. }
  201. #endregion
  202. }
  203. }