Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. #if UNITY_PURCHASING
  2.  
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine.Purchasing.Security;
  6. using UnityEngine.Purchasing;
  7.  
  8. namespace UnityEngine.Purchasing
  9. {
  10. /// <summary>
  11. /// Automatically initializes Unity IAP with the products defined in the IAP Catalog (if enabled in the UI).
  12. /// Manages IAPButtons and IAPListeners.
  13. /// </summary>
  14. public class CodelessIAPStoreListener : IStoreListener
  15. {
  16. private static CodelessIAPStoreListener instance;
  17. private List<IAPButton> activeButtons = new List<IAPButton>();
  18. private List<IAPListener> activeListeners = new List<IAPListener> ();
  19. private static bool unityPurchasingInitialized;
  20.  
  21. protected IStoreController controller;
  22. protected IExtensionProvider extensions;
  23. protected ProductCatalog catalog;
  24.  
  25.  
  26. // Allows outside sources to know whether the full initialization has taken place.
  27. public static bool initializationComplete;
  28.  
  29. [RuntimeInitializeOnLoadMethod]
  30. static void InitializeCodelessPurchasingOnLoad() {
  31. ProductCatalog catalog = ProductCatalog.LoadDefaultCatalog();
  32. if (catalog.enableCodelessAutoInitialization && !catalog.IsEmpty() && instance == null)
  33. {
  34. CreateCodelessIAPStoreListenerInstance();
  35. }
  36. }
  37.  
  38. private static void InitializePurchasing()
  39. {
  40. StandardPurchasingModule module = StandardPurchasingModule.Instance();
  41. module.useFakeStoreUIMode = FakeStoreUIMode.StandardUser;
  42.  
  43. ConfigurationBuilder builder = ConfigurationBuilder.Instance(module);
  44.  
  45. IAPConfigurationHelper.PopulateConfigurationBuilder(ref builder, instance.catalog);
  46.  
  47. UnityPurchasing.Initialize(instance, builder);
  48.  
  49. unityPurchasingInitialized = true;
  50. }
  51.  
  52. private CodelessIAPStoreListener()
  53. {
  54. catalog = ProductCatalog.LoadDefaultCatalog();
  55. }
  56.  
  57. public static CodelessIAPStoreListener Instance
  58. {
  59. get
  60. {
  61. if (instance == null)
  62. {
  63. CreateCodelessIAPStoreListenerInstance();
  64. }
  65. return instance;
  66. }
  67. }
  68.  
  69. void SearchSubscriptionButton(string status)
  70. {
  71. GameObject subscription = GameObject.FindGameObjectWithTag("Subscription");
  72.  
  73. if (subscription != null)
  74. {
  75. if (status != "")
  76. {
  77. IAPButton.defaultCountFreeCards = 131;
  78. }
  79. else
  80. {
  81. IAPButton.defaultCountFreeCards = 3;
  82. }
  83. }
  84. }
  85.  
  86. /// <summary>
  87. /// Creates the static instance of CodelessIAPStoreListener and initializes purchasing
  88. /// </summary>
  89. private static void CreateCodelessIAPStoreListenerInstance()
  90. {
  91. instance = new CodelessIAPStoreListener();
  92. if (!unityPurchasingInitialized)
  93. {
  94. Debug.Log("Initializing UnityPurchasing via Codeless IAP");
  95. InitializePurchasing();
  96. }
  97. }
  98.  
  99. public IStoreController StoreController
  100. {
  101. get { return controller; }
  102. }
  103.  
  104. public IExtensionProvider ExtensionProvider
  105. {
  106. get { return extensions; }
  107. }
  108.  
  109. public bool HasProductInCatalog(string productID)
  110. {
  111. foreach (var product in catalog.allProducts)
  112. {
  113. if (product.id == productID)
  114. {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120.  
  121. public Product GetProduct(string productID)
  122. {
  123. if (controller != null && controller.products != null && !string.IsNullOrEmpty(productID))
  124. {
  125. return controller.products.WithID(productID);
  126. }
  127. Debug.LogError("CodelessIAPStoreListener attempted to get unknown product " + productID);
  128. return null;
  129. }
  130.  
  131. public void AddButton(IAPButton button)
  132. {
  133. activeButtons.Add(button);
  134. }
  135.  
  136. public void RemoveButton(IAPButton button)
  137. {
  138. activeButtons.Remove(button);
  139. }
  140.  
  141. public void AddListener(IAPListener listener)
  142. {
  143. activeListeners.Add (listener);
  144. }
  145.  
  146. public void RemoveListener(IAPListener listener)
  147. {
  148. activeListeners.Remove (listener);
  149. }
  150.  
  151. public void InitiatePurchase(string productID)
  152. {
  153. if (controller == null)
  154. {
  155. Debug.LogError("Purchase failed because Purchasing was not initialized correctly");
  156.  
  157. foreach (var button in activeButtons)
  158. {
  159. if (button.productId == productID)
  160. {
  161. button.OnPurchaseFailed(null, Purchasing.PurchaseFailureReason.PurchasingUnavailable);
  162. }
  163. }
  164. return;
  165. }
  166.  
  167. controller.InitiatePurchase(productID);
  168. }
  169.  
  170. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  171. {
  172. initializationComplete = true;
  173. this.controller = controller;
  174. this.extensions = extensions;
  175.  
  176. foreach (Product p in controller.products.all)
  177. {
  178. GooglePurchaseData data = new GooglePurchaseData(p.receipt);
  179.  
  180. string status = data.json.autoRenewing;
  181.  
  182. SearchSubscriptionButton(status);
  183. }
  184.  
  185. foreach (var button in activeButtons)
  186. {
  187. button.UpdateText();
  188. }
  189. }
  190.  
  191. public void OnInitializeFailed(InitializationFailureReason error)
  192. {
  193. Debug.LogError(string.Format("Purchasing failed to initialize. Reason: {0}", error.ToString()));
  194. }
  195.  
  196. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  197. {
  198. PurchaseProcessingResult result;
  199.  
  200. // if any receiver consumed this purchase we return the status
  201. bool consumePurchase = false;
  202. bool resultProcessed = false;
  203.  
  204. foreach (IAPButton button in activeButtons)
  205. {
  206. if (button.productId == e.purchasedProduct.definition.id)
  207. {
  208. result = button.ProcessPurchase(e);
  209.  
  210. if (result == PurchaseProcessingResult.Complete) {
  211.  
  212. consumePurchase = true;
  213. }
  214.  
  215. resultProcessed = true;
  216. }
  217. }
  218.  
  219. foreach (IAPListener listener in activeListeners)
  220. {
  221. result = listener.ProcessPurchase(e);
  222.  
  223. if (result == PurchaseProcessingResult.Complete) {
  224.  
  225. consumePurchase = true;
  226. }
  227.  
  228. resultProcessed = true;
  229. }
  230.  
  231. // we expect at least one receiver to get this message
  232. if (!resultProcessed) {
  233.  
  234. Debug.LogError("Purchase not correctly processed for product \"" +
  235. e.purchasedProduct.definition.id +
  236. "\". Add an active IAPButton to process this purchase, or add an IAPListener to receive any unhandled purchase events.");
  237.  
  238. }
  239.  
  240. return (consumePurchase) ? PurchaseProcessingResult.Complete : PurchaseProcessingResult.Pending;
  241. }
  242.  
  243. public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
  244. {
  245. bool resultProcessed = false;
  246.  
  247. foreach (IAPButton button in activeButtons)
  248. {
  249. if (button.productId == product.definition.id)
  250. {
  251. button.OnPurchaseFailed(product, reason);
  252.  
  253. resultProcessed = true;
  254. }
  255. }
  256.  
  257. foreach (IAPListener listener in activeListeners)
  258. {
  259. listener.OnPurchaseFailed(product, reason);
  260.  
  261. resultProcessed = true;
  262. }
  263.  
  264. // we expect at least one receiver to get this message
  265. if (!resultProcessed) {
  266.  
  267. Debug.LogError("Failed purchase not correctly handled for product \"" + product.definition.id +
  268. "\". Add an active IAPButton to handle this failure, or add an IAPListener to receive any unhandled purchase failures.");
  269. }
  270.  
  271. return;
  272. }
  273. }
  274. }
  275.  
  276. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement