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

如何在React Native IAP Android中兑换订阅促销码?

Android 订阅促销码兑换实现方案

前提准备

确保你的React Native IAP库使用最新稳定版,且已完成Google Play Billing基础配置(如AndroidManifest.xml添加com.android.vending.BILLING权限、BillingClient初始化等)。

核心实现代码

促销码兑换无需依赖SKU或offerToken,直接调用专属接口即可:

// 先确保BillingClient已建立连接
await IAP.initConnection();

try {
  // 传入用户输入的促销码执行兑换
  const redeemResult = await IAP.redeemPromoCode('user_input_promo_code');
  console.log('促销码兑换成功:', redeemResult);
  
  // 兑换成功后同步订阅状态
  const latestPurchases = await IAP.getAvailablePurchases();
  // 此处更新本地订阅数据
} catch (error) {
  // 根据错误类型给出用户提示,比如无效码、已过期、已使用等
  console.error('促销码兑换失败:', error.message);
}

关键注意事项

  • 错误码处理:兑换失败会返回对应错误标识,如ERROR_INVALID_PROMO_CODE、ERROR_PROMO_CODE_ALREADY_REDEEMED,需针对性处理用户提示。
  • 状态同步:兑换成功后必须重新查询订阅状态,保证本地数据与Google Play一致。
  • 版本兼容:React Native IAP v6及以上版本才支持促销码兑换功能,若版本过低需升级。

原生层自定义实现(可选)

若React Native封装无法满足需求,可直接在Android原生代码中调用Google Play Billing Library:

val billingClient = BillingClient.newBuilder(context)
    .setListener(purchasesUpdatedListener)
    .enablePendingPurchases()
    .build()

billingClient.startConnection(object : BillingClientStateListener {
    override fun onBillingSetupFinished(result: BillingResult) {
        if (result.responseCode == BillingClient.BillingResponseCode.OK) {
            val params = RedeemOfferCodeParams.newBuilder()
                .setOfferCode("user_input_promo_code")
                .build()
            billingClient.redeemOfferCode(params) { billingResult ->
                if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                    // 兑换成功逻辑
                } else {
                    // 兑换失败错误处理
                }
            }
        }
    }

    override fun onBillingServiceDisconnected() {
        // 实现服务断开后的重连逻辑
    }
})

内容的提问来源于stack exchange,提问作者Muhammad Saad Ullah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:14:58