in app purchase with #win8dev #winrt

Windows 8 – WinRT API exposes few classes that make implementing #IAP a simple task. In additional it provides simulator class that makes testing the functionality easy.

The store related functionality is exposed by the following classes

CurrentApp class                       Defines methods and properties you can use to get license and listing info about the current app and perform in-app purchases.
CurrentAppSimulator class Defines methods and properties used to instantiate an object that you can  use to get simulated license info during testing.
LicenseInformation class Provides access to the current app’s license metadata.
ListingInformation class Provides the listing info that describes the app in the Windows Store.
ProductLicense class Provides info about a license that is associated with an in-app offer.
ProductListing class Provides localized info about an  in-app offer in your app.

To test the code in simulator, you need to load some data is. I used in-app-purchase.xml like in the store sample – it looks like this

<?xml version="1.0" encoding="utf-16" ?>
<CurrentApp>
  <ListingInformation>
    <App>
      <AppId>988b90e4-5d4d-4dea-99d0-e423e414ffbc</AppId>
      <LinkUri>http://apps.microsoft.com/webpdp/app/988b90e4-5d4d-4dea-99d0-e423e414ffbc</LinkUri>
      <CurrentMarket>en-us</CurrentMarket>
      <AgeRating>3</AgeRating>
      <MarketData xml:lang="en-us">
        <Name>In-app purchases</Name>
        <Description>AppDescription</Description>
        <Price>0.00</Price>
        <CurrencySymbol>$</CurrencySymbol>
        <CurrencyCode>USD</CurrencyCode>
      </MarketData>
    </App>
    <Product ProductId="SingleChar1Hint">
      <MarketData xml:lang="en-us">
        <Name>SingleChar1Hint</Name>
        <Price>0.99</Price>
        <CurrencySymbol>$</CurrencySymbol>
        <CurrencyCode>USD</CurrencyCode>
      </MarketData>
    </Product>
    <Product ProductId="SingleChar2Hint">
      <MarketData xml:lang="en-us">
        <Name>SingleChar2Hint</Name>
        <Price>0.99</Price>
        <CurrencySymbol>$</CurrencySymbol>
        <CurrencyCode>USD</CurrencyCode>
      </MarketData>
    </Product>
    <Product ProductId="DoubleCharHint">
      <MarketData xml:lang="en-us">
        <Name>DoubleCharHint</Name>
        <Price>1.29</Price>
        <CurrencySymbol>$</CurrencySymbol>
        <CurrencyCode>USD</CurrencyCode>
      </MarketData>
    </Product>
    <Product ProductId="AdFree">
      <MarketData xml:lang="en-us">
        <Name>AdFree</Name>
        <Price>0.99</Price>
        <CurrencySymbol>$</CurrencySymbol>
        <CurrencyCode>USD</CurrencyCode>
      </MarketData>
    </Product>
  </ListingInformation>
  <LicenseInformation>
    <App>
      <IsActive>true</IsActive>
      <IsTrial>false</IsTrial>
    </App>
    <Product ProductId="SingleChar1Hint">
      <IsActive>false</IsActive>
    </Product>
    <Product ProductId="SingleChar2Hint">
      <IsActive>false</IsActive>
    </Product>
    <Product ProductId="DoubleCharHint">
      <IsActive>true</IsActive>
    </Product>
    <Product ProductId="AdFree">
      <IsActive>true</IsActive>
    </Product>
  </LicenseInformation>
</CurrentApp>

In the app initialisation code, I tend to load this data into the CurrentAppSimulator.

#if DEBUG
    StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
    StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
    await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
#endif 

as you can see I use compiler preprocessing directive to make sure only debug code uses CurrentAppSimulator and the dummy IAP pricing data. Once the data is loaded, you can use CurrentAppSimulator like you CurrentApp class. Lets have a look further. I continue with compiler directives through out..
In the Page loaded event handler, I check to see if I need to show any particular IAP option

#if DEBUG
    LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
    ListingInformation productListing = await CurrentAppSimulator.LoadListingInformationAsync();
#else
    LicenseInformation licenseInformation = CurrentApp.LicenseInformation;
    ListingInformation  productListing = await CurrentApp.LoadListingInformationAsync();
#endif

    var adFreeIAPListing = productListing.ProductListings["AdFree"];
    this.spAdFree.Visibility = (bAdFree ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible);
    this.tbAdFree.Text = String.Format("buy for {0}", adFreeIAPListing.FormattedPrice);

the buy button itself is in stackpanel called spAdFree. Lets look at the IAP handling code

#if DEBUG
    await CurrentAppSimulator.RequestProductPurchaseAsync("AdFree", false);
#else
    await CurrentApp.RequestProductPurchaseAsync("AdFree", false);
#endif
    if (AdFreeChanged != null)
        AdFreeChanged();

The AdFreeChanged in an event I expose to toggle main page’s ad control. The IAP is shown in Settings Flyout. If you have any questions, drop me a message

11 thoughts on “in app purchase with #win8dev #winrt

  1. Pingback: Windows 8 Developer Links – 2012-09-17Dan Rigby | Dan Rigby

  2. Hi,

    is that enough or do I have to save when the user has bought the feature? When will the app update the status and what happens when the app has no internet connection?

    When I try to call await CurrentApp.RequestProductPurchaseAsync(“AdFree”, false) without having that In app purchasement. Sometimes I get a dialog that I don’t have it set up and sometimes just nothing happens and it doesn’t step over the line. Will this work better when using it with a real feature?

    Best regards,
    mos

  3. Hi, my app’s already in the windows store and I’d like to add a trial to it, I understand the sample code, just one thing I wanted to clarify is how would I specify a trial period, pricing (all the content that’s in the dummy data file) for a live app that’s already on the store? Thanks.

      • Hey thanks, I located those settings, another quick question, once the app is uploaded can I test it without providing the dummy file and using the currentappsimulator class (running the app in release mode from the machine I uploaded to the store), or would I have to download the app from the win8 store to test,

      • you configure so that the debug mode usese CurrentAppSimulator and the release code (store version) using CurrentApp.

        The only way to test release version is to test it from store

  4. hi is data .xml file create directly or we have to create if we have to create then tell me steps how to do please

Leave a comment