Peter
Peter

Reputation: 2802

Getting the ProductDetails price in android-billing-5.0

I have upgraded my Kotlin app to android-billing 5.0 from 4.0, and as such, SkuDetails is now deprecated so I am migrating to using ProductDetails using the migration instructions.

Previously I displayed the purchase price for in-app purchases to the user using SkuDetails.price, but I cannot find a similar method in ProductDetails.

In Google Play Billing Library 5.0, is there a non-deprecated way to retrieve the price for an in-app purchase or subscription?

Upvotes: 39

Views: 12577

Answers (3)

Al Ro
Al Ro

Reputation: 511

In java you can get the price as a string (complete with the appropriate currency sign) thusly:

product.get(i).getOneTimePurchaseOfferDetails().getFormattedPrice()

where product is a List<ProductDetails> from the onProductDetailsResponse listener

(I understand the question was for kotlin, but this is one of the first hits you find when search for this solution so a Java answer is worth including as well).

Upvotes: 5

Tyler V
Tyler V

Reputation: 10940

Based on the documentation you can call getOneTimePurchaseOfferDetails() on ProductDetails to return a ProductDetails.OneTimePurchaseOfferDetails object, which has a getFormattedPrice() method to return the price for in-app purchases.

For subscriptions you can call getSubscriptionOfferDetails() which returns a list of ProductDetails.SubscriptionOfferDetails objects, which have a getPricingPhases() method to return different pricing phases. The pricing phase objects have a getFormattedPrice() method to get the price from.

UPDATE

To better explain what this new approach allows, you can now create multiple "base plans" for a given subscription product. For example, you could create an "unlimited" product, then create an "unlimited-annual" plan for $50/year and an "unlimited-monthly" plan for $5/month.

The ProductDetails returned for a configuration like that would look like this - where you have a single productId with multiple payment rates/plans. If you add a promotion (e.g. discount for the first year) that shows up with a non-null offerId under an existing base plan ID.

{
  productId: "unlimited",

  subscriptionOfferDetails: 
  [
    {
      basePlanId: "unlimited-monthly",
      offerId: null,
      pricingPhases:
      [
        {formattedPrice: "$5", billingPeriod: P1M, recurrence: 1}
      ]
    },

    {
      basePlanId: "unlimited-annual",
      offerId: null,
      pricingPhases:
      [
        {formattedPrice: "$50", billingPeriod: P1Y, recurrence: 1}
      ]
    },

    {
      basePlanId: "unlimited-annual",
      offerId: "unlimited-annual-promotion",
      pricingPhases:
      [
        {formattedPrice: "$30", billingPeriod: P1Y, recurrence: 2}
        {formattedPrice: "$50", billingPeriod: P1Y, recurrence: 1}
      ]
    }
  ],
 
  oneTimePurchaseOfferDetails: null
}

There are also details from Google here about the new format.

Upvotes: 41

MrCreed
MrCreed

Reputation: 230

In Kotlin you can call this on the product

subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(0)?.formattedPrice

Upvotes: 19

Related Questions