简体   繁体   中英

Handling Windows App Store In-app purchase receipt

I would like to handle situation in such case, user performs purchasing in this machine, but which to run the feature in another machine.

I know I have to check for receipt. I was wondering, is this the correct way to do so? Note that, I bypass signature check. As, I need to setup a backend system which I do not want to consider at this moment. I can't even perform signature check locally, as Windows 8 Store App doesn't provide System.Security.Cryptography API.

I was wondering, whether the following code snippet is good enough to handle most of the cases? Note, I unable to test the code, as I need to publish a new version, in order for me to test the code.

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));
    }
}

You can test your code using CurrentAppSimulator instead of CurrentApp . It looks ok to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM