Search code examples
androidtransactionsrestorebillingin-app

How restore transaction in In-App Billing (Android) work?


I tried to search about that problem, but didnt get many information. I just know that:
- Restore in the first time user install app or when user reinstall when they uninstall/wipe data.
- Restore transaction just apply for managed-product.
I tried to read Dungeon Example, there are few line of code about restore transaction like: when to call restore request, when to get restore respone... but i dont know:
- How to get return information? (like item id that you have bought)

Someone please explain once again about Process of Restore Transaction in In-App billing.

Thanks you so much!!!

Edited: so sorry, I did not work in Android from long time, and right now I dont know what is right answer for this question, so I cant mark answer :P


Solution

  • The typical flow is as follows:

    1. User installs your app.

    2. On first load of your app, you check if you need to restore purchases.

    3. If you do, send a RESTORE_TRANSACTION synchronous request to Google.

    4. Google will respond with a acknowlegment response to your RESTORE_TRANSACTION request. (This is only an acknowlegement that they received your request.)

    5. At this point, you should mark that you had already sent a restore request to Google and no further restores needs to be sent from the app.

    6. Now asynchronously Google will start sending a 'PURCHASE_STATE_CHANGED' event to your app for each in-app purchase the user has previously purchased. This call is the same as what Google would had sent if the user had made that purchase for the first time.

    7. Since it's the same call, your app would pick up the event and handled it normally as if the user has just purchased the in-app product (thereby "restoring" the purchased feature).

    In regard to steps 2 and 5, what I've done for my app is to keep a SharedPreference value called 'APP_INITIALISED' that defaults to false. Everytime my app starts up, if 'APP_INITIALISED' is false, I tell Google to RESTORE_TRANSACTION (step 2) then I set APP_INITIALISED to true (step 5).