javaandroidin-app-billingplay-billing-library

Migrating to Google Play Billing Library Version 5.1.0 - SkuDetails.getOriginalJson() equivalent


I am upgrading from Google Play Billing version 4.0.0 to version 5.1.0. In version 4 SkuDetails has the function getOriginalJson() that contains a json object that I send to my back end. I dug in the API and could not find an official equivalent in version 5.1.0.

There are 2 places where I can see that this information may be available in com.android.billingclient.api.ProductDetails - an interal JSONObject member that we are not supposed to access and as part of the toString() function where this information is appended to parsedJson. I can probably extract what I need from the toString(), but I was wondering if there something better/official.


Solution

  • It is accessible in class Purchase.

    Purchase.getOriginalJson();
    

    Places you can access it:

    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(BillingResult billingResult) {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                QueryPurchasesParams queryPurchasesParams = QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build();
                billingClient.queryPurchasesAsync(queryPurchasesParams, (billingResult1, list) -> runOnUiThread(() -> {
                    for (Purchase purchase: list) {
                        Log.d("originalJson", purchase.getOriginalJson());
                    }
                }));
            }
        }
    
        @Override
        public void onBillingServiceDisconnected() { }
    });
    

    And:

    @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> purchases) {
        runOnUiThread(() -> {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) {
                for (Purchase purchase: purchases) {
                    Log.d("originalJson", purchase.getOriginalJson());
                }
            }
        }
    }
    

    Good luck.