IAP in Ouya – Quick Setup guide

This is a quick guide on how to implement the basic IAP (in-app-payments) functionality in your OUYA game.

Note: You should already have Ouya SDK installed & working OK in your game before trying this. If not, check out this quick start guide!

Let’s start!

  • Go to https://devs.ouya.tv/developers and login with your developer account.
  • Copy your developer UUID and paste it into your “OuyaGameObject” in your Unity scene. Select the object, look at the inspector and paste the UUID on “DEVELOPER_ID” field. Apply changes to the prefab so you don’t have to worry about this again.
  • Back on the website, you’ll need to create both a game and a product. Creating a game will give you a signing key (“key.der“) so you can use the IAP, and creating a product is obviously mandatory since that’s what you are going to be purchasing.
    • Creating a “game” entry:
      • Please note that you don’t need to upload an .apk yet. You just need to fill in some of the fields and that will allow you to download the signing key; you can always come back later when you have finished your game and complete this form & submit an .apk
      • Once you have created a game entry, click on “GAMES” at the top of the website and click on “Download” under “Signing key”.
      • Using a file explorer, navigate to the folder “Unity/Assets/Plugins/Android/res/raw” in your game project and paste in there the “key.der” file you just downloaded from OUYA website.
    • Creating a “product” entry:
      • Identifier: This is the ID you’ll use when requesting a purchase of this product. The players won’t see this. Example: “TR_001_Unlock”.
      • Name: The name of the product you are selling. This is what the players will see, along with the price. Example: “Twin Robots – Unlock all levels”.
      • Price: Choose the price of the product as you like.
      • Type:Consumable” is something that can be repeatedly bought, like a helth potion. “Entitlement” is something that can only be bought once. I chose “Entitlement” for my game since buying it gives you the full game, no need to buy it again and again and again…
  • Now, let’s go back to Unity. Time to write some code! This shows the very basic purchase functionality using UnityScript.

public class IAP extends MonoBehaviour implements OuyaSDK.IPurchaseListener
{
	//------------ SETUP ------------//

	function Awake() 
	{
		OuyaSDK.registerPurchaseListener(this);
	}
	function OnDestroy()
	{
		OuyaSDK.unregisterPurchaseListener(this);
	}


	//------------ INTERACTION ------------//

	function Buy()
	{
		// This is the function you call when pressing the "BUY" button
		// put here the identifier of the product you want to sell.
		// There is another way to do it, and involves putting all product IDs in OuyaGameObject but I haven't tested it since this works ok for me.
		OuyaSDK.requestPurchase("TR_001_Unlock");
	}

	//------------ PURCHASE CALLBACKS ------------//

	function OuyaPurchaseOnSuccess(product : OuyaSDK.Product)
	{
		// yay, success!! product purchased!!
		// show feedback to the user, unlock the game, etc.
	}
	function OuyaPurchaseOnFailure(errorCode : int, errorMessage : String)
	{
		// something happened, purchase failed
	}
	function OuyaPurchaseOnCancel()
	{
		// user cancelled the purchase
	}
}

That’s it! :)

The following example is almost the same but shows how you can get the price of any of your products from OUYA server:


public class IAP extends MonoBehaviour implements OuyaSDK.IPurchaseListener, OuyaSDK.IGetProductsListener
{
	//------------ SETUP ------------//

	private var needToGetPrice : boolean = true; // we will use this flag to get the price 1 single time.
	function Awake() 
	{
		OuyaSDK.registerPurchaseListener(this);
		OuyaSDK.registerGetProductsListener(this);
	}
	function OnDestroy()
	{
		OuyaSDK.unregisterPurchaseListener(this);
		OuyaSDK.unregisterGetProductsListener(this);
	}


	//------------ INTERACTION ------------//

	function Update ()
	{
		// we have to wait until the IAP code (in OUYA SDK) has been initialized before trying to use it
		if(needToGetPrice && OuyaSDK.isIAPInitComplete())
		{
			GetPrice(); // get the price of the game from OUYA server!
			needToGetPrice = false; // set this flag to false so we don't ever do this check again
		}
	}
	function GetPrice()
	{
		var productIdentifierList : List.<OuyaSDK.Purchasable> = new List.<OuyaSDK.Purchasable>();
		productIdentifierList.Add(new OuyaSDK.Purchasable("TR_001_Unlock"));
		// ...add the rest of your products here too (if you have multiple products)
	
		OuyaSDK.requestProductList(productIdentifierList);
	}
	function Buy()
	{
		// This is the function you call when pressing the "BUY" button
		// put here the identifier of the product you want to sell.
		// There is another way to do it, and involves putting all product IDs in OuyaGameObject but I haven't tested it since this works ok for me.
		OuyaSDK.requestPurchase("TR_001_Unlock");
	}


