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:
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.
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.
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?
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.
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