BillingFlowParams.Builder setSkuDetails for testing static Google Play Billing responses

夙愿已清 提交于 2019-12-10 12:31:18

问题


I'm testing in-app purchase using the three reserved product IDs for testing static Google Play Billing responses:

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

However, setSku and setType seem to be deprecated in the BillingFlowParams.Builder class. Instead, we should be using setSkuDetails(SkuDetails).

How should I change the BillingFlowParams in the example code to use SkuDetails for the test product IDs?

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

回答1:


you should get SkuDetails from BillingClient.querySkuDetailsAsync, the sample code may seems like this:

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 = billingClient.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.
    }
});

You can also take a look to https://developer.android.com/google/play/billing/billing_library_overview



来源:https://stackoverflow.com/questions/53026541/billingflowparams-builder-setskudetails-for-testing-static-google-play-billing-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!