Google Play Billing v5 - Get a product's price
Asked Answered
S

3

12

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?

Speos answered 19/6, 2022 at 17:31 Comment(1)
Does this answer your question? Getting the ProductDetails price in android-billing-5.0Bugs
M
13

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)

Miss answered 22/6, 2022 at 22:44 Comment(2)
Is it only for subscriptions? What about in-app products?Speos
There's a OnetimePurchaseOfferDetails object that has a getFormattedPrice() call. developer.android.com/reference/com/android/billingclient/api/…Miss
F
4

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.")
                }
            }
        }
    }
Fafnir answered 22/8, 2022 at 19:51 Comment(0)
A
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");
                    }
                }
            }
    );
Adlei answered 21/7, 2022 at 12:12 Comment(2)
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 thisMartinelli
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"Hemline

© 2022 - 2024 — McMap. All rights reserved.