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
You can play your game as though it were a desktop game from within the editor. You'll just have to implement some editor only code to handle things like looking around, tapping, and swiping. Also, you'll need to make sure any calls to OVRManager are only done when your code runs as Android, and not in the editor.

alfredchen
Honored Guest
I tried in editor and met the error "DllNotFoundException: LibOVRPlatform64_1", tomazvovk had met this error too and he found out need to install Oculus app on PC, I wonder whether his solution is right. If it is right how can I have the Oculus app installed on PC? Or can you suggest a better solution?

delphinius81
Rising Star
You can install the oculus app on your PC without having a Rift connected. This will allow Unity to talk to the Oculus runtime so you can test your game within the Unity editor.

alfredchen
Honored Guest
I tried in editor and met the error "DllNotFoundException: LibOVRPlatform64_1", tomazvovk had met this error too and he found out need to install Oculus app on PC, I wonder whether his solution is right. If it is right how can I have the Oculus app installed on PC? Or can you suggest a better solution?

ssc_mikey
Honored Guest
i get null reference exception when trying to call IAP.LaunchCheckoutFlow(sku).OnComplete(OnProcessingPurchasedProductCallback);

Here is whole post about it 
https://forums.oculus.com/community/discussion/42655/oculus-in-app-purchase-not-working-null-referen...

ssc_mikey
Honored Guest
it seems like they have change the way of getting a callback. as i call IAP.LaunchCheckoutFlow(sku); i can get rid of that null reference but i am not able to track wether the purchase was made or not. How do i get callback in new platform sdk version?

delphinius81
Rising Star
Are you sure that your Oculus.Platform.Core.Initialize() function call was successful? The only reason I know of for that to return null is if the platform was not initialized before making the call. I am running the latest Platform SDK version and the code I originally posted is still working fine for me.

infinite360vr
Explorer
I am getting the error at initialization:
In Editor:
DllNotFoundException: LibOVRPlatform64_1
In Gear VR:

UnityEngine.UnityException: Oculus Platform failed to initialize.

TrevJT
Protege
@infinite360vr I've been having issues with platform settings, mostly they stem from the entitlement check being unsuccessful. 

I updated my platform settings to 1.10 and downloaded the build through the alpha channel with a subscribed user account and got it to work. 

immersion-VR
Honored Guest
Hmm, same problem as previous user i.e.
DllNotFoundException: LibOVRPlatform64_1
Working
on GearVR, yet it appears that to test entitlement within the Unity editor, I
have to download a 1.5gb Oculus application for Rift from
https://www3.oculus.com/en-us/setup/ ...WTF!

I only want to test the entitlement requirement for submitting a build - surely there is a better way to obtain access to a 5mb dll than having to download a 1.5gb application that I don't want???

Somebody tell me I'm wrong...:/


immersionVR reader: immersionVR Web Site