android:Inapp计费:错误响应:7:项目已经拥有

pea*_*mak 28 android in-app-purchase in-app-billing

我正在学习为我的应用实施应用内结算,以便人们可以在按下捐赠按钮时捐赠$.

允许用户捐赠多次,即购买是消费品.

以下代码来自TrivalDrive示例和来自网络的一些教程:

码:

IabHelper mHelper;
static final String ITEM_SKU = "android.test.purchased"; 

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_app_billing);

    buy10Button = (Button) findViewById(R.id.buy10Button); 
    buy15Button = (Button) findViewById(R.id.buy15Button); 
    buy20Button = (Button) findViewById(R.id.buy20Button);      

    String base64EncodedPublicKey = "keykeykey";

    mHelper = new IabHelper(this, base64EncodedPublicKey);


    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() 
    {
          public void onIabSetupFinished(IabResult result) 
          {
            if (!result.isSuccess()) 
            {
               Log.d(TAG, "In-app Billing setup failed: " + result);
               return;
            } 
            if (mHelper == null) 
            {
                return;
            }          
            Log.d(TAG, "In-app Billing is set up OK");
          }
    });     
}

public void buy10Click(View view) 
{
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,  mPurchaseFinishedListener, "");
}

public void buy15Click(View view) 
{

}

public void buy20Click(View view) 
{

}   

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (mHelper == null) return;  
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) 
    {     
        super.onActivityResult(requestCode, resultCode, data);
    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() 
{
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
    {
        if (mHelper == null) return;
        if (result.isFailure()) 
        {
           // Handle error
               return;
        }      
        else if ((purchase.getSku().equals(ITEM_SKU)))   
        {
           consumeItem();
        }              
    }
};

public void consumeItem() 
{
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() 
{
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) 
    {
        if (mHelper == null) return;
        if (result.isFailure()) 
        {
            // Handle failure
        } 
        else 
        {
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() 
{
    public void onConsumeFinished(Purchase purchase, IabResult result) 
    {
        if (mHelper == null) return;
        if (result.isSuccess()) 
        {
            Toast.makeText(InAppBillingActivity.this, "Thank you for your donation!!", Toast.LENGTH_LONG).show();   
        } 
        else 
        {
            // handle error
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

题:

然而,我一直收到E/IabHelper(13392): In-app billing error: Unable to buy item, Error response: 7:Item Already Owned错误,并且Google Play的付款对话框不会弹出.

我已经研究并发现了许多类似的情况,有些人建议等待几分钟,然后购买将自行重置,但我已经等了将近一个小时,但它仍然很糟糕.

我还发现有人建议改变IabResult public boolean isSuccess() { return mResponse == IabHelper.BILLING_RESPONSE_RESULT_OK; }也返回BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNEDas isSuccess = true,但我不知道如何修改这样的...

怎么能解决这个问题?谢谢!!

Vin*_*uan 39

您购买了"android.test.purchased"但没有使用它.但是,如果您忘记立即食用,则再次食用它并不容易.我们可以等14天.假购买将自动清除.但这是不可接受的.

我花了很多时间找到解决方案:

添加此行以获取调试信息.

_iabHelper.enableDebugLogging(true, "TAG");
Run Code Online (Sandbox Code Playgroud)

运行应用程序.在LogCat中,您将看到类似的json字符串

{"packageName":"com.example","orderId":"transactionId.android.test.purchased","productId":"android.test.purchased","developerPayload":"123","purchaseTime":0,"purchaseState":0,"purchaseToken":"inapp:com.example:android.test.purchased"}
Run Code Online (Sandbox Code Playgroud)

手动使用它(用你的json字符串替换THAT_JSON_STRING)

    Purchase purchase;
    try {
        purchase = new Purchase("inapp", THAT_JSON_STRING, "");
        _iabHelper.consumeAsync(purchase, new OnConsumeFinishedListener() {

            @Override
            public void onConsumeFinished(Purchase purchase, IabResult result) {
                Log.d("TAG", "Result: " + result);
            }
        });
    } catch (JSONException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

_iabHelper是mHelper.


Mau*_*lik 20

在这里查看我的下面代码:

我在你的代码中不明白为什么你在购买完成监听器中使用了查询库存.当你获得与你请求的sku相同的sku时,应该调用ConsumeAsync()方法.

// Callback for when a purchase is finished
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            Log.d(TAG, "Purchase finished: " + result + ", purchase: "
                    + purchase);
            if (result.isFailure()) {
                complain("Error purchasing: " + result);
                return;
            }
            if (!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification failed.");
                return;
            }

            Log.d(TAG, "Purchase successful.");

            if (purchase.getSku().equals(SKU_GAS)) {

                 // remove query inventory method from here and put consumeAsync() directly
                mHelper.consumeAsync(purchase, mConsumeFinishedListener);

            }

        }
    };
Run Code Online (Sandbox Code Playgroud)

startSetup方法

//你忘了在startSetup方法中调用查询库存方法.

 mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                public void onIabSetupFinished(IabResult result) {
                    Log.d(TAG, "Setup finished.");

                    if (!result.isSuccess()) {
                        // Oh noes, there was a problem.
                        complain("Problem setting up in-app billing: " + result);
                        return;
                    }

                    // Hooray, IAB is fully set up. Now, let's get an inventory of
                    // stuff we own.
                    Log.d(TAG, "Setup successful. Querying inventory.");
                    mHelper.queryInventoryAsync(mGotInventoryListener);
                }
            });
Run Code Online (Sandbox Code Playgroud)

QueryInventoryFinishedListener

并且还检查条件购买是否与您请求的相同不等于null并且查询库存完成侦听器中的开发人员有效负载也相同.

if (gasPurchase != null && verifyDeveloperPayload(gasPurchase)){
    //code
}
Run Code Online (Sandbox Code Playgroud)
// Listener that's called when we finish querying the items and
        // subscriptions we own
        IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
            public void onQueryInventoryFinished(IabResult result,
                    Inventory inventory) {
                Log.d(TAG, "Query inventory finished.");
                if (result.isFailure()) {
                    complain("Failed to query inventory: " + result);
                    return;
                }

                Log.d(TAG, "Query inventory was successful.");

                /*
                 * Check for items we own. Notice that for each purchase, we check
                 * the developer payload to see if it's correct! See
                 * verifyDeveloperPayload().
                 */

                // // Check for gas delivery -- if we own gas, we should fill up the
                // tank immediately
                Purchase gasPurchase = inventory.getPurchase(SKU_GAS);
                if (gasPurchase != null && verifyDeveloperPayload(gasPurchase)) {
                    Log.d(TAG, "We have gas. Consuming it.");
                    mHelper.consumeAsync(inventory.getPurchase(SKU_GAS),
                            mConsumeFinishedListener);
                    return;
                }
            }
        };
Run Code Online (Sandbox Code Playgroud)

解释它为什么会发生:

每当您购买耗材时,谷歌游戏商店将无法管理其产品购买细节以及Google Play控制台中的其他内容.这就是我们必须调用consumeAsync()方法的原因.当我们购买商品时,谷歌游戏商店保留记录项目已经购买了一次,并允许您第二次购买.

希望它能解决你的问题.


Joh*_*hnK 6

我设法通过重启设备来"消费购买".


Mak*_*sim 5

您可以使用Google Play的“财务报告”->“访问商家帐户了解更多详细信息”->“订单”来查看和取消任何订单以“使用”。然后,您需要重新启动设备。=)