> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# RC / IAP Monetization — Integration Design

Design + plan for wiring RevenueCat into piano, **re-based onto dev's catalog-from-sheet +
entitlements model** (post `origin/dev` merge). Status: **DESIGN — approved, pending PO inputs.**
Scope: **subscription (premium)** + **No-Ads pack (no_ads)** + **IAP single-song deals**. Date: 2026-06-22.

## Decisions taken (design session)

- **Live prices** — the paywall renders localized prices from `hub.subscription.getOfferings()`, not
  static copy (App Store compliance + currency correctness).
- **Refresh-based entitlement sync** — `hub.subscription` exposes no live customer-info listener, so RC
  entitlements are synced to the app on **first `ensureHubPurchases()` configure** (`getCustomerInfo`) and
  after **purchase/restore** (both return `{ customerInfo, isPremium }`). A live listener can be added later
  for in-session renewal.
- **RC is the source of truth** — `entitlements-store` (`isPremium`, `adsRemoved`) + owned songs are a
  cache/mirror of RC entitlements, not hand-set flags.
- **No-Ads pack** → a real **RC entitlement `no_ads`** (RC-first, mirrors `premium`).
- **IAP songs** → each IAP song carries an App Store **`productId`** sourced from a **new `productId`
  column in the catalog Google Sheet** (per `isIAP` row), value `com.piano.songdeal1/2/3`. 1 product = 1 song.
- **Variant → offering** (confirmed): paywall `premium` / post-onboarding → `default` offering
  (`weekly` + `yearly`); `oneTimeOffer` → `discount` offering (`weekly50` + `yearly50`).

***

## 1. Product catalog (from PO spec)

| Type | Product id            | Price (USD) | Offering   | Entitlement     | Benefit                                              |
| ---- | --------------------- | ----------- | ---------- | --------------- | ---------------------------------------------------- |
| Sub  | `com.piano.weekly`    | 7.99        | `default`  | `premium`       | 3-day free trial · unlock ALL songs · remove ALL ads |
| Sub  | `com.piano.yearly`    | 39.99       | `default`  | `premium`       | same as weekly                                       |
| Sub  | `com.piano.weekly50`  | 3.99        | `discount` | `premium`       | no trial · same benefit                              |
| Sub  | `com.piano.yearly50`  | 19.99       | `discount` | `premium`       | no trial · same benefit                              |
| IAP  | `com.piano.noadspack` | 9.99        | —          | `no_ads`        | remove **interstitial + banner** (KEEP rewarded)     |
| IAP  | `com.piano.songdeal1` | 0.99        | —          | _(song unlock)_ | buy 1 specific song                                  |
| IAP  | `com.piano.songdeal2` | 0.99        | —          | _(song unlock)_ | buy 1 specific song                                  |
| IAP  | `com.piano.songdeal3` | 0.99        | —          | _(song unlock)_ | buy 1 specific song                                  |

**RC returns 4 offerings — verified live on-device 2026-06-22** (all products + localized prices load OK;
RC side is fully ready):

- `default` — `$rc_annual`→`com.piano.yearly` (1.199.000đ) · `$rc_weekly`→`com.piano.weekly` (249.000đ)
- `discount` — `$rc_annual`→`com.piano.yearly50` (599.000đ) · `$rc_weekly`→`com.piano.weekly50` (119.000đ)
- `iap_noads` — `iap_noads`→`com.piano.noadspack` (299.000đ)
- `iap_song` — `song_1`/`song_2`/`song_3` → `com.piano.songdeal1/2/3` (29.000đ each)

## 2. Entitlement model — 3 buckets (mapped to dev's model)

