Although we prefer Windows Phone 8.1 apps over Windows Phone 8 applications – read this post for more information – we were asked by a reader to write a tutorial on how to integrate in-app purchases in a Windows Phone 8 app.

There are multiple ways to implement in-app purchases in a testing environment, but in this tutorial we describe the fastest way to get the app ready for submission. By doing this you can immediately upload the app to the Windows Phone store without changing anything in the code.

Creating the app

First we’re going to create the Windows Phone 8 application. To do so, open Visual Studio, click File – New – Project and select Visual C# – Store Apps – Windows Phone Apps – Blank App (Windows Phone Silverlight). I gave the app the name ‘WP8 In app purchase’.

We’ll change the page titles, and add a button, which will be used to register a purchase.
When the app is successfully created, open Mainpage.xaml and replace the grid with the following code:

When the code is inserted, you should see this in the designer window:

2015-05-15_10-44-51

Don’t forget to create the buyButton_Click method in MainPage.xaml.cs, like this:

The last thing we have to do now, before submitting the app as a beta application to the store, is building the app. In Visual Studio, click on Build – Build Solution.

1

 

Submitting the app as a beta

Now we’re uploading the application to the Windows Phone store as a beta, when we’ve done that, we can add in-app purchase slots to the application.

To submit the application as a beta app, go to http://dev.windowsphone.com/dashboard and click on the ‘Submit App’ button. This is what you’ll get:

2
First, click on App info, and fill in all the information. Before saving, click on ‘More options’ and select ‘Beta’, under ‘Distribution channels’, type in your email address
, and eventually those of your testers. Now you can save the information, and you’ll get brought back to the overview.

Click ‘ Upload and describe your package(s). You’ll have to upload the application xap file, you’ll find this in the directory of your Visual Studio project, under bin – debug.

Also don’t forget to fill in the description for the store. It’s required too that you upload a 300×300 app title icon, and at least one screenshot. Then click ‘Save’.

Now submit your application.

2015-05-15_10-06-27

When your application is saved, go to the dashboard – Apps, select the application you just submitted and click on ‘Details’. Here you should copy the App ID.

2015-05-15_10-26-39

Adding In-App purchases

Now we have to add the in-app purchases to the Windows Phone store, so they will be available for the application to use.

Go to the dashboard and click on ‘Apps’ in the left bar, now select the application you just uploaded. Click on ‘Products’ and ‘Add in-app product’. Now you’ll get to an overview page, where you should select ‘In-app product properties’. Here you fill in all the properties like below:

2015-05-15_10-21-59We select ‘Durable’ as product type, because this will be a one-time payment, which will last forever. For the Base price, just keep the default setting, we can’t change that now, because it’s a beta application. Press the ‘Save’ button, and go on to the ‘Description’ section. Give your product a title and description, add a product image and choose your language. I kept the language at ‘English (international)’. Now you can save and submit the in-app purchase.

Adding in-app purchases to the app

We have created the in-app purchase slots now, but we still have to implement everything in our app. To do this, open your application again in Visual Studio. Go to MainPage.xaml and add the following code. Make sure to change the product ID (“Product1”) to whatever product ID you chose in the previous section.

Also add  using Windows.ApplicationModel.Store; to the top of the page (MainPage.xaml.cs).

The code is done, now we need to edit the Product ID in WMAppManifest. To open WMAppManifest, in solution explorer, unfold the ‘Properties’ item and double-click on ‘WMAppManifest.xml’. Select the ‘Packaging’ tab and paste your app ID, which you copied previously, between two curly brackets. Like this: {pasteYourAppID} .

2015-05-15_10-48-24

Now your app is ready to be build again and you can submit it to the Windows Phone Store. When the app is not published yet, it’s possible that you get errors when trying the in-app purchases, that should be solved after the app is available in the store (eventually still in beta).

 

You can download the source code of this project here.