Fix the purchase and subscribe functionality

This commit is contained in:
Tretzi
2026-04-15 22:36:13 -05:00
parent 937689cbfa
commit f2a9e3daa7
2 changed files with 43 additions and 39 deletions

View File

@@ -15,8 +15,8 @@ android {
applicationId = "solutions.tretter.mindmachine" applicationId = "solutions.tretter.mindmachine"
minSdk = 26 minSdk = 26
targetSdk = 35 targetSdk = 35
versionCode = 11 versionCode = 14
versionName = "1.0.6" versionName = "1.0.9"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@@ -18,8 +18,6 @@ import com.android.billingclient.api.QueryPurchasesParams
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import android.os.Handler
import android.os.Looper
class BillingManager( class BillingManager(
context: Context, context: Context,
@@ -80,9 +78,7 @@ class BillingManager(
log("Billing setup finished: ${result.responseCode} ${result.debugMessage}") log("Billing setup finished: ${result.responseCode} ${result.debugMessage}")
if (result.responseCode == BillingClient.BillingResponseCode.OK) { if (result.responseCode == BillingClient.BillingResponseCode.OK) {
Handler(Looper.getMainLooper()).post { refresh()
refresh()
}
} else { } else {
logError("Billing setup failed: ${result.responseCode} ${result.debugMessage}") logError("Billing setup failed: ${result.responseCode} ${result.debugMessage}")
} }
@@ -162,44 +158,52 @@ class BillingManager(
return return
} }
val requestedProducts = listOf( queryProductDetailsForType(
BillingProducts.SUB_REMOVE_ADS_MONTHLY to BillingClient.ProductType.SUBS, productType = BillingClient.ProductType.SUBS,
BillingProducts.INAPP_REMOVE_ADS_FOREVER to BillingClient.ProductType.INAPP, productIds = listOf(BillingProducts.SUB_REMOVE_ADS_MONTHLY)
) )
log("Querying product details for: $requestedProducts") queryProductDetailsForType(
productType = BillingClient.ProductType.INAPP,
val products = requestedProducts.map { (id, type) -> productIds = listOf(BillingProducts.INAPP_REMOVE_ADS_FOREVER)
QueryProductDetailsParams.Product.newBuilder()
.setProductId(id)
.setProductType(type)
.build()
}
log(
"Querying product details for: " +
"${BillingProducts.SUB_REMOVE_ADS_MONTHLY}:${BillingClient.ProductType.SUBS}, " +
"${BillingProducts.INAPP_REMOVE_ADS_FOREVER}:${BillingClient.ProductType.INAPP}"
) )
}
val params = QueryProductDetailsParams.newBuilder() private fun queryProductDetailsForType(
.setProductList(products) productType: String,
.build() productIds: List<String>
) {
try {
log("Querying $productType product details for $productIds")
log("Calling queryProductDetailsAsync now") val products = productIds.map { productId ->
QueryProductDetailsParams.Product.newBuilder()
billingClient.queryProductDetailsAsync(params) { result, queryResult -> .setProductId(productId)
log("queryProductDetails result: ${result.responseCode} ${result.debugMessage}") .setProductType(productType)
.build()
val detailsList = queryResult.productDetailsList
log("Fetched products: ${detailsList.map { it.productId }}")
val unfetched = queryResult.unfetchedProductList
log("Unfetched products: ${unfetched.map { "${it.productId}:${it.statusCode}" }}")
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
_productDetails.value = detailsList.associateBy { it.productId }
} }
val params = QueryProductDetailsParams.newBuilder()
.setProductList(products)
.build()
billingClient.queryProductDetailsAsync(params) { result, queryResult ->
log("queryProductDetailsAsync($productType) result: ${result.responseCode} ${result.debugMessage}")
val detailsList = queryResult.productDetailsList
log("Fetched $productType products: ${detailsList.map { it.productId }}")
if (queryResult.unfetchedProductList.isNotEmpty()) {
logError("Unfetched $productType products: ${queryResult.unfetchedProductList}")
}
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
_productDetails.value = _productDetails.value + detailsList.associateBy { it.productId }
log("Product details cache now has: ${_productDetails.value.keys}")
}
}
} catch (t: Throwable) {
Log.e("MindMachineBilling", "Exception querying $productType product details", t)
} }
} }