1

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"?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Pablo
  • 13
  • 2

1 Answers1

0

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);
            }
        }
    }

}
znat
  • 13,144
  • 17
  • 71
  • 106
  • NathanZ, thanks for your speed response! I have a one more doubt. My problem: I start the related billing services when I call MyBuyActivity.java, but while the user not click in the button, the billing services never be start. Then, if I refunded he the price, the app will be Premium for always. How can I call the billing services from my main Activity? The same case will be if the purchase take a few hours to take effect (although rare to happen). Sorry for my bad english. – Pablo Dec 05 '12 at 11:52
  • See this post about refunds: http://stackoverflow.com/questions/6817507/android-in-app-billing-cancel-payment – znat Dec 05 '12 at 15:37