0
votes

i have almost finished my first app. I have my in-app purchasing working, but it turns out i have a problem with consuming the purchases, since it should be possible to buy the item endless times. I think i have edited the code to consume the purchased item correctly now, I will provide the code so you can correct me if I'm wrong. But even though this code might be correct now, I can't purchase the product on my test device again, since it wasn't consumed when bought in the first place. How can i consume the product now? Here is my updated code.

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()) {
            Log.d("Unlock", "Error purchasing: " + result);
            return;
        } else if (purchase.getSku().equals(SKU_HINTS)) {
            consumeItem();              
        }

    }
};

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(SKU_HINTS),
                    mConsumeFinishedListener);
        }
    }
};

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

        if (result.isSuccess()) {
            add_hints();
            purchase1.setText("Purchase complete");
        } else {
            // handle error
        }
    }
};

public void onClick(View v) {
case R.id.bUnlockHints:
        mHelper.launchPurchaseFlow(this, SKU_HINTS, 10001,
                mPurchaseFinishedListener, "");
        break;
 }
1

1 Answers

0
votes

I figured out the answer. I had to change the startSetup method so it queries the inventory right away. Here is my new code.

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

        if (result.isFailure()) {
            // Handle failure
        } 
            // This is the updated part
        Purchase hintPurchase = inventory.getPurchase(SKU_HINTS);

        if (hintPurchase != null) {
            mHelper.consumeAsync(inventory.getPurchase(SKU_HINTS),
                    mConsumeFinishedListener);
        }
    }
};

Here I check if the hints have been purchased before consuming it. Otherwise i would receive an error.

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                Log.d("Ultimate Quiz",
                        "Problem setting up In-app Billing: " + result);
            }
            // Hooray, IAB is fully set up!
            // Here I run the queryInventory to check right away if the product has been bought, and consume it if it has.
            mHelper.queryInventoryAsync(mReceivedInventoryListener);
        }
    });