create-disposabl-app 0.14.18 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.md +15 -1
  2. package/dist/disposabl.js +357 -50
  3. package/dist/index.js +8973 -154
  4. package/dist/templates/default/.github/scripts/macos-release.sh +81 -0
  5. package/dist/templates/default/.github/scripts/platforms.sh +1 -1
  6. package/dist/templates/default/.github/scripts/release.sh +1 -1
  7. package/dist/templates/default/.github/workflows/pipeline.yml +14 -3
  8. package/dist/templates/default/apps/desktop/README.md +13 -2
  9. package/dist/templates/default/apps/desktop/forge.config.mts +9 -3
  10. package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
  11. package/dist/templates/default/apps/mobile/package.json +1 -0
  12. package/dist/templates/default/apps/web/eslint.config.ts +5 -1
  13. package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
  14. package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
  15. package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
  16. package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
  17. package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
  18. package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
  19. package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
  20. package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
  21. package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
  22. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
  23. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
  24. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
  25. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
  26. package/dist/templates/default/apps/web/routes.config.ts +14 -0
  27. package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
  28. package/dist/templates/default/apps/web/tsr.config.json +1 -1
  29. package/dist/templates/default/apps/web/vite.config.ts +10 -2
  30. package/dist/templates/default/bun.lock +6 -0
  31. package/dist/templates/default/bunfig.toml +1 -0
  32. package/dist/templates/default/disposabl.json +1 -1
  33. package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
  34. package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
  35. package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
  36. package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
  37. package/dist/templates/default/packages/backend/convex/http.ts +6 -0
  38. package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
  39. package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
  40. package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
  41. package/dist/templates/default/packages/backend/generate.ts +6 -0
  42. package/dist/templates/default/packages/cli/auth.ts +36 -3
  43. package/dist/templates/default/packages/cli/build.ts +6 -0
  44. package/dist/templates/default/packages/cli/client.ts +189 -19
  45. package/dist/templates/default/packages/cli/errors.ts +18 -0
  46. package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
  47. package/dist/templates/default/packages/cli/telemetry.ts +266 -0
  48. package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
  49. package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
  50. package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
  51. package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
  52. package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
  53. package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
  54. package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
  55. package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
  56. package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
  57. package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
  58. package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
  59. package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
  60. package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
  61. package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
  62. package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
  63. package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
  64. package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
  65. package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
  66. package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
  67. package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
  68. package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
  69. package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
  70. package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
  71. package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
  72. package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
  73. package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
  74. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
  75. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
  76. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
  77. package/dist/templates/default/packages/reconfigure-skill/SKILL.md +36 -0
  78. package/dist/templates/default/packages/sdk/README.md +21 -0
  79. package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
  80. package/dist/templates/default/packages/sdk/src/index.ts +2 -0
  81. package/dist/templates/default/packages/skill/SKILL.md +2 -0
  82. package/dist/templates/default/packages/skill/references/agent.md +2 -0
  83. package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
  84. package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
  85. package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
  86. package/dist/templates/default/packages/tests/billing.test.ts +48 -4
  87. package/dist/templates/default/packages/tests/ci-platform-selection.test.ts +54 -0
  88. package/dist/templates/default/packages/tests/desktop-macos-release.test.ts +226 -0
  89. package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
  90. package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
  91. package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
  92. package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
  93. package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
  94. package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
  95. package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
  96. package/package.json +1 -1
  97. package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
