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"
minSdk = 26
targetSdk = 35
versionCode = 11
versionName = "1.0.6"
versionCode = 14
versionName = "1.0.9"
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.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import android.os.Handler
import android.os.Looper
class BillingManager(
context: Context,
@@ -80,9 +78,7 @@ class BillingManager(
log("Billing setup finished: ${result.responseCode} ${result.debugMessage}")
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
Handler(Looper.getMainLooper()).post {
refresh()
}
refresh()
} else {
logError("Billing setup failed: ${result.responseCode} ${result.debugMessage}")
}
@@ -162,44 +158,52 @@ class BillingManager(
return
}
val requestedProducts = listOf(
BillingProducts.SUB_REMOVE_ADS_MONTHLY to BillingClient.ProductType.SUBS,
BillingProducts.INAPP_REMOVE_ADS_FOREVER to BillingClient.ProductType.INAPP,
queryProductDetailsForType(
productType = BillingClient.ProductType.SUBS,
productIds = listOf(BillingProducts.SUB_REMOVE_ADS_MONTHLY)
)
log("Querying product details for: $requestedProducts")
val products = requestedProducts.map { (id, type) ->
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}"
queryProductDetailsForType(
productType = BillingClient.ProductType.INAPP,
productIds = listOf(BillingProducts.INAPP_REMOVE_ADS_FOREVER)
)
val params = QueryProductDetailsParams.newBuilder()
.setProductList(products)
.build()
}
log("Calling queryProductDetailsAsync now")
private fun queryProductDetailsForType(
productType: String,
productIds: List<String>
) {
try {
log("Querying $productType product details for $productIds")
billingClient.queryProductDetailsAsync(params) { result, queryResult ->
log("queryProductDetails result: ${result.responseCode} ${result.debugMessage}")
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 products = productIds.map { productId ->
QueryProductDetailsParams.Product.newBuilder()
.setProductId(productId)
.setProductType(productType)
.build()
}
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)
}
}