Enabling In-App Purchases in Your Windows Phone 8 Application

July 19, 2013

Introduction

Windows Phone developers can monetize from the platform by enabling in-app purchases. There is a growing trend amongst developers to offer their application for free and upsell through digital goodies via in-app purchases.

In-App Purchase Fundamentals

To support in-app purchases, a Windows Phone developer needs to create products that can be sold in their applications and provide clear information about these. From an application standpoint, they need to create an ability to initiate an in-app purchase and also ensure product delivery/enabled upon a successful in-app purchase.

Microsoft provides a purchase user experience that is consistent across applications, and ensures authenticity of purchases in addition to the ability to pay application developers in 190 different countries.

The general flow is:

1. Application is submitted in the Dev center.

2. A virtual product list is submitted that can to be bought as an in-app purchase for the above submitted app.

3. These products should be available for listing in the application, to allow the user to choose from.

4. Upon in-app purchase initiation, the user is presented with a consistent purchasing experience, which is part of Windows Phone 8 platform.

5. Upon application launch/resumption and successful in-app purchase, the product license should be checked to ensure that the user has permissions to the content.

6. The application can get receipts (containing digital signatures) for all products purchased by the user.

7. Digital content is now available for download.

Content, which is available for in-app purchases can be durable (purchased once and then owned by the user forever) or consumable (items that need to be replenished frequently, like game points, game tokens).

Hands On

We will build a sample application, which will offer one token as to users as an in-app purchase. Upon successful purchase, the application will list the total number of tokens purchased.

Next, on MainPage.xaml, add the following controls: one checkbox titled “1 token”, one button titled “Purchase’,1 textblock titled “Purchased quantity” and one textbox to contain the total number of tokens purchased and available for use.

Next, we will add the code to handle the click event of the Purchase button. We need to make sure that the item was checked and only allow the transaction if it is checked. If the purchase is completed successfully, we will call helper methods to complete the fulfillment and finally, update the token count.

In this method, we need to list all of the available items for in-app purchase for our application. If the purchase is submitted, we will call the CurrentApp.RequestProductPurchaseAsync method with the product Id and a flag to indicate if a receipt should be included or not.

Next, we will implement the method that handles the fulfillment completion. In this method, we will extract the current product licenses for our application and if the license is active (for a recently purchased item), the application can now start the fulfillment (download the digital goodies just purchased).

Finally, we need to wire up the mock library in our application.

Implement a method to instantiate the Mock library and provide it the catalog contents. This needs to be done in App.xaml.cs.

Finally, we add code to call the newly implemented method from the end of the App’s constructor.

public App()
{
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
// Standard XAML initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Language display initialization
InitializeLanguage();
// Show graphics profiling information while debugging.if (Debugger.IsAttached)
{
// Display the current frame rate counters.Application.Current.Host.Settings.EnableFrameRateCounter = true;// Show the areas of the app that are being redrawn in each frame.//Application.Current.Host.Settings.EnableRedrawRegions = true;// Enable non-production analysis visualization mode,// which shows areas of a page that are handed off to GPU with a colored overlay.//Application.Current.Host.Settings.EnableCacheVisualization = true;// Prevent the screen from turning off while under the debugger by disabling// the application's idle detection.// Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run// and consume battery power when the user is not using the phone.PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
}
SetupMockIAP();
}

We now have a fully functional test application that can simulate an in-app purchase. If you have trouble following along, you can download the sample listing from here

Running the Application

When the application is executed in the emulator (note that you need to be compiling this as Debug to ensure the mock library will work, else you will need to register and deploy the application in AppHub before you can execute this successfully in Release mode).

Execute the App

Check the “1 token” checkbox and click purchase.

Mock UI

When you click OK, the purchase is completed and we can see that the purchased quantity has been updated to 1.

Quantity Purchased

As we make more in-app purchases, you will notice that the purchased quantity count goes up with every successful transaction.

Summary

In this article, we learned about in-app purchase. I hope you have found this information useful.

About the Author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com