Skip to content

flagbug/SaneInAppBillingHandler

Repository files navigation

Overview

SaneInAppBillingHandler is a sane wrapper over the Xamarin.InAppBilling component. Note that you have to install the Xamarin.InAppBilling component manually from the Xamarin component store, and the SaneInAppBillingHandler package from NuGet

Why

The Xamarin.InAppBilling API is, let's say, a bit strange to use.

For example: to buy a product through the API, you have to handle the following events:

  • OnProductPurchased
  • OnProductPurchasedError
  • BuyProductError
  • InAppBillingProcesingError [sic!]
  • OnUserCanceled
  • OnPurchaseFailedValidation

SaneInAppBillingHandler exposes a convenient asynchronous method instead.

Quickstart

Creating the billing handler

using SaneInAppBillingHandler;

var handler = new SaneInAppBillingHandler(yourActivity, "API key that you receive from Google");

// Call this method when creating your activity
try
{
  await handler.Connect();
}

catch (InAppBillingException ex)
{
  // Thrown if the commection fails for whatever reason (device doesn't support In-App billing, etc.)
  // All methods (except for Disconnect()) may throw this exception, 
  // handling it is omitted for brevity in the rest of the samples
}

Getting available purchases

// Retrieve the product infos for "myItem1" and "myItem2" 
// (these are the IDs that you give your products in the Google Play Developer Console)
// The second argument specifies if those products are subscriptions or normal one-time purchases
IReadOnlyList<Product> products = await handler.QueryInventory(new[]{"myItem1", "myItem2", ItemType.Product);

Getting a list of products that the user has purchased

IReadOnlyList<Purchase> purchases = await handler.GetPurchases(ItemType.Product);

Buying a product

// Buys the product and returns a billing result. Look this up in the BillingResult class.
int result = await handler.BuyProduct(product);

Consuming a purchase

await handler.ConsumePurchase(purchase);

Disconnecting the handler

// Call this method when destroying your activity
handler.Disconnect();

About

A sane wrapper over the Xamarin.InAppBilling library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages