I have implemented inApp purchase in my game app where first three levels are free and then user has to pay to unlock remaining 7 levels.
In app purchase is working fine. But once, the user buys the item, how should I handle it so that next time onwards he gets to see the levels as unlocked. Should I save it in sharedPreferences and check everytime. Or should I query everytime from google console to check the status for the particular id.
Would highly appriciate if someone can suggest me the right way to do it..
How should the behavior be like if user uninstalls the app and installs it again on same device?
Using RESTORE_TRANSACTION
is the only way if you use managed purchases. If you use unmanaged purchases, you can store the purchase state on your own server, but that is hardly simpler.
There is nothing complex about RESTORE_TRANSACTIONS
: you just fire the command and you get notified with transaction information that is in the exact same format as what you get when you first purchase an item. You should process it in the exact same way, and chances are you already have the code for that in your app. Testing this is somewhat harder, because it doesn't really work with test accounts, and you need a live app.
Go over the official documentation again to understand how it works.
Three way to manage your application's product data.
1) SharedPrefrence: you can use the share prefrence value and check whether it is purchased or not. if in case user uninstalled the app and then re-install the app then you can check whether user has purchased or not, at this you get the item is already purchased. And you have to manage the user to access your application data.
2) local database: you can also use local sqlite database to store the purchase detail and purchase status. and same as above if user clear data or uninstall the app then request for the purchase item again and check whether user purchased item or not.
or
2) Server database: It is the better way compare to above if you are using web server to store the user data. In this type, you doesn't even need to manage for the second time for the case if user uninstall the app or clear the application data.
3) obfuscation: (Most efficient way compare to shared prefrence)
For more details about implementation check HERE
Here is a snippet of how the transactions are restored:
private void restoreTransactions() {
if (!mBillingObserver.isTransactionsRestored()) {
BillingController.restoreTransactions(this);
Toast.makeText(this, R.string.restoring_transactions, Toast.LENGTH_LONG).show();
}
}
EDITED:
The V3 of the api does not require anymore to restore the purchases. Can directly query for purchased items.
Please read the section Querying for Purchased Item of:
http://developer.android.com/google/play/billing/billing_integrate.html
and the getPurchases() method description.
To get already purchased items details try out as below:
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
complain("Failed to query inventory: " + result);
return;
}
mIsPremium = inventory.hasPurchase(SKU_PREMIUM); ------> By here you can get information
}
};