Purchasing.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Reflection;
  3. namespace UnityEngine.Advertisements.Purchasing
  4. {
  5. /// <summary>
  6. /// Enumerated events related to in-app purchasing (IAP).
  7. /// </summary>
  8. public enum PurchasingEvent
  9. {
  10. COMMAND,
  11. VERSION,
  12. CATALOG,
  13. INITIALIZATION,
  14. EVENT
  15. }
  16. static class Purchasing
  17. {
  18. static Type s_PurchasingManagerType;
  19. static Boolean s_Initialized;
  20. static MethodInfo s_PurchasingInitiatePurchaseMethodInfo,
  21. s_PurchasingGetPromoVersionMethodInfo,
  22. s_PurchasingGetPromoCatalogMethodInfo;
  23. static string s_PurchasingManagerClassName = "UnityEngine.Purchasing.Promo,Stores";
  24. static string s_PurchasingInitiatePurchaseMethodName = "InitiatePurchasingCommand",
  25. s_PurchasingGetPromoVersionMethodName = "Version",
  26. s_PurchasingGetPromoCatalogMethodName = "QueryPromoProducts";
  27. static IPurchasingEventSender s_Platform;
  28. public static Boolean Initialize(IPurchasingEventSender platform)
  29. {
  30. if (!s_Initialized)
  31. {
  32. try
  33. {
  34. s_PurchasingManagerType = Type.GetType(s_PurchasingManagerClassName, true);
  35. s_PurchasingInitiatePurchaseMethodInfo = s_PurchasingManagerType.GetMethod(s_PurchasingInitiatePurchaseMethodName, new Type[] { typeof(string) });
  36. s_PurchasingGetPromoVersionMethodInfo = s_PurchasingManagerType.GetMethod(s_PurchasingGetPromoVersionMethodName);
  37. s_PurchasingGetPromoCatalogMethodInfo = s_PurchasingManagerType.GetMethod(s_PurchasingGetPromoCatalogMethodName);
  38. }
  39. catch (Exception exception)
  40. {
  41. Debug.LogWarning(exception.Message + "It is likely that a promo has been enabled on a placement, but IAP Promo has not been enabled in the project.");
  42. return false;
  43. }
  44. s_Initialized = true;
  45. s_Platform = platform;
  46. }
  47. return s_Initialized;
  48. }
  49. public static Boolean InitiatePurchasingCommand(string eventString)
  50. {
  51. Boolean isCommandSuccessful = false;
  52. if (s_PurchasingInitiatePurchaseMethodInfo != null)
  53. {
  54. try
  55. {
  56. isCommandSuccessful = (Boolean)s_PurchasingInitiatePurchaseMethodInfo.Invoke(s_PurchasingManagerType, new[] { eventString });
  57. }
  58. catch (Exception exception)
  59. {
  60. Debug.LogWarning(exception.Message);
  61. return false;
  62. }
  63. }
  64. return isCommandSuccessful;
  65. }
  66. public static String GetPurchasingCatalog()
  67. {
  68. String purchasingCatalog = "";
  69. if (s_PurchasingGetPromoCatalogMethodInfo != null)
  70. {
  71. try
  72. {
  73. purchasingCatalog = (String)s_PurchasingGetPromoCatalogMethodInfo.Invoke(s_PurchasingManagerType, null);
  74. }
  75. catch (Exception exception)
  76. {
  77. Debug.LogWarning(exception.Message);
  78. }
  79. }
  80. return purchasingCatalog ?? "NULL";
  81. }
  82. public static String GetPromoVersion()
  83. {
  84. String promoVersion = "";
  85. if (s_PurchasingGetPromoVersionMethodInfo != null)
  86. {
  87. try
  88. {
  89. promoVersion = (String)s_PurchasingGetPromoVersionMethodInfo.Invoke(s_PurchasingManagerType, null);
  90. }
  91. catch (Exception exception)
  92. {
  93. Debug.LogWarning(exception.Message);
  94. }
  95. }
  96. return promoVersion ?? "NULL";
  97. }
  98. public static Boolean SendEvent(string payload)
  99. {
  100. if (s_Platform == null)
  101. {
  102. return false;
  103. }
  104. else
  105. {
  106. s_Platform.SendPurchasingEvent(payload);
  107. return true;
  108. }
  109. }
  110. }
  111. }