我在测试我的应用时遇到问题。我有2个项目,用户可以购买。几天前我不知道我应该消费购买。今天我每次尝试买一件物品时都会收到代码7(物品已经拥有),因为我没有消耗它。我可以做些什么来消费旧的购买?
增加: 这两个项目在开发人员控制台上都没有“管理”
答案 0 :(得分:6)
我试图使用错误的令牌。这段代码帮助了我。
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();
}
}
}
答案 1 :(得分:2)
对于消费inapp产品,您需要购买该sku。 为此,您首先需要或获得所有产品的INVENTORY或购买确切的产品。
请检查下面的代码,它可以帮助您消费之前购买过的产品。
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);
}
}
});
}
答案 2 :(得分:0)
感谢以前的答案,我有这个问题。清除所有购买
import com.android.vending.billing.util.Inventory;
import com.android.vending.billing.util.Purchase;
public class BasePurchaseActivity {
protected IabHelper billingHelper;
...
/**
* Means it will clear our the purchase history
*/
protected void consumeAllPurchases() {
// You know your product ids
Set<String> allProductIds = com.demo.myapp.AppMgr.getAllAppProductIds();
try {
Inventory inventory = getInventory();
for (String curProdId : allProductIds) {
Purchase purchase = inventory.getPurchase(curProdId);
if (purchase != null) {
billingHelper.consumeAsync(purchase, null);
}
}
}
catch (IabException iabEx) {
iabEx.printStackTrace();
}
}