9

I have

BillingFlowParams purchaseParams = BillingFlowParams.newBuilder().setSku(skuId).setType(billingType).setOldSkus(oldSkus).build();

but now

setSku

setType

setOldSkus

are all deprecated.

I want to update the old code without releasing an update that mess with the active and future subscriptions. How should I properly update the above code?

Currently I use a String myProduct="my_newsweek_1"; to identify the purchase and BillingClient.SkuType.SUBS to identify the type, and I simply pass null to setOldSkus

Documentation reports that

setSku (String sku) and setType (String type) have been replaced with setSkuDetails(SkuDetails) (this SkuDetails object receives only a String as parameter in the constructor and throws JSONException so seems It doesn't work with old String constants)

and

setOldSkus(ArrayList<String> oldSkus) has been replaced with setOldSku(String)

AndreaF
  • 11,975
  • 27
  • 102
  • 168

2 Answers2

1

You need BillingFlowParams for launchBillingFlow(). You can create your SkuDetails with your own json string but it is not the intended way. You should at first call querySkuDetailsAsync() and get necessary skuDetailsList and then use them for launchBillingFlow()


public void querySkuDetailsAsync(@SkuType final String itemType, final List<String> skuList, final SkuDetailsResponseListener listener) {
    Runnable queryRequest = new Runnable() {
        @Override
        public void run() {
            // Query the purchase async
            SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
            params.setSkusList(skuList).setType(itemType);
            mBillingClient.querySkuDetailsAsync(params.build(),
                    new SkuDetailsResponseListener() {
                        @Override
                        public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
                            //use skuDetails in skuDetailsList
                        }
                    });
            }
        };
    executeServiceRequest(queryRequest);
}

public void initiatePurchaseFlow(final SkuDetails skuDetails) {
   Runnable purchaseFlowRequest = new Runnable() {
      @Override
      public void run() {
      Log.d(TAG, "Launching in-app purchase flow.");
      BillingFlowParams purchaseParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build();
         mBillingClient.launchBillingFlow(mActivity, purchaseParams);
      }
   };
   executeServiceRequest(purchaseFlowRequest);
}
underoid
  • 429
  • 4
  • 11
  • This isn't what I have asked. I know that I need `launchBillingFlow`, what I don't know is how to convert my sku parameters in a format suitable for the new versions of the deprecated methods – AndreaF Feb 18 '19 at 19:00
  • 2
    There is only one new version of deprecated methods and it is setSkuDetails(), so you need to pass SkuDetails object. And you get it by querySkuDetailsAsync(). – underoid Feb 18 '19 at 19:04
  • I don't know how to convert my String constants in a `SkuDetails object`, in order to use `setSkuDetails`, for this reason I have asked here. Could you help me? – AndreaF Feb 18 '19 at 19:20
  • 1
    As I wrote in the answer you can at first convert your constants to json and create `new SkyDetails(jsonString)` but it is **NOT** the correct solution. Google wants you to get those details by `querySkuDetailsAsync()` that's why those methods are deprecated. – underoid Feb 18 '19 at 19:27
  • @underoid is right. The idea is to write a function which first requests all your SKUs from google play with `querySkuDetailsAsync` and then you get a list of all your `SkuDetails`. Save one of the SkuDetails from the list you need, for example in mySkuDetail. Then call `BillingFlowParams.newBuilder().setSkuDetails(mySkuDetail).build();`. That's it. No need to create a json. The constants are not even defined by google to prevent users from creating their own custom jsons. – B Porr Feb 19 '19 at 10:32
  • This has been outlined here in the google developer blog: https://developer.android.google.cn/google/play/billing/billing_library_releases_notes?hl=fr#set-sku – B Porr Feb 26 '19 at 08:45
  • I cannot understand why you need 10 lines of code just to provide a param. How flawed can a language or library be. Why not just keeping it a simple string. Annoying builders everywhere doesnt make sense – Gillis Haasnoot Mar 15 '22 at 12:14
1

Replace your code to below for newer billing lib version

  mBillingFlowParams = BillingFlowParams.newBuilder()
                            .setSkuDetails(skuDetailsList.get(0))
                            .setSubscriptionUpdateParams(BillingFlowParams.SubscriptionUpdateParams.newBuilder()
                                .setOldSkuPurchaseToken(oldPurchaseToken)
                                .setReplaceSkusProrationMode(BillingFlowParams.ProrationMode.DEFERRED)
                                .build())
                            .build()
                        mBillingClient.launchBillingFlow(context, mBillingFlowParams)
Hardik Hirpara
  • 2,594
  • 20
  • 34