Events.Transaction.deprecated.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace Unity.Services.Analytics
  6. {
  7. public static partial class Events
  8. {
  9. [Obsolete("This enum has been moved outside the Events class, the standalone enum should be used instead. This enum will be removed in an upcoming release.")]
  10. public enum TransactionServer
  11. {
  12. APPLE = 0,
  13. AMAZON = 1,
  14. GOOGLE = 2
  15. }
  16. [Obsolete("This enum has been moved outside the Events class, the standalone enum should be used instead. This enum will be removed in an upcoming release.")]
  17. public enum TransactionType
  18. {
  19. INVALID = 0,
  20. SALE = 1,
  21. PURCHASE = 2,
  22. TRADE = 3
  23. }
  24. [Obsolete("This struct has been moved outside the Events class, and it's parameters now conform to C# guidelines. Please use the standalone struct instead. This struct will be removed in an upcoming release.")]
  25. public struct Item
  26. {
  27. public string itemName;
  28. public string itemType;
  29. public Int64 itemAmount;
  30. }
  31. [Obsolete("This struct has been moved outside the Events class, and it's parameters now conform to C# guidelines. Please use the standalone struct instead. This struct will be removed in an upcoming release.")]
  32. public struct VirtualCurrency
  33. {
  34. public string virtualCurrencyName;
  35. public string virtualCurrencyType;
  36. public Int64 virtualCurrencyAmount;
  37. }
  38. [Obsolete("This struct has been moved outside the Events class, and it's parameters now conform to C# guidelines. Please use the standalone struct instead. This struct will be removed in an upcoming release.")]
  39. public struct RealCurrency
  40. {
  41. public string realCurrencyType;
  42. public Int64 realCurrencyAmount;
  43. }
  44. [Obsolete("This struct has been moved outside the Events class, and it's parameters now conform to C# guidelines. Please use the standalone struct instead. This struct will be removed in an upcoming release.")]
  45. public struct Product
  46. {
  47. //Optional
  48. public RealCurrency? realCurrency;
  49. public List<VirtualCurrency> virtualCurrencies;
  50. public List<Item> items;
  51. }
  52. [Obsolete("This struct has been moved outside the Events class, and it's parameters now conform to C# guidelines. Please use the standalone struct instead. This struct will be removed in an upcoming release.")]
  53. public struct TransactionParameters
  54. {
  55. [Obsolete]
  56. public bool? isInitiator;
  57. /// <summary>
  58. /// Optional.
  59. /// If this is left null or empty, the machine's locale will be used
  60. /// </summary>
  61. public string paymentCountry;
  62. public string productID;
  63. public Int64? revenueValidated;
  64. public string transactionID;
  65. public string transactionReceipt;
  66. public string transactionReceiptSignature;
  67. public TransactionServer? transactionServer;
  68. public string transactorID;
  69. public string storeItemSkuID;
  70. public string storeItemID;
  71. public string storeID;
  72. public string storeSourceID;
  73. //Required
  74. public string transactionName;
  75. public TransactionType transactionType;
  76. public Product productsReceived;
  77. public Product productsSpent;
  78. }
  79. /// <summary>
  80. /// Record a Transaction event.
  81. /// </summary>
  82. /// <param name="transactionParameters">(Required) Helper object to handle parameters.</param>
  83. [Obsolete("The interface provided by this method has moved to AnalyticsService.Instance.Transaction, and should be accessed from there instead. This API will be removed in an upcoming release.")]
  84. public static void Transaction(TransactionParameters transactionParameters)
  85. {
  86. Analytics.TransactionServer? transactionServer = null;
  87. if (transactionParameters.transactionServer != null)
  88. {
  89. transactionServer = (Analytics.TransactionServer)(int)transactionParameters.transactionServer;
  90. }
  91. var newParameters = new Analytics.TransactionParameters
  92. {
  93. PaymentCountry = transactionParameters.paymentCountry,
  94. ProductID = transactionParameters.productID,
  95. RevenueValidated = transactionParameters.revenueValidated,
  96. TransactionID = transactionParameters.transactionID,
  97. TransactionReceipt = transactionParameters.transactionReceipt,
  98. TransactionReceiptSignature = transactionParameters.transactionReceiptSignature,
  99. TransactionServer = transactionServer,
  100. TransactorID = transactionParameters.transactorID,
  101. StoreItemSkuID = transactionParameters.storeItemSkuID,
  102. StoreItemID = transactionParameters.storeItemID,
  103. StoreID = transactionParameters.storeID,
  104. StoreSourceID = transactionParameters.storeSourceID,
  105. TransactionName = transactionParameters.transactionName,
  106. TransactionType = (Analytics.TransactionType)(int)transactionParameters.transactionType,
  107. ProductsReceived = ConvertProduct(transactionParameters.productsReceived),
  108. ProductsSpent = ConvertProduct(transactionParameters.productsSpent)
  109. };
  110. AnalyticsService.Instance.Transaction(newParameters);
  111. }
  112. static Analytics.Product ConvertProduct(Product from)
  113. {
  114. var newProduct = new Analytics.Product();
  115. if (from.items != null)
  116. {
  117. newProduct.Items = ConvertItems(from.items);
  118. }
  119. if (from.realCurrency != null)
  120. {
  121. var unwrappedRealCurrency = (RealCurrency)from.realCurrency;
  122. newProduct.RealCurrency = new Analytics.RealCurrency
  123. {
  124. RealCurrencyAmount = unwrappedRealCurrency.realCurrencyAmount,
  125. RealCurrencyType = unwrappedRealCurrency.realCurrencyType
  126. };
  127. }
  128. if (from.virtualCurrencies != null)
  129. {
  130. newProduct.VirtualCurrencies = from.virtualCurrencies.Select(vc =>
  131. {
  132. var virtualCurrencyType = VirtualCurrencyType.GRIND;
  133. if (!string.IsNullOrEmpty(vc.virtualCurrencyType) || !Enum.TryParse(vc.virtualCurrencyType, out virtualCurrencyType))
  134. {
  135. virtualCurrencyType = VirtualCurrencyType.GRIND;
  136. }
  137. return new Analytics.VirtualCurrency
  138. {
  139. VirtualCurrencyAmount = vc.virtualCurrencyAmount,
  140. VirtualCurrencyName = vc.virtualCurrencyName,
  141. VirtualCurrencyType = virtualCurrencyType
  142. };
  143. }).ToList();
  144. }
  145. return newProduct;
  146. }
  147. static List<Analytics.Item> ConvertItems(List<Item> from)
  148. {
  149. return from.Select(i => new Analytics.Item { ItemAmount = i.itemAmount, ItemName = i.itemName, ItemType = i.itemType }).ToList();
  150. }
  151. }
  152. }