PresentCodeRedemptionSheet.cs 3.3 KB

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