3

Following the docs on developer site , i have implemented InAppBilling v3 in my app recently . I have used the classes in the utils package provided in the TRIVIAL DRIVE sample.

The problem i am facing is if a user has purchased an in app product already on launching purchse flow again on another device the play store dialog shows ITEM ALREADY OWNED but the response code returned by IabResult does not match to the constant IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED . The response code returned is actually one of the error codes in IabHelper class (-1005 User cancelled).

I would really like to know how can i get the actual response code instead of error code. Any help would be appreciated.

Below is the code for callback

// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener =
        new IabHelper.OnIabPurchaseFinishedListener() {
            public void onIabPurchaseFinished(IabResult result, Purchase purchase) {

                if (result.isFailure()) {
                    if (result.getResponse() ==
                            IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
                        //already owned
                        boolean isPremium = true;
                        SharedPrefsUtils.setPremium(BaseActivity.this, isPremium);
                        EventBus.getDefault().post(new InAppBillingUiUpdateEvent(isPremium));
                        //setWaitScreen(false);
                        return;
                    }
                    //handle error
                    complain(result.getResponse() + " " + "Error purchasing: " + result);
                    //setWaitScreen(false);
                    return;
                }
                if (!verifyDeveloperPayload(purchase)) {
                    //corrupted
                    complain("Error purchasing. Authenticity verification failed.");
                    //setWaitScreen(false);
                    return;
                }

                //successful
                if (purchase.getSku().equals(NO_ADS_PRODUCT_ID)) {
                    // bought the premium upgrade!
                    alert("Thank you for upgrading to premium!");
                    boolean isPremium = true;
                    SharedPrefsUtils.setPremium(BaseActivity.this, isPremium);
                    EventBus.getDefault().post(new InAppBillingUiUpdateEvent(isPremium));
                    //setWaitScreen(false);
                }
            }
        }; 
Ravi
  • 4,872
  • 8
  • 35
  • 46

1 Answers1

4

I finally managed to find the problem in the IabHelper code , So here it goes whenever an Activity.RESULT_CANCELED result code is returned in handleActivityResult method the IabResult for all such cases is fixed with user cancelled (-1005) no matter what the reason is. So in order to get the correct actual response code replace the following code in handleActivityResult

 else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }

with this

 else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(responseCode, null);
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }

hope it will saves someones time

Ravi
  • 4,872
  • 8
  • 35
  • 46