在 android-billing-5.0 中获取 ProductDetails 价格

问题描述 投票:0回答:3

我已将我的 Kotlin 应用程序从 4.0 升级到 android-billing 5.0,因此,SkuDetails 现已弃用,因此我正在使用 迁移说明 迁移到使用 ProductDetails。

之前我使用 SkuDetails.price 向用户显示应用内购买的购买价格,但我在 ProductDetails 中找不到类似的方法。

在 Google Play Billing Library 5.0 中,是否有一种未弃用的方法来检索应用内购买或订阅的价格?

android kotlin in-app-billing android-billing play-billing-library
3个回答
21
投票

根据文档,您可以在 ProductDetails 上调用

getOneTimePurchaseOfferDetails()
返回一个
ProductDetails.OneTimePurchaseOfferDetails
对象,它有一个
getFormattedPrice()
方法来返回应用内购买的价格。

对于订阅,您可以调用

getSubscriptionOfferDetails()
返回一个
ProductDetails.SubscriptionOfferDetails
对象的列表,这些对象有一个
getPricingPhases()
方法来返回不同的定价阶段。 定价阶段对象有一个
getFormattedPrice()
方法来获取价格。

更新

为了更好地解释这种新方法允许的内容,您现在可以为给定的订阅产品创建多个“基本计划”。例如,您可以创建一个“无限制”产品,然后创建一个 50 美元/年的“无限制年度”计划和一个 5 美元/月的“无限制月度”计划。

返回的

ProductDetails
看起来像这样的配置 - 您有一个
productId
具有多个支付率/计划。如果您添加的促销活动(例如第一年的折扣)在现有基本计划 ID 下显示为非空
offerId

{
  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
}

Google 这里也有关于新格式的详细信息。


8
投票

在 Kotlin 中,您可以在产品上调用它

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

0
投票

在 java 中,您可以获得字符串形式的价格(带有适当的货币符号):

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

其中 product 是一个 List> 来自 onProductDetailsResponse listener

(我知道这个问题是针对 kotlin 的,但这是您在搜索此解决方案时发现的第一个结果,因此 Java 答案也值得包含)。

© www.soinside.com 2019 - 2024. All rights reserved.