android - 我们应该在购买成功后解锁应用内商品(onPurchasesUpdated),还是在确认成功后解锁(onAcknowledgePurchaseResponse)?
问题描述
自从在计费库 2 和 3 中,我们需要在购买成功后执行额外的确认步骤。
购买成功后,会触发如下回调。
public interface PurchasesUpdatedListener {
void onPurchasesUpdated(BillingResult billingResult, java.util.List<com.android.billingclient.api.Purchase> list);
}
确认成功后,会触发如下回调。
public interface AcknowledgePurchaseResponseListener {
void onAcknowledgePurchaseResponse(BillingResult billingResult);
}
在成功的情况下,我们应该解锁应用内购买项目,购买成功还是确认成功?
解决方案
购买后无法购买 InApp 产品。我们需要在成功购买后消费它,以便我们可以再次购买,并且在我们下次购买之前购买的相同产品时可以使用它。
您必须确认所有非消耗品购买,即必须确认订阅。这是参考代码或您
/**
* If you do not acknowledge a purchase, the Google Play Store will provide a refund to the
* users within a few days of the transaction. Therefore you have to implement
* [BillingClient.acknowledgePurchaseAsync] inside your app.
*
* @param purchase list of Purchase Details returned from the queries.
*/
private fun acknowledgeNonConsumablePurchasesAsync(purchase: Purchase) {
val acknowledgePurchaseRunnable = Runnable {
val params = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
myBillingClient.acknowledgePurchase(
params
) { billingResult: BillingResult ->
if (billingResult.responseCode == BillingResponseCode.OK) {
LogUtils.d(TAG, "onAcknowledgePurchaseResponse: " + billingResult.responseCode)
} else {
LogUtils.d(TAG, ("onAcknowledgePurchaseResponse: " + billingResult.debugMessage))
}
}
}
}
对于消耗品,这应该做
/**
* Consumes InApp Product Purchase after successful purchase of InApp Product Purchase. InApp
* Products cannot be bought after a purchase was made. We need to consume it after a
* successful purchase, so that we can purchase again and it will become available for the
* next time we make purchase of the same product that was bought before.
*
* @param purchase the purchase result contains Purchase Details.
*/
val onConsumeListener =
ConsumeResponseListener { billingResult: BillingResult, purchaseToken: String ->
// If billing service was disconnected, we try to reconnect 1 time
// (feel free to introduce your retry policy here).
if (billingResult.responseCode == BillingResponseCode.OK) {
LogUtils.d(TAG, "onConsumeResponse, Purchase Token: $purchaseToken")
} else {
LogUtils.d(TAG, "onConsumeResponse: " + billingResult.debugMessage)
}
}
// Creating a runnable from the request to use it inside our connection retry policy below
val consumeRequest = Runnable {
// Consume the purchase async
val consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
myBillingClient!!.consumeAsync(consumeParams, onConsumeListener)
}
推荐阅读
- java - 如何在 application-{profile}.properties 文件中包含 log4j-{profile}.properties 文件
- mysql - 使用 Group By 查询具有多个条件的多个列
- r - R data.table - 如何分解嵌套数组?
- pdf - TCPdf 在多个页面中将自定义作者或页面添加到页脚(每个页面上的作者/页面可以不同)
- sql - 从 oracle 触发器发送电子邮件 - 如果发生任何 DML
- ios - TableViewController 选择性单元格在表格重新加载时被取消选择
- amazon-web-services - AWS X-RAY 在同时获取跟踪摘要时显示奇怪的错误
- c++ - 在函数参数中区分数组和指针 - C++
- android - 为什么位置getTime滞后2-3分钟
- mysql - 无法在创建 MySQL 服务器的同一计划中使用 mysql