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

ThreeDeeVision
Superstar
Dupe deleted, have a good one 😉
i7 5960X @ 3.8 GHz | Corsair Vengeance LPX 16GB DDR4 PC2800 | GTX Titan X Pascal | Win 10 64 bit | Asus ROG PG348Q | EVGA X99 Classified

Anonymous
Not applicable
Thank you very much for this!

I got to the point no. 4, but when I try to run my app inside the editor it throws an error on init line: 
DllNotFoundException: LibOVRPlatform64_1

(I skipped step no. 2, since I have to wait for our client to fill in the information).

Ok, I found out that I need Oculus app installed on PC even though I am working on Gear VR app.

alfredchen
Honored Guest
I tried this on my gear vr, but it just throw the error message  "Oculus Platform failed to initialize", 
the function CAPI.ovr_UnityInitWrapper return false. I'm sure I pass the app ID correctly.

So how can I initialize the Oculus Platform successfully on gear vr? 


delphinius81
Rising Star
Did you try to set the app ID through the Unity editor or through the function call? If you did it within Unity, are you sure you put it in the correct spot?

alfredchen
Honored Guest
I set the app ID both in unity editor and function, but still got the error message.961adlr5yjye.png9bltk11h7e22.png

delphinius81
Rising Star
Is your project build set to Android? (Silly question, but have to ask.) 

alfredchen
Honored Guest
yes, I set the project build to Android and test it on my s7. 
 I modify the Platform.Initialize and AndroidPlatform.Initialize function, using a text to output the error message when I test this on my gear vr. Finally I found AndroidPlatform.Initialize got my app ID but the 
CAPI.ovr_UnityInitWrapper return false.
I'm the Admin of this app and have entitled to as developer. Do I need to do other settings?

delphinius81
Rising Star
Does the initialization fail when you run in the Unity editor too? Or just on GearVR?

alfredchen
Honored Guest
How can run it in Unity editor? I don't have a rift.