-1

The problem is that the app doesn't allow the users to purchase more than once. I need to make my item consumable, and the answer is all explained here.

What I need to know is EXACTLY WHERE I have to put this part:

mHelper.consumeAsync(purchase, mConsumeFinishedListener);

in my code?

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }

    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data)
    {
        if (!mHelper.handleActivityResult(requestCode,
                resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }




    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
            = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result,
                                          Purchase purchase)
        {
            if (result.isFailure()) {
                // Handle error
                return;
            }
            else if (purchase.getSku().equals(ITEM_SKU)) {
                consumeItem();
                buyButton.setEnabled(false);
            }

        }
    };





    public void consumeItem() {
        mHelper.queryInventoryAsync(mReceivedInventoryListener);


    }

    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {

        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory)

        {

            if (result.isFailure()) {
                // Handle failure
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);


            }
        }
    };



    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
            new IabHelper.OnConsumeFinishedListener() {
                public void onConsumeFinished(Purchase purchase,
                                              IabResult result) {

                    if (result.isSuccess()) {
                        clickButton.setEnabled(true);

                    } else {
                        // handle error
                    }
                }
            };




    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_in_app_billing);

        buyButton = (Button)findViewById(R.id.buyButton);
        clickButton = (Button)findViewById(R.id.clickButton);



        String base64EncodedPublicKey =
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3F8aC2kUFQHf/X3xnfgulD0UpgrQifcjZ66zzhUPhQ/TcrONl22V74Q/Uj57rCwSUfdzz7wbUPxuPayGKBozzoH+2vhMGSetgCFZLcrNbRpBBbihOZrj//GTXMa6VkpUPTAqthEF0oI1M/bW9vF75xZI3u2KAS/AYDfqLTRZ6mh+xh6n/3i0ntSZT+UwzguwyHfS9JwuGGg5AKSutaWhnvOTNeQjsxTskc483h9DfvvRiwdiQPlv7wJRSSIc3RHVwDHleEJ8rsRa8JTypBJuL5oRZSGePUlejWhJvs23tgy5xrvGsMgsICssGzIem2XXSUWm/NDjeO0v2Eh+quQKVQIDAQAB";

        mHelper = new IabHelper(this, base64EncodedPublicKey);


        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
        {

            public void onIabSetupFinished(IabResult result)
            {
                if (!result.isSuccess())
                {
                    Log.d(TAG, "In-app Billing setup failed: " + result);
                } else
                {
                    Log.d(TAG, "In-app Billing is set up OK");
                }
            }


        });

    }

}

Because in the other answer Haxis says "you have to call the consume function just after the purchase."

Could anyone help me please?

Giacomo
  • 1
  • 2
  • Please dont post garbage fillers. Explain your problem instead.. – Suraj Rao Aug 24 '17 at 09:22
  • Possible duplicate of [In app billing v3 Android non-consumable items](https://stackoverflow.com/questions/17804095/in-app-billing-v3-android-non-consumable-items) – Clijsters Aug 24 '17 at 09:57
  • I have EXACTLY the same problem. But the rules says to avoid to "asking for help, clarification, or responding to other answers" so I've made another question. – Giacomo Aug 24 '17 at 10:03

1 Answers1

2

As you can check in the google sample - https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/MainActivity.java

consumeAsync() is called twice:

-When the purchase is complete, in OnIabPurchaseFinishedListener

-When the inventory is queried at the start of the activity, in QueryInventoryFinishedListener

You also need to call it when the purchase was successful as well as when you query the inventory and find a consumable item in there (because if the item is consumed, then it won't appear in the inventory)

Terry
  • 660
  • 6
  • 8