后端与接口
在 Android 上用 RevenueCat 展示取消…
当需要在 Android 应用中展示订阅的取消、暂停、账单问题和挽留状态时,通过 RevenueCat 的 CustomerInfo 和 REST API 获取并处理这些状态。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
在 Android 上用 Revenue特定
解决思路
- 确定需要向用户展示的状态:已取消但仍可访问(entitlement.unsubscribeDetectedAt != null 且 isActive == true,显示“ends on [expirationDate]”)、账单问题(entitlement.billingIssuesDetectedAt != null,表示支付失败正在恢复)、已暂停(entitlement.isActive == false 但产品仍被拥有,恢复日期需通过 REST API 获取)、活跃且续订(entitlement.willRenew == true,正常状态)。
- 后端挽留活动细分使用 CANCELLATION webhook 的 cancel_reason 字段(UNSUBSCRIBE、BILLING_ERROR、DEVELOPER_INITIATED、PRICE_INCREASE)。
- 确认已通过 Purchases.sharedInstance.getCustomerInfo(...) 或监听器获取到 CustomerInfo,无需调用 BillingClient.queryPurchasesAsync 并设置 setIncludeSuspendedSubscriptions(true),RevenueCat 会从服务端订阅状态解析暂停状态。
- managementURL 已经是 Uri? 类型,直接传给 Intent,不要调用 Uri.parse(url.toString())。
- 不要手动根据 expirationDate 计算访问权限,应读取 isActive 和 willRenew。
- 暂停恢复日期不在 SDK 中,需在后端从 /v1/subscribers/{app_user_id} 获取。
- 检测取消但仍有访问权限:读取 customerInfo.entitlements["pro_access"],若 unsubscribeDetectedAt != null 且 isActive 为 true,则用 expirationDate 显示取消横幅;unsubscribeDetectedAt 在 RevenueCat 收到 SUBSCRIPTION_CANCELED RTDN 时设置,isActive 在计费周期结束前保持 true。
- 检测暂停:读取 entitlement.isActive,暂停时 isActive == false,暂停恢复日期不在 SDK 中。
- 打开订阅管理界面:使用 customerInfo.managementURL?.let { url -> startActivity(Intent(Intent.ACTION_VIEW, url)) },managementURL 类型为 Uri?,直接使用,不要用 Uri.parse(url.toString()) 包装。
- 通过 REST 查询暂停恢复日期:在后端调用 curl -H "Authorization: Bearer $RC_SECRET_API_KEY" https://api.revenuecat.com/v1/subscribers/$APP_USER_ID,订阅对象在暂停时包含 paused_expiration_time_ms,通过自己的端点暴露给客户端。
- 到期前重新订阅:Google 的到期前重新订阅流程会触发 SUBSCRIPTION_RESTARTED,RevenueCat 会清除 unsubscribeDetectedAt 并设置 willRenew = true,无需额外代码,只需重新读取 customerInfo。
- 到期后重新订阅:到期后重新订阅会触发 RENEWAL webhook 而非 INITIAL_PURCHASE,后端处理程序需在两个事件上都授予权益访问。
- 验证:在 Play Store 取消后拉取最新 CustomerInfo,确认 unsubscribeDetectedAt != null 且 isActive == true;点击管理链接确认打开正确的 Play 订阅界面;在 Play Store 沙盒中暂停,确认 entitlement.isActive == false;调用 /v1/subscribers/{id} 确认 paused_expiration_time_ms 存在;接受 Play 配置的挽留优惠,确认购买在 CustomerInfo 中显示为普通订阅。
- 延期和撤销通过后端使用 Google Play Developer API(purchases.subscriptionsv2.defer、purchases.subscriptionsv2.revoke)完成,RevenueCat 处理生成的 RTDN 并自动更新 CustomerInfo。
- 挽留活动无需 SDK 代码,在 Play Console 或 RevenueCat 仪表板中配置。
- RevenueCat 目前不发送专门的暂停 webhook,暂停状态在 Google Play RTDN 处理后出现在 CustomerInfo 中。
适用边界 · 注意事项
- 不适用于非 Android 平台或未使用 RevenueCat 的场景。
- 不适用于需要直接使用 Google Play Billing 库而非 RevenueCat 的情况。
- 不适用于无法访问后端或无法使用 RevenueCat 密钥 API 的场景(暂停恢复日期查询需要后端)。