| Bucket          | RC source                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | Synced to                                                                                                      | App effect                                                                                                                                                                                                                                                                               |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **premium**     | entitlement `premium` (any active sub)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | `entitlements-store.isPremium` + `grantPremiumEntitlement()` (library `setSubscription` + `sdk.player.setVip`) | unlock **ALL** songs + remove **ALL** ads                                                                                                                                                                                                                                                |
| **no_ads**      | Lifetime: `allPurchasedProductIdentifiers` ∋ `com.piano.noads.lifetime` (t1) or `com.piano.noadspack` (t2, legacy). Timed 1d / 7d: `nonSubscriptionTransactions` dates, each pass its own length from its purchase (no stacking: the cover ends with the pass that ends last), checked against the later of the device clock and `requestDate` — **product-ownership, no RC entitlement needed**. Table: [`noads-products.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/noads-products.ts), rule: [`noads-expiry.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/noads-expiry.ts) | `entitlements-store.adsRemoved` (a timer + foreground re-check turn it off when a timed pack ends)             | remove **interstitial, FSN, App Open + banner**; **KEEP** rewarded (the ads engine gate is per format: rewarded closes for VIP only, [`entitlement-gate.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/ads/entitlement-gate.ts)) |
| **song unlock** | `…allPurchasedProductIdentifiers` ∋ `com.piano.songdeal{1,2,3}` (offering `iap_song`)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | `unlockSongEntitlement(songId)` (library `acquireSong` + `sdk.player.addUserSong`)                             | unlock 1 specific song                                                                                                                                                                                                                                                                   |

**Lock model already exists** (`packages/catalog/src/lock-rules.ts` `getSongLockState`): VIP/owned →
`PLAYABLE`; else `isPremium`→`LOCKED_PREMIUM`, `isIAP`→`LOCKED_IAP`, `isAdsUnlock`→`LOCKED_ADS`. Premium
entitlement makes `ctx.isVip` true → every song resolves `PLAYABLE`. No lock-rule change needed.

### Ad-gating rules

| Ad type          | Skip when                                                                         |
| ---------------- | --------------------------------------------------------------------------------- |
| **Interstitial** | `premium` **OR** `no_ads`                                                         |
| **Banner**       | `premium` **OR** `no_ads`                                                         |
| **Rewarded**     | `premium` only (no_ads does **not** remove rewarded — ad-unlock songs still work) |

## 3. Architecture — entitlement sync

```
RC (source of truth) — configured lazily via ensureHubPurchases(reason)
  ├─ first configure: getCustomerInfo() ──► syncEntitlementsFromRC(customerInfo)
  ├─ purchase():       { customerInfo } ────► syncEntitlementsFromRC(customerInfo)
  └─ restore():        { customerInfo } ────► syncEntitlementsFromRC(customerInfo)
                                              + reconcileOwnedIapSongsFromRC() (IAP song re-unlock)

syncEntitlementsFromRC(info):
  premium = info.entitlements.active["premium"]?.isActive   → store.setPremium(premium) + grant/clear VIP (library + sdk.player.setVip)
  noAds   = info.entitlements.active["no_ads"]?.isActive     → store.setAdsRemoved(noAds)
  ownedSongs (non-sub purchases) ─ reconcile on restore / IAP purchase attempt (not on catalog load)
```

### RevenueCat lifetime (heap / memory)

`react-native-purchases` and the native RevenueCat SDK are **process-lifetime singletons**, not
screen-scoped. Once `ensureHubPurchases()` runs, the JS module, `hub.subscription`, and native configure
stay reachable until the app process ends.

**Deferred init** (so a browsing-only song list can avoid RC until paywall/IAP):

| Trigger                                                        | `ensureHubPurchases` reason tag                           |
| -------------------------------------------------------------- | --------------------------------------------------------- |
| Subscription paywall opens / purchase / restore                | `paywall:packages`, `paywall:purchase`, `paywall:restore` |
| IAP song offerings (IAP deal row scrolls into view or IAP tap) | `iap-offerings:fetch`                                     |
| IAP row / modal purchase                                       | `iap:row-purchase`, `iap:modal-purchase`                  |
| Remove-ads / sale-off50 flows                                  | `remove-ads:purchase`, `sale-off50:*`                     |

Guest session creation does **not** call RC — lock UI reads MMKV-hydrated `entitlements-store` until one of
the paths above runs. The first configure logs `[rc] init | reason = …` and stores the tag in
the IAP init reason for heap profiling.

**`entitlements-store` needs `setPremium(bool)` / `setAdsRemoved(bool)` setters** — subscriptions lapse,
so the sync must be able to **clear**, which dev's grant-only `grantPremium()`/`removeAds()` can't.
`grantPremiumEntitlement()` stays for the VIP library/SDK side-effects (called when premium flips true).

`hub.ts` already caches `isPremiumCached` for ad-gating; extend the launch refresh to also (a) cache
`isNoAdsCached` and (b) push both into `entitlements-store`. A single `syncEntitlementsFromRC()` is the
one place that reads `customerInfo` and writes the store/library.

## 4. Files to create

- **`src/features/monetization/catalog.ts`** — typed product catalog (id, kind, offering, entitlement).
  Pure data + types; single source of truth for product ids. No RN/RC imports.
- **`src/features/monetization/entitlements.ts`** — pure gating helpers from `{ isPremium, isNoAds }`:
  `shouldShowInterstitial`, `shouldShowBanner`, `shouldShowRewarded`. Unit-tested next to the file.
- **`src/features/monetization/syncEntitlementsFromRC.ts`** — reads a `CustomerInfo`, writes
  `entitlements-store` + `grantPremiumEntitlement`/clear. The launch + purchase/restore sync seam.

## 5. Integration points (dev's actual files → change)

| Where                                                                                                                                                                                                              | Change                                                                                                                                                                                            |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`revenuecat-iap-service.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/iap/adapters/revenuecat-iap-service.ts)                                            | on launch, sync entitlements from `getCustomerInfo()`; the ad gate reads `isNoAds` from the entitlements store                                                                                    |
| [`ads.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/ads.ts)                                                                                           | interstitial/banner gate on `premium ‖ no_ads`; rewarded stays `premium`-only — via `entitlements.ts`                                                                                             |
| [`PaywallModal`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/paywall/components/PaywallModal.tsx) + `PaywallPlanBody`/`OfferBody`                        | fetch offerings, render **live** localized prices; `variant→offering` map; loading/error                                                                                                          |
| [`PaywallHost`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/paywall/components/PaywallHost.tsx)                                                          | `handlePurchase(trialOn)` → pick package (plan + trial intro) → `hub.subscription.purchase(pkg)` → sync → close; replace the `grantPremiumEntitlement()` stub                                     |
| [`PaywallFooter`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/paywall) restore                                                                           | `onRestore` → `hub.subscription.restore()` → sync (currently `onClose` stub)                                                                                                                      |
| [`RemoveAdsPromoCard`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/songs/components/RemoveAdsPromoCard.tsx) → flow store `buyRemoveAds` → `iap.buyNoAds` | `purchase(noadspack)` straight from the song-list promo (no modal since 2026-11-05) → `syncEntitlements` → `no_ads` entitlement drives `adsRemoved`                                               |
| [`SaleOff50Modal`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/paywall/components/SaleOff50Modal.tsx)                                                    | yearly 50%-off sale card (Figma 7688:117350); wire `onClaim` → `hub.subscription.purchase(yearly50)` with live `originalPriceLabel` / `salePriceLabel` from RC                                    |
| [`PremiumUnlockModal`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/songs/components/PremiumUnlockModal.tsx) + `SongsScreen`                              | for `LOCKED_IAP` songs add a **"Buy ($0.99)"** CTA → `hub.subscription.purchase(song.productId)` → `unlockSongEntitlement(songId)` (rewarded "Play for free" path already wired for `LOCKED_ADS`) |
| [catalog schema](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/packages/catalog/src/schemas.ts) + sheet                                                                 | add optional **`productId`** column/field for `isIAP` rows                                                                                                                                        |

## 6. Hub change (required for no_ads)

The IAP adapter maps a single `premium` entitlement. `no_ads` needs a second entitlement check:

- The app checks `info.entitlements.active["no_ads"]` directly in the IAP adapter (the former hub is gone).

→ Checking `no_ads` via `getCustomerInfo()` in `syncEntitlementsFromRC` needs **no hub release**.

## 7. Plan (phases)

| Phase  | Work                                                                                                              | Depends on                                   |
| ------ | ----------------------------------------------------------------------------------------------------------------- | -------------------------------------------- |
| **P1** | `catalog.ts` + `entitlements.ts` + `syncEntitlementsFromRC.ts` (+ unit tests)                                     | —                                            |
| **P2** | `hub.ts` launch sync + `isNoAds` seam; `ads.ts` gate on `premium ‖ no_ads`                                        | P1 (+ PO `no_ads` entitlement for real data) |
| **P3** | Subscription paywall: live prices + `variant→offering` + `purchase(trialOn)`/restore end-to-end                   | RC subs (done)                               |
| **P4** | no_ads: song-list promo `buyRemoveAds` → `purchase(noadspack)` + restore                                          | PO creates RC `no_ads` entitlement           |
| **P5** | IAP songs: `productId` sheet column → `PremiumUnlockModal` Buy CTA → `purchase(songdeal) → unlockSongEntitlement` | PO: 3 songs + `productId` mapping            |

P1 + P3 start immediately. P2/P4 (no_ads) wait on PO's RC `no_ads` entitlement. P5 waits on PO's IAP mapping.

## Open items / PO

- ~~create RC entitlement `no_ads`~~ — **NOT needed**: non-consumable IAPs (`noadspack`, `songdeal*`) are read
  from `customerInfo.allPurchasedProductIdentifiers` (product-ownership). Only **`premium`** subscriptions need
  an entitlement (already configured — verify after a test purchase).
- **IAP songs (P5) — chosen: `Rush E` + `Passo Bem Solto (Slowed)`** (2 songs → 2 of the 3 `songdeal`
  products; `songdeal3` reserved). The `iap_song` offering exists (`song_1/2/3` → `songdeal1/2/3`). Still
  needed for P5: the 2 songs' **catalog ids**, which `songdeal{1,2}` each maps to, and a **`productId`
  column** in the Sheet (or a hardcoded 2-entry map). These 2 rows must have `isIAP=true` in the Sheet.
- Confirm the App Store subs carry the **3-day free-trial** intro offer on `weekly`/`yearly` (none on `*50`)
  — RC surfaces it via the package intro price.

***

<small>Source: [`docs/rc-iap-integration.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/rc-iap-integration.md)</small>
