首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在购买成功(onPurchasesUpdated)之后,还是在确认成功(onAcknowledgePurchaseResponse)之后,我们应该解锁应用程序中的项目?

在购买成功(onPurchasesUpdated)之后,还是在确认成功(onAcknowledgePurchaseResponse)之后,我们应该解锁应用程序中的项目?
EN

Stack Overflow用户
提问于 2020-07-28 10:50:38
回答 2查看 533关注 0票数 1

自从在计费库2和3,有一个额外的acknowledgement步骤,我们需要执行,在采购是成功的。

当购买成功时,将触发以下回呼.

代码语言:javascript
复制
public interface PurchasesUpdatedListener {
    void onPurchasesUpdated(BillingResult billingResult, java.util.List<com.android.billingclient.api.Purchase> list);
}

当确认成功时,将触发以下回调。

代码语言:javascript
复制
public interface AcknowledgePurchaseResponseListener {
    void onAcknowledgePurchaseResponse(BillingResult billingResult);
}

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

EN

回答 2

Stack Overflow用户

发布于 2020-09-04 08:34:32

InApp产品在购买后不能购买。我们需要在一次成功的购买之后使用它,这样我们就可以再次购买,并且它将在下次我们购买之前购买的相同的产品时使用。

您必须确认所有非可消费的购买,即必须确认订阅。这是一个参考代码或者你

代码语言:javascript
复制
            /**
             * 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))
                        }
                    }
                }
            }

对于耗材来说,这应该可以做到

代码语言:javascript
复制
 /**
     * 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)
        }
票数 2
EN

Stack Overflow用户

发布于 2020-09-04 05:09:46

请查阅“发放应享权利前核实购买情况”一节。

https://developer.android.com/google/play/billing/security#verify

只有在通过后端服务器验证购买是合法的之后,您才应该解锁内容。

您可以实现onPurchasesUpdated,以获得在应用程序内部或外部启动的采购更新通知。

如果您不承认购买,购买将自动退还。您可以实现onAcknowledgePurchaseResponse,以接收对购买操作的确认已经完成的通知。

但是要知道这是否是合法的购买,您必须在授予权利之前验证该购买是合法的。

--应该在后端处理的敏感数据和逻辑的特例是购买验证。用户购买后,应执行以下操作:

  • 将相应的purchaseToken发送到后端。这意味着您应该维护所有purchases.
  • Verify的所有purchaseToken值的记录,而当前购买的purchaseToken值与以前的purchaseToken值不匹配。legitimate.
  • If是全局唯一的,因此您可以安全地使用该值作为数据库中的主键。
  • 使用Google API中的Purchases.products:get或Purchases.subscriptions:get端点与谷歌验证购买是合法的,并且在过去没有使用过,然后,当linkedPurchaseToken在Purchases.subscriptions:get中设置时,您可以安全地授予在应用程序项目或subscription.
  • For订阅的权限。您还应该从数据库中删除linkedPurchaseToken,并撤销授予linkedPurchaseToken的权限,以确保多个用户无权进行相同的购买。

因此,只有在完成这三项工作之后,您才应该解锁内容。

  • onPurchaseUpdated,,因为最初知道购买是complete.
  • onAcknowledgePurchaseResponse,,所以您知道确认已经完成,并且购买不会被自动退还
  • ,您需要通过后端服务器验证购买是否合法。

当你做了这三件事,这是安全的解锁你的购买。如果你不做这三件事,就会有解锁内容的风险,因为购买的内容要么已经退还,要么是非法的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63132411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档