mirror of
https://github.com/Louis-CFM/coucou.git
synced 2026-12-07 07:32:35 +09:00
Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT Co-authored-by: Claude <noreply@anthropic.com>
100 lines
5.3 KiB
JavaScript
100 lines
5.3 KiB
JavaScript
#!/usr/bin/env node
|
|
// Coucou Cloud's Stripe, set up once per mode (test, then live). Idempotent.
|
|
//
|
|
// read -rs STRIPE_SECRET_KEY && export STRIPE_SECRET_KEY
|
|
// node scripts/stripe-setup.mjs https://coucou-cloud.<you>.workers.dev | npx wrangler secret put STRIPE_WEBHOOK_SECRET
|
|
//
|
|
// It makes the "Cloud with your key" price (found by the Worker by its lookup key), the customer
|
|
// portal Coucou opens, and the webhook endpoint. The webhook's signing secret is the only thing it
|
|
// writes to stdout, so it goes straight into the Worker's secrets; everything else goes to stderr.
|
|
const key = process.env.STRIPE_SECRET_KEY;
|
|
const worker = (process.argv[2] ?? "").replace(/\/+$/, "");
|
|
if (!/^(sk|rk)_(test|live)_/.test(key ?? "")) { console.error("STRIPE_SECRET_KEY missing (a secret key sk_… or a restricted key rk_…)"); process.exit(1); }
|
|
if (!/^https:\/\//.test(worker)) { console.error("usage: node scripts/stripe-setup.mjs https://coucou-cloud.<you>.workers.dev"); process.exit(1); }
|
|
const log = (...a) => console.error(...a);
|
|
|
|
function form(params, prefix = "", out = new URLSearchParams()) {
|
|
for (const [k, v] of Object.entries(params)) {
|
|
if (v === undefined || v === null) continue;
|
|
const name = prefix ? `${prefix}[${k}]` : k;
|
|
if (Array.isArray(v)) v.forEach((x, i) => typeof x === "object" ? form(x, `${name}[${i}]`, out) : out.append(`${name}[${i}]`, String(x)));
|
|
else if (typeof v === "object") form(v, name, out);
|
|
else out.append(name, String(v));
|
|
}
|
|
return out;
|
|
}
|
|
async function stripe(method, path, params = {}) {
|
|
const body = form(params);
|
|
const res = await fetch(`https://api.stripe.com${path}${method === "GET" && [...body].length ? `?${body}` : ""}`, {
|
|
method, headers: { authorization: `Bearer ${key}`, "content-type": "application/x-www-form-urlencoded" },
|
|
...(method !== "GET" ? { body: body.toString() } : {}),
|
|
});
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(`${method} ${path}: ${data?.error?.message ?? res.status}`);
|
|
return data;
|
|
}
|
|
|
|
const mode = /^(sk|rk)_live_/.test(key) ? "LIVE" : "test";
|
|
log(`Stripe (${mode}) → ${worker}`);
|
|
|
|
// 1. The offer that can be bought today: the Cloud with your own Anthropic key, $9/month. Prices are
|
|
// in dollars (Coucou's people are all over the world); a price in another currency left under the
|
|
// lookup key by an earlier run is replaced (the key moves to the new price, the old one is archived).
|
|
const LOOKUP = "coucou_cloud_byok_monthly";
|
|
const prices = await stripe("GET", "/v1/prices", { lookup_keys: [LOOKUP], active: true });
|
|
const old = prices.data[0];
|
|
if (old && old.currency === "usd") log(`✓ price ${LOOKUP} already there (${old.id})`);
|
|
else {
|
|
const product = old ? { id: typeof old.product === "string" ? old.product : old.product.id }
|
|
: await stripe("POST", "/v1/products", {
|
|
name: "Coucou Cloud — with your API key",
|
|
description: "Your Mochis keep working while your Mac sleeps. The AI runs on your own Anthropic API key.",
|
|
metadata: { coucou: "cloud", plan: "byok" },
|
|
});
|
|
const price = await stripe("POST", "/v1/prices", {
|
|
product: product.id, currency: "usd", unit_amount: 900, recurring: { interval: "month" },
|
|
lookup_key: LOOKUP, transfer_lookup_key: true, tax_behavior: "inclusive", metadata: { plan: "byok" },
|
|
});
|
|
if (old) await stripe("POST", `/v1/prices/${old.id}`, { active: false });
|
|
log(`✓ product ${product.id}, price ${price.id} ($9/month)${old ? ` (replaced ${old.id}, ${old.currency})` : ""}`);
|
|
}
|
|
|
|
// 2. The customer portal Coucou opens: card, invoices, cancel at the end of the period.
|
|
// Made once, then kept up to date (headline, privacy and terms links).
|
|
const portal = {
|
|
business_profile: {
|
|
headline: "Coucou Cloud",
|
|
privacy_policy_url: "https://louis-cfm.github.io/coucou/privacy.html",
|
|
terms_of_service_url: "https://louis-cfm.github.io/coucou/terms.html",
|
|
},
|
|
features: {
|
|
invoice_history: { enabled: true },
|
|
payment_method_update: { enabled: true },
|
|
customer_update: { enabled: true, allowed_updates: ["email", "address", "name"] },
|
|
subscription_cancel: { enabled: true, mode: "at_period_end" },
|
|
},
|
|
};
|
|
const configs = await stripe("GET", "/v1/billing_portal/configurations", { limit: 100 });
|
|
const existing = configs.data.find(c => c.metadata?.coucou === "cloud" && c.active);
|
|
if (existing) {
|
|
await stripe("POST", `/v1/billing_portal/configurations/${existing.id}`, portal);
|
|
log(`✓ customer portal ${existing.id} updated`);
|
|
} else {
|
|
const c = await stripe("POST", "/v1/billing_portal/configurations", { ...portal, metadata: { coucou: "cloud" } });
|
|
log(`✓ customer portal ${c.id}`);
|
|
}
|
|
|
|
// 3. The webhook. Its secret is only given when it's made: an old one for this URL is replaced.
|
|
const url = `${worker}/stripe/webhook`;
|
|
const endpoints = await stripe("GET", "/v1/webhook_endpoints", { limit: 100 });
|
|
for (const e of endpoints.data.filter(e => e.url === url)) {
|
|
await stripe("DELETE", `/v1/webhook_endpoints/${e.id}`);
|
|
log(` (replaced the old endpoint ${e.id})`);
|
|
}
|
|
const endpoint = await stripe("POST", "/v1/webhook_endpoints", {
|
|
url, description: "Coucou Cloud",
|
|
enabled_events: ["checkout.session.completed", "customer.subscription.created", "customer.subscription.updated", "customer.subscription.deleted", "invoice.payment_failed"],
|
|
});
|
|
log(`✓ webhook ${endpoint.id} → ${url}`);
|
|
process.stdout.write(endpoint.secret);
|