IPurchasingAdapter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections.Generic;
  2. namespace UnityEngine.Monetization
  3. {
  4. public interface IPurchasingAdapter
  5. {
  6. void RetrieveProducts(IRetrieveProductsListener listener);
  7. void Purchase(string productID, ITransactionListener listener, IDictionary<string, object> extras);
  8. }
  9. public interface IRetrieveProductsListener
  10. {
  11. void OnProductsRetrieved(ICollection<Product> products);
  12. }
  13. public interface ITransactionListener
  14. {
  15. void OnTransactionComplete(TransactionDetails details);
  16. void OnTransactionError(TransactionErrorDetails details);
  17. }
  18. // https://docs.unity3d.com/Manual/UnityAnalyticsMonetization.html
  19. public struct TransactionDetails
  20. {
  21. public string productId;
  22. public string transactionId;
  23. public decimal price;
  24. public string currency;
  25. public string receipt;
  26. public IDictionary<string, object> extras;
  27. internal IDictionary<string, object> ToJsonDictionary()
  28. {
  29. return new Dictionary<string, object>
  30. {
  31. {"productId", productId},
  32. {"transactionId", transactionId},
  33. {"receipt", receipt},
  34. {"price", price},
  35. {"currency", currency},
  36. {"extras", extras}
  37. };
  38. }
  39. }
  40. public struct TransactionErrorDetails
  41. {
  42. public TransactionError transactionError;
  43. public string exceptionMessage;
  44. public Store store;
  45. public string storeSpecificErrorCode;
  46. public IDictionary<string, object> extras;
  47. internal IDictionary<string, object> ToJsonDictionary()
  48. {
  49. return new Dictionary<string, object>
  50. {
  51. {"transactionError", TransactionErrorToString()},
  52. {"exceptionMessage", exceptionMessage},
  53. {"store", StoreToString()},
  54. {"storeSpecificErrorCode", storeSpecificErrorCode},
  55. {"extras", extras}
  56. };
  57. }
  58. private string StoreToString()
  59. {
  60. switch (store)
  61. {
  62. case Store.NotSpecified:
  63. return "NotSpecified";
  64. case Store.GooglePlay:
  65. return "GooglePlay";
  66. case Store.AmazonAppStore:
  67. return "AmazonAppStore";
  68. case Store.CloudMoolah:
  69. return "CloudMoolah";
  70. case Store.SamsungApps:
  71. return "SamsungApps";
  72. case Store.XiaomiMiPay:
  73. return "XiaomiMiPay";
  74. case Store.MacAppStore:
  75. return "MacAppStore";
  76. case Store.AppleAppStore:
  77. return "AppleAppStore";
  78. case Store.WinRT:
  79. return "WinRT";
  80. case Store.TizenStore:
  81. return "TizenStore";
  82. case Store.FacebookStore:
  83. return "FacebookStore";
  84. default:
  85. return "NotSpecified";
  86. }
  87. }
  88. private string TransactionErrorToString()
  89. {
  90. switch (transactionError)
  91. {
  92. case TransactionError.NotSupported:
  93. return "NotSupported";
  94. case TransactionError.ItemUnavailable:
  95. return "ItemUnavailable";
  96. case TransactionError.UserCancelled:
  97. return "UserCancelled";
  98. case TransactionError.NetworkError:
  99. return "NetworkError";
  100. case TransactionError.ServerError:
  101. return "ServerError";
  102. case TransactionError.UnknownError:
  103. return "UnknownError";
  104. default:
  105. return "UnknownError";
  106. }
  107. }
  108. }
  109. }