Windows optimize

In-app purchases in Windows 8

Justin James walks you through how to check your code to determine whether a feature is purchased in your Windows 8 app.

With Windows 8 applications, it is pretty simple to actually sell the application -- just upload it to the Windows Store and set the price. But what about in-app purchases? Microsoft has made that surprisingly easy for you too. Sadly, it is complicated by some mistakes in the documentation.

All you need to do in the code is to check if the feature has been purchased. Features are identified by string values. In the Windows Store, you can define the features (Microsoft calls them products) that can be purchased and the price. To check if the product has been purchased, use the static method call Windows.ApplicationModel.Store.CurrentApp.LicenseInformation.ProductLicenses[FeatureName].IsActive; this will return a boolean value. If it has not been bought, you can use Windows.ApplicationModel.Store.CurrentApp.RequestProductPurchaseAsync(FeatureName, IncludeReceipt) to request that the user be offered a chance to buy the product. Code sample A shows a complete example (set up for a simulated environment).
async private Task<bool> CheckFeature(string featureName) {
    var applicationInformation = CurrentAppSimulator.LicenseInformation;
    if (applicationInformation.ProductLicenses[featureName].IsActive)
    {
        return true;
    }
    else
    {
        await Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(featureName, false);
        return applicationInformation.ProductLicenses[featureName].IsActive;
    }
}
Code sample A When running in the emulator, do not use the CurrentApp class, but CurrentAppSimulator; this will make sure that it does not try to work against the Windows Store. In addition, you will need to create the file WindowsStoreProxy.xml in the location %AppData%\Local\Packages\<application id>\LocalState\Microsoft\Windows Store\ApiData. The "application id" can be found in the Package.appxmanifest file as the "Package Name" (Figure A). Figure A

Click the image to enlarge.
The XML file is used by CurrentAppSimulator to replicate what the Windows Store would have for the running application. A sample is in Code sample B. Use the "id" attribute of products to match the sting value names of them. In my testing, making a simulated purchase does not update the values, and the product purchase check still returned false.
<?xml version="1.0" encoding="utf-16" ?>
<CurrentApp>
    <ListingInformation>
        <App>
            <AppId>00000000-0000-0000-0000-000000000000</AppId>
            <LinkUri>http://apps.microsoft.com/webpdp/app/00000000-0000-0000-0000-000000000000</LinkUri>
            <CurrentMarket>en-US</CurrentMarket>
            <AgeRating>3</AgeRating>
            <MarketData xml:lang="en-us">
                <Name>AppName</Name>
                <Description>AppDescription</Description>
                <Price>1.00</Price>
                <CurrencySymbol>$</CurrencySymbol>
                <CurrencyCode>USD</CurrencyCode>
           </MarketData>
</App>
<Product ProductId="TestFeature" LicenseDuration="0">
    <MarketData xml:lang="en-us">
        <Name>Test Feature</Name>
        <Price>1.00</Price>
        <CurrencySymbol>$</CurrencySymbol>
        <CurrencyCode>USD</CurrencyCode>
        </MarketData>
    </Product>
</ListingInformation>
<LicenseInformation>
    <App>
        <IsActive>true</IsActive>
        <IsTrial>true</IsTrial>
    </App>
    <Product ProductId="TestFeature">
        <IsActive>true</IsActive>
    </Product>
  </LicenseInformation>
</CurrentApp>
Code sample B

That's all there is to it!

J.Ja

About

Justin James is the Lead Architect for Conigent.

0 comments