	//------------ PRODUCT & PURCHASE CALLBACKS ------------//

	function OuyaGetProductsOnSuccess(products : List.<OuyaSDK.Product>)
	{
		for (var oProduct : OuyaSDK.Product in products)
		{
			if(oProduct.getIdentifier() == "TR_001_Unlock")
			{
				fullGamePrice = oProduct.getPriceInCents();
			}
		}
	}
	function OuyaGetProductsOnFailure(errorCode : int, errorMessage : String)
	{
		// failed to get products
	}
	
	function OuyaGetProductsOnCancel()
	{
		// cancelled
	}
	function OuyaPurchaseOnSuccess(product : OuyaSDK.Product)
	{
		// yay, success!! product purchased!!
		// show feedback to the user, unlock the game, etc.
	}
	function OuyaPurchaseOnFailure(errorCode : int, errorMessage : String)
	{
		// something happened, purchase failed
	}
	function OuyaPurchaseOnCancel()
	{
		// user cancelled the purchase
	}
}

Hope it helps you!

PS: If you have any problems, try implementing “OuyaSDK.IPauseListener, OuyaSDK.IResumeListener, OuyaSDK.IMenuButtonUpListener & OuyaSDK.IMenuAppearingListener” in your class too, registering them in “Awake” and unregistering them on “Destroy” functions. The reason? I have those in my code for other functionality and I can’t really remember if they are needed for IAP or not.

Also, if you want to read more on this subject:
Post in OUYA forums that helped a lot.
Sophie Houlden’s guide to IAP (in C#)
OUYA Docs about IAP
Video: In-App-Purchase Setup – ShowProducts Example
Video: Key Store Setup

Discussion5 Comments

  1. Mark Wilson says:

    Hey,

    Thanks so much for making this. It seems the most straight forward of all the docs I’ve seen on the OUYA iap. That said, I’m still lost. Using the first listed example above, and pasting in my own product info I get the following error:

    “Assets/IAP.js(34,40): BCE0017: The best overload for the method ‘OuyaSDK.requestPurchase(OuyaSDK.Purchasable)’ is not compatible with the argument list ‘(String)’.”

    The line in question reads:

    OuyaSDK.requestPurchase(“Rionon1_FullUnlock”);

    and lives within the “Buy” function.

    Have been banging my head into a wall for two days trying to figure iaps out, and thought I’d reach out to you. Thanks, and God bless.

    • admin says:

      Hi Mark,

      You’ll probably want to use the code from the 2nd example; That’s because the 1st one lacks several things you’ll need to do before making a purchase, like making sure that the IAP is initiated & then requesting a list of the available products for your game from ouya’ servers (don’t forget to set this up beforehand from ouya’s dev panel!). Once the list of products is received, “OuyaGetProductsOnSuccess” function will get called automatically; That’s when you can call the “Buy” function like you did before.

      Let me know if this helps, I’ll gladly post a more detailed guide if you need.

    • admin says:

      Before changing all your code, you could try to simply replace

      OuyaSDK.requestPurchase(“Rionon1_FullUnlock”);

      with the following:

      OuyaSDK.requestPurchase(new OuyaSDK.Purchasable(“Rionon1_FullUnlock”)) ;

      … Worth giving it a try; if it doesn’t work, then go ahead with the code from the 2nd example.

  2. Mark Wilson says:

    Hey again :)

    Thanks so much for your response. Tim from Ouya was kind enough to Skype with me to help get things set up… Was able to get rid of the compiler errors, and have the purchase system work but purchases weren’t actually unlocking the game. After a few times on Skype he felt that the issue was that I was working with the old ODK, and recommended upgrading to the new one. (I’m always hesitant to upgrade given how many times I’ve nearly borked my project in the past by doing that.) But I did, and it did. After fixing some java compile errors and seeming to be back on track I now get 60 of these, for various scripts:

    Assets/Plugins/IOuyaController.cs(31,13): error CS0426: The nested type `KeyEnum’ does not exist in the type `OuyaSDK’

    I realize that isn’t in the scope of this post, but am throwing a line to anyone on the web who might be able to help. Tim’s out of the office this week =)

  3. admin says:

    Hi!

    Happy to hear that you sorted it out! (well, almost!).

    That error totally looks like you have some files mixed between the old & the new ODK. It seems they changed several things and that’s probably causing your issues here, trying to get some data from OuyaSDK.cs that is no longer there. In fact, it seems that even the file “IOuyaController.cs” is no longer there, so you sure have things mixed up in your project.

    I’d suggest removing all the ODK-related files in your project & do a clean import of the latest ODK (https://github.com/ouya/ouya-sdk-examples/tree/master/Unity/OuyaSDK)

    Sorry I can’t be of more help, I’m still using the old ODK & I’m not sure how the new one works.