BillingFlowParams.Builder setSkuDetails for testing static Google Play Billing responses

Ping picture Ping · Oct 27, 2018 · Viewed 8k times · Source

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);

Answer

Renkuei picture Renkuei · Oct 28, 2018

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