Unable to buy item response: 7:Item Already Owned

4.1k Views Asked by At

I'm testing my Android app and facing with the "Unable to buy item (response: 7:Item Already Owned)". The item is a "Managed In-app Product" type. I did some research and found this (http://developer.android.com/google/play/billing/api.html#managed):

Managed in-app products are items that have their ownership information tracked and managed
by Google Play. When a user purchases a managed in-app item, Google Play stores the
purchase information for each item on a per-user basis. This enables you to later query
Google Play at any time to restore the state of the items a specific user has purchased.
This information is persistent on the Google Play servers even if the user uninstalls the
application or if they change devices.

If you are using the Version 3 API, you can also consume managed items within your
application. You would typically implement consumption for items that can be purchased
multiple times (such as in-game currency, fuel, or magic spells). Once purchased, a managed
item cannot be purchased again until you consume the item, by sending a consumption request
to Google Play. To learn more about in-app product consumption, see Consuming Items.

...

You can use the consumption mechanism to track the user's ownership of in-app products.

In Version 3, all in-app products are managed. This means that the user's ownership of all
in-app item purchases is maintained by Google Play, and your application can query the
user's purchase information when needed. When the user successfully purchases an in-app
product, that purchase is recorded in Google Play. Once an in-app product is purchased, it
is considered to be "owned". In-app products in the "owned" state cannot be purchased from
Google Play. You must send a consumption request for the "owned" in-app product before
Google Play makes it available for purchase again. Consuming the in-app product reverts it
to the "unowned" state, and discards the previous purchase data.

How can I "send a consumption request" in Codename One?

2

There are 2 best solutions below

1
On

Assuming that you are using IabHelper.java provided by google in its android SDK. Following is the path:

Android_SDK\sdk\extras\google\play_billing\samples\TrivialDrive\src\com\example\android\trivialdrivesample\util

Below is code for initializing in-app billing, querying google play for user's purchase and consuming the purchase.

  1. Initializing In-app billing.

    /* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY
       (that you got from the Google Play developer console). 
    */
    IabHelper mHelper = new IabHelper(this, base64EncodedPublicKey);
    
    // Check whether in-app billing is supported or not.
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
    {
        @Override
        public void onIabSetupFinished(IabResult result)
        {
            if (result.isSuccess())
            {
                // In-app billing is supported. Now, queryInventoryAsync to whether user already purchased.
                ArrayList<String> productIDs = new ArrayList<String>();
                // ITEM_SKU -> Name of the billing item in google play developer console
                productIDs.add(ITEM_SKU);
                mHelper.queryInventoryAsync(true, productIDs, mReceivedInventoryListener);
            }
            else
            {
                // Billing not supported by the device
            }
        }
    });
    
  2. Query user's purchases

    mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener()
    {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory)
        {
            if (result.isSuccess())
            {
                // Will be true if already purchased
                boolean hasPurchase = inventory.hasPurchase(ITEM_SKU);
    
                // Here enable or disable your app features
                ...
    
                    /* How to consume the purchase */
    
                // First, get the Purchase from inventory
                Purchase purchase = inventory.getPurchase(ITEM_SKU);
    
                // Consume the purchase
                mHelper.consumeAsync(purchase, mConsumeFinishedListener);
    
            }
            else
            {
                // Failed to query google play for purchases.
            }
        }
    };
    
  3. Purchase consumed listener

    mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener()
    {
    
        @Override
        public void onConsumeFinished(Purchase purchase, IabResult result)
        {
            if (result.isSuccess())
            {
                // Purchase consumed.
            }
            else
            {
                // Purchase not consumed.
            }
        }
    };
    
7
On

Use the

Purchase.getInAppPurchase().wasPurchased(String sku)

If the item is a managed item and was already purchased it will return true