85 lines · 3.4 KB
/**
 * 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<string, string>,
): Promise<string> {
  const existing = await stripe.prices.list({ lookup_keys: [lookupKey], limit: 1 });