recipe-payments
Add in-app purchases / subscriptions with RevenueCat to an App in a Box app, with the entitlement decided SERVER-side (RevenueCat webhook -> entitlements table -> backend check), never trusted from the client. Use when the owner wants a paywall, subscriptions, "premium", "pro tier", IAP, or to charge for a feature. Covers store + RevenueCat setup, the webhook endpoint, migration, gating, env wiring, tests and the done-means check.
- License 许可
- MIT
- 最近更新
- 2026-10-06
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- recipe-payments
- description
- Add in-app purchases / subscriptions with RevenueCat to an App in a Box app, with the entitlement decided SERVER-side (RevenueCat webhook -> entitlements table -> backend check), never trusted from the client. Use when the owner wants a paywall, subscriptions, "premium", "pro tier", IAP, or to charge for a feature. Covers store + RevenueCat setup, the webhook endpoint, migration, gating, env wiring, tests and the done-means check.
03 SKILL.md 正文
Recipe: payments (RevenueCat, server-side entitlements)
Digital goods in iOS/Android apps must use store IAP (App Store 3.1.1, Play billing policy). RevenueCat wraps both stores. The client shows the paywall and makes the purchase; the server decides what the user is entitled to, from RevenueCat's webhook, because anything the client says can be forged.
⚖️ Owner decisions before starting: price points and trial, the entitlement names
(pro), and whether the paywall is hard (blocks the core loop) or soft.
Steps
- (owner) App Store Connect: agreements/tax/banking, subscription group + products.
Play Console: subscriptions with base plans. RevenueCat: project, both apps, products
→ one entitlement
pro→ an offering. - Migration
supabase/migrations/<ts>_entitlements.sql:
Plus an atomic-- Rollback: drop table if exists public.revenuecat_events; drop table if exists public.entitlements; create table if not exists public.entitlements ( user_id uuid not null references auth.users (id) on delete cascade, entitlement text not null, expires_at timestamptz, -- null = lifetime product_id text, store text, updated_at timestamptz not null default now(), primary key (user_id, entitlement) ); alter table public.entitlements enable row level security; create policy "entitlements_select_own" on public.entitlements for select using (auth.uid() = user_id); -- read-only to users; writes: webhook only create table if not exists public.revenuecat_events ( -- idempotency: RC retries event_id text primary key, received_at timestamptz not null default now() ); alter table public.revenuecat_events enable row level security; revoke all on public.revenuecat_events from anon, authenticated;apply_revenuecat_event(p_event_id, p_user_id, p_entitlement, p_expires_at, p_product, p_store)function (pattern:backend/AGENTS.md→ Atomic multi-row writes): insert the event id (return early on conflict), upsert the entitlement, in one transaction. - Config
backend/config.py:"payments (RevenueCat)": ("REVENUECAT_WEBHOOK_SECRET",). - Webhook
backend/routers/billing.py:POST /webhooks/revenuecat,include_in_schema=False:- 503 if
feature_missing("payments (RevenueCat)"); 401 unlesshmac.compare_digest(request Authorization header, "Bearer " + secret)(copyrequire_cron_secretinrouters/internal.py). app_user_idis the Supabase user id (step 6); ignore events whose id isn't a UUID (anonymous RC ids). MapINITIAL_PURCHASE/RENEWAL/UNCANCELLATION/PRODUCT_CHANGE→ setexpires_at = expiration_at_ms;EXPIRATION→ expires now;CANCELLATION→ keep until expiry (they paid for the period);TRANSFER→ move.- Call the rpc; always 200 after a verified, parsed event (RC retries non-2xx).
- 503 if
- Gate
backend/services/entitlements.py:has_entitlement(db, user_id, "pro")(.eq("user_id", user_id),expires_at is null or > now()), and a dependencyrequire_entitlement("pro")→ 402{"detail": "entitlement_required"}on premium routes.GET /api/v1/me/entitlementsreturns the list for the UI. - Mobile:
npx expo install react-native-purchases react-native-purchases-ui;Purchases.configure({ apiKey: <platform key> , appUserID: session.user.id })after sign-in,Purchases.logOut()on sign-out. Paywall viaRevenueCatUI.presentPaywall(). After a purchase, refetch/api/v1/me/entitlements(poll briefly; the webhook lands in seconds). Show "Restore purchases" (App Review requires it).
Env / wiring checklist
| Where | What |
|---|---|
| Railway | railway variables --set "REVENUECAT_WEBHOOK_SECRET=$(openssl rand -hex 32)" |
| RevenueCat | Integrations → Webhooks: URL https://<api>/webhooks/revenuecat, Authorization Bearer <same secret> |
| EAS (preview + production) | EXPO_PUBLIC_REVENUECAT_IOS_KEY, EXPO_PUBLIC_REVENUECAT_ANDROID_KEY via eas env:create, and add both to EAS_MANAGED |
| FEATURE_CONFIG | "payments (RevenueCat)" (step 3) |
secrets-rotation.md |
already lists the webhook secret |
Tests to add
- Webhook: 503 without secret; 401 wrong/missing header (constant-time compare);
a replayed
event_idchanges nothing;EXPIRATIONrevokes; a non-UUIDapp_user_idis ignored; the event's user id (not any caller) is the row written. - Gate: premium route 402 without entitlement, 200 with, 402 after
expires_at; user A's entitlement never unlocks user B (filter-honouring fake). - Migration static tests already require RLS + service-only function grants.
Done means
- [ ] A sandbox purchase on a preview build flips
/api/v1/me/entitlementsto includeprowithin 10 s, and the premium route returns 200. - [ ] Letting the sandbox subscription lapse (sandbox renews every few minutes) flips it back and the route returns 402.
- [ ]
/healthlists nothing underfeatures_unavailablein production. - [ ] The paywall screen fires a view event and purchase success/failure events.
04 同仓库更多 Skills
ship
Release checklist for getting a build to testers or the stores. Runs the risk gate first (refuses while a risk item is open) and walks the owner through the compliance checklist. Covers the version bump, EAS build and submit, OTA updates and when they're safe, changelog, store listing, privacy nutrition label / data safety form, the Accessibility Nutrition Labels, and the account-deletion requirement. Guidance-level; it never handles signing secrets. Use when asked to ship, release, submit to TestFlight / App Store / Play, or push an OTA update.
routines
Put the project's recurring rituals on a schedule, with the owner's consent. Weekly north-star report, monthly market watch, dependency triage, Supabase advisor sweep, reflect and harness-optimize. Uses Claude Code Routines (cloud cron) where they fit, and documents the Codex / local cron equivalent. Use when asked to automate, schedule or "keep the project moving on its own", or after setup finishes.
reflect
Consolidate what recent sessions learned into the project's long-term memory (.agents/memory). Use at the end of a substantial session, after a correction from the user, after a painful debugging session, when the session-start healthcheck says reflect is overdue or a pending-reflection seed is waiting, or when MEMORY.md exceeds its budget.
pr-review
Review a pull request on the owner's behalf and fix what's safe to fix. Runs the deterministic gates first, then correctness/security, design-system, accessibility and analytics review passes, verifies each blocking finding, applies safe fixes, re-checks, and ends with a plain-English verdict. Use when a PR is opened, when asked to review a PR, a branch or a pasted diff, or before merging anything.
north-star-report
Weekly product-analytics and reliability readout. Pulls the funnel of the brief's 5 north-star events from PostHog, finds the biggest drop-offs and errors, checks each SLO in docs/slo.yaml against its error budget, compares with last week, and proposes (then, with a yes, files) tickets that target them. Use weekly, when the session-start line says it's overdue, when the backlog is empty, or when asked "how is the app doing?".
next
Decide the single next best thing to work on, with two alternates. Reads setup progress, work in progress, open PRs and failing CI, the backlog (GitHub Issues or Linear), overdue harness rituals, product decisions parked until now, and north-star analytics if PostHog is connected. Use when someone asks "what next?", "what should I work on?", at the start of a session, or after a PR merges.