Reputation: 81
I've very confused on Android's in-appl billing in regard to RESTORE_TRANSACTIONS.
I have this snippet for making a donation within my app:
BillingHelper.requestPurchase(mContext, "donation");
It works great, no issues there. The problem is here, when the purchase is completed I set a boolean value:
if (BillingHelper.latestPurchase.isPurchased()) {
DONATE_VERSION = true;
}
The app works as intended after this, unless the user uninstalls the app. I store the DONATE_VERSION inside shared preferences. Storing the purchase information in a personal database on the internet is not an option.
When the user re-installs the app, the only way they can get the ads removed from donating is by donating again! I don't want this to be the case. I want to be able to query Google for the results of which items (in this case, jut the "donation" item) have been purchased. I call this in onCreate():
BillingHelper.restoreTransactionInformation(BillingSecurity.generateNonce());
But now what? If the user has previously purchased the managed in app purchase of "donation", how can I query google to get the information about which items have been purchased from in-app billing, so that I can set my boolean again? Please be as clear as possible as I've been messing with this, chatting on IRC, and scouring the API's for about 6 hours now and I can't figure this out.
EDIT:
My onReceive() method:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "Received action: " + action);
if (ACTION_PURCHASE_STATE_CHANGED.equals(action)) {
String signedData = intent.getStringExtra(INAPP_SIGNED_DATA);
String signature = intent.getStringExtra(INAPP_SIGNATURE);
purchaseStateChanged(context, signedData, signature);
} else if (ACTION_NOTIFY.equals(action)) {
String notifyId = intent.getStringExtra(NOTIFICATION_ID);
notify(context, notifyId);
} else if (ACTION_RESPONSE_CODE.equals(action)) {
long requestId = intent.getLongExtra(INAPP_REQUEST_ID, -1);
int responseCodeIndex = intent.getIntExtra(INAPP_RESPONSE_CODE, C.ResponseCode.RESULT_ERROR.ordinal());
checkResponseCode(context, requestId, responseCodeIndex);
} else {
Log.e(TAG, "unexpected action: " + action);
}
Upvotes: 2
Views: 2897
Reputation: 5372
I just answered a similiar question to this here: https://stackoverflow.com/a/12187921/455886
A typical scenario flow for restore transactions is as follows:
User installs your app.
On first load of your app, you check if you need to restore purchases.
If you do, send a RESTORE_TRANSACTION synchronous request to Google.
Google will respond with a acknowlegment response to your RESTORE_TRANSACTION request. (This is only an acknowlegement that they received your request.)
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.
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.
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).
Upvotes: 0
Reputation: 52936
You will get the transaction info in a PURCHASE_STATE_CHANGED message, just as when you do after a successful purchase. Process as it as usual and set whatever flags/preferences you need to. Also make sure you only call it on first install (when said preferences are missing/null), because calling it often will get your app blocked for a certain period of time.
Upvotes: 1