@@ -0,0 +1,116 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+ import { AppState, Platform } from "react-native";
3
+ import type { PurchasesStoreProduct } from "react-native-purchases";
4
+ import { billingSession } from "./session";
5
+ import {
6
+ loadTopup,
7
+ matchingTopupRoute,
8
+ purchaseTopup,
9
+ type TopupSnapshot,
10
+ } from "./topup";
11
+
12
+ const app =
13
+ Platform.OS === "ios"
14
+ ? process.env.EXPO_PUBLIC_REVENUECAT_IOS_APP_ID
15
+ : process.env.EXPO_PUBLIC_REVENUECAT_ANDROID_APP_ID;
16
+ const productId =
17
+ Platform.OS === "ios"
18
+ ? process.env.EXPO_PUBLIC_REVENUECAT_IOS_TOPUP_PRODUCT
19
+ : process.env.EXPO_PUBLIC_REVENUECAT_ANDROID_TOPUP_PRODUCT;
20
+
21
+ export function useTopup(
22
+ customerId: string,
23
+ version: React.RefObject<number>,
24
+ key: string | undefined,
25
+ snapshot: () => Promise<TopupSnapshot>,
26
+ refreshBalance: () => Promise<unknown>,
27
+ ) {
28
+ const [product, setProduct] = useState<PurchasesStoreProduct | null>(null);
29
+ const [busy, setBusy] = useState(false);
30
+ const [message, setMessage] = useState("");
31
+ const lifecycle = useRef({ sequence: 0 });
32
+ const refresh = useCallback(
33
+ async (generation: number) => {
34
+ const seq = ++lifecycle.current.sequence;
35
+ await Promise.resolve();
36
+ if (seq !== lifecycle.current.sequence) return;
37
+ setProduct(null);
38
+ if (!key || !app || !productId) return;
39
+ try {
40
+ const latest = await snapshot();
41
+ if (
42
+ latest.customerId !== customerId ||
43
+ !matchingTopupRoute(latest, Platform.OS, app, productId)
44
+ )
45
+ return;
46
+ const result = await billingSession.run(
47
+ customerId,
48
+ generation,
49
+ key,
50
+ async (sdk) => {
51
+ if (
52
+ !(await sdk.canMakePayments()) ||
53
+ (await sdk.getAppUserID()) !== customerId
54
+ )
55
+ return null;
56
+ return loadTopup(sdk, Platform.OS, productId);
57
+ },
58
+ );
59
+ if (
60
+ seq === lifecycle.current.sequence &&
61
+ billingSession.current(customerId, generation)
62
+ )
63
+ setProduct(result);
64
+ } catch {
65
+ /* Unavailable until next refresh. */
66
+ }
67
+ },
68
+ [customerId, key, snapshot],
69
+ );
70
+ useEffect(() => {
71
+ const state = lifecycle.current;
72
+ const update = () => void refresh(version.current);
73
+ update();
74
+ const listener = AppState.addEventListener("change", (state) => {
75
+ if (state === "active") update();
76
+ });
77
+ return () => {
78
+ state.sequence++;
79
+ listener.remove();
80
+ };
81
+ }, [refresh, version]);
82
+ async function purchase() {
83
+ if (!product || !key || !billingSession.beginTransaction()) return;
84
+ const generation = version.current;
85
+ const current = () => billingSession.current(customerId, generation);
86
+ setBusy(true);
87
+ setMessage("");
88
+ try {
89
+ await billingSession.run(customerId, generation, key, (sdk) =>
90
+ purchaseTopup(
91
+ sdk,
92
+ snapshot,
93
+ current,
94
+ customerId,
95
+ Platform.OS,
96
+ app,
97
+ productId,
98
+ ),
99
+ );
100
+ if (current())
101
+ setMessage("Purchase received. Credits appear after payment sync.");
102
+ } catch (error) {
103
+ if (current())
104
+ setMessage(
105
+ (error as { userCancelled?: boolean })?.userCancelled
106
+ ? "Purchase cancelled."
107
+ : "Store purchase unavailable. Refresh before trying again.",
108
+ );
109
+ } finally {
110
+ billingSession.endTransaction();
111
+ if (current()) setBusy(false);
112
+ }
113
+ if (current()) await refreshBalance().catch(() => undefined);
114
+ }
115
+ return { product, busy, message, purchase };
116
+ }
@@ -0,0 +1,190 @@
1
+ import { useMutation } from "convex/react";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { useCallback, useEffect, useRef, useState } from "react";
4
+ import { AppState, Linking, Platform } from "react-native";
5
+ import { billingSession } from "./session";
6
+ import {
7
+ paymentOptions,
8
+ preflightWebMonthly,
9
+ preflightWebTopup,
10
+ storeEligibility,
11
+ } from "./policy";
12
+ import { validMonthlyOffering, type BillingSnapshot } from "./paywall";
13
+ import { loadTopup, matchingTopupRoute, type TopupSnapshot } from "./topup";
14
+
15
+ const topupApp = process.env.EXPO_PUBLIC_REVENUECAT_IOS_APP_ID;
16
+ const topupProduct = process.env.EXPO_PUBLIC_REVENUECAT_IOS_TOPUP_PRODUCT;
17
+
18
+ const create = makeFunctionReference<
19
+ "mutation",
20
+ { purchase: "monthly" | "topup" },
21
+ { url: string; userId: string }
22
+ >("billingHandoff:create");
23
+ type Snapshot = BillingSnapshot &
24
+ TopupSnapshot & {
25
+ mobileWebCheckoutEnabled: boolean;
26
+ customerId: string;
27
+ };
28
+ export function useWebCheckout(
29
+ customerId: string,
30
+ version: React.RefObject<number>,
31
+ key: string | undefined,
32
+ productId: string | undefined,
33
+ snapshot: () => Promise<Snapshot>,
34
+ ) {
35
+ const handoff = useMutation(create);
36
+ const [available, setAvailable] = useState(false);
37
+ const [topupAvailable, setTopupAvailable] = useState(false);
38
+ const [busy, setBusy] = useState(false);
39
+ const [error, setError] = useState("");
40
+ const lifecycle = useRef({ sequence: 0 });
41
+ const refresh = useCallback(
42
+ async (generation: number) => {
43
+ const seq = ++lifecycle.current.sequence;
44
+ setAvailable(false);
45
+ setTopupAvailable(false);
46
+ if (Platform.OS !== "ios" || !key) return;
47
+ try {
48
+ const latest = await snapshot();
49
+ if (latest.customerId !== customerId) return;
50
+ const value = await billingSession.run(
51
+ customerId,
52
+ generation,
53
+ key,
54
+ async (sdk) => {
55
+ const eligibility = await storeEligibility(sdk, Platform.OS);
56
+ const offering = productId
57
+ ? (await sdk.getOfferings().catch(() => null))?.current
58
+ : null;
59
+ const common = {
60
+ platform: Platform.OS,
61
+ ...eligibility,
62
+ paymentsEnabled: true,
63
+ webCheckoutEnabled: latest.mobileWebCheckoutEnabled,
64
+ };
65
+ const topup =
66
+ matchingTopupRoute(latest, Platform.OS, topupApp, topupProduct) &&
67
+ (await sdk.getAppUserID()) === customerId &&
68
+ topupProduct
69
+ ? await loadTopup(sdk, Platform.OS, topupProduct).catch(
70
+ () => null,
71
+ )
72
+ : null;
73
+ return {
74
+ topup: paymentOptions({ ...common, nativeReady: !!topup }).web,
75
+ monthly: paymentOptions({
76
+ platform: Platform.OS,
77
+ ...eligibility,
78
+ paymentsEnabled: true,
79
+ webCheckoutEnabled: latest.mobileWebCheckoutEnabled,
80
+ nativeReady:
81
+ latest.nativeSubscriptionsEnabled &&
82
+ !!offering &&
83
+ !!productId &&
84
+ validMonthlyOffering(offering.availablePackages, productId),
85
+ }).web,
86
+ };
87
+ },
88
+ );
89
+ if (
90
+ seq === lifecycle.current.sequence &&
91
+ billingSession.current(customerId, generation)
92
+ ) {
93
+ setAvailable(value.monthly && latest.hasMonthlyPlan === false);
94
+ setTopupAvailable(value.topup);
95
+ }
96
+ } catch {
97
+ /* Fail closed until the next refresh. */
98
+ }
99
+ },
100
+ [customerId, key, productId, snapshot],
101
+ );
102
+ useEffect(() => {
103
+ const state = lifecycle.current;
104
+ const update = () => void refresh(version.current);
105
+ update();
106
+ const listener = AppState.addEventListener("change", (state) => {
107
+ if (state === "active") update();
108
+ });
109
+ return () => {
110
+ state.sequence++;
111
+ listener.remove();
112
+ };
113
+ }, [refresh, version]);
114
+ async function open(purchase: "monthly" | "topup" = "monthly") {
115
+ if (
116
+ !(purchase === "monthly" ? available && productId : topupAvailable) ||
117
+ !key ||
118
+ !billingSession.beginTransaction()
119
+ )
120
+ return false;
121
+ const generation = version.current;
122
+ const current = () => billingSession.current(customerId, generation);
123
+ setBusy(true);
124
+ setError("");
125
+ try {
126
+ const handoffResult = await billingSession.run(
127
+ customerId,
128
+ generation,
129
+ key,
130
+ async (sdk) => {
131
+ if (purchase === "monthly")
132
+ await preflightWebMonthly(
133
+ sdk,
134
+ Platform.OS,
135
+ productId!,
136
+ customerId,
137
+ snapshot,
138
+ current,
139
+ );
140
+ else
141
+ await preflightWebTopup(
142
+ sdk,
143
+ Platform.OS,
144
+ topupApp,
145
+ topupProduct,
146
+ customerId,
147
+ snapshot,
148
+ current,
149
+ );
150
+ if (!current()) throw new Error("Account changed.");
151
+ return handoff({ purchase });
152
+ },
153
+ );
154
+ const parsed = new URL(handoffResult.url);
155
+ if (
156
+ !current() ||
157
+ handoffResult.userId !== customerId ||
158
+ parsed.protocol !== "https:" ||
159
+ parsed.username ||
160
+ parsed.password ||
161
+ parsed.pathname !== "/billing-login" ||
162
+ parsed.search ||
163
+ !/^#[A-Za-z0-9_-]{43}$/.test(parsed.hash)
164
+ )
165
+ throw new Error("Invalid browser link.");
166
+ await Linking.openURL(handoffResult.url);
167
+ if (current())
168
+ setError("Return to the app and refresh credits after checkout.");
169
+ return true;
170
+ } catch {
171
+ if (current()) {
172
+ setAvailable(false);
173
+ setTopupAvailable(false);
174
+ setError("Website checkout is unavailable. Refresh and try again.");
175
+ }
176
+ return false;
177
+ } finally {
178
+ billingSession.endTransaction();
179
+ if (current()) setBusy(false);
180
+ }
181
+ }
182
+ return {
183
+ available,
184
+ topupAvailable,
185
+ busy,
186
+ error,
187
+ open,
188
+ refresh: () => refresh(version.current),
189
+ };
190
+ }
@@ -7,7 +7,8 @@ import { analyticsEnabled, authenticationEnabled } from "../features";
7
7
  import { authClasses } from "./components/auth-ui";
