Search code examples
androidmobilegoogle-playin-app-purchasein-app

How to retrieve in-app purchases and unlock premium version of my app?


I have an android app featuring some in-app products. Users buy the in-app product and the app unlocks the corresponding feature(s). Now I have a question: I read the Google documentation and doesn't seem to be a definitive answer to my problem:

The app retrieves the list of available in-app products at startup, then it proceeds to retrieve the list of purchased in-app products. If an in-app product shows up in the available list, then I unlock the feature.

Why I check the list of available in-app products at startup?

Because I need to write the prices on the UI (purchase button) and I don't want to hard code all the possible prices for different countries. The response has the list of all the available in-app products plus prices already localized so it's very convenient

Why I check the list of purchased in-app product at startup?

Because I want to check if the user has purchased an in-app product and then I unlock the features on the UI. The Google documentations says that the call doesn't require an internet connection so why not? Why bother saving the in-app JSON product locally when I just call the local Google Play API?

Now my problem is: the call to the available in-app products and the call to the purchased products may fail... and in fact it failed once. If it fail what should I do? Keep the features disabled is not an option because the user may think he/she lost the product. Enabling the features is also a no-no because I may enable features to people who never bought the products. So I'm stuck. Is a retry enough? what's the best practice here?


Solution

  • As I understand it:

    The user buys an in-app product which enabled the premium version of your app. Once purchased, no further purchases are required for the feature to be enabled. So it is a one-time purchase.

    Fixing your problem:

    At the moment you always try to get the state of the premium feature at the start of the app using the Google API.

    You could also do it like this: If the user buys the premium feature, save this as a SharedPreference. Example: premiumVersion = true

    Whenever the user returns and the app starts, you could first check the SharedPreference because this is very unlikely to fail (No internet connection required etc.). If premiumVersion == true, then don't ask the Google API for the current state. Only use the Google API if premiumVersion == false. In this case, a previous attempt might have failed, or the user just doesn't have the premium feature.

    Sidenote: If the premiumVersion == true, make sure you never override it with false!

    For displaying the prices:

    If anything goes wrong when trying to retrieve your products, just prompt an error message to the user with a message that something went wrong and he should retry it.