Question

This is part of the code to implement in-app billing. I have two doubts.

@Override
    public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
           int quantity, long purchaseTime, String developerPayload) {
        if (Consts.DEBUG) {
            Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
        }

        if (developerPayload == null) {
            logProductActivity(itemId, purchaseState.toString());
        } else {
            logProductActivity(itemId, purchaseState + "\n\t" + developerPayload);
        }

        if (purchaseState == PurchaseState.PURCHASED) {
            mOwnedItems.add(itemId);

            // At this point I have to put Premium changes
        }
    }

My questions:

  1. At the point where I say "At this point I have to put Premium changes", how can I assure you that the application has been purchased?

  2. I have understood that once the purchase is made, this can take a few hours to take effect. How do I ensure that my application will execute the code that's in point: "At this point I have to put Premium changes"?

Was it helpful?

Solution

If you reach this block

if (purchaseState == PurchaseState.PURCHASED) {
    ...
}

Then you know the purchase is successful. To my experience, the delay after the purchase is made is never more than a couple of seconds. But the user could close the app before the confirmation.

That is why you should implement restoreTransactions() as well. Each time your activity opens it checks with the server whether the app has been bought.

/**
 * A {@link PurchaseObserver} is used to get callbacks when Android Market sends
 * messages to this application so that we can update the UI.
 */
private class MyPurchaseObserver extends PurchaseObserver {
    public DownloaderPurchaseObserver(Handler handler) {
        super(home, handler);
    }

    @Override
    public void onBillingSupported(boolean supported) {
        ...
        yourBillingService.restoreTransactions();
        ...
    }

    @Override
    public void onRestoreTransactionsResponse(RestoreTransactions request,
            ResponseCode responseCode) {
        if (responseCode == ResponseCode.RESULT_OK) {
            // repeat your premium validation here
        } else {
            if (Consts.DEBUG) {
                Log.e(TAG, "RestoreTransactions error: " + responseCode);
            }
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top