8
8
  import { authStorage } from "./lib/auth-storage";
9
9
  import { api } from "./lib/convex";
10
- import { sendPageTelemetry } from "../lib/telemetry";
10
+ import { createPageTelemetry } from "../lib/telemetry";
11
+ import * as SecureStore from "expo-secure-store";
11
12
  import { sessionId } from "../lib/auth-session";
12
13
  import { UpdatePrompt } from "./updates/update-prompt";
13
14
  import { ThemeProvider, useResolvedColorScheme } from "./design/theme-provider";
@@ -201,6 +202,11 @@ export function PublicLayout() {
201
202
  );
202
203
  }
203
204
 
205
+ const screenTelemetry = createPageTelemetry({
206
+ getItem: SecureStore.getItemAsync,
207
+ setItem: SecureStore.setItemAsync,
208
+ });
209
+
204
210
  function AuthenticatedScreenTelemetry() {
205
211
  const token = useAuthToken();
206
212
  const { isLoading, isAuthenticated } = useConvexAuth();
@@ -227,13 +233,14 @@ function ScreenTelemetry({
227
233
  !analyticsEnabled ||
228
234
  __DEV__ ||
229
235
  process.env.EXPO_PUBLIC_DISPOSABL_ANALYTICS_ENABLED !== "true" ||
236
+ process.env.EXPO_PUBLIC_E2E === "true" ||
230
237
  !origin
231
238
  )
232
239
  return;
233
240
  const key = JSON.stringify([pathname, sessionId(token)]);
234
241
  if (lastView.current === key) return;
235
242
  lastView.current = key;
236
- void sendPageTelemetry(origin, "mobile", pathname, token);
243
+ screenTelemetry.view(origin, "mobile", pathname, token);
237
244
  }, [pathname, token, ready]);
