This post talks about new monetization feature in Windows Phone 8 – In-App-Purchases (IAP). According to multiple industry sources (link1, link2, link3) IAP can actually make more money that free/ad-supported application. Let’s see how to add this feature to application. In this post I will show how to build simplest “virtual” shop app. All IAP functionality process allocated in Windows.ApplicationModel.Store namespace.
First let’s understand the terminology associated with IAP process.
Product – something you are selling in application. Unlike Windows 8, in Windows Phone 8 we got two different product types: Durable and Consumable.
Durable – a Product that is purchased once and owned by the user/application.
Consumable – a Product that is purchased and used multiple times. This Product could be purchased again and again when user needs it.
Products are organized into Product List which managed through application’s settings on Dev Center Dashboard.
Purchase – the process of buying a Product.
License – a set of definitions describing the product type, whether it is active and its expiration date. Associated with each Product.
Receipt – a proof of purchase which used to verify that user Purchased the Product.
Fulfillment – the process of giving the user the Product he/she Purchased. Depends on application logic, but must me reported to marketplace. Available only for Consumables and plays key role in Product Purchase process – new Consumable cannot be Purchased again without reporting previous successful fulfillment.
Working with Catalog
Current application licensing info and associated product list is obtained using CurrentApp class. This app provides key functionality of searching catalog (by keyword or productId), purchasing the products and report fulfillments. The following code snippet searches all products by keywords:
Next code snippet shows how to search specific products from product list:
In addition to search functionality, CurrentApp class also provides access to full product list which is accessible through CurrentApp.LicenseInformation.ProductLicenses class. Each item in ProductLicenses is an object from ProductLicense class. This class enables querying product state by providing ExpirationDate, IsActive and IsConsumable properties. The following code snippet loads not purchased durables from product catalog:
In addition, this code snippet loads all product listings (info about the products) and shows them as list of products:
Loading consumables (also shown on screenshot) is similar:
Let’s see how to present the product on screen. On sample app screen (see above), when user changes selection we will be navigating to different screen with selected productId as navigation parameter:
Let’s see the sample ProductInfo page. I will be presenting product details on page as follows:
When application navigates to the page it searches product info from product list as follows:
This code produces the following results:
Now the interesting part – “Buy now button handler which does actual product purchase:
Previously purchased (durable) products could be retrieved from listing as follows:
Developing/Testing IAP scenarios
Working IAP is a server-based commerce. It relies on communication with Microsoft’s backend services to list products, enumerate licenses and process purchases. It may also rely on your servers to deliver purchased in-app products if needed. When purchasing the product (published application) marketplace will perform real transaction with client’s account. In development cycle it is not always possible to deploy application to marketplace (especially as public application). It is possible to deploy application as Beta on marketplace. Beta applications support full IAP mechanism through marketplace with only one difference – although the user experience shows prices as entered into Dev Center, all in-app purchases made from beta apps are free. This approach is strongly recommended once you application is in somewhat ready state. But what to do during initial development stages? Windows 8 provides CurrentAppSimulator class for this work. Unfortunately this functionality didn’t made it into Windows Phone 8. To overcome this, we want to use mock library which closely mimics Store APIs.
This library exists and could be downloaded here.
To work with this library it must be referenced on all pages which are using Store functionality as follows:
From now on, to test real marketplace functionality all you need is to compile your app as Release. While the library closely mimics store APIs it must be initiated with as it doesn’t uses server-based information from marketplace. It should be initiated from XML-based product catalog as follows:
Note: in real scenarios it is recommended to get product images from app’s server-side storage as the approach shown in this sample relays on having all possible product images in application package which is not recommended and prevents from extending product list after publishing the app.
Initialization code (in my case it is in App.xaml.cs:
Note: DONT_PRESERVE_LICENSE_INFO is pragma defined in the app and controls whether mock library preserves previous purchases between debug runs. Preserving cache makes library behave as real marketplace does.
When purchasing using mock library it presents message box as follows:
That’s it for now. Next time I will blog about Wallet functionality.