Enable in-app product purchases (XAML)

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

Your app can offer products and features that your customers can buy from within the app. Here we show you how to enable these products in your app.

Note that a customer can only buy an in-app product if they've purchased a full version of your app.

What you need to know

Technologies

Prerequisites

  • A Windows Runtime app in which to add features for customers to buy.

Instructions

Step 1: Initialize the license info for your app

When your app is initializing, get the LicenseInformation object for your app by initializing the CurrentApp or CurrentAppSimulator to enable purchases of an in-app product.

void AppInit()
{
    // some app initialization functions 

    // Get the license info
    // The next line is commented out for testing.
    // licenseInformation = CurrentApp.LicenseInformation;

    // The next line is commented out for production/release.       
    licenseInformation = CurrentAppSimulator.LicenseInformation;

    // other app initialization functions
}

Note  When you code and test new in-app products for the first time, you must use the CurrentAppSimulator object instead of the CurrentApp object. This way you can verify your license logic using simulated calls to the license server instead of calling the live server. To do this, you need to customize the file named "WindowsStoreProxy.xml" in %userprofile%\AppData\local\packages\<package name>\LocalState\Microsoft\Windows Store\ApiData. The Microsoft Visual Studio simulator creates this file when you run your app for the first time—or you can also load a custom one at runtime. For more info, see CurrentAppSimulator docs.

 

Step 2: Add the in-app offers to your app

For each feature that you want to make available through an in-app product, create an offer and add it to your app.

Important  You must add all the in-app products that you want to present to your customers to your app before you submit it to the Store. If you want to add new in-app products later, you must update your app and re-submit a new version.

 

  1. Create an in-app offer token

    You identify each in-app product in your app by a token. This token is a string that you define and use in your app and in the Store to identify a specific in-app product. Give it a unique (to your app) and meaningful name so that you can quickly identify the correct feature it represents while you are coding. Here are some examples of names:

    • "SpaceMissionLevel4"
    • "ContosoCloudSave"
    • "RainbowThemePack".
  2. Code the feature in a conditional block

    You must put the code for each feature that is associated with an in-app product in a conditional block that tests to see if the customer has a license to use that feature.

    Here's an example that shows how you can code a product feature named featureName in a license-specific conditional block. The string, featureName, is the token that uniquely identifies this product within the app and is also used to identify it in the Store.

    if (licenseInformation.ProductLicenses["featureName"].IsActive) 
    {
        // the customer can access this feature
    } 
    else
    {
        // the customer can't access this feature
    }
    
  3. Add the purchase UI for this feature

    Your app must also provide a way for your customers to purchase the product or feature offered by the in-app product. They can't purchase them through the Store in the same way they purchased the full app.

    Here's how to test to see if your customer already owns an in-app product and, if they don't, displays the purchase dialog so they can buy it. Replace the comment "show the purchase dialog" with your custom code for the purchase dialog (such as a page with a friendly "Buy this app!" button).

    void BuyFeature1() {
        if (!licenseInformation.ProductLicenses["featureName"].IsActive)
        {
            try
                                    {
                // The customer doesn't own this feature, so 
                // show the purchase dialog.
    
                await CurrentAppSimulator.RequestProductPurchaseAsync("featureName", false);
                //Check the license state to determine if the in-app purchase was successful.
            }
            catch (Exception)
            {
                // The in-app purchase was not completed because 
                // an error occurred.
            }
        } 
        else
        {
            // The customer already owns this feature.
        }
    }
    

Step 3: Change the test code to the final calls

This is an easy step: change every reference to CurrentAppSimulator to CurrentApp in your app's code. You don't need to provide the WindowsStoreProxy.xml file any longer, so remove it from your app's path (although you may want to save it for reference when you configure the in-app offer in the next step).

Step 4: Configure the in-app product offer in the Store

Before you submit your app to the Store, add each in-app product in the Submit an app workflow. This is where you specify the token, price, and feature lifetime of an in-app product. Make sure that you configure it identically to the configuration you set in WindowsStoreProxy.xml when testing.

Step 5: Describe the in-app product in the Store

After you upload your app's package to the Store, you can enter the description of each in-app product on the Description page when submitting your app. If your app supports more than one language, you must describe each in-app product in every language that your app supports.

Provide a clear, specific description that precisely calls out what feature the in-app product adds to the app. If you aren't clear about the value of the product above and beyond what the app itself provides, it will be hard for you to sell it. For more details on specific parts of an in-app product description, see Your app's description.

Remarks

If you're interested in providing your customers with consumable in-app product options, move on to the Enable consumable in-app product purchases topic.

If you need to use receipts to verify that user made an in-app purchase, be sure to review Using receipts to verify product purchases.

Enable consumable in-app product purchases

Trial app and in-app purchase sample