{"id":29970,"date":"2017-02-15T12:01:01","date_gmt":"2017-02-15T20:01:01","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=29970"},"modified":"2019-03-25T19:40:22","modified_gmt":"2019-03-26T03:40:22","slug":"consumable-in-app-purchases","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/consumable-in-app-purchases\/","title":{"rendered":"Consumable In-App Purchases"},"content":{"rendered":"<p>\t\t\t\tIf you are building a game or application with content that needs to be purchased multiple times, you&#8217;ll need to integrate consumable in-app purchases (IAPs) into your application. In my <a href=\"https:\/\/blog.xamarin.com\/integrating-in-app-purchases-in-mobile-apps\/\">previous IAP blog<\/a> post, I covered the basics of setting up your app for IAP transactions, adding the IAP items to Google Play and iTunes Connect, and introduced you to the <a href=\"https:\/\/github.com\/jamesmontemagno\/inappbillingplugin\">In-App Billing Plugin for Xamarin<\/a> that greatly simplifies the process of adding IAP to your application&#8217;s code. Today, we&#8217;ll take a look at consumables and how they offer a slight tweak to the development process.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/developer.xamarin.com\/guides\/ios\/application_fundamentals\/in-app_purchasing\/part_3_-_purchasing_consumable_products\/Images\/image27.png\" alt=\"\" width=\"513\" height=\"247\" \/><\/p>\n<p>The idea of a consumable is that they are items that are used up over the course of the application&#8217;s life, and therefore can be purchased multiple times. Examples of these are in-game currency, such as our monkey credits shown above, extra health, and one-time services, such as transcriptions.<\/p>\n<h2>Setting Up Consumables<\/h2>\n<p>Google doesn&#8217;t make a true distinction between a non-consumable and a consumable, only the API differs between them. For iOS, Apple requires developers to specify this information up front when creating the IAP in iTunes connect.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter  wp-image-29972\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Consumables.png\" alt=\"Consumables\" width=\"635\" height=\"395\" \/><\/p>\n<p>The idea here is that when we make the request to the iTunes server, this selection will trigger the correct flow during the purchase.<\/p>\n<h2>Purchasing and Consuming<\/h2>\n<p>For both iOS and Android, we&#8217;ll want to make sure that we have the <a href=\"https:\/\/github.com\/jamesmontemagno\/inappbillingplugin\">In-App Billing Plugin<\/a> installed in all projects in our solution via NuGet. iOS and Android each handle consumables a little bit differently, so we&#8217;ll start with iOS, which has a very similar flow to the non-consumables that we discussed earlier.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/developer.xamarin.com\/guides\/ios\/application_fundamentals\/in-app_purchasing\/part_3_-_purchasing_consumable_products\/Images\/image26.png\" alt=\"\" width=\"545\" height=\"313\" \/><\/p>\n<p>This diagram shows the steps to making the purchase; we must:<\/p>\n<ol>\n<li>Request our payment in the Queue.<\/li>\n<li>Send the request to the server.<\/li>\n<li>iTunes processes and returns.<\/li>\n<li>Process receipt in app.<\/li>\n<li>Display purchase information in app.<\/li>\n<\/ol>\n<p>If this diagram looks familiar, it&#8217;s because it&#8217;s the same exact flow as non-consumables. Thus, all we have to do is call the PurchaseAsync method and our consumable is purchased and ready to go! Since we marked the IAP as a consumable, we can continue to buy the product over and over.<\/p>\n<pre><code>\r\ntry\r\n{\r\n    var productId = \"mysku\";\r\n \r\n    var connected = await CrossInAppBilling.Current.ConnectAsync();\r\n \r\n    if (!connected)\r\n    {\r\n        \/\/Couldn't connect to billing, could be offline, alert user\r\n        return;\r\n    }\r\n \r\n    \/\/try to purchase item\r\n    var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, \"apppayload\");\r\n    if(purchase == null)\r\n    {\r\n        \/\/Not purchased, alert the user\r\n    }\r\n    else\r\n    {\r\n        \/\/Purchased, save this information\r\n        var id = purchase.Id;\r\n        var token = purchase.PurchaseToken;\r\n        var state = purchase.State;\r\n    }\r\n}\r\ncatch (Exception ex)\r\n{\r\n    \/\/Something bad has occurred, alert user\r\n}\r\nfinally\r\n{\r\n    \/\/Disconnect, it is okay if we never connected\r\n    await CrossInAppBilling.Current.DisconnectAsync();\r\n}\r\n<\/code><\/pre>\n<p>For Android, there&#8217;s one additional step in the process. We must first call the above code to actually purchase the consumable item, however, Google&#8217;s implementation of consumables integrates the scenario where the user may buy a consumable and actually use it in the future, such as buying coins, but cashing them in for goods later in the game. This means that we can&#8217;t purchase the consumable item again until we actually finalize the purchase and &#8220;Consume&#8221; it. For this, the plugin has an additional method, ConsumePurchaseAsync, that takes in specific purchase information. If this method is called on iOS, it will simply return null. We can use the <a href=\"https:\/\/github.com\/jamesmontemagno\/DeviceInfoPlugin\">Device Info Plugin<\/a> to only call <code>ConsumePurchaseAsync<\/code> on Android after we have the purchase:<\/p>\n<pre><code>\r\n\/\/ Called after we have a successful purchase or later on (must call ConnectAsync() ahead of time):\r\nif(CrossDeviceInfo.Current.Platform == Platform.Android)\r\n{\r\n     var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);\r\n\r\n    if(consumedItem != null)\r\n    {\r\n        \/\/ Item has been consumed\r\n    }\r\n}\r\n<\/code><\/pre>\n<h2>Learn More<\/h2>\n<p>There\u2019s a lot more to IAPs, including subscriptions and product information that are exposed in the In-App Billing Plugins APIs. You can find out more about this in the README file on the <a href=\"https:\/\/github.com\/jamesmontemagno\/InAppBillingPlugin\" target=\"_blank\">GitHub project<\/a>. The same documentation also walks you through a full set up, testing, and trouble shooting guide to integrating In-App Purchases. In addition,\u00a0I highly recommend reading through full documentation on In-App Purchases for <a href=\"https:\/\/developer.xamarin.com\/guides\/ios\/application_fundamentals\/in-app_purchasing\/\" target=\"_blank\">iOS<\/a> and <a href=\"https:\/\/components.xamarin.com\/gettingstarted\/xamarin.inappbilling\" target=\"_blank\">Android<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are building a game or application with content that needs to be purchased multiple times, you&#8217;ll need to integrate consumable in-app purchases (IAPs) into your application. In my <a href=\"https:\/\/blog.xamarin.com\/integrating-in-app-purchases-in-mobile-apps\/\">previous IAP blog<\/a> post, I covered the basics of setting up your app for IAP transactions, adding the IAP items to Google Play and iTunes Connect, and introduced you to the <a href=\"https:\/\/github.com\/jamesmontemagno\/inappbillingplugin\">In-App Billing Plugin for Xamarin<\/a> that greatly simplifies the process of adding IAP to your application&#8217;s code. Today, we&#8217;ll take a look at consumables and how they offer a slight tweak to the development process.<\/p>\n","protected":false},"author":544,"featured_media":41042,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,2,556,303],"tags":[5,429,1373,6,1374,4],"class_list":["post-29970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-developers","category-integrations","category-ios","tag-android","tag-components","tag-in-app-purchase","tag-ios","tag-plugins","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>If you are building a game or application with content that needs to be purchased multiple times, you&#8217;ll need to integrate consumable in-app purchases (IAPs) into your application. In my <a href=\"https:\/\/blog.xamarin.com\/integrating-in-app-purchases-in-mobile-apps\/\">previous IAP blog<\/a> post, I covered the basics of setting up your app for IAP transactions, adding the IAP items to Google Play and iTunes Connect, and introduced you to the <a href=\"https:\/\/github.com\/jamesmontemagno\/inappbillingplugin\">In-App Billing Plugin for Xamarin<\/a> that greatly simplifies the process of adding IAP to your application&#8217;s code. Today, we&#8217;ll take a look at consumables and how they offer a slight tweak to the development process.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/29970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=29970"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/29970\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/41042"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=29970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=29970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=29970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}