FAQs about In-app purchase for Windows Phone 8

Although that there is a great section that describes In-app purchase for Windows Phone 8 (IAP) on Windows Phone MSDN library, we've also received a lot of questions that were asking for some important particulars. I am writing this article to add some supplements of what the MSDNarticles don't cover in detail.

We've heard a lot that Windows Runtime and Windows Phone Runtime share a lot of code and components to each other. Yes it is true including the in-app purchase feature. And in fact we can see that Windows Phone MSDN library has richer contents than Windows Store App MSDN. However, developers must read the documents carefully as many APIs are only applying to Windows 8, like CurrentAppSimulator, ProductLicense.ExpirationDate and etc. This articles is mainly applying for Windows Phone IAP, however, the general process of a IAP is similar between Windows 8 Store app and WP Store app.

MSDN relative entries:

  1. In-app purchase for Windows Phone 8 https://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206949(v=vs.105).aspx
  2. In-App Purchase API overview for Windows Phone 8 https://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206950(v=vs.105).aspx
  3. In-app purchase testing for Windows Phone 8 https://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681689(v=vs.105).aspx
  4. Mock In-App Purchase Library https://code.msdn.microsoft.com/wpapps/Mock-In-App-Purchase-33080f0c
  5. In-app product properties https://msdn.microsoft.com/en-us/library/windowsphone/help/jj206721(v=vs.105).aspx
  6. In-app product description https://msdn.microsoft.com/en-us/library/windowsphone/help/jj206720(v=vs.105).aspx
  7. Beta testing your app and in-app products https://msdn.microsoft.com/en-us/library/windowsphone/help/jj215598(v=vs.105).aspx
  8. In-app purchase receipt verification https://code.msdn.microsoft.com/wpapps/In-app-purchase-receipt-c3e0bce4
  9. How to Set Up In-App Purchasing for Your Windows Phone App (VisualStudioMagazine.com) https://visualstudiomagazine.com/Articles/2013/07/01/In-App-Purchasing-for-Windows-Phone-8.aspx?Page=1

 

How to verify if a purchasing is succeeded

In-app purchase for Windows Phone 8 provides a great diagram and illustration for an in-app purchase workflow. Looking into the Step E) Enumerate licenses:

"When you app launches, is resumed, or following a purchase, you use the in-app purchase APIs to enumerate all licenses this user has for products in your app. Using this information, you can determine whether the user already has the products they licensed, or whether fulfillment of some products is still required. "

Probably we may rely on the return value of the CurrentApp.RequestProductPurchaseAsync function, but it is not a good practice especially for the transaction dealing with money. We should call API to enumerate the up-to-date product licenses instead.

 public async Task<List<string>> GetOwnedItems()
  
 {
  
 List<string> items = new List<string>();
  
 ListingInformation li = await CurrentApp.LoadListingInformationAsync();
  
 foreach (string key in li.ProductListings.Keys)
  
 {
  
 if (CurrentApp.LicenseInformation.ProductLicenses[key].IsActive && StoreItems.ContainsKey(key))
  
 items.Add(StoreItems[key]);
  
 }
  
 return items;
  
 } 

 

Considering the network performance, we may not need to "LoadListingInformationAsync()" all the time we purchase, but please keep in mind that enumerating the "ProductLicenses" by accessing CurrentApp.LicensesInformation property is always necessary.

Note for Consumable product

We have two types for an IAP product - This can be either ProductType.Consumable or ProductType.Durable. For durable products, once we purchase, it will be "IsActive == true" forever when we enumerate the ProductLicenses. But for consumable products, like coins in your game, we have to call

CurrentApp.ReportProductFulfillment method after the product purchase is successfully verified. This method "Notifies Store that the app has delivered the purchased goods to the user. A user can’t purchase the same product again until you have confirmed its delivery using this method. This applies only to consumable products." otherwise the product license of the "coins" would be always "active" and we cannot purchase it again.

About the receipts

It would be great to get used to requesting for a receipt for a real-life purchase and in-app purchase. Here we have some test results that are not mentioned in the official MSDN document.

