如何在Android中确认应用内购买?

时间:2019-06-13 17:18:33

标签: android in-app-purchase in-app-billing android-inapp-purchase

我已经浏览了Play计费库 https://developer.android.com/google/play/billing/billing_library_overview 您必须在三天内确认所有购买。如果未正确确认购买,将导致这些购买被退款。 该过程没有明确说明如何确认购买。 这就是我尝试过的 这是正确的方法吗? 在此先感谢

@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
    if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK&&purchases!=null){
        Toast.makeText(this, "Purchase Successful", Toast.LENGTH_SHORT).show();
        for(Purchase purchase:purchases){
            handlePurchase(purchase);
        }
    }else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.USER_CANCELED){
        Toast.makeText(this, "Purchase Cancelled", Toast.LENGTH_SHORT).show();
    }else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED){
        Toast.makeText(this, "Already Purchased", Toast.LENGTH_SHORT).show();
    } else{
        Toast.makeText(this, billingResult.getDebugMessage(), Toast.LENGTH_SHORT).show();
    }



//in handlePurchase()

if(!purchase.isAcknowledged()) {           AcknowledgePurchaseParams确认PurchaseParams                     = AcknowledgePurchaseParams.newBuilder()                     .setPurchaseToken(purchase.getPurchaseToken())                     .setDeveloperPayload(purchase.getDeveloperPayload())                     .build();

        client.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
            @Override
            public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
                    Toast.makeText(RemoveAdsActivity.this, "Purchase Acknowledged", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

它提到确认购物已通过link。根据类型,有不同的确认购买方式。

 private BillingClient mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();

//For non-consumables:
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener());

//For Consumables: 
client.consumeAsync(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);

我发布的链接包含有关如何处理订阅的示例。

更新

这是有关如何确认非消耗品购买的更扩展的示例:

首先,创建AcknowledgePurchaseParams参数。为此,您需要能够轻松获得的购买令牌,因为您应该在onPurchasesUpdated方法中或在onPurchasesUpdated之后将购买传递给的另一种方法中调用该令牌:

AcknowledgePurchaseParams acknowledgePurchaseParams =
            AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(purchase.getPurchaseToken())
                    .build();

接下来,创建将用作第二个参数的侦听器。这将使您在确认购买后可以做一些事情。在此示例中,我显示了一条快餐栏消息:

AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
        @Override
        public void onAcknowledgePurchaseResponse(BillingResult billingResult) {              

            getMessage("Purchase acknowledged");               
        }

    };

创建完这些后,请使用BillingClient调用acknowledgePurchase方法:

mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);

应该成功确认购买。

这将acknowledgePurchase用于非消耗性物品。

消费品购买

这只是它们被称为的相似而已-参见上面示例中的解释:

第一个参数-参数-设置:

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

第二个参数-侦听器-设置

    ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
        @Override
        public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
            getMessage("Purchase acknowledged");
        }
    }

现在使用您的BillingClint并使用ConsumerAsync:

mBillingClient.consumeAsync(consumeParams, consumeResponseListener);