238
245
  return null;
239
246
  }
@@ -21,6 +21,7 @@
21
21
  "convex": "catalog:",
22
22
  "jose": "^6.2.15",
23
23
  "lucide-react": "catalog:",
24
+ "next-themes": "^0.4.6",
24
25
  "react": "catalog:",
25
26
  "react-dom": "catalog:",
26
27
  "shadcn": "^4.23.0",
@@ -46,7 +47,11 @@
46
47
  "./client/components/ui/*": "./client/components/ui/*.tsx",
47
48
  "./server/http": "./server/http.ts",
48
49
  "./server/billing-functions": "./server/billing-functions.ts",
50
+ "./server/billing-handoff": "./server/billing-handoff.ts",
51
+ "./server/browser-session": "./server/browser-session.ts",
49
52
  "./server/billing": "./server/billing.ts",
53
+ "./server/revenuecat-validator": "./server/revenuecat-validator.ts",
54
+ "./server/revenuecat-functions": "./server/revenuecat-functions.ts",
50
55
  "./server/google": "./server/google.ts",
51
56
  "./server/lib/telemetry": "./server/lib/telemetry.ts",
52
57
  "./server/lib/functions": "./server/lib/functions.ts",
@@ -72,7 +77,8 @@
72
77
  "react-native-purchases": "10.16.0",
