啟用消費性應用程式內產品購買 (HTML)
[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]
您可以透過市集商業平台提供消費性的應用程式內產品—亦即可購買、使用然後再次購買的項目—為客戶提供既健全又可靠的購買體驗。 這對於像遊戲內貨幣 (金幣、錢幣等) 這種可在買來後用來購買特定火力升級配備的東西,特別有用。
您必須知道的事
技術
- Windows Runtime
- Windows.ApplicationModel.Store
先決條件
本主題涵蓋消費性應用程式內產品的購買和履行狀況報告。如果您不熟悉應用程式內產品,請檢閱啟用應用程式內產品購買,以了解授權資訊及如何在市集中正確列出應用程式內產品。
初次撰寫並測試新應用程式內產品的程式碼時,您必須使用 CurrentAppSimulator 物件,而不是 CurrentApp 物件。如此一來,您就可以利用對授權伺服器進行模擬呼叫來驗證授權邏輯,而不是呼叫使用中的伺服器。若要這樣做,您必須自訂 %userprofile%\AppData\local\packages\<package name>\LocalState\Microsoft\Windows Store\ApiData 中名為 "WindowsStoreProxy.xml" 的檔案。Microsoft Visual Studio 模擬器會在您第一次執行您的應用程式時建立這個檔案,或者您也可以在執行階段載入自訂的檔案。如需詳細資訊,請參閱 CurrentAppSimulator 文件。
本主題也會參照 MSDN 程式庫提供的試用版 app 和 App 內購買範例中的程式碼範例。這個範例很適合實際操作 Windows 市集應用程式提供的不同貨幣選項。
指示
步驟 1: 提出購買要求
就像透過市集進行的任何其他購買一樣,初始購買要求是以 RequestProductPurchaseAsync 提出。消費性應用程式內產品的不同之處在於,客戶在順利購買這類產品之後,除非應用程式已通知市集先前的購買已順利履行,否則客戶無法再次購買相同的產品。您的應用程式必須負責履行已購買的消費性產品,並在履行後通知市集。
下列範例示範消費性應用程式內產品購買要求。您會注意到程式碼註解指出在下列兩種不同的情況下,應用程式應該於何時在本機履行消費性應用程式內產品—在要求成功的情況下,以及在因為購買尚未履行的相同產品而導致要求失敗的情況下。
function purchaseProduct1() {
CurrentAppSimulator.requestProductPurchaseAsync("product1").done(
function (purchaseResults) {
if (purchaseResults.status === ProductPurchaseStatus.succeeded) {
tempTransactionId["product1"] = purchaseResults.transactionId;
// Grant the user their purchase here, and then pass the product ID and transaction ID to currentApp.reportConsumableFulfillment
// To indicate local fulfillment to the Windows Store.
} else if (purchaseResults.status === ProductPurchaseStatus.notFulfilled) {
tempTransactionId["product1"] = purchaseResults.transactionId;
// First check for unfulfilled purchases and grant any unfulfilled purchases from an earlier transaction.
// Once products are fulfilled pass the product ID and transaction ID to currentApp.reportConsumableFulfillment
// To indicate local fulfillment to the Windows Store.
}
}
);
}
步驟 2: 追蹤消費性產品的本機履行
將消費性應用程式內產品的存取權授與客戶時,請務必追蹤哪些產品已履行 (productId),以及該履行動作與哪個交易關聯 (transactionId)。
重要 您的應用程式必須將履行動作準確回報給市集。若要為客戶維護公平可靠的購買體驗,這個步驟是不可或缺的。
下列範例示範如何使用上一個步驟之 RequestProductPurchaseAsync 呼叫中的 PurchaseResults 屬性,來識別購買的產品是否已經履行。此範例使用陣列將產品資訊儲存在可供參照的位置,以便稍後確認是否已在本機順利履行。
function grantFeatureLocally(productId, transactionId) {
var nextIndex = grantedIds[productId].length;
grantedIds[productId][nextIndex] = transactionId;
// Grant the user the content, such as by increasing some kind of asset count
}
下個範例說明將履行回報給市集後,如何使用上個範例的陣列來存取之後要使用的產品識別碼/交易識別碼。
重要 無論您的應用程式使用哪種方法來追蹤和確認履行,都必須提供審查評鑑,以確保不會針對客戶尚未收到的項目向客戶收費。
function isLocallyFulfilled(productId, transactionId) {
for (var i in grantedIds[productId]) {
if (grantedIds[productId][i] === transactionId) {
return true;
}
}
return false;
}
步驟 3: 將產品履行回報給市集
一旦完成本機履行,您的應用程式必須進行 ReportConsumableFulfillmentAsync 呼叫,此呼叫包含了 productId 及含括該項產品購買的交易。
重要 若未將已履行的消費性應用程式內產品回報給市集,將導致使用者無法再次購買該產品,必須等到回報已履行上次的購買後才能再購買。
var result = FulfillmentResult;
result = CurrentAppSimulator.reportConsumableFulfillmentAsync("product1", tempTransactionId["product1"]);
步驟 4: 識別未履行的購買
您的應用程式可以隨時使用 GetUnfulfilledConsumablesAsync 方法,來檢查是否有任何未履行的消費性應用程式內產品。應該定期呼叫這個方法,以檢查是否有任何因為未預期的應用程式事件 (例如網路連線中斷或應用程式終止) 而未履行消費性產品存在。
下列範例示範如何使用 GetUnfulfilledConsumablesAsync 列舉未履行的項目,以及您的應用程式如何重複此清單來完成本機履行。
CurrentAppSimulator.getUnfulfilledConsumablesAsync().done(
function (unfulfilledList) {
unfulfilledList.forEach(function (product) {
logMessage += "\nProduct Id: " + product.productId + " Transaction Id: " + product.transactionId;
// This is where you would pass the product ID and transaction ID to currentAppSimulator.reportConsumableFulfillment
// To indicate local fulfillment to the Windows Store.
});
});