create-disposabl-app 0.15.0 → 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 (90) 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/apps/desktop/forge.config.mts +2 -1
  5. package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
  6. package/dist/templates/default/apps/mobile/package.json +1 -0
  7. package/dist/templates/default/apps/web/eslint.config.ts +5 -1
  8. package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
  9. package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
  10. package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
  11. package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
  12. package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
  13. package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
  14. package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
  15. package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
  16. package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
  17. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
  18. package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
  19. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
  20. package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
  21. package/dist/templates/default/apps/web/routes.config.ts +14 -0
  22. package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
  23. package/dist/templates/default/apps/web/tsr.config.json +1 -1
  24. package/dist/templates/default/apps/web/vite.config.ts +10 -2
  25. package/dist/templates/default/bun.lock +6 -0
  26. package/dist/templates/default/bunfig.toml +1 -0
  27. package/dist/templates/default/disposabl.json +1 -1
  28. package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
  29. package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
  30. package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
  31. package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
  32. package/dist/templates/default/packages/backend/convex/http.ts +6 -0
  33. package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
  34. package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
  35. package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
  36. package/dist/templates/default/packages/backend/generate.ts +6 -0
  37. package/dist/templates/default/packages/cli/auth.ts +36 -3
  38. package/dist/templates/default/packages/cli/build.ts +6 -0
  39. package/dist/templates/default/packages/cli/client.ts +189 -19
  40. package/dist/templates/default/packages/cli/errors.ts +18 -0
  41. package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
  42. package/dist/templates/default/packages/cli/telemetry.ts +266 -0
  43. package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
  44. package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
  45. package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
  46. package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
  47. package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
  48. package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
  49. package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
  50. package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
  51. package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
  52. package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
  53. package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
  54. package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
  55. package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
  56. package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
  57. package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
  58. package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
  59. package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
  60. package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
  61. package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
  62. package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
  63. package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
  64. package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
  65. package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
  66. package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
  67. package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
  68. package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
  69. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
  70. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
  71. package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
  72. package/dist/templates/default/packages/reconfigure-skill/SKILL.md +36 -0
  73. package/dist/templates/default/packages/sdk/README.md +21 -0
  74. package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
  75. package/dist/templates/default/packages/sdk/src/index.ts +2 -0
  76. package/dist/templates/default/packages/skill/SKILL.md +2 -0
  77. package/dist/templates/default/packages/skill/references/agent.md +2 -0
  78. package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
  79. package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
  80. package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
  81. package/dist/templates/default/packages/tests/billing.test.ts +48 -4
  82. package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
  83. package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
  84. package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
  85. package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
  86. package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
  87. package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
  88. package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
  89. package/package.json +1 -1
  90. package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
@@ -1,15 +1,22 @@
1
- import type { Auth } from "convex/server";
1
+ import { makeFunctionReference, type Auth } from "convex/server";
2
2
  import { getSessionUserId, internalAction } from "./lib/functions";
3
3
  import { v } from "convex/values";
4
- import { action } from "../../backend/convex/_generated/server";
4
+ import {
5
+ action,
6
+ internalMutation,
7
+ internalQuery,
8
+ } from "../../backend/convex/_generated/server";
5
9
  import { paymentsEnabled } from "../features";
6
10
  import { setupBilling } from "./billing-setup";
11
+ import { nativeTopupRoutes } from "./billing-native-capability";
7
12
  import {
8
13
  balanceFromCustomer,
9
14
  monthlyPlanFromCustomer,
10
15
  billingContext,
11
16
  billingRequest,
12
17
  purchaseRequest,
18
+ mobileWebCheckoutConfigured,
19
+ checkoutUrl,
13
20
  } from "./billing";
14
21
 
