8

In v4, I used to have SkuDetails.price to get the price of the product but now it's not available anymore in the new ProductDetails in v5.

How can I get the price of a product in this new version?

Jay N
  • 348
  • 3
  • 11
  • Does this answer your question? [Getting the ProductDetails price in android-billing-5.0](https://stackoverflow.com/questions/72235984/getting-the-productdetails-price-in-android-billing-5-0) – Tyler V Jul 16 '22 at 01:37

3 Answers3

9

When you call getSubscriptionOfferDetails, it returns the offers available to buy for the subscription product. Then you can call getPricingPhases() to get the list of the pricing phases. Each pricing phase object has a getFormattedPrice() call to get the price of an offer's pricing phrase (https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.PricingPhase)

misobean
  • 106
  • 1
  • Is it only for subscriptions? What about in-app products? – Jay N Jun 23 '22 at 00:13
  • 6
    There's a `OnetimePurchaseOfferDetails` object that has a `getFormattedPrice()` call. https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.OneTimePurchaseOfferDetails – misobean Jun 23 '22 at 20:40
3

You have to check for Available Products

fun getAvailableProducts() {
    Timber.d("!!! Getting available products to buy ...")

    val queryProductDetailsParams =
        QueryProductDetailsParams.newBuilder()
            .setProductList(
                listOf(
                    QueryProductDetailsParams.Product.newBuilder()
                        .setProductId(SKU_SUBSCRIBE_MONTHLY)
                        .setProductType(BillingClient.ProductType.SUBS)
                        .build(),
                    QueryProductDetailsParams.Product.newBuilder()
                        .setProductId(SKU_SUBSCRIBE_YEARLY)
                        .setProductType(BillingClient.ProductType.SUBS)
                        .build()
                ))
            .build()

    billingClient.queryProductDetailsAsync(queryProductDetailsParams) {
            billingResult,
            productDetailsList ->
        if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
            availableProducts.tryEmit(productDetailsList)
            getPrices(productDetailsList)
        } else {
            Timber.d("!!!Error getting available Products to buy: ${billingResult.responseCode} ${billingResult.debugMessage}")
        }
    }
}

And then

    private fun getPrices(productDetailsList: MutableList<ProductDetails>) {
        productDetailsList.forEach{
            when (it.productId) {
                SKU_SUBSCRIBE_MONTHLY -> {
                    currency.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.priceCurrencyCode.toString())
                    monthlyPrice.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.formattedPrice.toString())
                    Timber.d("!!!! $it.")
                }
                SKU_SUBSCRIBE_YEARLY -> {
//                    currency.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.priceCurrencyCode.toString())
                    yearlyPrice.tryEmit(it.subscriptionOfferDetails?.get(0)?.pricingPhases!!.pricingPhaseList[0]?.formattedPrice.toString())
                    Timber.d("!!!! $it.")
                }
            }
        }
    }
polis
  • 737
  • 7
  • 6
0

i use the following code to get price details.

private void queryProduct() {

    QueryProductDetailsParams queryProductDetailsParams
            = QueryProductDetailsParams.newBuilder().setProductList(
                    ImmutableList.of(QueryProductDetailsParams.Product.newBuilder()
                            .setProductId("your_product_id")
                            .setProductType(BillingClient.ProductType.INAPP).build()))
            .build();

    billingClient.queryProductDetailsAsync(
            queryProductDetailsParams,

            new ProductDetailsResponseListener() {
                @Override
                public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List<ProductDetails> list) {
                    if(!list.isEmpty()){

                        productDetails = list.get(0);
                        itemdesc.setText(productDetails.getName());
                        itemprice.setText(productDetails.getOneTimePurchaseOfferDetails().getFormattedPrice());

                        itemprice.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                makePurchase();
                            }
                        });

                    }else {

                        Log.i("playsresponse", "no response from google play");
                    }
                }
            }
    );
Vivek
  • 115
  • 2
  • 13
  • Please make sure you add explanation to and around your code to make it useful for the OP and future readers as to they *why* you'd do this – Can O' Spam Jul 21 '22 at 12:15
  • That won't work for Subscriptions prices, only "INAPP" purchases. "Attempt to invoke virtual method 'java.lang.String com.android.billingclient.api.ProductDetails$OneTimePurchaseOfferDetails.getFormattedPrice()' on a null object reference" – Bruno Oct 18 '22 at 04:54