If you are building a game or application with content that needs to be purchased multiple times, you’ll need to integrate consumable in-app purchases (IAPs) into your application. In my previous IAP blog post, I covered the basics of setting up your app for IAP transactions, adding the IAP items to Google Play and iTunes Connect, and introduced you to the In-App Billing Plugin for Xamarin that greatly simplifies the process of adding IAP to your application’s code. Today, we’ll take a look at consumables and how they offer a slight tweak to the development process.
The idea of a consumable is that they are items that are used up over the course of the application’s life, and therefore can be purchased multiple times. Examples of these are in-game currency, such as our monkey credits shown above, extra health, and one-time services, such as transcriptions.
Setting Up Consumables
Google doesn’t make a true distinction between a non-consumable and a consumable, only the API differs between them. For iOS, Apple requires developers to specify this information up front when creating the IAP in iTunes connect.
The idea here is that when we make the request to the iTunes server, this selection will trigger the correct flow during the purchase.
Purchasing and Consuming
For both iOS and Android, we’ll want to make sure that we have the In-App Billing Plugin installed in all projects in our solution via NuGet. iOS and Android each handle consumables a little bit differently, so we’ll start with iOS, which has a very similar flow to the non-consumables that we discussed earlier.
This diagram shows the steps to making the purchase; we must:
- Request our payment in the Queue.
- Send the request to the server.
- iTunes processes and returns.
- Process receipt in app.
- Display purchase information in app.
If this diagram looks familiar, it’s because it’s the same exact flow as non-consumables. Thus, all we have to do is call the PurchaseAsync method and our consumable is purchased and ready to go! Since we marked the IAP as a consumable, we can continue to buy the product over and over.
try
{
var productId = "mysku";
var connected = await CrossInAppBilling.Current.ConnectAsync();
if (!connected)
{
//Couldn't connect to billing, could be offline, alert user
return;
}
//try to purchase item
var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, "apppayload");
if(purchase == null)
{
//Not purchased, alert the user
}
else
{
//Purchased, save this information
var id = purchase.Id;
var token = purchase.PurchaseToken;
var state = purchase.State;
}
}
catch (Exception ex)
{
//Something bad has occurred, alert user
}
finally
{
//Disconnect, it is okay if we never connected
await CrossInAppBilling.Current.DisconnectAsync();
}
For Android, there’s one additional step in the process. We must first call the above code to actually purchase the consumable item, however, Google’s implementation of consumables integrates the scenario where the user may buy a consumable and actually use it in the future, such as buying coins, but cashing them in for goods later in the game. This means that we can’t purchase the consumable item again until we actually finalize the purchase and “Consume” it. For this, the plugin has an additional method, ConsumePurchaseAsync, that takes in specific purchase information. If this method is called on iOS, it will simply return null. We can use the Device Info Plugin to only call ConsumePurchaseAsync
on Android after we have the purchase:
// Called after we have a successful purchase or later on (must call ConnectAsync() ahead of time):
if(CrossDeviceInfo.Current.Platform == Platform.Android)
{
var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);
if(consumedItem != null)
{
// Item has been consumed
}
}
Learn More
There’s a lot more to IAPs, including subscriptions and product information that are exposed in the In-App Billing Plugins APIs. You can find out more about this in the README file on the GitHub project. The same documentation also walks you through a full set up, testing, and trouble shooting guide to integrating In-App Purchases. In addition, I highly recommend reading through full documentation on In-App Purchases for iOS and Android.
0 comments