0

i was following a tutorial to code In-app billing items and i manage to make all good, but when i want to know if the user purchased an item o not, its always false, even when i test it with others devices that have a beta tester account.

This is what i use to get the item purchased:

mHelper = new IabHelper(MainActivity.this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
       public void onIabSetupFinished(IabResult result) {
           IabHelper.QueryInventoryFinishedListener mGotInventoryListener
                   = new IabHelper.QueryInventoryFinishedListener() {
               public void onQueryInventoryFinished(IabResult result,
                                                    Inventory inventory) {

                   if (result.isFailure()) {
                       // handle error here
                   }
                   else {
                       // does the user have the premium upgrade?
                       boolean mIsPremium = inventory.hasPurchase(ITEM_SKU);
                       // update UI accordingly
                       Toast.makeText(getApplicationContext(), "" + mIsPremium, Toast.LENGTH_LONG).show();
                       if(mIsPremium){
                           buy.setVisibility(View.INVISIBLE);
                       }
                   }
               }
           };
           mHelper.queryInventoryAsync(mGotInventoryListener);
       }
    });

And this is the code to purchase that item:

buy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mHelper.launchPurchaseFlow(MainActivity.this, ITEM_SKU, 10001,
                    new IabHelper.OnIabPurchaseFinishedListener() {
                        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
                            if (result.isFailure()) {
                                return;
                            } else if (purchase.getSku().equals(ITEM_SKU)) {
                                mHelper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener() {
                                    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                                        if (result.isFailure()) {
                                        } else {
                                            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                                                    new IabHelper.OnConsumeFinishedListener() {
                                                        public void onConsumeFinished(Purchase purchase, IabResult result) {
                                                            if (result.isSuccess()) {
                                                                buy.setVisibility(View.GONE);
                                                            } else {
                                                            }
                                                        }
                                                    });
                                        }
                                    }
                                });
                            }
                        }
                    }, "mypurchasetoken");
        }
    });

When i test the app in a real device with the account on my beta testers, the code works fine when buy the item, but when i close the app and open it again, the first code sais that the account didnt buy the item.

2 Answers2

0

You're probably consuming the product after the purchase returns.

See here about consumable / non-consumable products: https://developer.android.com/google/play/billing/api.html#consumetypes

marmor
  • 27,641
  • 11
  • 107
  • 150
  • While this is likely the answer, shouldnt this be a comment requesting additional information? You are answering with a question. – Bonatti Jul 28 '16 at 12:04
  • i can rephrase to "you're probably consuming the products...", would that be better? – marmor Jul 28 '16 at 12:08
  • I believe it so. Basically, another user that checks this answer can see the "most likely" sort of issues they might be having. A consumable product would not appear as a code error, but would show the same behavior. – Bonatti Jul 28 '16 at 12:14
  • I read it now and the item i want to sell is a non-consumable item, in oder words, is a "get premium" item. As i see in this question [link](http://stackoverflow.com/questions/23370403/android-in-app-billing-non-consumable-items) i have to use shared preferences, but i think that that is a bad idea, what if the user delete the app? I think that google may have a way to retrieve the items buyed, but in my case, the result is always false. – Pedro Rios Fernandez Jul 28 '16 at 14:53
0

Ok guys, i´ve solved it, it was an error following a tutorial, but i will never solve it without reading about consumable / non-consumable items. My item is a one-time purchase, so there is no need to have:

mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                                                new IabHelper.OnConsumeFinishedListener() {
                                                    public void onConsumeFinished(Purchase purchase, IabResult result) {
                                                        if (result.isSuccess()) {
                                                            buy.setVisibility(View.GONE);
                                                        } else {
                                                        }
                                                    }
                                                });

In the second part of the code, all i have to do is delete that and sudenly all is OK, that way, the second part of the code will be:

buy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mHelper.launchPurchaseFlow(MainActivity.this, ITEM_SKU, 10001,
                    new IabHelper.OnIabPurchaseFinishedListener() {
                        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
                            if (result.isFailure()) {
                                return;
                            } else if (purchase.getSku().equals(ITEM_SKU)) {
                                mHelper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener() {
                                    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                                        if (!result.isFailure()) {
                                            buy.setVisibility(View.GONE);
                                        }
                                    }
                                });
                            }
                        }
                    }, "mypurchasetoken");
        }
    });

Hope this will help someone in the future, and thank you!.