自从在计费库2和3,有一个额外的acknowledgement步骤,我们需要执行,在采购是成功的。
当购买成功时,将触发以下回呼.
public interface PurchasesUpdatedListener {
void onPurchasesUpdated(BillingResult billingResult, java.util.List<com.android.billingclient.api.Purchase> list);
}当确认成功时,将触发以下回调。
public interface AcknowledgePurchaseResponseListener {
void onAcknowledgePurchaseResponse(BillingResult billingResult);
}在成功的情况下,我们应该解锁应用程序内的购买项目,当购买是成功,还是承认是成功的?
发布于 2020-09-04 08:34:32
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)
}发布于 2020-09-04 05:09:46
请查阅“发放应享权利前核实购买情况”一节。
https://developer.android.com/google/play/billing/security#verify
只有在通过后端服务器验证购买是合法的之后,您才应该解锁内容。
您可以实现onPurchasesUpdated,以获得在应用程序内部或外部启动的采购更新通知。
如果您不承认购买,购买将自动退还。您可以实现onAcknowledgePurchaseResponse,以接收对购买操作的确认已经完成的通知。
但是要知道这是否是合法的购买,您必须在授予权利之前验证该购买是合法的。
--应该在后端处理的敏感数据和逻辑的特例是购买验证。用户购买后,应执行以下操作:
因此,只有在完成这三项工作之后,您才应该解锁内容。
当你做了这三件事,这是安全的解锁你的购买。如果你不做这三件事,就会有解锁内容的风险,因为购买的内容要么已经退还,要么是非法的。
https://stackoverflow.com/questions/63132411
复制相似问题