IAPManager.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. namespace Oculus.Platform.Samples.VrBoardGame
  2. {
  3. using UnityEngine;
  4. using Oculus.Platform;
  5. using Oculus.Platform.Models;
  6. using UnityEngine.UI;
  7. // This class coordinates In-App-Purchases (IAP) for the application. Follow the
  8. // instructions in the Readme for setting up IAP on the Oculus Dashboard. Only
  9. // one consumable IAP item is used is the demo: the Power-Ball!
  10. public class IAPManager : MonoBehaviour
  11. {
  12. // the game controler to notify when the user purchaes more powerballs
  13. [SerializeField] private GameController m_gameController = null;
  14. // where to record to display the current price for the IAP item
  15. [SerializeField] private Text m_priceText = null;
  16. // purchasable IAP products we've configured on the Oculus Dashboard
  17. private const string CONSUMABLE_1 = "PowerballPack1";
  18. void Start()
  19. {
  20. FetchProductPrices();
  21. FetchPurchasedProducts();
  22. }
  23. // get the current price for the configured IAP item
  24. public void FetchProductPrices()
  25. {
  26. string[] skus = { CONSUMABLE_1 };
  27. IAP.GetProductsBySKU(skus).OnComplete(GetProductsBySKUCallback);
  28. }
  29. void GetProductsBySKUCallback(Message<ProductList> msg)
  30. {
  31. if (msg.IsError)
  32. {
  33. PlatformManager.TerminateWithError(msg);
  34. return;
  35. }
  36. foreach (Product p in msg.GetProductList())
  37. {
  38. Debug.LogFormat("Product: sku:{0} name:{1} price:{2}", p.Sku, p.Name, p.FormattedPrice);
  39. if (p.Sku == CONSUMABLE_1)
  40. {
  41. m_priceText.text = p.FormattedPrice;
  42. }
  43. }
  44. }
  45. // fetches the Durable purchased IAP items. should return none unless you are expanding the
  46. // to sample to include them.
  47. public void FetchPurchasedProducts()
  48. {
  49. IAP.GetViewerPurchases().OnComplete(GetViewerPurchasesCallback);
  50. }
  51. void GetViewerPurchasesCallback(Message<PurchaseList> msg)
  52. {
  53. if (msg.IsError)
  54. {
  55. PlatformManager.TerminateWithError(msg);
  56. return;
  57. }
  58. foreach (Purchase p in msg.GetPurchaseList())
  59. {
  60. Debug.LogFormat("Purchased: sku:{0} granttime:{1} id:{2}", p.Sku, p.GrantTime, p.ID);
  61. }
  62. }
  63. public void BuyPowerBallsPressed()
  64. {
  65. #if UNITY_EDITOR
  66. m_gameController.AddPowerballs(1);
  67. #else
  68. IAP.LaunchCheckoutFlow(CONSUMABLE_1).OnComplete(LaunchCheckoutFlowCallback);
  69. #endif
  70. }
  71. private void LaunchCheckoutFlowCallback(Message<Purchase> msg)
  72. {
  73. if (msg.IsError)
  74. {
  75. PlatformManager.TerminateWithError(msg);
  76. return;
  77. }
  78. Purchase p = msg.GetPurchase();
  79. Debug.Log("purchased " + p.Sku);
  80. m_gameController.AddPowerballs(3);
  81. }
  82. }
  83. }