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

Pet*_*ter 33 android in-app-billing kotlin android-billing play-billing-library

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

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

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

Tyl*_*r V 38

根据文档,您可以调用getOneTimePurchaseOfferDetails()ProductDetails 返回一个ProductDetails.OneTimePurchaseOfferDetails对象,该对象具有getFormattedPrice()返回应用内购买价格的方法。

对于订阅,您可以调用getSubscriptionOfferDetails()它返回对象列表ProductDetails.SubscriptionOfferDetails,这些对象具有getPricingPhases()返回不同定价阶段的方法。定价阶段对象有一个getFormattedPrice()获取价格的方法。

更新

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

类似的配置返回的结果ProductDetails将如下所示 - 您有一个productId具有多个付款率/计划的单一配置。offerId如果您添加的促销活动(例如第一年的折扣)在现有基本计划 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
}
Run Code Online (Sandbox Code Playgroud)

谷歌还提供了有关新格式的详细信息。

  • ...为什么他们要把一个完美而简单的方法变成如此不必要的复杂? (37认同)
  • @Big_Chair,不是开玩笑,版本 4 中的 API 已经非常复杂,他们决定添加一个新的复杂层。 (2认同)

小智 15

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

subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(0)?.formattedPrice
Run Code Online (Sandbox Code Playgroud)

  • 它有效,但是......天哪,我不敢相信这是我们为了得到价格而必须写的东西......!??? (9认同)
  • 当订阅基础计划有多个优惠时,此操作将会失败。subscriptionOfferDetails?.get(0)? -> 据我所知,不能保证基本计划位于位置 0 (6认同)