1

I am implementing In App Billing in my first app using trivial drive 2 example.

My simple implementation: the App is free and can be upgraded to a premium version via In App Billing providing some additonal features. These features are in different activities, sometimes an action can only be performed if premium is purchased, sometimes the activity looks different. So I only can have two different states: basic or premium.

I read a lot about this topic, but still have some questions about it. I guess many of the answers from stackoverflwo belong to an older implementation of in App Billing (like this one: How to best save InApp purchase status locally?).

Now my question: I have to check if user has premium status in different activities. What is the best practice doing it without always query purchases (performance)? Can it be checked once and used all over the app? Does anybody has some code examples of something similar? I thought this might be a common implementation, but didn't find anything...

By the way: the right method of checking if user purchased premium is queryPurchases from BillingClint - is that correct?

Thanks a lot for your help and thoughts!

Raspberry
  • 139
  • 1
  • 11

2 Answers2

2

Yes, you shouldn't query this all the time. You should do a check on the app startup and right after the user bought your in-app item.

You should have a singleton repository which you inject/pass into all your activities where it matters. The activities should ask this repository for the current state of purchase (ie: bought, not bought, unknown)

Probably you should implement a retry behaviour if it couldn't check online for whatever reason.

jbarat
  • 2,382
  • 21
  • 23
  • Thanks for your answer jbarat. I hoped there will be some easy ways,but it doesn't sound like that... I'm new to this topic and a little bit overwhelmed. What is the correct point of the app startup? Every activities onCreate? I have to do all the stuff thats described on https://developer.android.com/reference/com/android/billingclient/api/BillingClient, also startConnection and waiting till setup is complete - whre can I find the correct place in my singleton repository, especially for the call endConnection()? Thank you a lot for your help! Maybe you know some examples that can help me? – Raspberry Jun 15 '19 at 11:16
  • 1. Yes, inject it into all of your activities, that is the easiest. 2. Yes, you need to do all the things described on that website. Setup a connection, retry that connection if it failed and query it for your item. You don't really need to close the connection if you have a singleton. 3. You can have a look here: https://developer.android.com/google/play/billing/billing_library_overview but most of this you don't need – jbarat Jun 15 '19 at 11:34
0

Yes queryPurchases is best as it doesn't require network. Don't forget to acknowledge the purchase if using billing 2.0 or above.

Surya Ganesh
  • 492
  • 1
  • 6
  • 15
  • Surya Ganesh, Thanks for your answer! Can you explain what "Don't forget to acknowledge the purchase" means? – Raspberry Jun 15 '19 at 11:02
  • You must acknowledge all purchases within three days. Failure to properly acknowledge purchases results in those purchases being refunded. Go through this https://developer.android.com/google/play/billing/billing_library_overview – Surya Ganesh Jun 15 '19 at 11:43