Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PresentCodeRedemptionSheet.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Purchasing;
  4. using UnityEngine.Purchasing.Extension;
  5. using UnityEngine.UI;
  6. namespace Samples.Purchasing.AppleAppStore.PresentCodeRedemptionSheet
  7. {
  8. [RequireComponent(typeof(UserWarningAppleAppStore))]
  9. public class PresentCodeRedemptionSheet : MonoBehaviour, IDetailedStoreListener
  10. {
  11. IStoreController m_StoreController;
  12. IAppleExtensions m_AppleExtensions;
  13. public string normalSubscriptionId = "com.mycompany.mygame.my_normal_pass_subscription";
  14. public Text ownsSubscription;
  15. void Start()
  16. {
  17. InitializePurchasing();
  18. UpdateWarningMessage();
  19. }
  20. void InitializePurchasing()
  21. {
  22. var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  23. builder.AddProduct(normalSubscriptionId, ProductType.Subscription);
  24. UnityPurchasing.Initialize(this, builder);
  25. }
  26. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  27. {
  28. Debug.Log("In-App Purchasing successfully initialized");
  29. m_StoreController = controller;
  30. m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
  31. UpdateUI();
  32. }
  33. public void DoPresentCodeRedemptionSheet()
  34. {
  35. Debug.Log("$Calling Apple API to present code redemption sheet ...");
  36. Debug.LogWarning($"Next, user should input the previously generated Offer Code from App Store Connect, for Product ID: {normalSubscriptionId}");
  37. Debug.Log("After, Apple StoreKit should generate a purchase, and trigger a ProcessPurchase callback for it.");
  38. Debug.Log("See README.md for more information.");
  39. m_AppleExtensions.PresentCodeRedemptionSheet();
  40. }
  41. public void BuyNormalSubscription_DoNotCallForThisSample()
  42. {
  43. // Ownership of this product for the purposes of this Sample MUST happen, indirectly,
  44. // with the Apple "Code Redemption Sheet"
  45. m_StoreController.InitiatePurchase(normalSubscriptionId);
  46. }
  47. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  48. {
  49. var product = args.purchasedProduct;
  50. Debug.Log($"Processing Purchase: {product.definition.id}");
  51. UpdateUI();
  52. return PurchaseProcessingResult.Complete;
  53. }
  54. void UpdateUI()
  55. {
  56. ownsSubscription.text = HasNormalSubscription() ? "Subscription is owned" : "Subscription is not yet owned";
  57. }
  58. bool HasNormalSubscription()
  59. {
  60. var normalSubscriptionProduct = m_StoreController.products.WithID(normalSubscriptionId);
  61. return normalSubscriptionProduct != null && normalSubscriptionProduct.hasReceipt;
  62. }
  63. public void OnInitializeFailed(InitializationFailureReason error)
  64. {
  65. OnInitializeFailed(error, null);
  66. }
  67. public void OnInitializeFailed(InitializationFailureReason error, string message)
  68. {
  69. var errorMessage = $"Purchasing failed to initialize. Reason: {error}.";
  70. if (message != null)
  71. {
  72. errorMessage += $" More details: {message}";
  73. }
  74. Debug.Log(errorMessage);
  75. }
  76. public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
  77. {
  78. Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
  79. }
  80. public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
  81. {
  82. Debug.Log($"Purchase failed - Product: '{product.definition.id}'," +
  83. $" Purchase failure reason: {failureDescription.reason}," +
  84. $" Purchase failure details: {failureDescription.message}");
  85. }
  86. void UpdateWarningMessage()
  87. {
  88. GetComponent<UserWarningAppleAppStore>().UpdateWarningText();
  89. }
  90. }
  91. }