73
78
  "react-native-css": "3.1.0-rc.1",
74
79
  "@expo/ui": "~58.0.27",
75
- "expo-glass-effect": "~58.0.5"
80
+ "expo-glass-effect": "~58.0.5",
81
+ "react-native-purchases-ui": "10.16.0"
76
82
  },
77
83
  "peerDependenciesMeta": {
78
84
  "expo-clipboard": {
@@ -110,6 +116,9 @@
110
116
  },
111
117
  "expo-glass-effect": {
112
118
  "optional": true
119
+ },
120
+ "react-native-purchases-ui": {
121
+ "optional": true
113
122
  }
114
123
  }
115
124
  }
@@ -0,0 +1,64 @@
1
+ export type CheckoutResult = { url: string | null; retryAllowed?: true };
2
+
3
+ /** Keep an uncertain attempt's identity until the server confirms its outcome. */
4
+ export async function requestCheckout(
5
+ storage: Pick<Storage, "getItem" | "setItem" | "removeItem">,
6
+ customerId: string,
7
+ purchase: "monthly" | "topup",
8
+ start: (args: {
9
+ purchase: "monthly" | "topup";
10
+ requestId: string;
11
+ }) => Promise<CheckoutResult>,
12
+ newId: () => string = () => crypto.randomUUID(),
13
+ ) {
14
+ const key = `billing-attempt:${customerId}:${purchase}`;
15
+ const requestId = storage.getItem(key) ?? newId();
16
+ storage.setItem(key, requestId);
17
+ const result = await start({ purchase, requestId });
18
+ if (result.url || result.retryAllowed === true) storage.removeItem(key);
19
+ return result;
20
+ }
21
+
22
+ /** A query parameter marks the iOS context but cannot grant a purchase option. */
23
+ export function handoffSelection(value: string | null) {
24
+ try {
25
+ const parsed: unknown = JSON.parse(value ?? "null");
26
+ if (!parsed || typeof parsed !== "object") return null;
27
+ const record = parsed as Record<string, unknown>;
28
+ return typeof record.userId === "string" &&
29
+ typeof record.expiresAt === "number" &&
30
+ record.expiresAt > Date.now() &&
31
+ (record.purchase === "monthly" || record.purchase === "topup")
32
+ ? {
33
+ userId: record.userId,
34
+ purchase: record.purchase,
35
+ expiresAt: record.expiresAt,
36
+ }
37
+ : null;
38
+ } catch {
39
+ return null;
40
+ }
41
+ }
42
+ export function handoffAllows(
43
+ fromIos: boolean,
44
+ selection: ReturnType<typeof handoffSelection>,
45
+ customerId: string | null,
46
+ purchase: "monthly" | "topup",
47
+ ) {
48
+ return (
49
+ !fromIos ||
50
+ (!!customerId &&
51
+ selection?.userId === customerId &&
52
+ selection.expiresAt > Date.now() &&
53
+ selection.purchase === purchase)
54
+ );
55
+ }
56
+
57
+ /** Preserve native origin even when its selected purchase has expired or is malformed. */
58
+ export function handoffContext(stored: string | null, search: string) {
59
+ return {
60
+ handoff: handoffSelection(stored),
61
+ fromIos:
62
+ stored !== null || new URLSearchParams(search).get("source") === "ios",
63
+ };
64
+ }
@@ -2,7 +2,7 @@ export type Pricing = {
2
2
  currency: string;
3
3
  creditsPerDollar: number;
4
4
  featureId: string;
5
- monthly: { planId: string; price: number };
5
+ monthly: { planId: string; price: number; creditMultiplier: number };
6
6
  topup: { planId: string; price: number };
7
7
  };
