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,45 +158,53 @@ 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( private fun queryProductDetailsForType(
"Querying product details for: " + productType: String,
"${BillingProducts.SUB_REMOVE_ADS_MONTHLY}:${BillingClient.ProductType.SUBS}, " + productIds: List<String>
"${BillingProducts.INAPP_REMOVE_ADS_FOREVER}:${BillingClient.ProductType.INAPP}" ) {
) try {
log("Querying $productType product details for $productIds")
val products = productIds.map { productId ->
QueryProductDetailsParams.Product.newBuilder()
.setProductId(productId)
.setProductType(productType)
.build()
}
val params = QueryProductDetailsParams.newBuilder() val params = QueryProductDetailsParams.newBuilder()
.setProductList(products) .setProductList(products)
.build() .build()
log("Calling queryProductDetailsAsync now")
billingClient.queryProductDetailsAsync(params) { result, queryResult -> billingClient.queryProductDetailsAsync(params) { result, queryResult ->
log("queryProductDetails result: ${result.responseCode} ${result.debugMessage}") log("queryProductDetailsAsync($productType) result: ${result.responseCode} ${result.debugMessage}")
val detailsList = queryResult.productDetailsList val detailsList = queryResult.productDetailsList
log("Fetched products: ${detailsList.map { it.productId }}") log("Fetched $productType products: ${detailsList.map { it.productId }}")
val unfetched = queryResult.unfetchedProductList if (queryResult.unfetchedProductList.isNotEmpty()) {
log("Unfetched products: ${unfetched.map { "${it.productId}:${it.statusCode}" }}") logError("Unfetched $productType products: ${queryResult.unfetchedProductList}")
}
if (result.responseCode == BillingClient.BillingResponseCode.OK) { if (result.responseCode == BillingClient.BillingResponseCode.OK) {
_productDetails.value = detailsList.associateBy { it.productId } _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)
}
} }
private fun queryActivePurchases() { private fun queryActivePurchases() {