📜 ⬆️ ⬇️

The first development experience for Windows Phone: In-App Purchasing



This article discusses the possibility of using internal payments in your mobile Windows Phone 8 applications using the example of the Daily Horoscope own application.
If this topic is interesting, then please under the cat.

At the moment in dev.windowsphone.com there is an opportunity to create free, paid, trial / demo versions of applications, as well as in-app purchases. In turn, in-app purchases are - consumable and permanent. In my application, I used the "permanent" (Fig. 1), that is, the user installed the application, and pays for additional functionality once. "Permanent" can also have a term of its action, or without urgent. An example of "permanent" is the purchase of a weapon or a unique recipe, and with a validity period - a subscription to read a magazine or a license. By "consumable" can be attributed to the currency of the application (crystals). If you have many recipes, and in order not to create an “internal product” for each recipe, you can use the consumable purchase, then the control of the purchase lies with the developer. The most important thing in the implementation of any code, try everything that is done for a long time to make asynchronous requests, otherwise the moderators will not miss such an application.

Note: if you have a trial version of the program, until the user purchases it - In-App payments will not work.
')
image
rice 1 - UI In-App Purchasing Example

In order to test your payments, you can use the special MockIAPLib library - read more [EN] , but for a simple application there is a way out much easier - just publish your application in the dev center as Beta, then all your purchases will be equal to 0 rubles, and Tests are as close as possible to combat conditions.


Fig.2 - purchase in Beta version

And so, how to create In-App Purchasing?

1. Go to the Dev Center WP and click on the link "Send application".
2. Fill out the “Application Details” (in my case, the application is completely free), immediately release or beta, etc. is indicated in the “Advanced Settings” section, etc.
3. In the left menu, select "Applications", we find there the newly created. And go to the tab "Products", there we create our product, the main thing that we need is the "Product ID".
4. Now open your project and open the WMAppManifest.xml file in it, then go to the “Packing” tab. Look at the contents of the Product ID, it should correspond to what is specified in the information about your application in the Dev Center WP - "Application ID".

Done, you have a blank application, and the products of this application. If screenshots are interesting, you can see them here [EN] .

Dependencies:
using Windows.ApplicationModel.Store; using Store = Windows.ApplicationModel.Store; 


View the list of your products in the application, in case the products appear dynamically:
code
 private async void btnListAllProducts_Click_1(object sender, RoutedEventArgs e) { StringBuilder sb = new StringBuilder(); var listing = await CurrentApp.LoadListingInformationAsync(); foreach (var product in listing.ProductListings) { sb.AppendLine(string.Format("{0}, {1}, {2},{3}, {4}", product.Key, product.Value.Name, product.Value.FormattedPrice, product.Value.ProductType, product.Value.Description)); } MessageBox.Show(sb.ToString(), "List all products", MessageBoxButton.OK); } 

Remember that the user can have bad internet, so show him that the application is working, and not hung!

Realization of a permanent purchase:
code
 async private void btnBuy_Click_1(object sender, RoutedEventArgs e) { //     var listing=await CurrentApp.LoadListingInformationAsync(); var superweapon= listing.ProductListings.FirstOrDefault( p => p.Value.ProductId == "  ID " ); try { //,     if (CurrentApp.LicenseInformation.ProductLicenses[superweapon.Value.ProductId].IsActive) { MessageBox.Show("  !"); } else { //  UI    .  receipt = await CurrentApp.RequestProductPurchaseAsync(superweapon.Value.ProductId, false); //  -   var productLicenses = CurrentApp.LicenseInformation.ProductLicenses; ProductLicense tokenLicense = productLicenses["  ID "]; if (tokenLicense.IsActive)//    { //  ,   IsolatedStorageSettings,    txtBoughtSW.Visibility=System.Windows.Visibility.Visible; txtBoughtSW.Text="  "; } else { //    } } } catch (Exception ex) { //        MessageBox.Show(ex.ToString()); } } 

How to keep everything simple, received a list with information and treat as we please. Showed the user UI purchase and wait for the result.

Example of a Consumable Purchase:
code
 async private void btnBuy50Points_Click_1(object sender, RoutedEventArgs e) { // 50 Points -      var listing=await CurrentApp.LoadListingInformationAsync(); var fiftypoints= listing.ProductListings.FirstOrDefault( p => p.Value.ProductId == "  ID " ); try { // UI  receipt=await CurrentApp.RequestProductPurchaseAsync(fiftypoints.Value.ProductId, false); if (CurrentApp.LicenseInformation.ProductLicenses[fiftypoints.Value.ProductId].IsActive) { // 50  m_pointCount+=50; //  ,  "",         50  CurrentApp.ReportProductFulfillment(fiftypoints.Value.ProductId); txtBought50Pts.Visibility=System.Windows.Visibility.Visible; txtBought50Pts.Text= "Bought 50 Points " + i++ + " times for a total of " + m_pointCount + "!"; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } 

The only difference in the consumable and permanent purchase, what in the consumable MUST be reported to the “store” that you have processed the purchase response. Otherwise, the user will not be able to buy your product (crystals) again until you issue the command:
 CurrentApp.ReportProductFulfillment(fiftypoints.Value.ProductId); 

As well as storage of purchased products falls on the developer. He needs to worry that the user can remove the application and re-install it, and the developer must correctly identify it.

An example of a permanent purchase in my application:
code
 private async void Button_Click(object sender, RoutedEventArgs e) { //,     if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { ******     ***** try { //      ListingInformation LicensePremiumID = await Store.CurrentApp.LoadListingInformationByProductIdsAsync(new string[] { "ID" }); //  string x = await CurrentApp.RequestProductPurchaseAsync(LicensePremiumID.ProductListings.ToList()[0].Value.ProductId, false); //  var productLicenses = CurrentApp.LicenseInformation.ProductLicenses; ProductLicense tokenLicense = productLicenses["ID"]; if (tokenLicense.IsActive)//    { IsolatedStorageSettings.ApplicationSettings[" "] = "1";// AppSetting.ApplicationFullVersiy = true; // ,    !      //CurrentApp.ReportProductFulfillment(tokenLicense.ProductId); MessageBox.Show("«  Premium»  .   !"); *** } else { MessageBox.Show("IsActive - false"); *** } } catch (Exception ex) { //MessageBox.Show(ex.ToString()); MessageBox.Show("*****"); } } else { MessageBox.Show("  ..."); } } 


If the user has already bought before, then:
code
  private void Button_Click_1(object sender, RoutedEventArgs e) { if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { *** try { var productLicenses = CurrentApp.LicenseInformation.ProductLicenses; ProductLicense tokenLicense = productLicenses["ID"]; if (tokenLicense.IsActive)//    { IsolatedStorageSettings.ApplicationSettings["ept"] = "1";// AppSetting.ApplicationFullVersiy = true; **** MessageBox.Show("«  Premium»  ."); } else { MessageBox.Show("«  Premium»  ."); } } catch (Exception ex) { //MessageBox.Show(ex.ToString()); MessageBox.Show("****.."); } } else { MessageBox.Show("  ..."); } } 


Result:
Hidden text




In games, the implementation is similar, as in Windows Stor. If you forgot to describe something, write in the comments, I will be glad to answer all your questions.

off top
I decided to tell about In-App Purchasing, because I did not find exhaustive information on the Russian Internet, including habr, I decided to skip the rest of the development stages (UI, Async / await, JSON) because there are similar articles on Habré, but if there will be those who want to know about it, write in the comments, I also don’t leave links to my application, so that I don’t think that I am promoting, it can be easily found by name

Source: https://habr.com/ru/post/226777/


All Articles