/** * Creates (or finds) the Stripe test-mode objects gitfor uses: a Pro monthly price and six prepaid packs, * then writes STRIPE_PRICE_PRO and STRIPE_TOPUP_PRICES into .dev.vars. Idempotent via price lookup keys. * * STRIPE_SETUP_KEY=sk_test_... bun run scripts/stripe-test-setup.ts [--write .dev.vars] * * Refuses anything but a test-mode key (sk_test_ / rk_test_) and never prints the key. */ import { existsSync, readFileSync, writeFileSync } from "node:fs"; import Stripe from "stripe"; const HOUR = 3600; const MILLION = 1_000_000; type PackSpec = { id: string; meter: string; quantity: number; cents: number; name: string }; const PACKS: PackSpec[] = [ { id: "compute_10h", meter: "compute.runner", quantity: 10 * HOUR, cents: 500, name: "10 runner hours" }, { id: "compute_50h", meter: "compute.runner", quantity: 50 * HOUR, cents: 2000, name: "50 runner hours" }, { id: "compute_200h", meter: "compute.runner", quantity: 200 * HOUR, cents: 7000, name: "200 runner hours" }, { id: "tokens_10m", meter: "ai.tokens", quantity: 10 * MILLION, cents: 500, name: "10M AI tokens" }, { id: "tokens_50m", meter: "ai.tokens", quantity: 50 * MILLION, cents: 2000, name: "50M AI tokens" }, { id: "tokens_200m", meter: "ai.tokens", quantity: 200 * MILLION, cents: 7000, name: "200M AI tokens" }, ]; const key = process.env.STRIPE_SETUP_KEY ?? ""; if (!/^(sk|rk)_test_/.test(key)) { console.error("STRIPE_SETUP_KEY must be a Stripe test-mode key (sk_test_ or rk_test_); refusing to continue."); process.exit(1); } const stripe = new Stripe(key); async function findOrCreatePrice( lookupKey: string, product: string, cents: number, recurring: boolean, metadata: Record, ): Promise { const existing = await stripe.prices.list({ lookup_keys: [lookupKey], limit: 1 }); if (existing.data[0]) return existing.data[0].id; const price = await stripe.prices.create({ currency: "usd", unit_amount: cents, lookup_key: lookupKey, metadata, ...(recurring ? { recurring: { interval: "month" } } : {}), product_data: { name: product, metadata }, }); return price.id; } const pro = await findOrCreatePrice("gitfor_pro_monthly", "gitfor Pro", 1000, true, { plan: "pro" }); const topups: Record = {}; for (const pack of PACKS) { const price = await findOrCreatePrice(`gitfor_${pack.id}`, `gitfor ${pack.name}`, pack.cents, false, { pack: pack.id, meter: pack.meter, quantity: String(pack.quantity), }); topups[pack.id] = { price, meter: pack.meter, quantity: pack.quantity, label: pack.name }; } const output = { STRIPE_PRICE_PRO: pro, STRIPE_TOPUP_PRICES: JSON.stringify(topups) }; console.log( JSON.stringify( { pro, packs: Object.fromEntries(Object.entries(topups).map(([id, pack]) => [id, pack.price])) }, null, 2, ), ); const writeIndex = process.argv.indexOf("--write"); if (writeIndex !== -1) { const file = process.argv[writeIndex + 1] ?? ".dev.vars"; let text = existsSync(file) ? readFileSync(file, "utf8") : ""; for (const [name, value] of Object.entries(output)) { const line = `${name}='${value}'`; const pattern = new RegExp(`^${name}=.*$`, "m"); text = pattern.test(text) ? text.replace(pattern, line) : `${text.replace(/\n?$/, "\n")}${line}\n`; } writeFileSync(file, text); console.log(`Wrote ${Object.keys(output).join(", ")} to ${file}`); }