Rc Plan Changes

RevenueCat/ai-toolkit/revenuecat-play-billing/skills/rc-plan-changes

作者 RevenueCatccfc038185ec457b2ac9077f3bbf1ba947a44dbbApache-2.0; see LICENSE收录于 2026年10月9日更新于 2026年10月9日

Use this skill when implementing subscription upgrades, downgrades, and plan switches via RevenueCat on Android. Covers GoogleProductChangeInfo, picking a Google replacement mode, and letting the SDK manage linkedPurchaseToken chaining.

AI 生成的概览

指导 Android 开发者通过 RevenueCat 实现订阅升级、降级和套餐切换。

功能
该技能引导代理使用 RevenueCat Android SDK 将用户的 Google Play 订阅切换到其他 SKU。内容涵盖前置条件、选择 Google 替换模式(如 WITH_TIME_PRORATION 或 DEFERRED)、使用 GoogleProductChangeInfo 构建 PurchaseParams 请求,以及通过 CustomerInfo 权益验证结果。它还列出常见错误,包括硬编码产品 ID 和自行编写 linkedPurchaseToken 链式处理代码。
适用场景
当用户已有有效的 Google Play 订阅并需要切换到其他套餐时使用,包括升级、降级、交叉升级或试用转换。不适用于首次购买或没有有效订阅的用户。
运行要求
需要 RevenueCat Android SDK,且类路径中包含 Google Play Billing Library 7 或更高版本,还需要 Android 应用上下文以及对 CustomerInfo 和 offerings 的访问权限。不包含脚本,仅为说明性指令。

Plan Changes on Android with RevenueCat

You use this skill when a user already has an active Google Play subscription and you need to move them to a different SKU (upgrade, downgrade, cross-grade, or trial conversion). RevenueCat exposes Google's replacement modes through a single PurchaseParams builder and resolves the linkedPurchaseToken chain server side so you do not write token chaining code.

Phase 1: Preconditions

Confirm the following before invoking a plan change:

  • The user has exactly one active Google Play subscription you intend to replace.
  • You have a fresh CustomerInfo from Purchases.sharedInstance.awaitCustomerInfo() or a cached value from a recent listener callback.
  • You have the target Package resolved from offerings.current (see fetch-offerings skill).
  • Google Play Billing Library 7+ is on the classpath via the RevenueCat SDK.

Skip this skill if the user has no active subscription. For a fresh purchase, use make-purchase instead.

Phase 2: Plan (pick a replacement mode)

GoogleReplacementMode maps one to one onto Google's billing modes. Pick based on the user intent:

ScenarioModeBilling effect
Standard upgrade (monthly to annual)WITH_TIME_PRORATIONImmediate switch, remaining time credited
Upgrade, keep the existing billing dateCHARGE_PRORATED_PRICEImmediate switch, prorated charge now
Switch to or from a prepaid planCHARGE_FULL_PRICEImmediate switch, full charge now
Upgrade during an active free trialCHARGE_PRORATED_PRICEImmediate switch, prorated charge now
Downgrade (annual to monthly)DEFERREDSwitch applies at next renewal

Do not default to WITHOUT_PRORATION for trial upgrades. WITHOUT_PRORATION applies the new plan immediately but charges nothing until the next renewal, which gives the user free premium access they did not pay for. Use CHARGE_PRORATED_PRICE to charge the upgrade price on the spot.

If you set no mode, PurchaseParams defaults to WITHOUT_PRORATION. Set the mode explicitly every time.

DEFERRED is valid only for downgrades. Google rejects deferred upgrades.

Phase 3: Execute

Derive currentProductId from CustomerInfo. Hardcoded SKUs break when a user has migrated between plans.

kotlin
val customerInfo = Purchases.sharedInstance.awaitCustomerInfo()
// activeSubscriptions entries are "productId:basePlanId", strip the base plan suffixval currentProductId = customerInfo.activeSubscriptions    .firstOrNull()    ?.substringBefore(":")    ?: return  // nothing active, route to make-purchase instead
val newPackage = offerings.current    ?.availablePackages    ?.firstOrNull { it.identifier == "premium_annual_package" }    ?: return
val params = PurchaseParams.Builder(activity, newPackage)    .googleProductChangeInfo(        GoogleProductChangeInfo(            oldProductId = currentProductId,            replacementMode = GoogleReplacementMode.WITH_TIME_PRORATION,        )    )    .build()
try {    val result = Purchases.sharedInstance.awaitPurchase(params)    // result.customerInfo reflects the new subscription} catch (e: PurchasesTransactionException) {    if (!e.userCancelled) showError(e.error.message)}

Notes on oldProductId:

  • Pass the subscription product ID only. If you pass "basic_monthly:monthly_plan", the SDK strips :monthly_plan for you, but the intent is clearer when you slice it yourself.
  • CustomerInfo.activeSubscriptions uses the productId:basePlanId shape. substringBefore(":") gives you the correct value.

Phase 4: Verify

After the suspending call returns, read the updated CustomerInfo:

  • customerInfo.activeSubscriptions now contains the new productId:basePlanId.
  • customerInfo.entitlements["pro"]?.isActive stays true across the switch; do not gate UI on the SKU string.
  • For DEFERRED mode, activeSubscriptions still reports the old product until the next renewal. RevenueCat tracks the pending switch server side and flips the entitlement after Google sends the renewal RTDN.

You do not write linkedPurchaseToken traversal code. RevenueCat resolves the chain, marks the old token as replaced, and attributes both tokens to the same App User ID. Client code reads entitlements and trusts them.

Common mistakes

MistakeFix
Hardcoding oldProductId as a constantDerive it from customerInfo.activeSubscriptions.firstOrNull()?.substringBefore(":")
Passing "productId:basePlanId" as oldProductIdSlice off the base plan with substringBefore(":")
Using WITHOUT_PRORATION for a trial upgradeUse CHARGE_PRORATED_PRICE so the user is charged now
Using DEFERRED for an upgradeDEFERRED is downgrade only; Google rejects deferred upgrades
Writing backend code to follow linkedPurchaseTokenRevenueCat does this server side, delete the code
Reading the SKU to decide UI stateRead entitlements[...]?.isActive instead

References

来源与署名

来源:RevenueCat/ai-toolkit位于revenuecat-play-billing/skills/rc-plan-changes提交ccfc038

许可证: Apache-2.0; see LICENSE

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架