fix(iap): don't initialize billing on Android without google play service, closes #2630 (#2677)

This commit is contained in:
Huang Xin 2025-12-10 22:34:23 +08:00 committed by GitHub
parent 669d3950e2
commit 5141be1c3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 140 additions and 8 deletions

View file

@ -8,6 +8,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.common.ConnectionResult
import java.text.SimpleDateFormat
import java.util.*
@ -16,12 +18,27 @@ class BillingManager(private val activity: Activity) : PurchasesUpdatedListener
private val productsCache = mutableMapOf<String, ProductDetails>()
private var purchaseCallback: ((PurchaseData?) -> Unit)? = null
private val scope = CoroutineScope(Dispatchers.Main)
private val isGooglePlayAvailable: Boolean by lazy {
val availability = GoogleApiAvailability.getInstance()
val resultCode = availability.isGooglePlayServicesAvailable(activity)
resultCode == ConnectionResult.SUCCESS
}
companion object {
private const val TAG = "BillingManager"
}
fun isBillingAvailable(): Boolean {
return isGooglePlayAvailable
}
fun initialize(callback: (Boolean) -> Unit) {
if (!isGooglePlayAvailable) {
Log.d(TAG, "Google Play Services not available, skipping billing setup")
callback(false)
return
}
billingClient = BillingClient.newBuilder(activity)
.setListener(this)
.enablePendingPurchases()

View file

@ -511,6 +511,14 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
invoke.resolve(ret)
}
@Command
fun iap_is_available(invoke: Invoke) {
val isAvailable = billingManager.isBillingAvailable()
val result = JSObject()
result.put("available", isAvailable)
invoke.resolve(result)
}
@Command
fun iap_initialize(invoke: Invoke) {
billingManager.initialize { success ->