Inapp billing: Error response: 7:Item Already Owned Issue With Test Transactions

浪尽此生 提交于 2020-02-23 05:38:17

问题


First time implementing in-app billing. i was able to complete first transaction, but second time while purchasing same item i am getting "Error response: 7:Item Already Owned" on onIabPurchaseFinished Method, how to make it re-purchase-able? any help would be appreciated.

screenshot of code structure is also attached.

Onclick purchase i am calling following method: initilizeInAppPurchasePakages();

 public void initilizeInAppPurchasePakages()
    {
        String base64EncodedPublicKey=getString(R.string.inAppBillingKey);
        mHelper = new IabHelper(getActivity(), base64EncodedPublicKey);
        mPurchaseFinishedListener
                = new IabHelper.OnIabPurchaseFinishedListener() {
            public void onIabPurchaseFinished(IabResult result, Purchase purchase)
            {
                if (result.isFailure()) {
                    Log.d(TAG, "Error purchasing: " + result);
               //     Toast.makeText(getActivity(),"fail to purchase"+result, Toast.LENGTH_SHORT).show();
                    return;
                }
                else if (purchase.getSku().equals(purchaseItemId)) {
                    transactionId=purchase.getOrderId();
                    PackageFragment.isNeedToUpdate = true;
                    // consume the gas and update the UI
                   // Toast.makeText(getActivity(), "purchase successfully", Toast.LENGTH_SHORT).show();
                    mHelper.consumeAsync(purchase,
                            mConsumeFinishedListener);
                }
                else if (purchase.getSku().equals(purchaseItemId)) {
                    // give user access to premium content and update the UI
                   // Toast.makeText(getActivity(), "purchase successfully", Toast.LENGTH_SHORT).show();
                }
            }
        };


        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    // Oh no, there was a problem.
                   // Toast.makeText(getActivity(), "connection Error", Toast.LENGTH_SHORT).show();
                }
                else
                {
                 //   Toast.makeText(getActivity(), "connected successfully", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "Problem setting up In-app Billing: " + result);
                    mHelper.launchPurchaseFlow(getActivity(), purchaseItemId, 10001,
                            mPurchaseFinishedListener, "testing");

                }
                // Hooray, IAB is fully set up!
            }
        });
         mConsumeFinishedListener =
                new IabHelper.OnConsumeFinishedListener() {
                    public void onConsumeFinished(Purchase purchase, IabResult result) {
                        if (result.isSuccess()) {
                            serverUtilities.savePayment(pakageId,pakagePrice,transactionId);
                          //  Toast.makeText(getActivity(), "consumed", Toast.LENGTH_SHORT).show();
                            // provision the in-app purchase to the user
                            // (for example, credit 50 gold coins to player's character)
                        }
                        else {

                            // handle error
                        }
                    }
                };
    }

回答1:


Alright,

After spending time on reading documentation of google in app billing i found that i need to test in app billing through testing so what i did

test billing:

mHelper.launchPurchaseFlow(getActivity(), purchaseItemIdSKU, 10001,
                            mPurchaseFinishedListener, "testing");

Calling only first time to remove that already purchased entry record above the test billing line that i used above: ArrayList skusToBeListed = null;

                skusToBeListed = new ArrayList<String> ();
                skusToBeListed.add (SKU_PREMIUM);
                skusToBeListed.add (SKU_PREMIUM_ELITE);
                skusToBeListed.add (SKU_PREMIUM_PLUS);

mHelper.queryInventoryAsync(true, skusToBeListed, mGotInventoryListener);

and once its done you can do many test bookings without any headache.

 mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
            @Override
            public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                if (result.isFailure()) {
                    //complain("Failed to query inventory: " + result);
                    return;
                }

                Purchase currentPurchase = inventory.getPurchase(purchaseItemIdSKU);
                if(currentPurchase != null)
                {
                    //Boolean  mIsPremium = (currentPurchase != null && verifyDeveloperPayload(currentPurchase));
                                       mHelper.consumeAsync(currentPurchase,mConsumeFinishedListener);

                }





        }
    } ;

Note: In case of test billing transaction id will not be returned so make sure how to handle that, i did tested my in app billing by hard coding it to following format: "GPA.1234-5678-9012-34567"

In short above code of mine was fine if i would i have used it on production mode, but in case of testing mode it was giving me errors that i explained and given solution how i solved it.



来源:https://stackoverflow.com/questions/44976757/inapp-billing-error-response-7item-already-owned-issue-with-test-transactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!