Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if in-app billing via PlayStore is available to user

Tags:

So we want to support in-app billing via Google's billing API and via AliPay for China. I've written a method that should return either a GooglePlay or an AliPay billing client (whichever is available). I need a way to determine whether Google's billing service is available to the user so I know which client to return.

So far I've come across a few different options and I'm unsure which one is the one I need:

  1. Create a ServiceConnection and check the result of IInAppBillingService.Stub.asInterface(service) .isBillingSupported(3, context.packageName, "inapp")

Here's the full code: https://gist.github.com/first087/9088162

This is a bit tedious since I need to wait for the service to connect, get the asynchronous result and then return the correct billing manager, but at first glance seems to be exactly what I need.

  1. Use the GoogleApiAvailability class and check the result of isGooglePlayServicesAvailable(context)

This option is a lot cleaner than the 1st one, but I'm unsure whether it returns what I need and also requires me to add the com.google.android.gms:play-services-base library to my project.

  1. Check if the GooglePlay app is installed on the device.

This is the most unreliable option (I think), because you can manually install the app, even though it's not been pre-installed by the manufacturer, and then you might not be able to make purchases since you're in China and they don't allow that.


Has anybody had similar experience? How do I correctly determine whether the user can make purchases via the PlayStore?

like image 511
Schadenfreude Avatar asked Oct 17 '18 15:10

Schadenfreude


1 Answers

So after testing the methods in China, with a phone that did and didn't have a the PlayStore app installed, here's what we found:

With PlayStore app installed and without VPN

  • GoogleApiAvailability.isGooglePlayServicesAvailable() returns code 2 - ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED
  • IInAppBillingService.isBillingSupported() returns code 3 - BillingResponse.BILLING_UNAVAILABLE

Without PlayStore app installed and without VPN

  • GoogleApiAvailability.isGooglePlayServicesAvailable() returns code 9 - ConnectionResult.SERVICE_INVALID
  • IInAppBillingService.isBillingSupported() returns code 3 - BillingResponse.BILLING_UNAVAILABLE

With PlayStore app installed and with VPN

  • GoogleApiAvailability.isGooglePlayServicesAvailable() returns code 0 - ConnectionResult.SUCCESS
  • IInAppBillingService.isBillingSupported() returns code 3 - BillingResponse.BILLING_UNAVAILABLE

Conclusion: The safest way to determine whether billing is actually available is via the isBillingSupported() method. If you don't want to use it via the "hacky" way shown in option 1 of the question, you can just instantiate a new BillingClient and wait for the callback of its startConnection() method.

Here's a gist of the coroutine I wrote which gives you one of the two implementations of a BillingManager depending on whether in-app billing via the PlayStore is available.

like image 66
Schadenfreude Avatar answered Nov 15 '22 05:11

Schadenfreude