处理Windows App Store应用内购买收据

本文关键字:应用 Windows App Store 处理 | 更新日期: 2023-09-27 17:49:18

我想处理这种情况,用户在这台机器上执行购买,但在另一台机器上运行功能。

我知道我必须检查收据。我想知道,这样做是正确的吗?注意,我绕过了签名检查。因为,我需要设置一个后端系统,我不想在这个时候考虑。我甚至不能在本地执行签名检查,因为Windows 8商店应用程序不提供System.Security.Cryptography API。

我想知道,下面的代码片段是否足以处理大多数情况?注意,我无法测试代码,因为我需要发布一个新版本,以便我测试代码。

private async void history_Click(object sender, RoutedEventArgs e)
{
    bool OK = true;
    // The next line is commented out for production/release.       
    LicenseInformation licenseInformation = CurrentApp.LicenseInformation;
    if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
    {
        try
        {
            // The customer doesn't own this feature, so 
            // show the purchase dialog.
            String receipt = await CurrentApp.RequestProductPurchaseAsync("PremiumFeatures", true);
            // the in-app purchase was successful
            licenseInformation = CurrentApp.LicenseInformation;
            if (licenseInformation.ProductLicenses["PremiumFeatures"].IsActive || receipt.Length > 0)
            {
                // In some situations, you may need to verify that a user made an in-app purchase. 
                // For example, imagine a game that offers downloaded content. If the user who 
                // purchased the content wants to play the game on another PC, you need to verify 
                // that the user purchased the content.
                //
                // Receipt is used to handle such situation.
                //
                // Validate receipt is complicated, as it requires us to setup a backend system.
                // http://msdn.microsoft.com/en-US/library/windows/apps/jj649137
                //
                // I will just assume non empty receipt string means valid receipt.
                OK = true;
            }
            else
            {
                OK = false;
            }
        }
        catch (Exception)
        {
            // The in-app purchase was not completed because 
            // an error occurred.
            OK = false;
        }
    }
    else
    {
        // The customer already owns this feature.
        OK = true;
    }
    if (OK)
    {
        // Launch premium feature.
        Frame.Navigate(typeof(HistoryPage));
    }
}

处理Windows App Store应用内购买收据

您可以使用CurrentAppSimulator而不是CurrentApp来测试您的代码。