Android IAB V3 - 消费购买

时间:2013-06-01 07:22:32

标签: android in-app-billing android-billing

我已经在我的Android游戏中实现了应用内结算v3,但是当我尝试购买时,我遇到了问题。

游戏已发布,产品可以成功购买,但由于消费失败,因此只能购买一次。当第二次尝试购买时,将返回响应5 - developer error

有人可以给我任何建议吗?

这是我的代码:

        public boolean orderBoostersV3(String packId, int alreadyAfterConsume) {
            Bundle bundle = null;
            try {
    // for tetsing               packId = "android.test.purchased";
                if (mService == null) {
                    Log.e(TAG, "mService is null");
                    return false;
                }
                // Bundle ownedItems = mService.getPurchases(3, "eu.acgame", "inapp", null);


                bundle = mService.getBuyIntent(3, "eu.acgame", packId, ITEM_TYPE_INAPP, "AC game");

                PendingIntent pendingIntent = bundle.getParcelable(RESPONSE_BUY_INTENT);
                if (bundle.getInt(RESPONSE_CODE) == BILLING_RESPONSE_RESULT_OK) {
                    // Start purchase flow (this brings up the Google Play UI).
                    // Result will be delivered through onActivityResult().
                    try {
                        a.startIntentSenderForResult(pendingIntent.getIntentSender(), RC_BUY, new Intent(),
                                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
                        // consume(packId); // consuming right after prdering does not work
                        return true;
                    } catch (IntentSender.SendIntentException e) {
                        Log.e(TAG, "Billing unavailable. Error:" + e.getMessage());
                        e.printStackTrace();
                    }
                } else if (bundle.getInt(RESPONSE_CODE) == BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE) {
                    Log.d(TAG, "Boostering not available");
                    return false;
                } else if (bundle.getInt(RESPONSE_CODE) == BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED && alreadyAfterConsume > 10) {
                    Log.e(TAG, "Consume did not work " + alreadyAfterConsume + " Panic!");
                    return false;
                } else if (bundle.getInt(RESPONSE_CODE) == BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
                    Log.d(TAG, "Product " + packId + " already purchased. Calling consume");
                    int result = consume(packId);   // if result not 0, probably all consumed
                    if (result == 0 || result == 5)   // consumed properly, try once more
                        return orderBoostersV3(packId, alreadyAfterConsume + 1);
                    else
                        return orderBoostersV3(packId, 99);  // finish consuming

                } else
                    Log.d(TAG, "Billing unexpected code:" + bundle.getInt(RESPONSE_CODE));

            } catch (RemoteException e) {
                Log.e(TAG, "Billing error");
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
            return false;
        }

private int consume(String packId) throws RemoteException {
    Bundle purchases = mService.getPurchases(3, "eu.acgame", "inapp", null);
    Log.d(TAG, "Purchases:" + purchases);
    int response = mService.consumePurchase(3, "eu.acgame", "inapp:eu.acgame:" + packId); // android.test.purchased");
    Log.d(TAG, "Consume response:" + response);
    return response;
}

1 个答案:

答案 0 :(得分:0)

我设法解决了自己的问题。我必须将原始购买令牌传递给consumePurchase,这是从查询购买物品集合中获得的。