I have implement in app purchase (managed) in my application using billing services when ever user wants to buy item he will make purchase request that required internet even if user has already bought the item. so for user convenience i am doing that if user has bought the item then a value will be save in shared pref. and when ever user click on that particular item then 1st it will check shared pref. value that either user has bought item or not if not then go for purchase request else show him/her item. My Question is that is it safe way? or i have to do something else?
Tap on your profile icon on the top-right corner of the screen, then select Settings in the pop-up menu. 3. On the Settings page, tap Authentication, and then tap Require authentication for purchases. 4.
If you haven't received an in-app item you bought, try closing and restarting the app or game you're using. Tap Apps or Manage applications (depending on your device, this may be different). Tap the app you used to make your in-app purchase.
Complete example of this technique is within TrivialDrive sample app and it's work for me, but Im thinking about offline case. Google Play app caches purchases and gives responses to the getPurchases(3, "pkg", "inapp", null) offline but is this works infinitly or there is something like a timeout for this cache...
This is not safe. I would discourage you from implementing such a check this way. You should rather go for standard approach and use getPurchases() method. You can call this method at any time (even offline) and if a user has purchases, they will be returned back from that method. Here is a sample code:
IInAppBillingService service; // initialize it first
Bundle response = service.getPurchases(3, "you app package", "inapp", null);
int responseCode = response.getInt(KEY_RESPONSE_CODE);
if (responseCode == RESPONSE_OK) {
ArrayList<String> purchases = response.getStringArrayList(KEY_INAPP_PURCHASE_ITEM_LIST);
...
}
Of curse you need to verify that purchases are signed with correct certificate and purchase state is not cancelled. But this is much safer than storing data in shared properties. Another advantage of this approach is that after user reinstalls the app, all purchases will be automatically available there too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With