user1778857 user1778857 - 5 months ago 28
Android Question

Android InApp Billing : How to detect back press interrupting the purchase process?

I'm trying to implement the inApp billing service with IabHelper. I manage to go through the full purchase process without problems.

//-----------------------------------------------
public void billingServiceLaunchPurchase(String item) {
//-----------------------------------------------
if (isNetworkAvailableSync(getBaseContext())) {
currBuyItem=item;
billingConsummeType=1;
mHelper.launchPurchaseFlow(BaseActivity.this, item, 10001, mPurchaseFinishedListener, "");
}
else {
onBillingServiceFailed();
}
}

//-----------------------------------------------
mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
//-----------------------------------------------
public void onIabPurchaseFinished(IabResult result, Purchase purchase)
{
if (result.isFailure()) {
// Handle error
onBillingServiceFailed();
return;
}
else if (purchase.getSku().equals(currBuyItem)) {
billingServiceConsumeItem();
}

}
};

@Override
//-----------------------------------------------------------------------
protected void onActivityResult(int requestCode, int resultCode, Intent data)
//-----------------------------------------------------------------------
{
if (billingServiceConnected) {
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
else {
// onActivityResult handled by IABUtil.
}
}
else
super.onActivityResult(requestCode, resultCode, data);

}


However, I cannot detect the event when the user launches the purchase but then press the backspace on the Google confirmation screen with the button "BUY" to interrupt it.

It neither triggers a failure on onIabPurchaseFinished nor it triggers onActivityResult so my application stays in an intermediary status.

Please help me to solve my problem.

Answer

According to what I have understood your question, you are searching for purchase cancel event in app billing.

You can trigger this via onActivityResult() method.Put below code in onActivityResult() method. There is simple RESEULT_CANCEL type that shows you user has been cancel purchasing.

    if (mHelper == null)
                return;

            if (requestCode == 10001) {

                int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
                String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
                Log.d("INAPP_PURCHASE_DATA", ">>>" + purchaseData);
                String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");
                Log.d("INAPP_DATA_SIGNATURE", ">>>" + dataSignature);
                String continuationToken = data
                        .getStringExtra("INAPP_CONTINUATION_TOKEN");
                Log.d("INAPP_CONTINUATION_TOKEN", ">>>" + continuationToken);

                if (resultCode == RESULT_OK) {
                    try {
                        Log.d("purchaseData", ">>>"+purchaseData);
                        JSONObject jo = new JSONObject(purchaseData);
                        String sku = jo.getString("productId");
                        alert("You have bought the " + sku
                                + ". Excellent choice, adventurer!");
                    } catch (JSONException e) {
                        alert("Failed to parse purchase data.");
                        e.printStackTrace();
                    }
                } else if (resultCode == RESULT_CANCELED) {

                    // } else if (resultCode == RESULT_CANCELED) {

                    Toast.makeText(AppMainTest.this,
                            "Sorry, you have canceled purchase Subscription.",
                            Toast.LENGTH_SHORT).show();

                } else if (resultCode == IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
                    Toast.makeText(AppMainTest.this, "Item already owned",
                            Toast.LENGTH_SHORT).show();
                }

            }
}

or

you can also handle manually by using your business logic. check if user cancel purchase product then put flag user has been purchased or not if not then call launchPurchaseFlow() method again.

EDIT

onDestroy() method 

@Override
    public void onDestroy() {
        super.onDestroy();

        // very important:
        Log.d(TAG, "Destroying helper.");
        if (mHelper != null)
            mHelper.dispose();
        mHelper = null;
    }

if you have button then you can directly call launchPurchaseFlow() method into onClick event so that every time your mHelper created as new purchase.

or

if you are using it in onCreate method and you haven't any button click event to purchase product then you have to give value as null according to my knowledge.

Hope it will solve your problem.