我是 Android 计费新手。我正在实施一项包含 4 种消费品购物选项的活动。所以我有 4 个按钮。当用户点击其中任何一个时,会出现 toast initiatedPurchase(productId: String) ,表明他们已进入该功能,但此后没有其他任何反应。知道可能出了什么问题吗?
private fun initiatePurchase(productId: String) {
Toast.makeText(applicationContext, "initiatePurchase(productId: String)", Toast.LENGTH_SHORT).show()
// Define the product details to query
val productList =
listOf(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(productId)
.setProductType(BillingClient.ProductType.SUBS)
.build()
)
// Create query parameters
val params = QueryProductDetailsParams.newBuilder().setProductList(productList).build()
// Query product details
billingClient.queryProductDetailsAsync(params) { billingResult, productDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (productDetailsList.size > 0) {
// Extract the product details
val productDetails = productDetailsList[0]
// Get the price of the product
val price = productDetails.getOneTimePurchaseOfferDetails()?.getFormattedPrice()
// Update the corresponding TextView with the price
when (productId) {
PRODUCT_ID_1 -> findViewById<TextView>(R.id.price1).text = price
PRODUCT_ID_2 -> findViewById<TextView>(R.id.price2).text = price
PRODUCT_ID_3 -> findViewById<TextView>(R.id.price3).text = price
PRODUCT_ID_4 -> findViewById<TextView>(R.id.price4).text = price
}
// Create BillingFlowParams using the product details
val productDetailsParamsList =
listOf(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.build()
)
val billingFlowParams =
BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.build()
// Launch the billing flow
billingClient.launchBillingFlow(this@Purchase, billingFlowParams)
} else {
Toast.makeText(applicationContext, "Not found", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(applicationContext, "Error " + billingResult.debugMessage, Toast.LENGTH_SHORT).show()
}
}
}
谢谢。
计费流程参数需要设置
setOfferToken
。
类似于下面,
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.setOfferToken(offerToken)
.build()
您可以提供以下代币
productDetails.getSubscriptionOfferDetails().get(0).getOfferToken()