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