Bewerken

Delen via


Enable in-app purchases of apps and add-ons

This article demonstrates how to use members in the Windows.Services.Store namespace to request the purchase the current app or one of its add-ons for the user. For example, if the user currently has a trial version of the app, you can use this process to purchase a full license for the user. Alternatively, you can use this process to purchase an add-on, such as a new game level for the user.

To request the purchase of an app or add-on, the Windows.Services.Store namespace provides several different methods:

Each method presents a standard purchase UI to the user and then completes asynchronously after the transaction is complete. The method returns an object that indicates whether the transaction was successful.

Note

The Windows.Services.Store namespace was introduced in Windows 10, version 1607, and it can only be used in projects that target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio. If your app targets an earlier version of Windows 10, you must use the Windows.ApplicationModel.Store namespace instead of the Windows.Services.Store namespace. For more information, see this article.

Prerequisites

This example has the following prerequisites:

  • A Visual Studio project for a Universal Windows Platform (UWP) app that targets Windows 10 Anniversary Edition (10.0; Build 14393) or a later release.
  • You have created an app submission in Partner Center and this app is published in the Store. You can optionally configure the app so it is not discoverable in the Store while you test it. For more information, see our testing guidance.
  • If you want to enable in-app purchases for an add-on for the app, you must also create the add-on in Partner Center.

The code in this example assumes:

  • The code runs in the context of a Page that contains a ProgressRing named workingProgressRing and a TextBlock named textBlock. These objects are used to indicate that an asynchronous operation is occurring and to display output messages, respectively.
  • The code file has a using statement for the Windows.Services.Store namespace.
  • The app is a single-user app that runs only in the context of the user that launched the app. For more information, see In-app purchases and trials.

Note

If you have a desktop application that uses the Desktop Bridge, you may need to add additional code not shown in this example to configure the StoreContext object. For more information, see Using the StoreContext class in a desktop application that uses the Desktop Bridge.

Code example

This example demonstrates how to use the RequestPurchaseAsync method of the StoreContext class to purchase an app or add-on with a known Store ID. For a complete sample application, see the Store sample.

private StoreContext context = null;

public async void PurchaseAddOn(string storeId)
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    workingProgressRing.IsActive = true;
    StorePurchaseResult result = await context.RequestPurchaseAsync(storeId);
    workingProgressRing.IsActive = false;

    // Capture the error message for the operation, if any.
    string extendedError = string.Empty;
    if (result.ExtendedError != null)
    {
        extendedError = result.ExtendedError.Message;
    }

    switch (result.Status)
    {
        case StorePurchaseStatus.AlreadyPurchased:
            textBlock.Text = "The user has already purchased the product.";
            break;

        case StorePurchaseStatus.Succeeded:
            textBlock.Text = "The purchase was successful.";
            break;

        case StorePurchaseStatus.NotPurchased:
            textBlock.Text = "The purchase did not complete. " +
                "The user may have cancelled the purchase. ExtendedError: " + extendedError;
            break;

        case StorePurchaseStatus.NetworkError:
            textBlock.Text = "The purchase was unsuccessful due to a network error. " +
                "ExtendedError: " + extendedError;
            break;

        case StorePurchaseStatus.ServerError:
            textBlock.Text = "The purchase was unsuccessful due to a server error. " +
                "ExtendedError: " + extendedError;
            break;

        default:
            textBlock.Text = "The purchase was unsuccessful due to an unknown error. " +
                "ExtendedError: " + extendedError;
            break;
    }
}