Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming old purchases Android IAB V3

I have a problem with testing my app. I have 2 items which user can buy. Some days ago I didn't know that I should consume the purchases. Today I receive code 7 (Item Already Owned) every time when I try to buy one of items because I didn't consume it. What can I do to consume old purchase?

ADDED: Both items are 'not managed' at Developer's Console

like image 792
Yara_M Avatar asked Mar 03 '14 12:03

Yara_M


2 Answers

I tried to consume with wrong token. This code helped me.

Bundle ownedItems = mService.getPurchases(3, context.getPackageName(), "inapp", null);
int response = ownedItems.getInt("RESPONSE_CODE");
if (response == 0)
{
    ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
    ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
    //ArrayList<String> signatureList = ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
    //String continuationToken = ownedItems.getString("INAPP_CONTINUATION_TOKEN");
    for (int i = 0; i < purchaseDataList.size(); ++i) {
        try {
            String purchaseData = purchaseDataList.get(i);
            JSONObject jo = new JSONObject(purchaseData);
            final String token = jo.getString("purchaseToken");
            String sku = null;
            if (ownedSkus != null)
                sku = ownedSkus.get(i);
            consume(sku, token, purchaseData);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
like image 66
Yara_M Avatar answered Nov 17 '22 08:11

Yara_M


For consuming inapp product you need get purchase of that sku. For that you firstly need or get INVENTORY of all products or do PURCHASE that exact product.

Please check code below which can help you to consume products which were pruchased before.

public void consume(final String skuName) {
    mHelper.queryInventoryAsync(true, new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (inventory.getSkuDetails(skuName) != null){
                mHelper.consumeAsync(inventory.getPurchase(skuName), null);
            }
        }
    });
}
like image 2
Airon Tark Avatar answered Nov 17 '22 10:11

Airon Tark