15
22
  export const setup = internalAction({
@@ -28,12 +35,188 @@ async function context(ctx: { auth: Auth }) {
28
35
  );
29
36
  }
30
37
 
38
+ const purchaseValidator = v.union(v.literal("monthly"), v.literal("topup"));
39
+ const attemptArgs = {
40
+ userId: v.string(),
41
+ purchase: purchaseValidator,
42
+ requestId: v.string(),
43
+ };
44
+ const beginRef = makeFunctionReference<
45
+ "mutation",
46
+ { userId: string; purchase: "monthly" | "topup"; requestId: string },
47
+ string | null | { retryAllowed: true }
48
+ >("billing:beginAttempt");
49
+ const finishRef = makeFunctionReference<
50
+ "mutation",
51
+ {
52
+ userId: string;
53
+ purchase: "monthly" | "topup";
54
+ requestId: string;
55
+ url: string;
56
+ },
57
+ null
58
+ >("billing:finishAttempt");
59
+ const pendingRef = makeFunctionReference<"query", { userId: string }, boolean>(
60
+ "billing:hasUnknownAttempt",
61
+ );
62
+
63
+ // Persist before the provider call. A crash or lost response requires operator
64
+ // reconciliation; an automatic retry could charge the same customer twice.
65
+ export const beginAttempt = internalMutation({
66
+ args: attemptArgs,
67
+ returns: v.union(
68
+ v.string(),
69
+ v.null(),
70
+ v.object({ retryAllowed: v.literal(true) }),
71
+ ),
72
+ handler: async (ctx, args) => {
73
+ const same = await ctx.db
74
+ .query("billingAttempts")
75
+ .withIndex("by_userId_requestId", (q) =>
76
+ q.eq("userId", args.userId).eq("requestId", args.requestId),
77
+ )
78
+ .unique();
79
+ if (same) {
80
+ if (same.purchase !== args.purchase)
81
+ throw new Error("A previous checkout needs review.");
82
+ if (same.resolvedAt !== undefined) return { retryAllowed: true as const };
83
+ if (!same.url) throw new Error("A previous checkout needs review.");
84
+ return same.url;
85
+ }
86
+ const prior = await ctx.db
87
+ .query("billingAttempts")
88
+ .withIndex("by_userId_purchase_resolvedAt_url", (q) => {
89
+ const open = q
90
+ .eq("userId", args.userId)
91
+ .eq("purchase", args.purchase)
92
+ .eq("resolvedAt", undefined);
93
+ // A returned monthly link remains payable until the provider closes it.
94
+ return args.purchase === "monthly" ? open : open.eq("url", undefined);
95
+ })
96
+ .first();
97
+ if (prior) {
98
+ if (args.purchase === "monthly" && prior.url) return prior.url;
99
+ throw new Error("A previous checkout needs review.");
100
+ }
101
+ await ctx.db.insert("billingAttempts", args);
102
+ return null;
103
+ },
104
+ });
105
+ export const finishAttempt = internalMutation({
106
+ args: { ...attemptArgs, url: v.string() },
107
+ returns: v.null(),
108
+ handler: async (ctx, args) => {
109
+ const prior = await ctx.db
110
+ .query("billingAttempts")
111
+ .withIndex("by_userId_requestId", (q) =>
112
+ q.eq("userId", args.userId).eq("requestId", args.requestId),
113
+ )
114
+ .unique();
115
+ if (
116
+ !prior ||
117
+ prior.purchase !== args.purchase ||
118
+ prior.resolvedAt !== undefined ||
119
+ prior.url
120
+ )
121
+ throw new Error("Invalid checkout attempt.");
122
+ await ctx.db.patch("billingAttempts", prior._id, { url: args.url });
123
+ return null;
124
+ },
125
+ });
126
+ // Internal operator reconciliation retains evidence; never release a payable link.
127
+ export const resolveAttempt = internalMutation({
128
+ args: {
129
+ userId: v.string(),
130
+ requestId: v.string(),
131
+ outcome: v.union(
132
+ v.literal("completed"),
133
+ v.literal("expired"),
134
+ v.literal("cancelled"),
135
+ v.literal("not_charged"),
136
+ ),
137
+ evidence: v.string(),
138
+ },
139
+ returns: v.null(),
140
+ handler: async (ctx, { userId, requestId, outcome, evidence }) => {
141
+ const row = await ctx.db
142
+ .query("billingAttempts")
143
+ .withIndex("by_userId_requestId", (q) =>
144
+ q.eq("userId", userId).eq("requestId", requestId),
145
+ )
146
+ .unique();
147
+ if (!row || row.resolvedAt !== undefined)
148
+ throw new Error("Open attempt not found.");
149
+ if (
150
+ evidence.trim().length < 10 ||
151
+ evidence.length > 2000 ||
152
+ (row.url && outcome === "not_charged")
153
+ )
154
+ throw new Error(
155
+ "Verify that the provider checkout is completed, cancelled or expired before releasing it.",
156
+ );
157
+ await ctx.db.patch("billingAttempts", row._id, {
158
+ resolvedAt: Date.now(),
159
+ resolution: { outcome, evidence: evidence.trim() },
160
+ });
161
+ return null;
162
+ },
163
+ });
164
+ export const hasUnknownAttempt = internalQuery({
165
+ args: { userId: v.string() },
166
+ returns: v.boolean(),
167
+ handler: async (ctx, { userId }) => {
168
+ for (const purchase of ["monthly", "topup"]) {
169
+ if (
170
+ await ctx.db
171
+ .query("billingAttempts")
172
+ .withIndex("by_userId_purchase_resolvedAt_url", (q) =>
173
+ q
174
+ .eq("userId", userId)
175
+ .eq("purchase", purchase)
176
+ .eq("resolvedAt", undefined)
177
+ .eq("url", undefined),
178
+ )
179
+ .first()
180
+ )
181
+ return true;
182
+ }
183
+ return false;
184
+ },
185
+ });
186
+
187
+ export const hasPendingMonthlyAttempt = internalQuery({
188
+ args: { userId: v.string() },
189
+ returns: v.boolean(),
190
+ handler: async (ctx, { userId }) =>
191
+ !!(await ctx.db
192
+ .query("billingAttempts")
193
+ .withIndex("by_userId_purchase_resolvedAt_url", (q) =>
194
+ q
195
+ .eq("userId", userId)
196
+ .eq("purchase", "monthly")
197
+ .eq("resolvedAt", undefined),
198
+ )
199
+ .first()),
200
+ });
201
+ const monthlyPendingRef = makeFunctionReference<
202
+ "query",
203
+ { userId: string },
204
+ boolean
205
+ >("billing:hasPendingMonthlyAttempt");
206
+
31
207
  export const snapshot = action({
32
208
  args: {},
33
209
  returns: v.object({
34
210
  balance: v.union(v.number(), v.null()),
35
211
  hasMonthlyPlan: v.union(v.boolean(), v.null()),
36
212
  nativeSubscriptionsEnabled: v.boolean(),
213
+ nativeTopupRoutes: v.array(
214
+ v.object({ app: v.string(), store: v.string(), product: v.string() }),
215
+ ),
216
+ mobileWebCheckoutEnabled: v.boolean(),
217
+ checkoutNeedsReview: v.boolean(),
218
+ monthlyCheckoutPending: v.boolean(),
219
+ customerId: v.string(),
37
220
  }),
38
221
  handler: async (ctx) => {
39
222
  const { key, userId } = await context(ctx);
@@ -46,6 +229,16 @@ export const snapshot = action({
46
229
  hasMonthlyPlan: monthlyPlanFromCustomer(customer),
47
230
  nativeSubscriptionsEnabled:
48
231
  process.env.REVENUECAT_SYNC_ENABLED === "true",
232
+ nativeTopupRoutes: nativeTopupRoutes(paymentsEnabled, process.env),
233
+ mobileWebCheckoutEnabled: mobileWebCheckoutConfigured(
234
+ paymentsEnabled,
235
+ process.env,
236
+ ),
237
+ checkoutNeedsReview: await ctx.runQuery(pendingRef, { userId }),
238
+ monthlyCheckoutPending: await ctx.runQuery(monthlyPendingRef, {
239
+ userId,
240
+ }),
241
+ customerId: userId,
49
242
  };
50
243
  } catch {
51
244
  throw new Error("Billing is temporarily unavailable.");
@@ -54,9 +247,14 @@ export const snapshot = action({
54
247
  });
55
248
 
56
249
  export const checkout = action({
57
- args: { purchase: v.union(v.literal("monthly"), v.literal("topup")) },
58
- returns: v.object({ url: v.union(v.string(), v.null()) }),
59
- handler: async (ctx, { purchase }) => {
250
+ args: { purchase: purchaseValidator, requestId: v.string() },
251
+ returns: v.object({
252
+ url: v.union(v.string(), v.null()),
253
+ retryAllowed: v.optional(v.literal(true)),
254
+ }),
255
+ handler: async (ctx, { purchase, requestId }) => {
256
+ if (!/^[0-9a-f-]{36}$/.test(requestId))
257
+ throw new Error("Invalid checkout request.");
60
258
  const { key, userId, returnUrl } = await context(ctx);
61
259
  if (purchase === "monthly") {
62
260
  const customer = await billingRequest(key, "customers.get_or_create", {
@@ -70,17 +268,28 @@ export const checkout = action({
70
268
  : "Could not check your current plan. Please refresh and try again.",
71
269
  );
72
270
  }
271
+ const existing = await ctx.runMutation(beginRef, {
272
+ userId,
273
+ purchase,
274
+ requestId,
275
+ });
276
+ if (typeof existing === "string") return { url: existing };
277
+ if (existing?.retryAllowed)
278
+ return { url: null, retryAllowed: true as const };
73
279
  try {
74
280
  const result = await billingRequest(
75
281
  key,
76
282
  "billing.attach",
77
283
  purchaseRequest(userId, purchase, returnUrl),
78
284
  );
79
- return {
80
- url: typeof result.payment_url === "string" ? result.payment_url : null,
81
- };
285
+ const url = checkoutUrl(result);
286
+ if (!url) throw new Error("Checkout response is unknown.");
287
+ await ctx.runMutation(finishRef, { userId, purchase, requestId, url });
288
+ return { url };
82
289
  } catch {
83
- throw new Error("Unable to start checkout. Please try again.");
290
+ throw new Error(
291
+ "Checkout status is unknown. Contact support before retrying.",
292
+ );
84
293
  }
85
294
  },
86
295
  });
@@ -0,0 +1,126 @@
1
+ import { getAuthUserId } from "@convex-dev/auth/server";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { ConvexError, v } from "convex/values";
4
+ import { components } from "../../backend/convex/_generated/api";
5
+ import {
6
+ mutation,
7
+ query,
8
+ internalMutation,
9
+ type QueryCtx,
10
+ } from "../../backend/convex/_generated/server";
11
+ import { paymentsEnabled } from "../features";
12
+ import { billingConfig, mobileWebCheckoutConfigured } from "./billing";
13
+ import {
14
+ hashBrowserCode as digest,
15
+ exchangeBrowserSession,
16
+ } from "./browser-session";
17
+
18
+ const ttl = 120_000;
19
+ const invalid = () =>
20
+ new ConvexError("This link expired. Open billing again from the app.");
21
+ const cleanupRef = makeFunctionReference<"mutation", { hash: string }, null>(
22
+ "billingHandoff:cleanup",
23
+ );
24
+ async function lookup(ctx: QueryCtx, code: string) {
25
+ if (!/^[A-Za-z0-9_-]{43}$/.test(code)) throw invalid();
26
+ const hash = await digest(code);
27
+ const row = await ctx.db
28
+ .query("billingHandoffs")
29
+ .withIndex("by_hash", (q) => q.eq("hash", hash))
30
+ .unique();
31
+ if (!row || row.expiresAt <= Date.now()) throw invalid();
32
+ return row;
33
+ }
34
+ export const create = mutation({
35
+ args: { purchase: v.union(v.literal("monthly"), v.literal("topup")) },
36
+ returns: v.object({ url: v.string(), userId: v.string() }),
37
+ handler: async (ctx, { purchase }) => {
38
+ const config = billingConfig(paymentsEnabled, process.env);
39
+ if (!config || !mobileWebCheckoutConfigured(paymentsEnabled, process.env))
40
+ throw new ConvexError("Website checkout is unavailable.");
41
+ const userId = await getAuthUserId(ctx);
42
+ const identity = await ctx.auth.getUserIdentity();
43
+ if (!userId || !identity) throw invalid();
44
+ const code = btoa(
45
+ String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32))),
46
+ )
47
+ .replaceAll("+", "-")
48
+ .replaceAll("/", "_")
49
+ .replaceAll("=", "");
50
+ const hash = await digest(code);
51
+ await ctx.db.insert("billingHandoffs", {
52
+ hash,
53
+ userId,
54
+ sessionId: identity.subject,
55
+ purchase,
56
+ expiresAt: Date.now() + ttl,
57
+ });
58
+ await ctx.scheduler.runAfter(ttl, cleanupRef, { hash });
59
+ const url = new URL("/billing-login", config.returnUrl);
60
+ url.hash = code;
61
+ return { url: url.href, userId };
62
+ },
63
+ });
64
+ export const inspect = query({
65
+ args: { code: v.string() },
66
+ returns: v.union(v.null(), v.object({ name: v.union(v.string(), v.null()) })),
67
+ handler: async (ctx, { code }) => {
68
+ try {
69
+ const row = await lookup(ctx, code);
70
+ const session = await ctx.runQuery(components.auth.public.getSession, {
71
+ sessionId: row.sessionId,
72
+ });
73
+ if (session?.userId !== row.userId) return null;
74
+ const email = await ctx.runQuery(
75
+ components.authEmail.verifiedEmails.getPrimaryEmail,
76
+ { userId: row.userId },
77
+ );
78
+ const username = await ctx.runQuery(
79
+ components.authUsername.public.getUsername,
80
+ { userId: row.userId },
81
+ );
82
+ const id = ctx.db.normalizeId("users", row.userId);
83
+ const user = id ? await ctx.db.get("users", id) : null;
84
+ return {
85
+ name:
86
+ email ?? user?.profile?.email ?? (username ? `@${username}` : null),
87
+ };
88
+ } catch {
89
+ return null;
90
+ }
91
+ },
92
+ });
93
+ export const redeem = mutation({
94
+ args: { code: v.string() },
95
+ returns: v.object({
96
+ accessToken: v.string(),
97
+ refreshToken: v.string(),
98
+ refreshTokenExpiresAt: v.number(),
99
+ userId: v.string(),
100
+ purchase: v.union(v.literal("monthly"), v.literal("topup")),
101
+ expiresAt: v.number(),
102
+ }),
103
+ handler: async (ctx, { code }) => {
104
+ const row = await lookup(ctx, code);
105
+ const tokens = await exchangeBrowserSession(ctx, row.sessionId, row.userId);
106
+ await ctx.db.delete("billingHandoffs", row._id);
107
+ return {
108
+ ...tokens,
109
+ purchase: row.purchase ?? "monthly",
110
+ expiresAt: row.expiresAt,
111
+ };
112
+ },
113
+ });
114
+ export const cleanup = internalMutation({
115
+ args: { hash: v.string() },
116
+ returns: v.null(),
117
+ handler: async (ctx, { hash }) => {
118
+ const row = await ctx.db
119
+ .query("billingHandoffs")
120
+ .withIndex("by_hash", (q) => q.eq("hash", hash))
121
+ .unique();
122
+ if (row && row.expiresAt <= Date.now())
123
+ await ctx.db.delete("billingHandoffs", row._id);
124
+ return null;
125
+ },
126
+ });
@@ -0,0 +1,12 @@
1
+ import { revenueCatConfigs } from "./revenuecat-contract";
2
+
3
+ /** Expose only non-secret routing metadata after an operator verifies the integration. */
4
+ export function nativeTopupRoutes(
5
+ enabled: boolean,
6
+ env: Record<string, string | undefined>,
7
+ ) {
8
+ if (env.REVENUECAT_CONSUMABLE_INTEGRATION_VERIFIED !== "true") return [];
9
+ return (revenueCatConfigs(enabled, env) ?? []).map(
10
+ ({ app, store, product }) => ({ app, store, product }),
11
+ );
12
+ }
@@ -24,6 +24,8 @@ export async function setupBilling(
24
24
  !/^[A-Z]{3}$/i.test(config.currency) ||
25
25
  !Number.isFinite(config.creditsPerDollar) ||
26
26
  config.creditsPerDollar <= 0 ||
27
+ !Number.isFinite(config.monthly.creditMultiplier) ||
28
+ config.monthly.creditMultiplier <= 0 ||
27
29
  config.monthly.planId === config.topup.planId ||
28
30
  (["monthly", "topup"] as const).some(
29
31
  (kind) =>
@@ -31,6 +31,25 @@ export function billingConfig(
31
31
  return { key, returnUrl: url.toString() };
32
32
  }
33
33
 
34
+ export function mobileWebCheckoutConfigured(
35
+ enabled: boolean,
36
+ env: Record<string, string | undefined>,
37
+ ) {
38
+ if (
39
+ !enabled ||
40
+ env.MOBILE_WEB_CHECKOUT_ENABLED !== "true" ||
41
+ env.REVENUECAT_SYNC_ENABLED !== "true"
42
+ )
43
+ return false;
44
+ try {
45
+ const config = billingConfig(true, env)!;
46
+ const url = new URL(config.returnUrl);
47
+ return url.protocol === "https:" && url.pathname === "/pricing";
48
+ } catch {
49
+ return false;
50
+ }
51
+ }
52
+
34
53
  export async function billingRequest(
35
54
  key: string,
36
55
  method: string,
@@ -55,6 +74,18 @@ export async function billingRequest(
55
74
  return result as Record<string, unknown>;
56
75
  }
57
76
 
77
+ export function checkoutUrl(result: Record<string, unknown>): string | null {
78
+ if (typeof result.payment_url !== "string") return null;
79
+ try {
80
+ const url = new URL(result.payment_url);
81
+ return url.protocol === "https:" && !url.username && !url.password
82
+ ? url.href
83
+ : null;
84
+ } catch {
85
+ return null;
86
+ }
87
+ }
88
+
58
89
  export function purchaseRequest(
59
90
  customerId: string,
60
91
  purchase: string,
@@ -0,0 +1,27 @@
1
+ import { components } from "../../backend/convex/_generated/api";
2
+ import type { MutationCtx } from "../../backend/convex/_generated/server";
3
+
4
+ export const browserSessionTtl = {
5
+ accessTokenTtlSeconds: 60,
6
+ refreshInactivityTimeoutSeconds: 30 * 24 * 60 * 60,
7
+ sessionMaxLifetimeSeconds: 30 * 24 * 60 * 60,
8
+ };
9
+ export async function hashBrowserCode(value: string) {
10
+ return Array.from(
11
+ new Uint8Array(
12
+ await crypto.subtle.digest("SHA-256", new TextEncoder().encode(value)),
13
+ ),
14
+ (byte) => byte.toString(16).padStart(2, "0"),
15
+ ).join("");
16
+ }
17
+ export function exchangeBrowserSession(
18
+ ctx: MutationCtx,
19
+ sessionId: string,
20
+ userId: string,
21
+ ) {
22
+ return ctx.runMutation(components.auth.public.exchangeSession, {
23
+ sessionId,
24
+ userId,
25
+ ...browserSessionTtl,
26
+ });
27
+ }