Jay N
Jay N

Reputation: 438

Google Play Billing v5 - Get a product's price

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?

Upvotes: 13

Views: 6045

Answers (3)

polis
polis

Reputation: 1033

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.")
                }
            }
        }
    }

Upvotes: 4

Vivek
Vivek

Reputation: 115

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

Upvotes: 0

misobean
misobean

Reputation: 146

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)

Upvotes: 13

Related Questions