BillingFlowParams.Builder setSkuDetails,用于测试静态Google Play结算响应

时间:2018-10-27 21:45:27

标签: android play-billing-library

我正在使用三个保留的产品ID测试应用内购买,以测试静态Google Play结算responses

  • android.test.purchased
  • android.test.canceled
  • android.test.item_unavailable

但是,setSku class中似乎已弃用setTypeBillingFlowParams.Builder。相反,我们应该使用setSkuDetails(SkuDetails)

如何更改示例代码中的BillingFlowParams以将SkuDetails用作测试产品ID?

BillingFlowParams flowParams = BillingFlowParams.newBuilder()
     .setSku(skuId)
     .setType(SkuType.INAPP) 
     .build();
int responseCode = mBillingClient.launchBillingFlow(flowParams);

2 个答案:

答案 0 :(得分:8)

您应该从BillingClient.querySkuDetailsAsync获取SkuDetails,示例代码可能看起来像这样:

    private BillingClient mBillingClient;

    // ....

    mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
        @Override
        public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
            if (responseCode == BillingClient.BillingResponse.OK
                    && purchases != null) {

                // do something you want

            } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
            } else {
            }
        }
    }).build();


    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
            if (billingResponseCode == BillingClient.BillingResponse.OK) {
                // The billing client is ready. You can query purchases here.

                List<String> skuList = new ArrayList<>();
                skuList.add("android.test.purchased");

                SkuDetailsParams skuDetailsParams = SkuDetailsParams.newBuilder()
                        .setSkusList(skuList).setType(BillingClient.SkuType.INAPP).build();
                mBillingClient.querySkuDetailsAsync(skuDetailsParams,
                        new SkuDetailsResponseListener() {
                            @Override
                            public void onSkuDetailsResponse(int responseCode,
                                                             List<SkuDetails> skuDetailsList) {

                                BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                                        .setSkuDetails(skuDetailsList.get(0))
                                        .build();
                                int billingResponseCode = mBillingClient.launchBillingFlow(SkuActivity.this, flowParams);
                                if (billingResponseCode == BillingClient.BillingResponse.OK) {
                                    // do something you want
                                }
                            }
                        });

            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    });

您也可以看看https://developer.android.com/google/play/billing/billing_library_overview

答案 1 :(得分:0)

我正在发布应用内结算的分步流程,我最近在 Billing Library 4.0 版发布后对其进行了测试

可以找到 Google 帮助资源 here

第一件事:更新您的build.gradle

dependencies {
    def billing_version = "4.0.0"

    implementation "com.android.billingclient:billing:$billing_version"
}

现在是编码部分:BillingActivity.java

private BillingClient mBillingClient;

// ...


submitButton.setOnClickListener(v -> {
    mBillingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(new PurchasesUpdatedListener() {
        @Override
        public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                    && purchases != null) {
                for (Purchase purchase : purchases) {
                    handlePurchase(purchase);
                }
                // Perform your Successful Purchase Task here 
                
                Snackbar.make(findViewById(android.R.id.content), "Great!! Purchase Flow Successful! :)", Snackbar.LENGTH_LONG).show();
                dismiss();
            } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
                // Handle an error caused by a user cancelling the purchase flow.
                Snackbar.make(findViewById(android.R.id.content), "User Cancelled the Purchase Flow!", Snackbar.LENGTH_LONG).show();
            } else {
                // Handle any other error codes.
                Snackbar.make(findViewById(android.R.id.content), "Error! Purchase Task was not performed!", Snackbar.LENGTH_LONG).show();
            }
        }
    }).build();

    mBillingClient.startConnection(new BillingClientStateListener() {

        @Override
        public void onBillingSetupFinished(@NonNull BillingResult billingResult) {

            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                // The BillingClient is ready. You can query purchases here.

                List<String> skuList = new ArrayList<>();
                skuList.add("billing_template_1");
                skuList.add("billing_template_2");
                SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
                mBillingClient.querySkuDetailsAsync(params.build(),
                        new SkuDetailsResponseListener() {
                            @Override
                            public void onSkuDetailsResponse(BillingResult billingResult,
                                                             List<SkuDetails> skuDetailsList) {
                                // Process the result.
                                // Retrieve a value for "skuDetails" by calling querySkuDetailsAsync().
                                BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                        .setSkuDetails(skuDetailsList.get(0))
                                        .build();
                                int responseCode = mBillingClient.launchBillingFlow(activity, billingFlowParams).getResponseCode();
                            }
                        });
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to Google Play by calling the startConnection() method.
            Toast.makeText(BillingActivity.this, "Service Disconnected!", Toast.LENGTH_SHORT).show();
        }
    });
});
        
void handlePurchase(Purchase purchase) {
    ConsumeParams consumeParams =
            ConsumeParams.newBuilder()
                    .setPurchaseToken(purchase.getPurchaseToken())
                    .build();

    ConsumeResponseListener listener = new ConsumeResponseListener() {
        @Override
        public void onConsumeResponse(BillingResult billingResult, @NonNull String purchaseToken) {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                // Handle the success of the consume operation.
            }
        }
    };
    mBillingClient.consumeAsync(consumeParams, listener);
}
    
    
private void dismiss() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        finish();
    }

仅供参考:

  • 这只是@Renkuei 回答的更新版本。
  • 别忘了.enablePendingPurchase()
  • 不要忘记 skuList.add("billing_template_1"),这必须在您的开发者控制台中完成。因此,billing_template_1 是您的Monetise > Products > In-app products > Product ID