8
8
 
@@ -11,7 +11,7 @@ export const pricing: Pricing = {
11
11
  currency: "USD",
12
12
  creditsPerDollar: 100,
13
13
  featureId: "credits",
14
- monthly: { planId: "credits_monthly", price: 5 },
14
+ monthly: { planId: "credits_monthly", price: 5, creditMultiplier: 1.5 },
15
15
  topup: { planId: "credits_topup", price: 5 },
16
16
  };
17
17
 
@@ -19,7 +19,20 @@ export type Purchase = "monthly" | "topup";
19
19
 
20
20
  export function creditQuantity(purchase: Purchase, config: Pricing = pricing) {
21
21
  // Credits are whole units; decimal prices must not leak floating-point noise.
22
- return Math.round(config[purchase].price * config.creditsPerDollar);
22
+ return Math.round(
23
+ config[purchase].price *
24
+ config.creditsPerDollar *
25
+ (purchase === "monthly" ? config.monthly.creditMultiplier : 1),
26
+ );
27
+ }
28
+
29
+ export function monthlyLabel(config: Pricing = pricing) {
30
+ const monthlyRate = creditQuantity("monthly", config) / config.monthly.price;
31
+ const topupRate = creditQuantity("topup", config) / config.topup.price;
32
+ const percent = (monthlyRate / topupRate - 1) * 100;
33
+ return Number.isFinite(percent) && percent > 0
34
+ ? `Monthly · ${new Intl.NumberFormat("en-US", { maximumFractionDigits: 1 }).format(percent)}% more`
35
+ : "Monthly";
23
36
  }
24
37
 
25
38
  export function formatPrice(amount: number, config: Pricing = pricing) {