Question

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

Was it helpful?

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).

OTHER TIPS

I'm not sure, but i think, after call restoreTransactions() will be call onPurchaseStateChange with ids of purchaised items.

I used this method:

public static void restoreTransactionInformation(Long nonce){
    if (amIDead()) 
    {
        return;
    }
    Log.i(TAG, "confirmTransaction()");
    Bundle request = makeRequestBundle("RESTORE_TRANSACTIONS");
    // The REQUEST_NONCE key contains a cryptographically secure nonce (number used once) that you must generate
    request.putLong("NONCE", nonce);
    try 
    {
        Bundle response = mService.sendBillingRequest(request);

        //The REQUEST_ID key provides you with a unique request identifier for the request
        Long requestIndentifier     = (Long) response.get("REQUEST_ID");
        Log.i(TAG, "current request is:" + requestIndentifier);

        //The RESPONSE_CODE key provides you with the status of the request
        Integer responseCodeIndex   = (Integer) response.get("RESPONSE_CODE");
        C.ResponseCode responseCode = C.ResponseCode.valueOf(responseCodeIndex);
        Log.i(TAG, "RESTORE_TRANSACTIONS Sync Response code: "+responseCode.toString());
    } 
    catch (RemoteException e) 
    {
        Log.e(TAG, "Failed, internet error maybe", e);
        Log.e(TAG, "Billing supported: " + isBillingSupported());
    }
}

Call this by using

BillingHelper.restoreTransactionInformation(BillingSecurity.generateNonce());

I would attest @Frank Leigh's answer with an edit that all the purchases come in one PURCHASE_STATE_CHANGED, with signed data as follows

signedData:{

    "nonce":1234*,
    "orders":[
         {
         "orderId":"1234*.1234*",
         "packageName":"com.*",
         "productId":"**p1**",
         "purchaseTime":time,
         "purchaseState":0,
         "purchaseToken":"*"
        },
        {
         "orderId":"1234*.1234*",
         "packageName":"com.*",
         "productId":"**p2**",
         "purchaseTime":time,
         "purchaseState":0,
         "purchaseToken":"*"
        },
        {
         "orderId":"1234*.1234*",
         "packageName":"com.*",
         "productId":"**p3**",
         "purchaseTime":time,
         "purchaseState":0,
         "purchaseToken":"*"
        }
    ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top