title: Working around API-purchase-logic-flaws for consumables in Google Play's Billing API v3 (Relevant to everyone using consumables with API v3)
body: <p>With version 3 of the Billing API, <a href="https://developer.android.com/google/play/billing/api.html#consume" rel="noreferrer">Google has removed the distinction between consumable and non-consumable products</a>. Both have been combined into a new type called "managed" and behave somewhat like a hybrid: Your app needs to actively call a method to "consume" the items. If that is never done for a set of skus, those items basically behave as if they were non-consumable.</p>
  
  <p>The <a href="https://developer.android.com/google/play/billing/api.html#managingconsumables" rel="noreferrer">documentation</a> describes the intended purchase flow as follows:</p>
  
  <ol>
  <li>Launch a purchase flow with a <code>getBuyIntent</code> call.</li>
  <li>Get a response <code>Bundle</code> from Google Play indicating if the purchase completed successfully.</li>
  <li>If the purchase was successful, consume the purchase by making a <code>consumePurchase</code> call.</li>
  <li>Get a response code from Google Play indicating if the consumption completed successfully.</li>
  <li>If the consumption was successful, provision the product in your application.</li>
  </ol>
  
  <p>I see two problems with this approach. One is fairly obvious and more a "bug" in the documentation than the API, but the other is rather subtle and I still haven't figured out how to best handle it. Let's start with the obvious one for completeness:</p>
  
  <p><strong>Problem 1: Lost purchases on single device:</strong></p>
  
  <p>The docs say that an app should call <code>getPurchases</code> every time it is launched to "check if the user owns any outstanding consumable in-app products". If so, the app should consume these and provision the associated item. This covers the case where the purchase flow is interrupted after the purchase is completed, but before the item is consumed (i.e. around step 2).</p>
  
  <p>But what if the purchase flow is interrupted between step 4 and 5? I.e. the app has successfully consumed the purchase but it got killed (phone call came in and there wasn't enough memory around, battery died, crash, whatever) before it had a chance to provision the product to the user. In such a case, the purchase will no longer be included in <code>getPurchases</code> and basically the user never receives what he paid for <em>(insert angry support email and one-star review here)</em>...</p>
  
  <p>Luckily this problem is fairly easy to fix by introducing a "journal" (<a href="https://en.wikipedia.org/wiki/Journaling_file_system" rel="noreferrer">like in a file system</a>) to change the purchase flow to something more like this (Steps 1 and 2 same as above):</p>
  
  <ol start="3">
  <li><p>If the purchase was successful, make entry into journal saying "increase coins from 300 to 400 once purchase <em>&lt;order-id here&gt;</em> is successfully consumed."</p></li>
  <li><p>After journal entry is confirmed, consume the purchase by making a <code>consumePurchase</code> call.</p></li>
  <li>Get a response code from Google Play indicating if the consumption completed successfully.</li>
  <li>If the consumption was successful, provision the product in your application.</li>
  <li>When provisioning is confirmed, change journal entry to "purchase <em>&lt;order-id here&gt;</em> completed".</li>
  </ol>
  
  <p>Then, every time the app starts, it shouldn't just check <code>getPurchases</code>, but also the journal. If there is an entry there for an incomplete purchase that wasn't reported by <code>getPurchases</code>, continue at step 6. If a later <code>getPurchase</code> should ever return that order ID as owned again (e.g. if the consumption failed after all), simply ignore the transaction if the journal lists this order ID as complete.</p>
  
  <p>This <em>should</em> fix problem 1, but please do let me know if you find any flaws in this approach.</p>
  
  <p><strong>Problem 2: Issues when multiple devices are involved:</strong> </p>
  
  <p>Let's say a user owns two devices (a phone and a tablet, for example) with the same account on both.</p>
  
  <p>He (or she - to be implied from now on) could try to purchase more coins on his <strong>phone</strong> and the app could get killed after the purchase completed, but before it is consumed. Now, if he opens the app on his <strong>tablet</strong> next, <code>getPurchases</code> will report the product as owned.</p>
  
  <p>The app on the tablet will have to assume that the purchase was initiated there and that it died before the journal entry was created, so it will create the journal entry, consume the product, and provision the coins.</p>
  
  <p>If the phone app died before it had a chance to make the journal entry, the coins will never be provisioned on the phone <em>(insert angry support email and one-star review here)</em>. And if the phone app died after the journal entry was created, the coins will <strong>also</strong> be provisioned on the phone, basically giving the user a purchase for free on the tablet <em>(insert lost revenue here)</em>.</p>
  
  <p>One way around this is to add some unique install or device ID as a payload to the purchase to check whether the purchase was meant for this device. Then, the tablet can simply ignore the purchase and only the phone will ever credit the coins and consume the item.</p>
  
  <p><strong>BUT:</strong> Since the sku is still in the user's possession at this point, the Play Store will not allow the user to buy another copy, so basically, until the user launches the app again on his phone to complete the pending transaction, he will not be able to purchase any more virtual coins on the tablet <em>(insert angry support email, one-star review, and lost revenue here)</em>.</p>
  
  <p>Is there an elegant way to handle this scenario? The only solutions I can think of are:</p>
  
  <ul>
  <li>Show a message to the user to please launch the app on the other device first (yuck!)</li>
  <li>or add multiple skus for the same consumable item (should work, but still yuck!)</li>
  </ul>
  
  <p>Is there a better way? Or am I maybe just fundamentally misunderstanding something and there really is no issue here? (I realize that the chances of this problem ever coming up are slim, but with a large enough user-base, "unlikely" eventually becomes "all-the-time".)</p>