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
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();
}
}
}
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);
}
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With