We can get a string in XML that represents a "Receipt" as proof of purchase on a server by calling CurrentGetProductReceiptAsync. A typical receipt XML looks like this:

 <?xml version="1.0"?>
  
 <Receipt Version="1.0" CertificateId="A656B9B1B3AA509EEA30222E6D5E7DBDA9822DCD" xmlns="https://schemas.microsoft.com/windows/2012/store/receipt">
  
 <ProductReceipt PurchasePrice="$20.89" PurchaseDate="2012-11-30T21:32:07.096Z" Id="2f9c5c8f-3e1d-4fc7-a871-ac58f7e78053" AppId="3ec6cd9a-ca82-4d38-bfdf-ecafdb35a738" ProductId="Test" ProductType="Consumable" PublisherDeviceId="Test" MicrosoftProductId="59ef70aa-7099-4679-889e-f21919bfd2c6" />
  
 <Signature xmlns="https://www.w3.org/2000/09/xmldsig#">
  
 <SignedInfo>
  
 <CanonicalizationMethod Algorithm="https://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
  
 <SignatureMethod Algorithm="https://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
  
 <Reference URI="">
  
 <Transforms>
  
 <Transform Algorithm="https://www.w3.org/2000/09/xmldsig#enveloped-signature" />
  
 </Transforms>
  
 <DigestMethod Algorithm="https://www.w3.org/2001/04/xmlenc#sha256" />
  
 <DigestValue>FyFb1HGm+yeOIjt18M6TPD4Qzeu469vwDbQs7w72mdA=</DigestValue>
  
 </Reference>
  
 </SignedInfo> <SignatureValue>noct5CYBtRwBxVxkUeZIzeDyruLGVBqBMuFuBytpouPLACnQ5dbzdRvWX4XN67IUo0J2FW8DoYcMbf3sAS+PeKKV8SLnU+l8K1hWEbbbugHZezStTzwwkYcZuCTnAk7BYO0aiZWuXm9GiZGT9iyXsYtU1/u87L+llnVibU/m7gV8tD3vG0tVkjzV20C8666mHUsY/jxeq3ed7YY9CT0SDrh5PeL4ESaopBLcncHo/e6lcjyoKbO3e6YuIpsi8DVueeKNhpTlwa5yc0O3qzc5SGnT4Kbhj9NBEXf15/oTaLlg7lJhnQZ0mY+yR8vc4D0SkqD6e5Uc4u64hnu+g3Hphg==</SignatureValue>
  
 </Signature>
  
 </Receipt>

 

Note that, the "<Signature>" node only appears when we do an in-app purchase action on a publicly published app that is installed from Store, which means that if it is a beta app, or it is side loaded/being debugged on a device or emulator, we will not see the Signature node. This behavior seems to be annoying because we might not be able to fully test it before publicly releasing, but in fact the logic of receipt verification is 100% on the server side, we just make sure the full receipt xml could be sent to the app server.

Don't miss out this sample In-app purchase receipt verification which illustrates how we verify the transaction associating with the receipt. Someone asks us where to apply for the public key certification "IapReceiptProduction.cer" - it is shipped within the sample package, and it is used for all in-app purchase receipts and is independent with app publisher/app/product.

Testing

The only solutions for In-app purchase testing for Windows Phone 8 are to create our own Mock object by following this sample Mock In-App Purchase Library (unlike Windows 8, we don't have CurrentAppSimulator for Windows Phone Runtime), and to create a beta app on your Windows Phone Dev Center. Beta app can simulate all the transactions like a real app, but please note that the beta users won't be charged and we will not see the receipt's signature mentioned above.

-Ranger

Comments

  • Anonymous
    November 26, 2013
    Can you provide "In-app purchase receipt verification" in java edition?

  • Anonymous
    December 08, 2013
    Add java implementation for receipt verification - stackoverflow.com/.../windows-store-validate-receipts-with-java Please make sure there is no additional whitespace in the XML before processing.

  • Anonymous
    July 04, 2014
    Thank you, this information was very helpful.

  • Anonymous
    October 12, 2015
    I don't understand what's the mean of  "Beta app can simulate all the transactions like a real app, but please note that the beta users won't be charged and we will not see the receipt's signature mentioned above." ? in-app purchase a durable product in beta app will be reseted after uninstall ? Thnaks