×
Namespaces

Variants
Actions
Revision as of 12:09, 28 November 2013 by SB Dev (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Increase your revenue with In-App Purchase on Windows Phone 8

From Nokia Developer Wiki
Jump to: navigation, search

In-App Purchase is one of the key success to monetize from your application since you could distribute your product for free to get the number of users and then gain the money after that. This article will show you how to enable In-App Purchase on your Windows Phone 8 in few easy steps

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 920
Compatibility
Platform(s):
Windows Phone 8
Article
Created: nuuneoi (15 Dec 2012)
Reviewed: SB Dev (15 Sep 2013)
Last edited: SB Dev (28 Nov 2013)

Contents

Preparation

You need an active Windows Phone Dev Center Account before implementing In-App Purchase even for testing. Multiple ways of getting an account are outlined in this article: App deployment options for Windows Phone.

Warning.pngWarning: You will get an exception with the article test code if there is no complete Beta App submission for testing. Please note that after creation of the submission it can take several hours before the required MetaData has been deployed to servers worldwide.

Submit the Windows Phone App as Beta version

First of all, you need to create new simple project for example Hello World. Compile the project to make XAP file. Place it somewhere on your computer.

Now go to Windows Phone Dev Center portal and Submit new App at https://dev.windowsphone.com/en-us/AppSubmission/Hub

StoreRegisterStep0.png

Click at 1 App Info and fill in Application's info. The important part of this page is you need to expand More Options and set Distribution channels to Beta. Don't forget to assign the Live email account of your beta tester in the textbox below as well. If you have more than one beta tester, you could separate each one by semi-colons.

StoreRegisterStep1.jpg

Once done, click Submit and go to next step 2 Upload and describe you XAP(s)

StoreRegisterStep2.jpg

Browse for the XAP file you compiled in the first step and upload it to Store.

StoreRegisterStep3.jpg

Fill in every single mandatory fill (which is not so many) and upload the App Icon (300x300 px), Background Image (1000x800px) and screenshots for every single supported screen resolution. Since this is a beta app submission, you could just submit the blank image with specific resolution. So don't worry about it.

StoreRegisterStep4.jpg

Save and Submit

Go to App Details page and note down the App ID.

StoreRegisterStep15.jpg

Change Product ID in WMAppManifest.xml file of your Windows Phone project to the App ID provided above.

StoreRegisterStep17.jpg

Add In-App Product(s)

Now it's time to add In-App product associated with the application. To do that, click on Products link inside application info page.

StoreRegisterStep8.jpg

Go on the instruction. Just fill in all of required fields. Please note that there are two types of In-App product, Consumable and Durable

Type Description
Consumable A product that is purchased, used (consumed), and can be purchased again.
Durable A product that is purchased and owned by the purchaser forever. It is not consumed.

Choose one suit your product best and ... done ! =D Your In-App product is now ready. However please note that it might take up to 24 hours to make the item appear in the system.

StoreRegisterStep18.jpg

Now move to coding part.

Coding Part 1: In-App product listing

Add a namespace using like this.

using Windows.ApplicationModel.Store;
using Store = Windows.ApplicationModel.Store;

To list the products associated with this application, just simply call the command.

       ListingInformation li = await Store.CurrentApp.LoadListingInformationAsync();
foreach (string key in li.ProductListings.Keys)
{
ProductListing pListing = li.ProductListings[key];
System.Diagnostics.Debug.WriteLine(key);
}

You could get the product information from the variable pListing for example pListing.Name will present the product name entered in Windows Phone Dev Center portal, pListing.FormattedPrice present the price, etc.

To check that user has already bought the product or not yet, simply call:

Store.CurrentApp.LicenseInformation.ProductLicenses[key].IsActive

Now integrate with the UI

        <ScrollViewer HorizontalAlignment="Left" Margin="12,0,12,0" Grid.Row="1">
<ItemsControl x:Name="pics">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Margin="4" Source="{Binding imgLink}"/>
<StackPanel Grid.Column="1" Margin="0,30,0,0">
<TextBlock Foreground="white" FontWeight="ExtraBold" Text="{Binding Name}" />
<TextBlock Foreground="white" FontWeight="Normal" Text="{Binding Status}" />
<Button Content="Buy Now" Visibility="{Binding BuyNowButtonVisible}" Click="ButtonBuyNow_Clicked" Tag="{Binding key}" />
</StackPanel>
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
    public class ProductItem
{
public string imgLink { get; set; }
public string Status { get; set; }
public string Name { get; set; }
public string key { get; set; }
public System.Windows.Visibility BuyNowButtonVisible { get; set; }
}
        public ObservableCollection<ProductItem> picItems = new ObservableCollection<ProductItem>();
 
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
RenderStoreItems();
base.OnNavigatedTo(e);
}
 
private async void RenderStoreItems()
{
picItems.Clear();
 
try
{
//StoreManager mySM = new StoreManager();
ListingInformation li = await Store.CurrentApp.LoadListingInformationAsync();
 
foreach (string key in li.ProductListings.Keys)
{
ProductListing pListing = li.ProductListings[key];
System.Diagnostics.Debug.WriteLine(key);
 
string status = Store.CurrentApp.LicenseInformation.ProductLicenses[key].IsActive ? "Purchased" : pListing.FormattedPrice;
 
string imageLink = string.Empty;
 
picItems.Add(
new ProductItem {
imgLink = key.Equals("molostickerdummy") ? "/Res/41.png" : "/Res/18.png",
Name = pListing.Name,
Status = status,
key = key,
BuyNowButtonVisible = Store.CurrentApp.LicenseInformation.ProductLicenses[key].IsActive ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible
}
);
}
 
pics.ItemsSource = picItems;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}

And this is what you get as a result

Wp ss 20121215 0002.jpg

Easy, right? Huh ;)

Coding Part 2: Buy an item

To make a buy request, you could simply call this command

string receipt = await Store.CurrentApp.RequestProductPurchaseAsync(pID, false);

which pID represent the ProductId. And this is the complete way to call it.

        private async void ButtonBuyNow_Clicked(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
 
string key = btn.Tag.ToString();
 
if (!Store.CurrentApp.LicenseInformation.ProductLicenses[key].IsActive)
{
ListingInformation li = await Store.CurrentApp.LoadListingInformationAsync();
string pID = li.ProductListings[key].ProductId;
 
string receipt = await Store.CurrentApp.RequestProductPurchaseAsync(pID, false);
 
RenderStoreItems();
}
}

Once user press Buy Now button, application will navigate to Store like this.

Wp ss 20121215 0004.jpg

Store will proceed the payment and redirect back to the application.

Wp ss 20121215 0005.jpg

And as you can see, the item is now marked as Purchased !

Congratulations. Your application is now able to work with IAP ^ ^. You could apply these basic step for your product to increase the revenue gain from your Windows Phone 8 application. For more information about IAP API, please scroll down to Reference part. Valuable resources are right there.

Remark: Don't forget to change the Distribution Channels to Public Store once you finish the testing process to publish the application to Public.

Code Snippets

You can download the code for this example from File:BuyItemIAP.zip

Reference

For more information about IAP, you could find from links below

This page was last modified on 28 November 2013, at 12:09.
1233 page views in the last 30 days.