首页 > 解决方案 > 我们应该在购买成功后解锁应用内商品(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);
}

在成功的情况下,我们应该解锁应用内购买项目,购买成功还是确认成功?

标签: androidin-app-billingandroid-billing

解决方案


购买后无法购买 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)
        }

推荐阅读