android 应用内结算确认购买问题[使用Google Play应用内结算库]

bd1hkmkf  于 2023-08-01  发布在  Android
关注(0)|答案(2)|浏览(142)

我是Android的新手,第一次尝试实现应用内计费。
我正在使用Google Play应用程序内库。https://developer.android.com/google/play/billing/billing_library_overview
我想实现消耗品应用内购买。我正在使用'android.test. built'保留ID进行测试。我可以加载skuDetails并成功进行购买并成功消费购买
下面是我使用consumeAsync的handlePurchase方法

void handlePurchase(Purchase purchase) {


BillingClient client = BillingClient.newBuilder(NewAdActivity.this)
        .enablePendingPurchases() 
        .setListener(this)
        .build();

if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
     System.out.println("item successfully purchased");

        if (!purchase.isAcknowledged()) {

                    ConsumeParams consumeParams = ConsumeParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .setDeveloperPayload(purchase.getDeveloperPayload())
                        .build();

                     ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
                    @Override
                    public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
                          if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchaseToken != null) {
                              System.out.println("SUCCESSFULLY consumed PURCHASE");
                              providecontent();

                           } 
                        else { 
                              System.out.println("FAILED TO consume:”);
                            }

                    }

                };

                client.consumeAsync(consumeParams, consumeResponseListener); 


        }

    }


}

字符串
当我消费时,它是否也确认购买?我需要在purchase.originalJson中手动设置“acknowledged”:true吗?
我的代码是否正确,可以消费购买的物品?或者我需要包括一个单独的acknowledgePurchase之前消费项目。
请回复。任何帮助都是真正的赞赏。

  • 谢谢-谢谢
eit6fx6z

eit6fx6z1#

对于消耗品,您需要使用consumeAsync()。对于非消费品,您需要使用acknowledgePurchase()。有关在应用中确认购买的更多信息,请查看官方文档:https://developer.android.com/google/play/billing/billing_library_overview#acknowledge

tjrkku2a

tjrkku2a2#

您错误地使用了consumeAsync(),要确认购买,您应该调用acknowledgePurchase()
consumeAsync()删除购买的项目,例如,如果购买允许用户玩10次,他使用了所有这些,那么您将调用consumeAsync()让用户再次购买该项目
举个例子:

if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
            // Grant entitlement to the user.
            boolean signOk = verifyPurchaseSignature(purchase.getOriginalJson(), purchase.getSignature());
            if (!signOk) {
                // Alert the user about wrong signature
                return;
            } else if (!purchase.isAcknowledged()) {
                AcknowledgePurchaseParams acknowledgePurchaseParams =
                        AcknowledgePurchaseParams.newBuilder()
                                .setPurchaseToken(purchase.getPurchaseToken())
                                .build();
                billingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
                    @Override
                    public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                        //Give thanks for the purchase                        

                    }
                });
            }
        }

字符串

相关问题