Skip to content

Instantly share code, notes, and snippets.

@KonajuGames
Created February 4, 2014 23:47
Show Gist options
  • Select an option

  • Save KonajuGames/8814780 to your computer and use it in GitHub Desktop.

Select an option

Save KonajuGames/8814780 to your computer and use it in GitHub Desktop.
Checking receipts and making purchases with ouya-csharp
using System;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Ouya.Console.Api;
namespace Iap
{
class OuyaComponent : GameComponent
{
static OuyaComponent instance;
bool disposed;
OuyaFacade facade;
Product fullGameProduct;
readonly string fullGameProductIdentifier = "your full game product identifier";
readonly string developerUuid = "your developer uuid";
static public OuyaComponent Instance
{
get
{
return instance;
}
}
public OuyaComponent(Game game)
: base(game)
{
}
public override void Initialize()
{
base.Initialize();
instance = this;
facade = OuyaFacade.Instance;
facade.Init(Game.Activity, developerUuid);
if (!Guide.SimulateTrialMode)
CheckPurchase();
}
protected override void Dispose(bool disposing)
{
if (!disposed)
{
// Be nice and shutdown the OUYA interface
if (facade != null)
{
facade.Shutdown();
facade = null;
}
instance = null;
}
disposed = true;
base.Dispose(disposing);
}
async void CheckPurchase()
{
Guide.IsTrialMode = true;
// Wait for a couple of secs before making the first call to OUYA. Recommended by OUYA people.
await System.Threading.Tasks.Task.Delay(1000);
try
{
// Check receipts first so we get the purchased state quicker
Android.Util.Log.Debug("RequestReceipts", "Requesting receipts");
var receipts = await facade.RequestReceiptsAsync();
Android.Util.Log.Debug("RequestReceipts", receipts.Count.ToString() + " receipts returned");
bool purchased = false;
foreach (var receipt in receipts)
{
if (receipt.Identifier == fullGameProductIdentifier)
{
purchased = true;
break;
}
}
Guide.IsTrialMode = !purchased;
Android.Util.Log.Debug("RequestReceipts", "Guide.IsTrialMode = " + Guide.IsTrialMode);
}
catch (Exception e)
{
Android.Util.Log.Debug("RequestReceipts", e.Message);
//Guide.ShowMessageBox("Marketplace Error", "There was an error retrieving receipts.\n" + e.Message, new string[] { "OK" }, 0, MessageBoxIcon.Error);
}
try
{
// Retrieve the product for full game purchase if required
if (Guide.IsTrialMode)
{
var products = await facade.RequestProductListAsync(fullGameProductIdentifier);
foreach (var product in products)
{
if (product.Identifier == fullGameProductIdentifier)
{
fullGameProduct = product;
break;
}
}
}
}
catch (Exception e)
{
Android.Util.Log.Debug("RequestProducts", e.Message);
//Guide.ShowMessageBox("Marketplace Error", "There was an error retrieving products.\n" + e.Message, new string[] { "OK" }, 0, MessageBoxIcon.Error);
}
}
public async void RequestPurchase()
{
if (Guide.IsTrialMode)
{
if (Guide.SimulateTrialMode)
{
Guide.BeginShowMessageBox("Simulate Purchase", "Simulate purchasing the game?", new string[] { "Purchase", "Cancel" }, 1, MessageBoxIcon.None, r =>
{
int? b = Guide.EndShowMessageBox(r);
if (b.HasValue)
Guide.IsTrialMode = b.Value == 0;
}, null);
}
else
{
try
{
if (await facade.RequestPurchaseAsync(fullGameProduct))
{
Guide.IsTrialMode = false;
}
}
catch (TaskCanceledException)
{
// Do nothing. The user simply canceled the task
}
catch (Exception e)
{
Android.Util.Log.Debug("Purchase", e.Message);
Guide.ShowMessageBox("Purchase Error", "There was an error while making the purchase.\nPlease check your internet connection and try again later.", new string[] { "OK" }, 0, MessageBoxIcon.Error);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment