diff --git a/src/main.el b/src/main.el index 7bb0ad9..087cdd5 100644 --- a/src/main.el +++ b/src/main.el @@ -46,7 +46,7 @@ from safety import { safety } // ── Founding counter ────────────────────────────────────────────────────────── let FOUNDING_TOTAL: Int = 1000 -let FOUNDING_SOLD: Int = 47 +let FOUNDING_SOLD: Int = 47 // floor — real count comes from Stripe at startup // ── Founding count helpers ───────────────────────────────────────────────────── @@ -66,6 +66,47 @@ fn get_total() -> Int { return parse_int(s) } +// fetch_founding_count_stripe — queries Stripe PaymentIntents search for the +// real founding count. Uses the secret key with Bearer auth. Falls back to the +// FOUNDING_SOLD floor if Stripe is unavailable or not yet configured. +fn fetch_founding_count_stripe(stripe_key: String) -> Int { + if str_eq(stripe_key, "") { + return FOUNDING_SOLD + } + let url: String = "https://api.stripe.com/v1/payment_intents/search?query=metadata%5B%27plan%27%5D%3A%27founding%27+AND+status%3A%27succeeded%27" + let resp: String = http_get_auth(url, stripe_key) + if str_eq(resp, "") { + return FOUNDING_SOLD + } + let count: Int = json_get_int(resp, "total_count") + if count <= 0 { + return FOUNDING_SOLD + } + return count +} + +// load_founding_count — reads from persist file first (survives restarts), +// falls back to Stripe query if file doesn't exist yet. +fn load_founding_count(sold_file: String, stripe_key: String) -> Int { + if fs_exists(sold_file) { + let s: String = str_trim(fs_read(sold_file)) + let n: Int = parse_int(s) + if n > 0 { + return n + } + } + return fetch_founding_count_stripe(stripe_key) +} + +// persist_founding_count — writes current sold count to disk so it survives +// server restarts. Called at startup and on every Stripe webhook increment. +fn persist_founding_count(sold: Int) { + let sold_file: String = state_get("__founding_sold_file__") + if !str_eq(sold_file, "") { + fs_write(sold_file, int_to_str(sold)) + } +} + // ── Page assembly ───────────────────────────────────────────────────────────── fn page(sold: Int, total: Int) -> String { @@ -207,7 +248,7 @@ fn handle_request(method: String, path: String, body: String) -> String { if str_contains(body, "\"professional\"") { plan = "professional" } - let origin: String = "http://localhost:3001" + let origin: String = state_get("__origin__") let price_id: String = "" let mode: String = "subscription" if str_eq(plan, "founding") { @@ -269,6 +310,7 @@ fn handle_request(method: String, path: String, body: String) -> String { let current_sold: Int = get_sold() let new_sold: Int = current_sold + 1 state_set("__founding_sold__", int_to_str(new_sold)) + persist_founding_count(new_sold) println("[webhook] founding sold: " + int_to_str(new_sold)) } // Forward to license API for key provisioning. @@ -304,18 +346,35 @@ fn handle_request(method: String, path: String, body: String) -> String { // ── Startup ─────────────────────────────────────────────────────────────────── // -// 1. Generate the landing page HTML using El components. -// 2. Write it to src/index.html (El-generated, never hand-authored). -// 3. Set __html_file__ so the El HTTP runtime serves it for GET /. -// The runtime also uses __html_file__'s parent dir for /assets/* serving. +// Order matters: +// 1. Load Stripe config + origin from env (needed before founding count) +// 2. Seed founding count from file or Stripe API +// 3. Generate all HTML with the real count +// 4. Register with El HTTP runtime and serve let src_dir: String = cwd() + "/src" let html_path: String = src_dir + "/index.html" -// Generate page HTML with founding counter seed values. -let page_html: String = page(FOUNDING_SOLD, FOUNDING_TOTAL) +// Stripe config from environment — loaded first so founding count can use it. +let stripe_key: String = env("STRIPE_SECRET_KEY") +let stripe_pub_key: String = env("STRIPE_PUBLISHABLE_KEY") +let stripe_price_founding: String = env("STRIPE_PRICE_FOUNDING") +let stripe_price_professional: String = env("STRIPE_PRICE_PROFESSIONAL") +let license_api_url: String = env("NEURON_LICENSE_API_URL") +let resend_api_key: String = env("RESEND_API_KEY") -// Write El-generated HTML to disk. +// Origin — drives Stripe redirect URLs; never hardcoded to localhost. +let neuron_origin: String = env("NEURON_ORIGIN") +if str_eq(neuron_origin, "") { + neuron_origin = "https://neurontechnologies.ai" +} + +// Founding count — seeded from persist file or live Stripe query. +let sold_file: String = src_dir + "/founding_sold.txt" +let real_sold: Int = load_founding_count(sold_file, stripe_key) + +// Generate all page HTML using the real founding count. +let page_html: String = page(real_sold, FOUNDING_TOTAL) fs_write(html_path, page_html) // Generate about page HTML. @@ -335,24 +394,19 @@ state_set("__about_html_file__", about_html_path) state_set("__terms_html_file__", terms_html_path) state_set("__enterprise_terms_html_file__", ent_terms_html_path) state_set("__src_dir__", src_dir) -state_set("__founding_sold__", int_to_str(FOUNDING_SOLD)) +state_set("__stripe_secret_key__", stripe_key) +state_set("__stripe_publishable_key__", stripe_pub_key) +state_set("__stripe_price_founding__", stripe_price_founding) +state_set("__stripe_price_professional__", stripe_price_professional) +state_set("__license_api_url__", license_api_url) +state_set("__resend_api_key__", resend_api_key) +state_set("__origin__", neuron_origin) +state_set("__founding_sold_file__", sold_file) +state_set("__founding_sold__", int_to_str(real_sold)) state_set("__founding_total__", int_to_str(FOUNDING_TOTAL)) +persist_founding_count(real_sold) -// Stripe config from environment. -let stripe_key: String = env("STRIPE_SECRET_KEY") -let stripe_pub_key: String = env("STRIPE_PUBLISHABLE_KEY") -let stripe_price_founding: String = env("STRIPE_PRICE_FOUNDING") -let stripe_price_professional: String = env("STRIPE_PRICE_PROFESSIONAL") -let license_api_url: String = env("NEURON_LICENSE_API_URL") -let resend_api_key: String = env("RESEND_API_KEY") -state_set("__stripe_secret_key__", stripe_key) -state_set("__stripe_publishable_key__", stripe_pub_key) -state_set("__stripe_price_founding__", stripe_price_founding) -state_set("__stripe_price_professional__", stripe_price_professional) -state_set("__license_api_url__", license_api_url) -state_set("__resend_api_key__", resend_api_key) - -println(color_bold("Neuron landing") + " - http://localhost:3001") +println(color_bold("Neuron landing") + " - " + neuron_origin) println(" HTML generated by El → " + html_path) println(" About generated by El → " + about_html_path) println(" Terms generated by El → " + terms_html_path)