caelum0x 247e860a16 feat(console): Railyard console with Scribase sign-in and the Dodo test-mode money ladder (RYP-D1, Y-2)
Next.js console for Cloudflare Workers (OpenNext config, wrangler.jsonc)
with data in the console's own Scribase project, schema railyard
(migration 0001: orgs, org_members, apps, deploys, entitlements,
billing_events, api_tokens, device_codes; idempotent, RLS on, no anon
grants).

- Sign-in with Google or GitHub through Scribase Auth (PKCE), user read
  from the auth server, org and entitlements row created, signed
  HttpOnly session.
- Money ladder (pure, immutable): $1 card check credited back to the
  customer wallet, 14-day trial with the plan's limits, $19 a month or
  $190 a year, pause switch and one-click cancel (ends at the next
  billing date; a trial ends now).
- Webhook: Standard Webhooks signature and timestamp verified, replays
  ignored by unique message id, signed checkout metadata, other brands
  ignored, a refund of the card check reverses the credit and the
  trial, a failed wallet call releases the id for the provider's retry.
- Apps, Usage and Billing screens, one primary action each; Add app
  shows the deploy command and the Insights tag in two clicks.
- CLI device-flow login and org-scoped hashed tokens for Y-3.
- Test mode only: the live payment host is refused unless the owner
  sets RAILYARD_DODO_LIVE=1.

Tests (node --test, 25): journey from sign-in to entitlements and the
snippet, refund, pause and cancel, webhook security, ladder, tokens and
org isolation, PostgREST repo, a component test of Add app with the copy
lint, a next build grep for sentinel secrets in the client bundle, and
the migration applied twice on a scratch Postgres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 15:35:24 +03:00

96 lines
4.2 KiB
TypeScript

// Dodo Payments client for the console (brand "railyard"). Test mode by
// default: the live host is refused unless the owner sets RAILYARD_DODO_LIVE=1.
import { DODO_LIVE_BASE } from "./config.ts";
import { b64url, constantTimeEqual, hmacSha256 } from "./crypto.ts";
type Fetch = typeof fetch;
export class DodoError extends Error {
status: number;
constructor(message: string, status: number) {
super(message);
this.status = status;
}
}
export interface CheckoutInput {
productId: string;
email: string;
name?: string;
metadata: Record<string, string>;
returnUrl: string;
}
export class Dodo {
base: string;
apiKey: string;
fetchImpl: Fetch;
constructor(opts: { base: string; apiKey: string; allowLive: boolean; fetchImpl?: Fetch }) {
if (opts.base.replace(/\/$/, "") === DODO_LIVE_BASE && !opts.allowLive) throw new Error("live Dodo calls are off (test mode)");
this.base = opts.base.replace(/\/$/, "");
this.apiKey = opts.apiKey;
this.fetchImpl = opts.fetchImpl ?? fetch;
}
async call<T>(method: string, path: string, body?: unknown): Promise<T> {
const res = await this.fetchImpl(`${this.base}${path}`, {
method,
headers: { authorization: `Bearer ${this.apiKey}`, "content-type": "application/json" },
body: body === undefined ? undefined : JSON.stringify(body),
});
const text = await res.text();
if (!res.ok) throw new DodoError(`payment provider ${method} ${path.split("/").slice(0, 2).join("/")} failed (${res.status})`, res.status);
return (text ? JSON.parse(text) : {}) as T;
}
async createCheckout(input: CheckoutInput): Promise<{ checkoutUrl: string; sessionId: string | null }> {
const out = await this.call<{ checkout_url?: string; session_id?: string }>("POST", "/checkouts", {
product_cart: [{ product_id: input.productId, quantity: 1 }],
customer: { email: input.email, name: input.name ?? input.email.split("@")[0] },
minimal_address: true,
metadata: input.metadata,
return_url: input.returnUrl,
});
if (!out.checkout_url || !/^https:\/\//.test(out.checkout_url)) throw new DodoError("checkout did not return a URL", 502);
return { checkoutUrl: out.checkout_url, sessionId: out.session_id ?? null };
}
async wallet(customerId: string, entry: { type: "credit" | "debit"; cents: number; idempotencyKey: string; reason: string }) {
return this.call("POST", `/customers/${encodeURIComponent(customerId)}/wallets/ledger-entries`, {
amount: entry.cents,
currency: "USD",
entry_type: entry.type,
idempotency_key: entry.idempotencyKey,
reason: entry.reason,
});
}
/** Pause or resume: Dodo requires `status` on its own in the body. */
async setPaused(subscriptionId: string, paused: boolean) {
return this.call("PATCH", `/subscriptions/${encodeURIComponent(subscriptionId)}`, { status: paused ? "paused" : "active" });
}
/** One-click cancel keeps the paid time: it ends at the next billing date. */
async cancelAtPeriodEnd(subscriptionId: string) {
return this.call("PATCH", `/subscriptions/${encodeURIComponent(subscriptionId)}`, { cancel_at_next_billing_date: true });
}
}
// Checkout metadata is signed so a payment link with made-up metadata cannot
// name an org (anyone can pay for a product with metadata of their choosing).
export async function signMetadata(secret: string, orgId: string, kind: string, requestId: string): Promise<string> {
return b64url(await hmacSha256(`${secret}:checkout-metadata`, `${orgId}.${kind}.${requestId}`));
}
export async function verifiedMetadataOrg(secret: string, metadata: unknown): Promise<{ orgId: string; kind: string; interval: string | null } | null> {
if (!metadata || typeof metadata !== "object") return null;
const m = metadata as Record<string, unknown>;
const { org_id: orgId, kind, request_id: requestId, sig, service } = m;
if (service !== "railyard" || typeof orgId !== "string" || typeof kind !== "string" || typeof requestId !== "string" || typeof sig !== "string") return null;
const expected = await signMetadata(secret, orgId, kind, requestId);
if (!constantTimeEqual(sig, expected)) return null;
return { orgId, kind, interval: typeof m.interval === "string" ? m.interval : null };
}