BuyingSubscription.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Purchasing;
  5. using UnityEngine.UI;
  6. namespace Samples.Purchasing.Core.BuyingSubscription
  7. {
  8. public class BuyingSubscription : MonoBehaviour, IStoreListener
  9. {
  10. IStoreController m_StoreController;
  11. // Your subscription ID. It should match the id of your subscription in your store.
  12. public string subscriptionProductId = "com.mycompany.mygame.my_vip_pass_subscription";
  13. public Text isSubscribedText;
  14. void Start()
  15. {
  16. InitializePurchasing();
  17. }
  18. void InitializePurchasing()
  19. {
  20. var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  21. // Add our purchasable product and indicate its type.
  22. builder.AddProduct(subscriptionProductId, ProductType.Subscription);
  23. UnityPurchasing.Initialize(this, builder);
  24. }
  25. public void BuySubscription()
  26. {
  27. m_StoreController.InitiatePurchase(subscriptionProductId);
  28. }
  29. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  30. {
  31. Debug.Log("In-App Purchasing successfully initialized");
  32. m_StoreController = controller;
  33. UpdateUI();
  34. }
  35. public void OnInitializeFailed(InitializationFailureReason error)
  36. {
  37. Debug.Log($"In-App Purchasing initialize failed: {error}");
  38. }
  39. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  40. {
  41. // Retrieve the purchased product
  42. var product = args.purchasedProduct;
  43. Debug.Log($"Purchase Complete - Product: {product.definition.id}");
  44. UpdateUI();
  45. // We return Complete, informing IAP that the processing on our side is done and the transaction can be closed.
  46. return PurchaseProcessingResult.Complete;
  47. }
  48. public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
  49. {
  50. Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
  51. }
  52. bool IsSubscribedTo(Product subscription)
  53. {
  54. // If the product doesn't have a receipt, then it wasn't purchased and the user is therefore not subscribed.
  55. if (subscription.receipt == null)
  56. {
  57. return false;
  58. }
  59. //The intro_json parameter is optional and is only used for the App Store to get introductory information.
  60. var subscriptionManager = new SubscriptionManager(subscription, null);
  61. // The SubscriptionInfo contains all of the information about the subscription.
  62. // Find out more: https://docs.unity3d.com/Packages/com.unity.purchasing@3.1/manual/UnityIAPSubscriptionProducts.html
  63. var info = subscriptionManager.getSubscriptionInfo();
  64. return info.isSubscribed() == Result.True;
  65. }
  66. void UpdateUI()
  67. {
  68. var subscriptionProduct = m_StoreController.products.WithID(subscriptionProductId);
  69. try
  70. {
  71. var isSubscribed = IsSubscribedTo(subscriptionProduct);
  72. isSubscribedText.text = isSubscribed ? "You are subscribed" : "You are not subscribed";
  73. }
  74. catch (StoreSubscriptionInfoNotSupportedException)
  75. {
  76. var receipt = (Dictionary<string, object>)MiniJson.JsonDecode(subscriptionProduct.receipt);
  77. var store = receipt["Store"];
  78. isSubscribedText.text =
  79. "Couldn't retrieve subscription information because your current store is not supported.\n" +
  80. $"Your store: \"{store}\"\n\n" +
  81. "You must use the App Store, Google Play Store or Amazon Store to be able to retrieve subscription information.\n\n" +
  82. "For more information, see README.md";
  83. }
  84. }
  85. }
  86. }