Search code examples
androidin-app-purchase

Android In-App Purchase Failure on Successful Purchase


I am developing an with in-app purchases. I have put the app on play store in closed testing. With tester emails and added tester emails in account settings. I have 2 managed products on play store. I tested android.test.purchased in debug mode and it's working fine. But on release build installed from play store when I purchase a SKU it completes purchase successfully but I am getting failure response for some reason. I am trying to figure this out. Also I am consuming the items after purchasing so It becomes available again. Here's my code

 mHelper = new IabHelper(this, Constants.BASE64_ENCODED_PUBLIC_KEY);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG, "In-app Billing setup failed due to: " +
                        result.getMessage());
            } else {
                //do further steps here.
                Log.d(TAG, "In-app Billing is set up OK");
            }
        }
    });

Then on button click

mHelper.launchPurchaseFlow(PurchaseActivity.this, ITEM_SKU, 10001,
                                mPurchaseFinishedListener, "mypurchasetoken");
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            if (result.isFailure()) {
                Toast.makeText(PurchaseActivity.this, "mPurchaseFinishedListener: " + result.getMessage(), Toast.LENGTH_SHORT).show();
                if (result.getResponse() == 7) {
                    consumeItem();
                } else {
                    dismissProgressDialog();
                    Log.d(TAG, "onIabPurchaseFinishedWithError: " + result.getMessage());
                    AlertHelper.errorAlert(PurchaseActivity.this,
                            getString(R.string.purchase_unsuccessful_text), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            });

                }
            } else if (purchase.getSku().equals(ITEM_SKU)) {
                Log.d(TAG, "onIabPurchaseFinishedWithSuccess: ");
                consumeItem();
            }

        }
    };

public void consumeItem() {
   /* if (isTest) {
        try {
            int response = mHelper.mService.consumePurchase(3, getPackageName(), "inapp:" + getPackageName() + ":android.test.purchased");
            if (response == 0) {
                //Save purchase status to shared pref.
                updateUserAccount(ITEM_SKU);

                //Sign up user to our server.
                signUpUserToServer("android.test.purchased:9090");
                Log.d(TAG, "onConsumeFinishedSuccessfully");
            } else {
                Log.d(TAG, "onConsumeFinishedWithError: " + response);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } else*/
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
                                         Inventory inventory) {

        if (result.isFailure()) {
            dismissProgressDialog();
            Log.d(TAG, "onQueryInventoryFinishedWithError: " + result.getMessage());
            Toast.makeText(PurchaseActivity.this, "mReceivedInventoryListener: " + result.getMessage(), Toast.LENGTH_SHORT)
                    .show();
        } else {
            Log.d(TAG, "onQueryInventoryFinished. asyncConsume fired. ");
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                    mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {

        if (result.isSuccess()) {
            if (isAlreadyOwned) {
                isAlreadyOwned = false;
                processPurchaseClick();
            } else {
                //Save purchase status to shared pref.
                updateUserAccount(ITEM_SKU);

                //Sign up user to our server.
                signUpUserToServer(purchase.getToken());
                Log.d(TAG, "onConsumeFinishedSuccessfully");
            }
        } else {
            Toast.makeText(PurchaseActivity.this, "onConsumeFinishedWithError: " + result.getMessage(), Toast.LENGTH_SHORT)
                    .show();
            Log.d(TAG, "onConsumeFinishedWithError: " + result.getMessage());
        }
    }
};

On successful purchase from my test account I am getting failure result and R.string.purchase_unsuccessful_text in onPurchasefinishedListener. Help plz


Solution

  • I figured it out guys so I am posting the answer so it might help someone. My Base64 Public key was of another app. I put the right key and it worked. This answer helped me.