You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Kotlin中使用Billing Library 5.0获取订阅入门优惠详情遇问题

解决Billing Library 5.0无法获取订阅入门优惠详情的问题

问题描述

使用Billing Library 5.0调用queryProductDetails接口时,仅能获取到订阅的基础方案,无法查看入门优惠或免费试用的详情,但官方文档说明该API应返回用户符合购买条件的所有基础方案和优惠。

解决方案

1. 检查Google Play后台的优惠配置

  • 确认入门优惠/免费试用已正确创建并关联到目标订阅产品,且状态为激活(非草稿、未过期)
  • 检查优惠的资格规则:比如是否设置了「仅新用户可用」,而当前测试账号已购买过该订阅;是否有地区限制导致测试账号不符合条件
  • 验证优惠的计费阶段配置:确保优惠的计费周期、价格等参数已正确设置

2. 验证Billing Client初始化与查询逻辑

  • 确保Billing Client初始化流程正确,已成功建立连接:
    val billingClient = BillingClient.newBuilder(context)
        .setListener(purchaseUpdateListener)
        .enablePendingPurchases()
        .build()
    
    billingClient.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(result: BillingResult) {
            if (result.responseCode == BillingClient.BillingResponseCode.OK) {
                // 连接成功后再执行queryProductDetails
                querySubscriptions()
            }
        }
    
        override fun onBillingServiceDisconnected() {
            // 处理断开连接逻辑
        }
    })
    
  • 查询时先判断响应码是否正常,避免空指针风险:
    val productDetailsResult = withContext(Dispatchers.IO) {
        billingClient.queryProductDetails(params.build())
    }
    
    if (productDetailsResult.responseCode != BillingClient.BillingResponseCode.OK) {
        // 处理查询失败逻辑,比如打印错误码
        return@withContext
    }
    

3. 修正优惠详情的获取逻辑

当前代码通过匹配billingPeriod查找优惠,逻辑存在缺陷:入门优惠的首个计费周期可能与基础方案不同(比如免费试用7天为P7D),且优惠阶段价格通常为0或折扣价。正确的做法是遍历所有subscriptionOfferDetails,根据计费阶段特征区分优惠:

productDetailsResult.productDetailsList?.forEach { productDetails ->
    productDetails.subscriptionOfferDetails?.forEach { offerDetails ->
        val firstPricingPhase = offerDetails.pricingPhases.pricingPhaseList.firstOrNull() ?: return@forEach
        
        // 区分基础方案与优惠方案
        when {
            // 免费试用:首个阶段价格为0
            firstPricingPhase.priceAmountMicros == 0L -> {
                val offerToken = offerDetails.offerToken
                println("找到免费试用优惠,token: $offerToken,周期: ${firstPricingPhase.billingPeriod}")
                // 可将该token用于后续发起购买
            }
            // 入门折扣:首个阶段价格低于基础方案价格
            firstPricingPhase.priceAmountMicros < getBaseSubscriptionPrice(productDetails) -> {
                val offerToken = offerDetails.offerToken
                println("找到入门折扣优惠,token: $offerToken,折扣价: ${firstPricingPhase.priceAmountMicros}")
            }
            else -> {
                // 基础方案
                println("基础方案,token: ${offerDetails.offerToken}")
            }
        }
    }
}

// 辅助函数:获取基础方案的价格
private fun getBaseSubscriptionPrice(productDetails: ProductDetails): Long {
    return productDetails.subscriptionOfferDetails?.firstOrNull {
        // 基础方案的offer类型为BASE_PLAN(Billing Library 5.0+支持)
        it.offerType == SubscriptionOfferDetails.OFFER_TYPE_BASE_PLAN
    }?.pricingPhases?.pricingPhaseList?.firstOrNull()?.priceAmountMicros ?: 0L
}

4. 测试环境注意事项

  • 使用已添加到Google Play Console「测试管理」中的测试账号,且账号未购买过目标订阅(若优惠仅面向新用户)
  • 可启用静态测试响应:初始化Billing Client时添加测试ID,强制返回配置的测试优惠:
    val billingClient = BillingClient.newBuilder(context)
        .setListener(purchaseUpdateListener)
        .enablePendingPurchases()
        .setTestIds(listOf("your_subscription_product_id")) // 替换为你的订阅ID
        .build()
    

内容的提问来源于stack exchange,提问作者Checkmate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 00:15:17