啟用應用程式內產品購買Enable in-app product purchases

無論您的 app 是否免費,都可以直接從 app 內銷售內容、其他 app 或新的 app 功能 (例如解除鎖定遊戲的下一個關卡)。Whether your app is free or not, you can sell content, other apps, or new app functionality (such as unlocking the next level of a game) from right within the app. 以下示範如何在 app 中啟用這些產品。Here we show you how to enable these products in your app.

重要

這篇文章示範如何使用 Windows.ApplicationModel.Store 命名空間的成員來啟用在應用程式內產品購買。This article demonstrates how to use members of the Windows.ApplicationModel.Store namespace to enable in-app product purchases. 此命名空間不再提供新功能更新,建議您改為使用 Windows.Services.Store 命名空間。This namespace is no longer being updated with new features, and we recommend that you use the Windows.Services.Store namespace instead. Windows. Store 命名空間支援最新的附加元件類型(例如儲存管理的可使用附加元件和訂閱),其設計目的是要與合作夥伴中心和存放區所支援的未來產品和功能類型相容。The Windows.Services.Store namespace supports the latest add-on types, such as Store-managed consumable add-ons and subscriptions, and is designed to be compatible with future types of products and features supported by Partner Center and the Store. Windows.Services.Store 命名空間在 Windows 10 (版本 1607) 中引進,只適用於目標為 Visual Studio 中 Windows 10 Anniversary Edition (10.0;組建 14393) 或更新版本的專案。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. 如需有關使用 Windows.Services.Store 命名空間來啟用 app 內產品購買的詳細資訊,請參閱 本文For more information about enabling in-app product purchases using the Windows.Services.Store namespace, see this article.

注意

試用版的 App 無法提供應用程式內產品。In-app products cannot be offered from a trial version of an app. 使用試用版 App 的客戶只有在購買 App 的完整版本後,才能購買應用程式內產品。Customers using a trial version of your app can only buy an in-app product if they purchase a full version of your app.

PrerequisitesPrerequisites

  • 要新增功能讓客戶購買的 Windows 應用程式。A Windows app in which to add features for customers to buy.
  • 初次撰寫並測試新應用程式內產品的程式碼時,您必須使用 CurrentAppSimulator 物件,而不是 CurrentApp 物件。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. 若要這樣做,您必須自訂% userprofile% \ AppData \ 本機 \ 套件 \ < 套件名稱 > \ LocalState \ Microsoft \ Windows Store \ ApiData 中名為 WindowsStoreProxy.xml 的檔案。To do this, you need to customize the file named WindowsStoreProxy.xml in %userprofile%\AppData\local\packages\<package name>\LocalState\Microsoft\Windows Store\ApiData. Microsoft Visual Studio 模擬器會在您第一次執行您的 App 時建立這個檔案,或者您也可以在執行階段載入自訂的檔案。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. 如需詳細資訊,請參閱使用 WindowsStoreProxy.xml 檔案搭配 CurrentAppSimulatorFor more info, see Using the WindowsStoreProxy.xml file with CurrentAppSimulator.
  • 本主題也會參照 Microsoft Store 範例中提供的程式碼範例。This topic also references code examples provided in the Store sample. 這個範例非常適合用來體驗實機操作針對通用 Windows 平台 (UWP) app 提供的不同貨幣選項。This sample is a great way to get hands-on experience with the different monetization options provided for Universal Windows Platform (UWP) apps.

步驟 1:初始化 App 的授權資訊Step 1: Initialize the license info for your app

在您的 App 進行初始化時,請透過初始化 CurrentAppCurrentAppSimulator 來為應用程式取得 LicenseInformation 物件,以啟用購買應用程式內產品的功能。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 InitializeApp()
{
    // Some app initialization code...

    // Initialize the license info for use in the app that is uploaded to the Store.
    // Uncomment the following line in the release version of your app.
    //   licenseInformation = CurrentApp.LicenseInformation;

    // Initialize the license info for testing.
    // Comment the following line in the release version of your app.
    licenseInformation = CurrentAppSimulator.LicenseInformation;

    // Other app initialization code...
}

步驟 2:在您的 App 新增應用程式內的購買選項Step 2: Add the in-app offers to your app

針對您想要透過應用程式內產品提供的每項功能,建立一個購買選項,然後新增至您的 App。For each feature that you want to make available through an in-app product, create an offer and add it to your app.

重要

在將您的 App 送出至市集之前,您必須先把要呈現給客戶的所有應用程式內產品新增至 App。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. 如果您想要稍後再新增應用程式內產品,您就必須更新 App,然後重新送出新版本。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""SpaceMissionLevel4"
    • "ContosoCloudSave""ContosoCloudSave"
    • "RainbowThemePack""RainbowThemePack"

注意

您在程式碼中使用的應用程式內提供權杖必須符合您在合作夥伴中心中為應用程式定義對應的附加元件時所指定的產品識別碼值。The in-app offer token that you use in your code must match the product ID value you specify when you define the corresponding add-on for your app in Partner Center.

  1. 以條件性區塊來撰寫功能的程式碼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.

    下列範例示範如何在授權專屬的條件性區塊中,撰寫 featureName 產品功能的程式碼。Here's an example that shows how you can code a product feature named featureName in a license-specific conditional block. featureName 字串是可在 App 內唯一識別這個產品的權杖,同時也可用來在市集中識別此產品。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
    }
    
  2. 新增此功能的購買 UIAdd 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. 請以您為購買對話方塊自訂的程式碼取代 "show the purchase dialog" 註解 (例如提供一個含有親切提醒「購買此應用程式!」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).

    async void BuyFeature()
    {
        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.
        }
    }
    

步驟 3:將測試程式碼變更成最後呼叫Step 3: Change the test code to the final calls

這個步驟很簡單:在您 App 的程式碼中,將 CurrentAppSimulator 的每個參考變更成 CurrentAppThis is an easy step: change every reference to CurrentAppSimulator to CurrentApp in your app's code. 您不再需要提供 WindowsStoreProxy.xml 檔案,因此可以將該檔案從您 App 的路徑中移除 (不過您可以加以儲存,以供在下個步驟中設定應用程式內的購買選項時參考)。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).

步驟 4:在市集中設定應用程式內產品購買選項Step 4: Configure the in-app product offer in the Store

在合作夥伴中心中,流覽至您的應用程式,並建立符合您的應用程式內產品供應專案的 附加 元件。In Partner Center, navigate to your app and create an add-on that matches your in-app product offer. 定義產品識別碼、類型、價格與附加元件的其他屬性。Define the product ID, type, price, and other properties for your add-on. 請確定在測試時,將它設定為與 WindowsStoreProxy.xml 相同的設定。Make sure that you configure it identically to the configuration you set in WindowsStoreProxy.xml when testing.

注意

您在程式碼中使用的應用程式內提供權杖必須符合您在合作夥伴中心中為對應附加元件指定的 產品識別碼 值。The in-app offer token that you use in your code must match the product ID value you specify for the corresponding add-on in Partner Center.

備註Remarks

如果您想要為客戶提供消費性應用程式內產品選項 (可購買、耗盡,並在想要時再次購買的項目),請前往啟用消費性應用程式內產品購買主題。If you're interested in providing your customers with consumable in-app product options (items that can be purchased, used up, and then purchased again if desired), move on to the Enable consumable in-app product purchases topic.

如果您需要使用收據來確認使用者已進行 App 內購買,請務必檢閱使用收據來驗證產品購買If you need to use receipts to verify that user made an in-app purchase, be sure to review Use receipts to verify product purchases.