cancel
Showing results for 
Search instead for 
Did you mean: 

Unity IAP Tutorial

delphinius81
Rising Star
I've seen a lot of people ask about how to do IAP in Unity, so I thought I'd share some pseudo-code for how to do it.

1) Create an organization/app through the Oculus developer dashboard. Once you have done that, go to the API tab and find your AppID. Now click on generate user token. You'll need both of these keys to configure and test in Unity. 

2) You'll also need to configure the available IAP skus through the developer dashboard. From your app on the dashboard, click the platform tab. You'll need to upload a tab separated file that contains all the information such as sku name, description, price, etc. You can edit the provided template as necessary.

3) Download the Platform SDK and added the provided Unity framework to your project. You should now see a menu item for Oculus Platform, and under it Edit Settings. Provide the AppID in the Rift or GearVR section, depending on your app type. If you plan on testing from the Unity Editor, you should also provide a user token

4) Unity should now be configured, and you can start coding. The first thing you need to do is initialize the platform. You do this by making the following function call:
Oculus.Platform.Core.Initialize();
If you did not enter your AppID through the Unity editor, you can also pass it in to this function, such as:
Oculus.Platform.Core.Initialize("<your app id here>");
Once initialized, you can make other calls to the Platform SDK. The Platform SDK code uses a callback system. You call the Platform function and provide the name of the callback function that should be triggered when the Platform function is done processing.

For example, say you want to make sure that the user is Entitled to your application (i.e. they have purchased it). You would use the Oculus.Platform.Entitlements.IsUserEntitledToApplication() function for that.

public void CheckApplicationEntitlement() {
   // Call the entitlement check function and give it the function to call when it's done
   Oculus.Platform.Entitlements.IsUserEntitledToApplication().OnComplete(IsUserEntitledToApplicationCallback);
}

In this case, I specify that the Platform should should call my IsUserEntitledToApplicationCallback function when IsUserEntitledToApplication is complete.

void IsUserEntitledToApplicationCallback(Message msg) {
   // If msg.IsError is true, then the user is not entitled to the app
   if (!msg.IsError) {
      mApplicationEntitled = true;
   } else {
      //Debug.Log("Application is NOT entitled.");
      mApplicationEntitled = false;
   }
}

The rest of the IAP relate calls work the same way. There are three IAP related calls you might want to make.


1) See what products are available for purchase
2) See what products a user has already purchased
3) Initiate a new purchase

Let's look at each of these.

Available Products

public void RetrieveProducts() {
   // Now retrieve products for the listed skus
   string[] skus = { "sku 1", "sku 2", "sku 3"};
   IAP.GetProductsBySKU(skus).OnComplete(GetProductsBySKUCallback);
}
void GetProductsBySKUCallback(Message msg) {
   if (!msg.IsError) {
      ProductList products = msg.GetProductList();
      // Update the entitlements object
      foreach (Product p in products) {
         //Debug.Log("Product " + p.Sku + " is available for purchase for " + p.FormattedPrice + ".");
         // You might want to save this sku and it's price for display somewhere else in your app. A Dictionary is a good choice for this
      }
   } else {
      Error e = msg.GetError();
      Debug.Log("GetProductsBySKUCallback Error code: " + e.Code + " http: " + e.HttpCode + " msg: " + e.Message);
   }
}

Purchased Products

public void RetrievePurchases() {
   Debug.Log("Retrieve Purchases called");
   IAP.GetViewerPurchases().OnComplete(GetViewerPurchasesCallback);
}
void GetViewerPurchasesCallback(Message msg) {
   if (!msg.IsError) {
      // Get the list of products purchased by the uer
      PurchaseList purchasedProducts = msg.GetPurchaseList();
      foreach (Purchase p in purchasedProducts) {
         Debug.Log("Purchase " + p.Sku + " is owned by user.");
         // Do something with this knowledge, perhaps by saving entitlements to a file or for updating app state
      }
   } else {
      Error e = msg.GetError();
      Debug.Log("GetViewerPurchasesCallback Error code: " + e.Code + " http: " + e.HttpCode + " msg: " + e.Message);
   }
}

Initiate Purchase

There is one thing to note about initiating a purchase. It will NOT work from the Unity Editor. You need to make a real build to get the purchase to initiate. For this reason, it is important that for you to create test users from the developer dashboard.

public void InitiatePurchase(string sku) {
   // For testing within the Unity Editor, I highly recommend you short-circuit your IAP callback flow in some way that your user is simply granted access to the purchase
   if (Debug.isDebugBuild) {
      // Call some function to just grant the entitlement to the IAP
   } else
      IAP.LaunchCheckoutFlow(sku).OnComplete(LaunchCheckoutFlowCallback);
   }
}
void LaunchCheckoutFlowCallback(Message msg) {
   if (!msg.IsError) {
      Purchase p = msg.GetPurchase();
      Debug.Log("User purchased " + p.Sku + ".");
      // Call your code to grant the user the IAP
   } else {
      Error e = msg.GetError();
      Debug.Log("LaunchCheckoutFlowCallback Error code: " + e.Code + " http: " + e.HttpCode + " msg: " + e.Message);
   }
}
5) Testing IAP

The final step is testing an actual in-app purchase. Once you have created test users, you'll need to add them to your build's test channel. Getting access to the test channels requires Oculus to enable something for you, so message cybereality.

Once that is done, make a build and upload it to through the developer dashboard to the alpha/beta/rc channel. Add your test users to the channel. You will need to login to Oculus Home with a test user. Once there, add one of the test credit cards listed in the OVRPlatform docs.

You should now have access to your build from within Oculus Home and can test that your IAP works properly.
23 REPLIES 23

delphinius81
Rising Star
1) Make sure you've set your build target to 64-bit.
2) You need to have the oculus runtime installed on your pc to test Rift entitlement checking.

immersion-VR
Honored Guest
Yes - build target is 64 bit but entitlement checking is for GearVR NOT Rift.
So, do I have to download the 1.5gb oculus runtime app to test entitlement checking for GearVR?

immersionVR reader: immersionVR Web Site

A1_Mandeep
Honored Guest
Hey @delphinius81
I have tried to go through the same process but when I try to run it I get the unity exception,

Oculus platform failed to initialize.

Please help me.

I am using Unity 2017.2.0 b11 and platform sdk 1.18 and building the app for Gear VR

Cullis_Wullis
Explorer

Can you help with this errorScreenshot 2022-06-08 165550.png