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

1

7

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?

Banuelos answered 17/10, 2018 at 15:22 Comment(5)
You might find BillingClient helpful if managing the service connection manually is too tedious for you.Bramblett
@Bramblett I am using the BillingClient but in the implementation, I want to be able to decide whether to use the Google billing client or our AliPay client before getting an instance of the implementation.Banuelos
I think I meant to say BillingClient.isFeatureSupported()Bramblett
It does not, check the values of the FeatureType enum: developer.android.com/reference/com/android/billingclient/api/…Banuelos
please consider starring issuetracker.google.com/issues/118767098Radius
B
6

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.

Banuelos answered 26/10, 2018 at 9:15 Comment(6)
But isBillingSupported() returns the same value in all 3 scenarios. I thought you wanted a method that distinguishes functioning billing from non-functioning billing?Radius
@Mark, well that's exactly what this does. PlayStore billing is not supported in China and this method correctly determines that billing is unavailable in all the different scenarios.Banuelos
But for "PlayStore app installed with VPN", PlayStore billing does work for me in China. Are you saying it doesn't?Radius
Well from the tests of our colleagues in China it appears it doesn't, or at least these are the responses they logged and gave us.Banuelos
They can try following these steps: hanpingchinese.com/forum/viewtopic.php?f=3&t=22 I'd be surprised if something changed in the last few months so it no longer works with VPN.Radius
How we can achieve this with latest Google Play Billing library as AIDL approach got deprecated?Lepage

© 2022 - 2024 — McMap. All rights reserved.