Search code examples
javaandroidin-app-purchasein-app-billing

Android in-app-billing - How to buy a single in-app-purchase multiple times?


How can I buy one thing multiple times? Here is my code:

  // [...]
  String base64EncodedPublicKey =
                "MIIB...;

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new
                                   IabHelper.OnIabSetupFinishedListener() {
                                       public void onIabSetupFinished(IabResult result) {
                                           if (!result.isSuccess()) {
                                               Toast.makeText(gift.this, "Setup no Success", Toast.LENGTH_SHORT).show();
                                               Log.d(TAG, "In-app Billing setup failed: " + result);
                                           } else {
                                               Toast.makeText(gift.this, "Setup Success", Toast.LENGTH_SHORT).show();
                                           }
                                       }
                                   });


    }


    public void insert(View view) {
        mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
                mPurchaseFinishedListener, "mypurchasetoken");
    }

    @Override
    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()) {
                Toast.makeText(gift.this, "Error 1", Toast.LENGTH_SHORT).show();
            }
            else if (purchase.getSku().equals(ITEM_SKU)) {
                consumeItem();
                Toast.makeText(gift.this, "OK 1", Toast.LENGTH_SHORT).show();
               // mHelper.consumeAsync(purchase, mConsumeFinishedListener);
            }

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

    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory) {

            if (result.isFailure()) {
                Toast.makeText(gift.this, "Error 2", Toast.LENGTH_SHORT).show();
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);
                Toast.makeText(gift.this, "OK 2", Toast.LENGTH_SHORT).show();
            }
        }
    };
    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
            new IabHelper.OnConsumeFinishedListener() {
                public void onConsumeFinished(Purchase purchase,
                                              IabResult result) {

                    if (result.isSuccess()) {
                        Toast.makeText(gift.this, "Success 1", Toast.LENGTH_SHORT).show();
                       // mHelper.consumeAsync(purchase, mConsumeFinishedListener);
                    } else {
                        Toast.makeText(gift.this, "Error 3", Toast.LENGTH_SHORT).show();
                    }
                }
            };

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

When I click on my button it says: "payment successful" and after that when I click on my button again it does not do anything.

I have 2 questions:

  1. How to buy a single in-app-purchase multiple times?
  2. How can I display a Toast after the payment was successful?

Thanks for answers.


Solution

    1. The purchase must be consumed after every purchase, then it can be purchased again. If you are able to purchase the item once but not again, then almost assuredly the problem is that you are not consuming it correctly.

    2. There doesn't appear to be any problem with your code, Toast.makeText(...) should work fine.