Fix the purchase and subscribe functionality
This commit is contained in:
@@ -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"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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")
|
|
||||||
|
|
||||||
val products = requestedProducts.map { (id, type) ->
|
|
||||||
QueryProductDetailsParams.Product.newBuilder()
|
|
||||||
.setProductId(id)
|
|
||||||
.setProductType(type)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
log(
|
queryProductDetailsForType(
|
||||||
"Querying product details for: " +
|
productType = BillingClient.ProductType.INAPP,
|
||||||
"${BillingProducts.SUB_REMOVE_ADS_MONTHLY}:${BillingClient.ProductType.SUBS}, " +
|
productIds = listOf(BillingProducts.INAPP_REMOVE_ADS_FOREVER)
|
||||||
"${BillingProducts.INAPP_REMOVE_ADS_FOREVER}:${BillingClient.ProductType.INAPP}"
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
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 ->
|
val products = productIds.map { productId ->
|
||||||
log("queryProductDetails result: ${result.responseCode} ${result.debugMessage}")
|
QueryProductDetailsParams.Product.newBuilder()
|
||||||
|
.setProductId(productId)
|
||||||
val detailsList = queryResult.productDetailsList
|
.setProductType(productType)
|
||||||
log("Fetched products: ${detailsList.map { it.productId }}")
|
.build()
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user