Rc Purchase Flow

RevenueCat/ai-toolkit/revenuecat-play-billing/skills/rc-purchase-flow

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

Use this skill when implementing a RevenueCat purchase on Android. Covers awaitPurchase, PurchasesTransactionException, reading CustomerInfo entitlements to grant access, restoring purchases, and the six SDK managed steps that happen inside a single call.

AI 產生的概覽

指導在 Android 上實作 RevenueCat 應用程式內購買,涵蓋 awaitPurchase、權益與還原購買。

功能
此技能為在 Android 應用程式中實作 RevenueCat 購買流程提供逐步指引。它說明 awaitPurchase 內部執行的動作、如何準備 Package 或 SubscriptionOption、如何讀取 CustomerInfo 權益以控管存取,以及如何處理 PurchasesTransactionException。它也涵蓋還原購買,以及購買與還原呼叫所擲回例外類型的差異。
適用情境
適用於在使用 RevenueCat SDK 的 Android 應用程式中新增或檢閱應用程式內購買與訂閱邏輯。適合涉及 awaitPurchase、購買錯誤處理、權益檢查或還原流程的工作。
執行需求
需要一個使用 RevenueCat SDK 的 Android 專案,以及已設定產品與權益的 RevenueCat 帳戶。不隨附指令碼,僅為指示說明。

Purchase Flow

A RevenueCat purchase on Android is two lines of code. The SDK handles the billing params, the Play sheet, the purchase token round trip, server side verification, and acknowledgment. You decide which package to pass in and what to do with the returned entitlements.

Phase 1: Understand what awaitPurchase() does

One call to Purchases.sharedInstance.awaitPurchase(params) runs six steps under the hood. You do not write any of this.

StepWhat happens
1Builds BillingFlowParams with the correct ProductDetailsParams from your Package
2Calls BillingClient.launchBillingFlow() against the activity you passed
3Suspends until the PurchasesUpdatedListener result arrives
4If OK, posts the Play purchase token to the RevenueCat backend
5Backend verifies via purchases.subscriptionsv2.get or purchases.products.get
6SDK acknowledges (subs or non-consumables) or consumes (consumables) within the 3 day Google window

The call returns PurchaseResult(storeTransaction, customerInfo). Retriable failures are retried automatically.

See the Purchase Flow chapter on revenuecat.com for the six-step diagram and the full picture.

Phase 2: Prepare the package

You need a Package from offerings before you can purchase. Fetch offerings, let your UI pick one, and hold the activity.

kotlin
val offerings = Purchases.sharedInstance.awaitOfferings()val pkg = offerings.current?.monthly ?: return

If you need a specific offer instead of the default, resolve a SubscriptionOption and pass that to PurchaseParams.Builder instead of the package.

kotlin
val option = pkg.product.subscriptionOptions    ?.firstOrNull { it.tags.contains("promo_50_off") }    ?: pkg.product.defaultOption    ?: return

For EU personalized pricing, chain .isPersonalizedPrice(true) on the builder so Play shows the customized price notice.

Phase 3: Execute the purchase

Build the params, await the purchase, read customerInfo to gate access, and handle the two expected error branches.

kotlin
try {    val result = Purchases.sharedInstance.awaitPurchase(        PurchaseParams.Builder(activity, pkg).build()    )    val customerInfo = result.customerInfo    if (customerInfo.entitlements["pro"]?.isActive == true) {        navigateToApp()    }} catch (e: PurchasesTransactionException) {    when {        e.userCancelled -> { /* backed out, do nothing */ }        e.error.code == PurchasesErrorCode.ProductAlreadyPurchasedError ->            showMessage("You already have this subscription")        else -> showError(e.error.message)    }}

Rules for this block:

  • PurchaseResult is @Poko. Access fields as result.customerInfo and result.storeTransaction. Do not destructure with val (transaction, customerInfo) = result.
  • customerInfo.entitlements["<id>"]?.isActive == true is the gate. Do not check storeTransaction to decide access.
  • PurchasesTransactionException is the only exception type thrown by awaitPurchase. Catch it, branch on userCancelled first, then on e.error.code.
  • User cancellation is not an error to surface. Swallow it.

If you prefer callbacks over coroutines, purchaseWith(params, onError, onSuccess) is the equivalent entry point.

Phase 4: Inspect StoreTransaction only if you need it

result.storeTransaction is available but usually unused. customerInfo is the source of truth for entitlements. If you need transaction level data for logging or your own backend:

FieldTypeNotes
orderIdString?Null for restored purchases
purchaseTokenStringRaw Play purchase token
productIdsList<String>Product IDs in the transaction
purchaseTimeLongEpoch millis
typeProductTypeSUBS or INAPP

Phase 5: Restore on reinstall or device switch

Restore runs queryPurchasesAsync(), posts everything found to RevenueCat, and returns the fresh CustomerInfo. Gate access the same way you do after a purchase.

kotlin
try {    val customerInfo = Purchases.sharedInstance.awaitRestore()    if (customerInfo.entitlements["pro"]?.isActive == true) {        navigateToApp()    } else {        showMessage("No active purchases found")    }} catch (e: PurchasesException) {    showError(e.error.message)}

Note the exception type. awaitRestore() throws PurchasesException, not PurchasesTransactionException. There is no userCancelled flag to check because no billing sheet is shown.

Exception types at a glance

CallThrown exceptionHas userCancelled?
awaitPurchase(params)PurchasesTransactionExceptionYes
awaitRestore()PurchasesExceptionNo

References

來源與署名

來源:RevenueCat/ai-toolkit位於revenuecat-play-billing/skills/rc-purchase-flow提交ccfc038

授權條款: Apache-2.0; see LICENSE

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架