InAppBillingPlugin

Cross-platform In App Billing Plugin for Xamarin


Project maintained by jamesmontemagno Hosted on GitHub Pages — Theme by mattgraham

Getting Started

Setup

Using InAppBilling APIs

It is drop dead simple to gain access to the In-App Billing APIs in any project. All you need to do is get a reference to the current instance of IInAppBilling via CrossInAppBilling.Current. Before making any calls to InAppBilling you must use ConnectAsync to ensure a valid connection to the app store of the device and always ensure that you call DisconnectAsync when you are finished. It is recommended to call DisconnectAsync inside of a finally block.

public async Task<bool> MakePurchase()
{
    var billing = CrossInAppBilling.Current;
    try
    {
        var connected = await billing.ConnectAsync(ItemType.InAppPurchase);
        if(!connected)
            return false;
        
        //make additional billing calls
    }
    finally
    {
        await billing.DisconnectAsync();
    }
}

There may be instances where you install a plugin into a platform that it isn’t supported yet. This means you will have access to the interface, but no implementation exists. You can make a simple check before calling any API to see if it is supported on the platform where the code is running. This if nifty when unit testing:

public async Task<bool> MakePurchase()
{
    if(!CrossInAppBilling.IsSupported)
        return false;

    try
    {
        var billing = CrossInAppBilling.Current;
        var connected = await billing.ConnectAsync(ItemType.InAppPurchase);
        if(!connected)
            return false;
        
        //make additional billing calls
    
    }
    finally
    {
        await billing.DisconnectAsync();
    }
}

Due to the complex nature of In-App Billing I highly recommend reading exactly how they work on each platform before you start using this plugin:

In addition to this core reading I recommend the following:

Creating an In-App Purchase

Each app store has you create them in a different area.

Permissions & Additional Setup Considerations

Android:

The com.android.vending.BILLING permission is required to use In-App Billing on Android and this library ill automatically added it your Android Manifest when you compile. No need to add them manually!

You must place this code in your Main/Base Activity where you will be requesting purchases from.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    InAppBillingImplementation.HandleActivityResult(requestCode, resultCode, data);
}

Android Current Activity Setup

This plugin uses the Current Activity Plugin to get access to the current Android Activity. Be sure to complete the full setup if a MainApplication.cs file was not automatically added to your application. Please fully read through the Current Activity Plugin Documentation. At an absolute minimum you must set the following in your Activity’s OnCreate method:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

It is highly recommended that you use a custom Application that are outlined in the Current Activity Plugin Documentation](https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/master/README.md)

<= Back to Table of Contents