Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LocalReceiptValidation.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Purchasing;
  5. using UnityEngine.Purchasing.Extension;
  6. using UnityEngine.Purchasing.Security;
  7. using UnityEngine.UI;
  8. namespace Samples.Purchasing.Core.LocalReceiptValidation
  9. {
  10. public class LocalReceiptValidation : MonoBehaviour, IStoreListener
  11. {
  12. IStoreController m_StoreController;
  13. CrossPlatformValidator m_Validator = null;
  14. //Your products IDs. They should match the ids of your products in your store.
  15. public string goldProductId = "com.mycompany.mygame.gold1";
  16. public ProductType productType = ProductType.Consumable;
  17. public Text GoldCountText;
  18. public UserWarning userWarning;
  19. public Toggle appleCertificateToggle;
  20. int m_GoldCount;
  21. bool m_UseAppleStoreKitTestCertificate;
  22. void Start()
  23. {
  24. userWarning.Clear();
  25. appleCertificateToggle.onValueChanged.AddListener(OnAppleStoreKitTestCertificateChanged);
  26. m_UseAppleStoreKitTestCertificate = appleCertificateToggle.isOn;
  27. InitializePurchasing();
  28. UpdateUI();
  29. }
  30. static bool IsCurrentStoreSupportedByValidator()
  31. {
  32. //The CrossPlatform validator only supports the GooglePlayStore and Apple's App Stores.
  33. return IsGooglePlayStoreSelected() || IsAppleAppStoreSelected();
  34. }
  35. static bool IsGooglePlayStoreSelected()
  36. {
  37. var currentAppStore = StandardPurchasingModule.Instance().appStore;
  38. return currentAppStore == AppStore.GooglePlay;
  39. }
  40. static bool IsAppleAppStoreSelected()
  41. {
  42. var currentAppStore = StandardPurchasingModule.Instance().appStore;
  43. return currentAppStore == AppStore.AppleAppStore ||
  44. currentAppStore == AppStore.MacAppStore;
  45. }
  46. void InitializePurchasing()
  47. {
  48. var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  49. builder.AddProduct(goldProductId, productType);
  50. UnityPurchasing.Initialize(this, builder);
  51. }
  52. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  53. {
  54. Debug.Log("In-App Purchasing successfully initialized");
  55. m_StoreController = controller;
  56. InitializeValidator();
  57. }
  58. void InitializeValidator()
  59. {
  60. if (IsCurrentStoreSupportedByValidator())
  61. {
  62. #if !UNITY_EDITOR
  63. var appleTangleData = m_UseAppleStoreKitTestCertificate ? AppleStoreKitTestTangle.Data() : AppleTangle.Data();
  64. m_Validator = new CrossPlatformValidator(GooglePlayTangle.Data(), appleTangleData, Application.identifier);
  65. #endif
  66. }
  67. else
  68. {
  69. userWarning.WarnInvalidStore(StandardPurchasingModule.Instance().appStore);
  70. }
  71. }
  72. public void OnInitializeFailed(InitializationFailureReason error)
  73. {
  74. OnInitializeFailed(error, null);
  75. }
  76. public void OnInitializeFailed(InitializationFailureReason error, string message)
  77. {
  78. var errorMessage = $"Purchasing failed to initialize. Reason: {error}.";
  79. if (message != null)
  80. {
  81. errorMessage += $" More details: {message}";
  82. }
  83. Debug.Log(errorMessage);
  84. }
  85. public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
  86. {
  87. Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
  88. }
  89. public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
  90. {
  91. Debug.Log($"Purchase failed - Product: '{product.definition.id}'," +
  92. $" Purchase failure reason: {failureDescription.reason}," +
  93. $" Purchase failure details: {failureDescription.message}");
  94. }
  95. public void BuyGold()
  96. {
  97. m_StoreController.InitiatePurchase(goldProductId);
  98. }
  99. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  100. {
  101. //Retrieve the purchased product
  102. var product = args.purchasedProduct;
  103. var isPurchaseValid = IsPurchaseValid(product);
  104. if (isPurchaseValid)
  105. {
  106. //Add the purchased product to the players inventory
  107. UnlockContent(product);
  108. Debug.Log("Valid receipt, unlocking content.");
  109. }
  110. else
  111. {
  112. Debug.Log("Invalid receipt, not unlocking content.");
  113. }
  114. //We return Complete, informing Unity IAP that the processing on our side is done and the transaction can be closed.
  115. return PurchaseProcessingResult.Complete;
  116. }
  117. bool IsPurchaseValid(Product product)
  118. {
  119. //If we the validator doesn't support the current store, we assume the purchase is valid
  120. if (IsCurrentStoreSupportedByValidator())
  121. {
  122. try
  123. {
  124. var result = m_Validator.Validate(product.receipt);
  125. //The validator returns parsed receipts.
  126. LogReceipts(result);
  127. }
  128. //If the purchase is deemed invalid, the validator throws an IAPSecurityException.
  129. catch (IAPSecurityException reason)
  130. {
  131. Debug.Log($"Invalid receipt: {reason}");
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. void UnlockContent(Product product)
  138. {
  139. if (product.definition.id == goldProductId)
  140. {
  141. AddGold();
  142. }
  143. }
  144. void AddGold()
  145. {
  146. m_GoldCount++;
  147. UpdateUI();
  148. }
  149. void UpdateUI()
  150. {
  151. GoldCountText.text = $"Your Gold: {m_GoldCount}";
  152. }
  153. static void LogReceipts(IEnumerable<IPurchaseReceipt> receipts)
  154. {
  155. Debug.Log("Receipt is valid. Contents:");
  156. foreach (var receipt in receipts)
  157. {
  158. LogReceipt(receipt);
  159. }
  160. }
  161. static void LogReceipt(IPurchaseReceipt receipt)
  162. {
  163. Debug.Log($"Product ID: {receipt.productID}\n" +
  164. $"Purchase Date: {receipt.purchaseDate}\n" +
  165. $"Transaction ID: {receipt.transactionID}");
  166. if (receipt is GooglePlayReceipt googleReceipt)
  167. {
  168. Debug.Log($"Purchase State: {googleReceipt.purchaseState}\n" +
  169. $"Purchase Token: {googleReceipt.purchaseToken}");
  170. }
  171. if (receipt is AppleInAppPurchaseReceipt appleReceipt)
  172. {
  173. Debug.Log($"Original Transaction ID: {appleReceipt.originalTransactionIdentifier}\n" +
  174. $"Subscription Expiration Date: {appleReceipt.subscriptionExpirationDate}\n" +
  175. $"Cancellation Date: {appleReceipt.cancellationDate}\n" +
  176. $"Quantity: {appleReceipt.quantity}");
  177. }
  178. }
  179. void OnAppleStoreKitTestCertificateChanged(bool value)
  180. {
  181. m_UseAppleStoreKitTestCertificate = value;
  182. InitializeValidator();
  183. }
  184. }
  185. }