"""Bean Box offline clone: catalogue, subscription configuration and safe checkout.""" from __future__ import annotations import hashlib import hmac import html import json import math import os import re import secrets import sys from pathlib import Path from urllib.parse import quote_plus from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, Response from starlette.staticfiles import StaticFiles ROOT = Path(__file__).resolve().parent if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from backend import business # noqa: E402 from backend.site_backend_integration import open_site_services # noqa: E402 from websitebench.site_backend import PaymentError # noqa: E402 from websitebench.local_clone_auth import AuthError # noqa: E402 SITE_ID = "bean-box" BACKEND, _AUTH = open_site_services() ADMIN_TOKEN = os.environ.get("WEBSITEBENCH_BEAN_BOX_ADMIN_TOKEN") or secrets.token_urlsafe(32) SESSION_COOKIE = "__Host-websitebench-bean-box-session" LOCAL_SESSION_COOKIE = "websitebench-bean-box-session" AUTH_COOKIE = "__Host-websitebench-bean-box-auth" LOCAL_AUTH_COOKIE = "websitebench-bean-box-auth" OWNER_RE = re.compile(r"^[A-Za-z0-9._:-]{8,240}$") FIXTURE = { "first_name": "Jamie", "last_name": "Rivera", "email": "jamie.rivera@example.test", "address": "101 Test Market St", "city": "Seattle", "state": "WA", "zip": "98101", } TASTES = [ ("curators-choice", "CURATOR'S CHOICE™", "Discover the full spectrum of specialty coffee, hand picked by our expert curator.", "641 coffees curated"), ("single-origin", "SINGLE ORIGIN", "Explore distinctive microlots from celebrated farms and producers.", "255 coffees curated"), ("light-bright", "LIGHT & BRIGHT", "Lively, floral and fruit-forward coffees.", "234 coffees curated"), ("medium-cozy", "MEDIUM & COZY", "Balanced, sweet and crowd-pleasing coffees.", "279 coffees curated"), ("dark-toasty", "DARK & TOASTY", "Deep chocolate and caramelized roast notes.", "128 coffees curated"), ("espresso", "ESPRESSO", "Rich coffees selected for concentrated brewing.", "121 coffees curated"), ("decaf", "DECAF", "Full flavor with a gentler finish.", "94 coffees curated"), ("cold-brew", "COLD BREW", "Smooth coffees that shine over ice.", "139 coffees curated"), ] QUANTITIES = [ ("solo-sipper", "The Solo Sipper", "1 × 12 oz", "About 24–36 cups"), ("duo", "The Duo", "2 × 12 oz", "Share or stock up"), ("large-solo", "Large Solo", "1 × 2 lb", "About 72–108 cups"), ("office-duo", "Office Duo", "2 × 2 lb", "Built for busy mornings"), ("trace-six-cup", "6-Cup Size", "6 cups", "Candidate-only compatibility option; current source exposes bag quantities"), ] def _load_session_secret() -> bytes: injected = os.environ.get("WEBSITEBENCH_BEAN_BOX_SESSION_SECRET") if injected: if len(injected) < 32: raise RuntimeError("session secret must contain at least 32 characters") return injected.encode() with BACKEND.lifecycle.connection(transaction=True) as connection: connection.execute( "CREATE TABLE IF NOT EXISTS bean_box_runtime_secrets(secret_name TEXT PRIMARY KEY, secret_value BLOB NOT NULL)" ) row = connection.execute( "SELECT secret_value FROM bean_box_runtime_secrets WHERE secret_name='session-hmac'" ).fetchone() if row is None: value = secrets.token_bytes(32) connection.execute( "INSERT INTO bean_box_runtime_secrets(secret_name, secret_value) VALUES ('session-hmac', ?)", (value,), ) else: value = bytes(row[0]) if len(value) < 32: raise RuntimeError("persisted session secret is invalid") return value SESSION_SECRET = _load_session_secret() app = FastAPI(title="Bean Box offline clone", docs_url=None, redoc_url=None, openapi_url=None) app.mount("/static", StaticFiles(directory=str(ROOT / "static")), name="static") @app.middleware("http") async def offline_headers(request: Request, call_next): cookie_name = LOCAL_SESSION_COOKIE if request.url.hostname in {"127.0.0.1", "localhost", "::1"} else SESSION_COOKIE raw_session = request.cookies.get(cookie_name, "") owner = _decode_session(raw_session) if owner is None: owner = f"fixture-actor-{secrets.token_hex(12)}" auth_cookie_name = LOCAL_AUTH_COOKIE if request.url.hostname in {"127.0.0.1", "localhost", "::1"} else AUTH_COOKIE supplied_auth_token = request.cookies.get(auth_cookie_name) auth_token, auth_info = _AUTH.ensure_session(supplied_auth_token) request.state.auth_token = auth_token request.state.auth_info = auth_info account = auth_info.get("account") if auth_info.get("authenticated") else None request.state.owner = f"account:{account['account_id']}" if account else owner response = await call_next(request) response.headers["Content-Security-Policy"] = "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'; connect-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'self'" response.headers["Referrer-Policy"] = "no-referrer" response.headers["X-Content-Type-Options"] = "nosniff" response.headers["X-Frame-Options"] = "DENY" if _decode_session(raw_session) is None: # Secure cookies are required on HTTPS deployments. Loopback capture is # intentionally allowed over HTTP so the offline browser can retain a # stable actor without weakening public deployments. response.set_cookie( cookie_name, _encode_session(owner), secure=request.url.hostname not in {"127.0.0.1", "localhost", "::1"}, httponly=True, samesite="lax", path="/", ) if auth_token != supplied_auth_token: response.set_cookie( auth_cookie_name, auth_token, secure=request.url.hostname not in {"127.0.0.1", "localhost", "::1"}, httponly=True, samesite="lax", path="/", ) return response def esc(value: object) -> str: return html.escape(str(value), quote=True) def money(minor: int) -> str: return f"${minor / 100:,.2f}" def owner_value(value: object | None) -> str: text = str(value or "public-fixture-actor") return text if OWNER_RE.fullmatch(text) else "public-fixture-actor" def _encode_session(owner: str) -> str: signature = hmac.new(SESSION_SECRET, owner.encode(), hashlib.sha256).hexdigest() return f"{owner}.{signature}" def _decode_session(value: str) -> str | None: owner, separator, signature = value.rpartition(".") if not separator or not OWNER_RE.fullmatch(owner): return None expected = hmac.new(SESSION_SECRET, owner.encode(), hashlib.sha256).hexdigest() return owner if hmac.compare_digest(signature, expected) else None def request_owner(request: Request) -> str: return str(getattr(request.state, "owner", "public-fixture-actor")) def authenticated_account(request: Request) -> dict | None: info = getattr(request.state, "auth_info", {}) return info.get("account") if info.get("authenticated") else None def _set_auth_cookie(request: Request, response: Response, token: str) -> None: loopback = request.url.hostname in {"127.0.0.1", "localhost", "::1"} response.set_cookie( LOCAL_AUTH_COOKIE if loopback else AUTH_COOKIE, token, secure=not loopback, httponly=True, samesite="lax", path="/", ) def _synthetic_email(value: str) -> str: normalized = value.strip().casefold() if not normalized.endswith("@example.test"): raise ValueError("Only synthetic @example.test email addresses are accepted.") return normalized def submitted_owner(request: Request, value: object | None) -> str: owner = request_owner(request) if value is not None and str(value) != owner: raise ValueError("session actor mismatch") return owner async def _json_object(request: Request) -> tuple[dict | None, JSONResponse | None]: content_type = request.headers.get("content-type", "").split(";", 1)[0].strip().casefold() if content_type != "application/json": return None, JSONResponse({"error": "application/json required"}, status_code=415) try: payload = await request.json() except (json.JSONDecodeError, UnicodeDecodeError, ValueError): return None, JSONResponse({"error": "malformed JSON"}, status_code=400) if not isinstance(payload, dict): return None, JSONResponse({"error": "JSON object required"}, status_code=422) return payload, None def cookie_layer() -> str: return """""" def cart_count(owner: str) -> int: with BACKEND.lifecycle.connection() as connection: return sum(int(row["quantity"]) for row in business.cart(connection, owner)) def header(owner: str, current: str = "") -> str: def nav(path: str, label: str, key: str) -> str: active = " aria-current='page'" if current == key else "" return f"{label}" return f"""""" def footer() -> str: return """""" def page(title: str, body: str, owner: str = "public-fixture-actor", current: str = "", cookies: bool = False, status: int = 200) -> HTMLResponse: document = f"{esc(title)}{header(owner,current)}
{body}
{footer()}{cookie_layer() if cookies else ''}" return HTMLResponse(document, status_code=status) def steps(active: int) -> str: labels = ("Tasting Experience", "Quantity", "Review") return "
" + "".join(f"
{i}{label}
" for i, label in enumerate(labels, 1)) + "
" def hidden_owner(owner: str) -> str: return f"" @app.get("/__websitebench/health", include_in_schema=False) async def health() -> Response: return Response('{"status":"ok"}', media_type="application/json") @app.get("/healthz", include_in_schema=False) async def healthz() -> JSONResponse: return JSONResponse({"ok": True, "site_id": SITE_ID}) @app.post("/__admin/reset", include_in_schema=False) async def reset(request: Request) -> JSONResponse: token = request.headers.get("X-WebsiteBench-Admin-Token", "") if ADMIN_TOKEN is None: return JSONResponse({"error": "admin reset disabled"}, status_code=503) if request.client and request.client.host not in {"127.0.0.1", "localhost", "::1", "testclient"}: return JSONResponse({"error": "loopback only"}, status_code=403) if not hmac.compare_digest(token, ADMIN_TOKEN): return JSONResponse({"error": "forbidden"}, status_code=403) def reset_all(connection) -> None: BACKEND.lifecycle.reset_embedded(connection, confirm_site_id=SITE_ID) business.reset_mutable(connection) _AUTH.reset_site_state(site_reset=reset_all, seed_accounts=[]) response = JSONResponse({"reset": True, "site_id": SITE_ID}) _set_auth_cookie(request, response, _AUTH.create_anonymous_session()) return response @app.get("/", include_in_schema=False) async def home(request: Request) -> HTMLResponse: owner = request_owner(request) body = """
4.5 ★   from 2,000+ coffee lovers

Say hello to your next favorite coffee.

Explore 240 searchable coffee selections modeled after a broad independent-roaster catalogue.

REVEAL YOUR NEXT FAVORITE
Specialty coffee bags with a pour-over carafe and mug on a marble counter

Personalized coffee subscriptions,
curated by our Cup of Excellence judge

MG

★★★★★   2079 reviews

Wake up to the world's best coffeeSupport the top local roasters in the U.S.Enjoy coffee fresh and bursting with flavorCurated from a broad selection of specialty coffeeExclusive member pricing and shipping perksPersonalize every delivery
PERSONALIZE A PLAN

We're on a mission to bring you better mornings

◒

Let us be your guide

Tell us how you brew and what you love. Our curator narrows the choices.

☕

Better coffee for everyone

Explore a rotating world of excellent coffees for better mornings at home.

♧

Farm-to-cup sustainability

Every coffee selection represents an independent roaster and responsible sourcing.

☆

Enjoy the perks

Flexible plans, member-style savings and complete control.

How it works

You personalize

Tell us your coffee preference and how often you'd like a shipment.

We curate

Our experts select coffees that fit the profile you choose.

You enjoy

Every Bean Box delivery arrives ready for you to explore.

Join today and get access to

Membership has its perks

CHOOSE MY PLAN

See why thousands trust us with their mornings

★★★★★   2079 REVIEWS

“An eye-opening way to discover coffees from around the world.”DESHANT
“The rotating selections make every delivery a welcome surprise.”VICTORIA
“Fresh coffee and flexible timing make mornings easier.”KATHERINE
“A responsive experience with consistently good coffee.”PATRICK
SEE MORE REVIEWS

Ratings & reviews

★★★★★   2079 reviews

NICHOLAS★★★★★

Great variety and a useful way to discover a better fit over time.

VICKI★★★★★

Convenient deliveries and interesting coffees from different roasters.

JOHN★★★★★

A long-running rotation that keeps each month interesting.

JOYEE★★★★★

Easy to adjust around changing coffee usage.

Frequently asked questions

WHAT IS BEAN BOX?

A specialty coffee discovery and subscription experience curated around your preferences.

HOW DOES THE COFFEE SUBSCRIPTION WORK?

Choose preparation, taste, quantity and cadence; then review before checkout.

CAN I CUSTOMIZE MY SUBSCRIPTION?

Yes. The observed configurator offers taste profiles, bag quantities and delivery every 2–6 weeks.

HOW FRESH IS THE COFFEE?

The source emphasizes delivery within days of roasting rather than shelf storage.

CAN I PAUSE OR CHANGE A PLAN?

You can move backward and change choices before confirmation.

READ THE FULL FAQ

Bringing you the world's best coffee roasters

Broadcast   ·   Camber   ·   Olympia   ·   Partners   ·   Sightglass   ·   Temple
""" return page("World's Best Coffee Subscription | Bean Box®", body, owner, cookies=True) def subscription_body(draft: dict, owner: str, error: str = "") -> str: active = int(draft["step"]) prefix = steps(active) + "
" if error: prefix += f"" if active == 1: cards = "".join(f"" for key,label,copy,count in TASTES) return prefix + f"

What's your ideal coffee tasting experience?

Choose a preparation, then a profile for your plan.

{hidden_owner(owner)}
{cards}
Back
" if active == 2: def render_quantity(item: tuple[str, str, str, str]) -> str: key, label, amount, detail = item candidate_class = " candidate-only" if key == "trace-six-cup" else "" candidate_badge = "TRACE COMPATIBILITY" if key == "trace-six-cup" else "" checked = "checked" if draft["quantity"] == key else "" return ( f"" ) quantities = "".join(render_quantity(item) for item in QUANTITIES) cadence = "".join(f"" for week in ("2","3","4","5","6")) return prefix + f"

How much coffee would you like per delivery?

Choose the amount and schedule that fits your mornings.

{hidden_owner(owner)}
{quantities}

Delivery frequency

{cadence}
" plan_cards = "".join(f"" for key,label,copy in (("pay-per-delivery","Pay-Per-Delivery","About $17 per 12 oz bag plus $5.45 shipping."),("annual","Annual Plan","About $15 per bag with shipping included."))) labels = {"whole-bean":"Whole Bean","freshly-ground":"Freshly Ground","curators-choice":"Curator's Choice™"} amount = business.draft_amount_minor(draft) return prefix + f"

One last step to better mornings

Review your coffee and choose how you would like to pay.

{hidden_owner(owner)}
{plan_cards}
Preparation{esc(labels.get(draft['preparation'],draft['preparation']))}Taste{esc(labels.get(draft['taste'],draft['taste']))}Quantity{esc(dict((k,a) for k,_,a,_ in QUANTITIES).get(draft['quantity']))}DeliveryEvery {esc(draft['cadence'])} weeksToday's total{money(amount)}
" @app.get("/coffee-subscription/configure", include_in_schema=False) async def configure(request: Request) -> HTMLResponse: owner = request_owner(request) with BACKEND.lifecycle.connection() as connection: draft = business.load_draft(connection, owner) return page("Bean Box® | Configure Your Coffee Subscription", subscription_body(draft, owner), owner, "subscriptions", cookies=True) @app.post("/coffee-subscription/configure", include_in_schema=False) async def configure_post(request: Request) -> Response: form = await request.form() try: owner = submitted_owner(request, form.get("owner")) except ValueError: return JSONResponse({"error": "session actor mismatch"}, status_code=403) action = str(form.get("action") or "") with BACKEND.lifecycle.connection(transaction=True) as connection: draft = business.load_draft(connection, owner) if action == "back-tasting": draft["step"] = 1 elif action == "back-quantity": draft["step"] = 2 elif action == "to-quantity": prep, taste = str(form.get("preparation") or ""), str(form.get("taste") or "") if prep not in {"whole-bean","freshly-ground"} or taste not in {item[0] for item in TASTES}: return page("Choose your coffee", subscription_body(draft, owner, "Choose a preparation and tasting experience."), owner, "subscriptions", status=422) draft.update(preparation=prep, taste=taste, step=2) elif action == "to-review": quantity, cadence = str(form.get("quantity") or ""), str(form.get("cadence") or "") if quantity not in {item[0] for item in QUANTITIES} or cadence not in {"2","3","4","5","6"}: return page("Choose quantity", subscription_body(draft, owner, "Choose a quantity and delivery frequency."), owner, "subscriptions", status=422) draft.update(quantity=quantity, cadence=cadence, step=3) elif action == "checkout": plan = str(form.get("plan") or "") if plan not in {"pay-per-delivery","annual"}: return page("Choose a plan", subscription_body(draft, owner, "Choose Pay-Per-Delivery or Annual Plan."), owner, "subscriptions", status=422) draft.update(plan=plan, step=3) business.save_draft(connection, owner, draft) return RedirectResponse("/checkout", status_code=303) business.save_draft(connection, owner, draft) return page("Bean Box® | Configure Your Coffee Subscription", subscription_body(draft, owner), owner, "subscriptions") def product_card(row) -> str: colors = ("#d7b77d", "#6f75aa", "#d8c9c0", "#9ca878", "#d6907a", "#b3a08b") return f"
{esc(row['roaster'].split()[0])}
COFFEE

{esc(row['name'])}

{esc(row['roast'].title())} · {esc(row['origin'])}{money(int(row['price_minor']))}
" @app.get("/coffee", include_in_schema=False) async def coffee(request: Request, q: str = "", roast: str = "", page_number: int = 1) -> HTMLResponse: owner = request_owner(request) raw_page = request.query_params.get("page", str(page_number)) try: current_page = int(raw_page or "1") except ValueError: return page("Catalogue | Bean Box®", "
Page must be a positive integer.
", owner, "coffee", status=422) if current_page < 1 or current_page > 1000: return page("Catalogue | Bean Box®", "
Page must be between 1 and 1000.
", owner, "coffee", status=422) with BACKEND.lifecycle.connection() as connection: rows, total = business.list_coffees(connection, q.strip()[:80], roast, current_page) grid = "".join(product_card(row) for row in rows) if rows else "

No coffees found

Try another coffee, roaster or roast profile.

CLEAR SEARCH
" pages = max(1, math.ceil(total / 18)) pager = "" sidebar = "".join(f"{key.title()} ({count})" for key,count in (("light",80),("medium",80),("dark",80))) mobile_filters = f"
FILTER
Roast profile{sidebar}Blend (80)Single origin (160)Clear filters
" body = f"

The best specialty coffee beans, curated by a Cup of Excellence judge

{mobile_filters}

{total} coffees · page {current_page} of {pages}

{grid}
{pager}
" return page("Specialty Coffee | Bean Box®", body, owner, "coffee", cookies=True) @app.get("/coffee/{slug}", include_in_schema=False) async def coffee_detail(slug: str, request: Request) -> HTMLResponse: owner = request_owner(request) with BACKEND.lifecycle.connection() as connection: row = business.coffee_by_slug(connection, slug) if row is None: return not_found(owner) colors = ("#d7b77d", "#6f75aa", "#d8c9c0", "#9ca878") body = f"
{esc(row['roaster'].split()[0])}
COFFEE
{esc(row['roaster'])}

{esc(row['name'])}

{esc(row['notes'])}

★★★★★ {row['rating']} · {row['reviews']} reviews

{esc(row['description'])}

{esc(row['roast'].title())} roast {esc(row['coffee_type'].title())}

{money(int(row['price_minor']))} · 12 oz

{hidden_owner(owner)}

← Back to all coffee

" return page(f"{row['name']} | Bean Box®", body, owner, "coffee") @app.post("/cart/add", include_in_schema=False) async def cart_add(request: Request) -> Response: form = await request.form() try: owner = submitted_owner(request, form.get("owner")) except ValueError: return JSONResponse({"error": "session actor mismatch"}, status_code=403) try: coffee_id = int(str(form.get("coffee_id") or "0")) except ValueError: coffee_id = 0 with BACKEND.lifecycle.connection(transaction=True) as connection: exists = connection.execute("SELECT 1 FROM bean_box_coffees WHERE id=?", (coffee_id,)).fetchone() if exists is None: return JSONResponse({"error":"coffee-not-found"}, status_code=404) business.add_cart(connection, owner, coffee_id) return RedirectResponse("/cart", status_code=303) @app.get("/cart", include_in_schema=False) async def cart_page(request: Request) -> HTMLResponse: owner = request_owner(request) with BACKEND.lifecycle.connection() as connection: rows = business.cart(connection, owner) if not rows: body = "

Your cart is empty

Explore our curated coffee collection or configure a subscription.

SHOP COFFEE CHOOSE A SUBSCRIPTION
" else: items = "".join(f"
COFFEE

{esc(row['name'])}

{esc(row['roaster'])} · Qty {row['quantity']}
{money(int(row['price_minor'])*int(row['quantity']))}
" for row in rows) total = sum(int(row['price_minor'])*int(row['quantity']) for row in rows) body = f"

Your cart

{items}

Subtotal: {money(total)}

KEEP SHOPPING
" return page("Your Cart | Bean Box®", body, owner) def checkout_body(draft: dict, owner: str, values: dict[str,str] | None = None, error: str = "", success: dict | None = None) -> str: amount = business.draft_amount_minor(draft) if success: return f"
Simulation complete

Your better mornings are queued

Order {esc(success['order_id'])} was recorded in the Bean Box account.

No subscription, email, address or payment was sent to Bean Box or any provider.

BACK TO SUBSCRIPTIONS
" values = values or {} scenario_value = values.get("scenario_id", "sandbox-approved") def val(name: str) -> str: return esc(values.get(name, "")) error_html = f"" if error else "" summary = f"

Order summary

{esc(draft['taste'].replace('-', ' ').title())}{esc(draft['preparation'].replace('-', ' ').title())}
Quantity{esc(dict((k,a) for k,_,a,_ in QUANTITIES).get(draft['quantity']))}
DeliveryEvery {esc(draft['cadence'])} weeks
Plan{esc(draft['plan'].replace('-', ' ').title())}
Total{money(amount)}

Simulation only. No card fields exist.

" fields = "".join(f"
" for name,label in (("first_name","First name"),("last_name","Last name"),("email","Email fixture"),("address","Address fixture"),("city","City"),("state","State"),("zip","ZIP code"))) options = "".join(f"" for key, label in (("sandbox-approved", "Simulated approval"), ("sandbox-declined", "Simulated decline"), ("sandbox-retry", "Simulated retry"))) form = f"
Safety fixture: use only the synthetic values supplied here.
{error_html}

Shipping details

{fields}

Payment simulation

No card number, CVV, expiry, wallet or banking input is accepted.

{hidden_owner(owner)}
" return f"

Checkout

{form}
" @app.get("/checkout", include_in_schema=False) async def checkout_get(request: Request) -> HTMLResponse: owner = request_owner(request) with BACKEND.lifecycle.connection() as connection: draft = business.load_draft(connection, owner) return page("Checkout | Bean Box®", checkout_body(draft, owner), owner) @app.post("/checkout", include_in_schema=False) async def checkout_post(request: Request) -> HTMLResponse: form = await request.form() try: owner = submitted_owner(request, form.get("owner")) except ValueError: return JSONResponse({"error": "session actor mismatch"}, status_code=403) items = [(str(key), str(value)) for key, value in form.multi_items()] keys = [key for key, _value in items] if len(keys) != len(set(keys)) or any(len(key) > 80 or len(value) > 500 for key, value in items): owner = request_owner(request) with BACKEND.lifecycle.connection() as connection: draft = business.load_draft(connection, owner) return page("Checkout rejected | Bean Box®", checkout_body(draft, owner, error="Duplicate or oversized checkout fields are forbidden."), owner, status=422) fields = dict(items) with BACKEND.lifecycle.connection(transaction=True) as connection: draft = business.load_draft(connection, owner) allowed_fields = set(FIXTURE) | {"owner", "scenario_id", "idempotency_key"} unknown = set(fields) - allowed_fields if unknown: return page("Checkout rejected | Bean Box®", checkout_body(draft, owner, fields, "Only the synthetic shipping fixture and opaque simulation fields are accepted; payment credentials are forbidden."), owner, status=422) mismatched = [name for name, expected in FIXTURE.items() if fields.get(name) != expected] if mismatched: return page("Synthetic fixture required | Bean Box®", checkout_body(draft, owner, fields, "Use the supplied synthetic fixture exactly; real or arbitrary personal data is not accepted."), owner, status=422) scenario = fields.get("scenario_id", "") if scenario not in {"sandbox-approved","sandbox-declined","sandbox-retry"}: return page("Choose payment simulation | Bean Box®", checkout_body(draft, owner, fields, "Choose an approved simulation scenario."), owner, status=422) idempotency = fields.get("idempotency_key", "")[:200] existing = connection.execute("SELECT order_id,status FROM bean_box_orders WHERE owner=? AND idempotency_key=?", (owner, idempotency)).fetchone() if existing: prior = connection.execute("SELECT amount_minor,snapshot_json FROM bean_box_orders WHERE order_id=?", (existing["order_id"],)).fetchone() prior_snapshot = json.loads(prior["snapshot_json"]) if int(prior["amount_minor"]) != business.draft_amount_minor(draft) or prior_snapshot.get("draft") != draft: return page("Checkout conflict | Bean Box®", checkout_body(draft, owner, fields, "This idempotency key is bound to a different order configuration."), owner, status=409) business.create_subscription(connection, owner, existing["order_id"], draft) return page("Order recorded | Bean Box®", checkout_body(draft, owner, success={"order_id": existing["order_id"]}), owner) amount = business.draft_amount_minor(draft) fingerprint = business.payment_fingerprint(owner, draft, amount) try: flow = BACKEND.payments.create_intent(owner=owner, amount_minor=amount, currency="USD", fingerprint=fingerprint, idempotency_key=f"create.{idempotency}", connection=connection) attempt = BACKEND.payments.attempt(flow_id=flow["flow_id"], owner=owner, amount_minor=amount, currency="USD", fingerprint=fingerprint, scenario_id=scenario, idempotency_key=f"attempt.{idempotency}.{scenario}", connection=connection) if attempt["status"] != "APPROVED": message = "The local payment simulation was declined. Choose Simulated approval and retry." if attempt["status"] == "DECLINED" else "The local payment simulation is retryable. Please retry or choose Simulated approval." status = 402 if attempt["status"] == "DECLINED" else 409 return page("Payment simulation | Bean Box®", checkout_body(draft, owner, fields, message), owner, status=status) BACKEND.payments.consume_approval(connection, flow_id=flow["flow_id"], owner=owner, amount_minor=amount, currency="USD", fingerprint=fingerprint) order_id = "BB-" + hashlib.sha256((owner + idempotency).encode()).hexdigest()[:10].upper() snapshot = {"site_id": SITE_ID, "draft": draft, "shipping_fixture": FIXTURE, "payment": {"adapter": "local-sandbox", "scenario_id": scenario, "is_simulation": True}} connection.execute("INSERT INTO bean_box_orders(order_id,owner,idempotency_key,payment_flow_id,status,amount_minor,snapshot_json,created_at) VALUES (?,?,?,?,?,?,?,datetime('now'))", (order_id, owner, idempotency, flow["flow_id"], "local-confirmed", amount, json.dumps(snapshot, sort_keys=True))) business.create_subscription(connection, owner, order_id, draft) except PaymentError as exc: connection.rollback() return page("Payment simulation error | Bean Box®", checkout_body(draft, owner, fields, str(exc)), owner, status=422) return page("Order recorded | Bean Box®", checkout_body(draft, owner, success={"order_id": order_id}), owner) @app.get("/api/catalogue/count", include_in_schema=False) async def catalogue_count() -> JSONResponse: with BACKEND.lifecycle.connection() as connection: count = connection.execute("SELECT COUNT(*) FROM bean_box_coffees").fetchone()[0] return JSONResponse({"site_id": SITE_ID, "coffee_records": count}) @app.post("/api/cart/items", include_in_schema=False) async def api_cart_add(request: Request) -> JSONResponse: payload, error = await _json_object(request) if error is not None: return error if not isinstance(payload, dict) or set(payload) - {"coffee_id", "owner"}: return JSONResponse({"error": "unknown field"}, status_code=422) owner = request_owner(request) if payload.get("owner") is not None and payload.get("owner") != owner: return JSONResponse({"error": "session actor mismatch"}, status_code=403) coffee_id = payload.get("coffee_id") if type(coffee_id) is not int or not 1 <= coffee_id <= 240: return JSONResponse({"error": "coffee_id required"}, status_code=422) with BACKEND.lifecycle.connection(transaction=True) as connection: if connection.execute("SELECT 1 FROM bean_box_coffees WHERE id=?", (coffee_id,)).fetchone() is None: return JSONResponse({"error": "coffee not found"}, status_code=404) business.add_cart(connection, owner, coffee_id) count = sum(int(row["quantity"]) for row in business.cart(connection, owner)) return JSONResponse({"site_id": SITE_ID, "cart_count": count}, status_code=201) @app.get("/api/cart", include_in_schema=False) async def api_cart(request: Request) -> JSONResponse: owner = request_owner(request) supplied = request.query_params.get("owner") if supplied is not None and supplied != owner: return JSONResponse({"error": "session actor mismatch"}, status_code=403) with BACKEND.lifecycle.connection() as connection: count = sum(int(row["quantity"]) for row in business.cart(connection, owner)) return JSONResponse({"site_id": SITE_ID, "cart_count": count}) @app.post("/api/checkout/validate", include_in_schema=False) async def api_checkout_validate(request: Request) -> JSONResponse: payload, error = await _json_object(request) if error is not None: return error if not isinstance(payload, dict) or set(payload) != {"scenario_id"}: return JSONResponse({"error": "only opaque scenario_id is accepted"}, status_code=422) scenario = payload.get("scenario_id") if scenario not in {"sandbox-approved", "sandbox-declined", "sandbox-retry"}: return JSONResponse({"error": "unknown local-sandbox scenario"}, status_code=422) return JSONResponse({"site_id": SITE_ID, "adapter": "local-sandbox", "scenario_id": scenario}) @app.get("/coffee-gifts", include_in_schema=False) async def gifts(request: Request) -> HTMLResponse: owner = request_owner(request) body = "
Share a better morning

Coffee gifts for every kind of coffee lover

Explore subscription gifts, tasting boxes, pairings and equipment picks.

GIVE A SUBSCRIPTION
" return page("Coffee Gifts | Bean Box®", body, owner, "gifts") @app.get("/coffee-equipment", include_in_schema=False) async def gear(request: Request) -> HTMLResponse: owner = request_owner(request) body = "
Brew it beautifully

Coffee equipment

Thoughtful brewers, kettles, mugs and scales selected for a better home setup.

PAIR WITH COFFEE
" return page("Coffee Equipment | Bean Box®", body, owner, "gear") @app.get("/faq", include_in_schema=False) async def faq(request: Request) -> HTMLResponse: owner = request_owner(request) questions = (("What is Bean Box?","A curated specialty coffee discovery and subscription experience."),("Can I get ground coffee?","Yes. Choose Freshly Ground during Tasting Experience."),("How often can coffee arrive?","Every 2, 3, 4, 5 or 6 weeks. Four weeks is the monthly option."),("Can I change my plan?","Return to the configurator before confirming."),("Is this a real payment?","No. Checkout accepts only synthetic simulation scenarios and makes zero remote requests.")) body = "

Frequently asked questions

" + "".join(f"
{esc(q)}

{esc(a)}

" for q,a in questions) + "
" return page("FAQ | Bean Box®", body, owner) @app.get("/roasters", include_in_schema=False) @app.get("/about", include_in_schema=False) @app.get("/contact", include_in_schema=False) @app.get("/blog", include_in_schema=False) @app.get("/resources", include_in_schema=False) @app.get("/terms", include_in_schema=False) @app.get("/privacy", include_in_schema=False) @app.get("/returns", include_in_schema=False) async def information_page(request: Request) -> HTMLResponse: owner = request_owner(request) slug = request.url.path.strip("/") content = { "roasters": ("Coffee roasters", "A directory-style view of the independent-roaster catalogue represented by the 240 searchable coffee fixtures."), "about": ("About this experience", "This experience preserves observed public structure and interactions without claiming source affiliation or redistribution rights."), "contact": ("Contact", "Contact submission and real email are outside the frozen anonymous scope. No message or address is collected here."), "blog": ("Coffee stories", "An index for the publicly observed editorial route; source articles were not redistributed."), "resources": ("Coffee resources", "Explore the working catalogue, configurator and FAQ without any runtime request to the source website."), "terms": ("Terms of service", "No source legal text is reproduced. This evaluation experience cannot create a real order or subscription."), "privacy": ("Privacy", "The site uses only a site-bound anonymous session. It rejects real payment data and does not send runtime traffic to the source."), "returns": ("Returns", "Real fulfillment and returns are unavailable because checkout creates no source order."), } title, copy = content[slug] body = f"
Bean Box

{esc(title)}

{esc(copy)}

EXPLORE COFFEE
" return page(f"{title} | Bean Box®", body, owner) @app.get("/account", include_in_schema=False) async def account(request: Request) -> HTMLResponse: owner = request_owner(request) account_info = authenticated_account(request) if account_info is None: body = "

My account

Sign in

Use a synthetic account to manage subscriptions.

SIGN IN

Create an account

Only @example.test addresses are accepted; no email leaves this site.

REGISTER

Forgot your password?

" return page("My Account | Bean Box®", body, owner) body = f"
Account

Welcome, {esc(account_info['display_name'])}

{esc(account_info['email_normalized'])}

Subscriptions

Modify, pause, skip, cancel or reactivate plans.

Order history

Review checkout records.

" return page("My Account | Bean Box®", body, owner) def auth_form(title: str, fields: str, action: str, error: str = "", note: str = "") -> str: alert = f"" if error else "" return f"

{esc(title)}

{alert}

{esc(note)}

{fields}
" @app.get("/account/register", include_in_schema=False) async def register_get(request: Request) -> HTMLResponse: fields = "
" return page("Register | Bean Box®", auth_form("Create an account", fields, "/account/register", note="Synthetic identity only. No source account or real email is used."), request_owner(request)) @app.post("/account/register", include_in_schema=False) async def register_post(request: Request) -> HTMLResponse: form = await request.form() token = str(request.state.auth_token) phase = str(form.get("phase") or "start") try: if phase == "start": email = _synthetic_email(str(form.get("email") or "")) _AUTH.start_registration(token, email=email, display_name=str(form.get("display_name") or ""), password=str(form.get("password") or "")) fields = "
Check your email for the verification code.
" return page("Verify registration | Bean Box®", auth_form("Verify your account", fields, "/account/register", note="The verification email was delivered."), request_owner(request)) _AUTH.verify_registration_code(token, str(form.get("code") or "")) result = _AUTH.complete_registration(token) response = RedirectResponse("/account", status_code=303) _set_auth_cookie(request, response, str(result["session_token"])) return response except (AuthError, ValueError) as exc: fields = "
" return page("Registration error | Bean Box®", auth_form("Create an account", fields, "/account/register", str(exc), "Use a unique @example.test fixture."), request_owner(request), status=422) @app.get("/account/signin", include_in_schema=False) async def signin_get(request: Request) -> HTMLResponse: fields = "
" return page("Sign in | Bean Box®", auth_form("Sign in", fields, "/account/signin", note="Synthetic accounts only."), request_owner(request)) @app.post("/account/signin", include_in_schema=False) async def signin_post(request: Request) -> HTMLResponse: form = await request.form() try: email = _synthetic_email(str(form.get("email") or "")) result = _AUTH.sign_in(str(request.state.auth_token), email=email, password=str(form.get("password") or "")) response = RedirectResponse("/account", status_code=303) _set_auth_cookie(request, response, str(result["session_token"])) return response except (AuthError, ValueError): fields = "
" return page("Sign in error | Bean Box®", auth_form("Sign in", fields, "/account/signin", "Credentials are invalid.", "Synthetic accounts only."), request_owner(request), status=401) @app.post("/account/signout", include_in_schema=False) async def signout(request: Request) -> Response: _AUTH.sign_out(str(request.state.auth_token)) response = RedirectResponse("/account", status_code=303) _set_auth_cookie(request, response, _AUTH.create_anonymous_session()) return response @app.get("/account/password-reset", include_in_schema=False) async def password_reset_get(request: Request) -> HTMLResponse: fields = "
" return page("Password recovery | Bean Box®", auth_form("Reset your password", fields, "/account/password-reset", note="Only a matching synthetic @example.test account can produce a verification code."), request_owner(request)) @app.post("/account/password-reset", include_in_schema=False) async def password_reset_post(request: Request) -> HTMLResponse: form = await request.form() token = str(request.state.auth_token) phase = str(form.get("phase") or "start") try: if phase == "start": email = _synthetic_email(str(form.get("email") or "")) _AUTH.start_password_reset(token, email=email) mail = _AUTH.local_mail_for_session(token, purpose="password-reset") if mail is None: return page("Password recovery | Bean Box®", auth_form("Check the account", "
", "/account/password-reset", note="If the account exists, a verification code is available. No email was sent."), request_owner(request)) fields = "
Check your email for the reset code.
" return page("Verify password reset | Bean Box®", auth_form("Choose a new local password", fields, "/account/password-reset", note="The reset email was delivered."), request_owner(request)) _AUTH.verify_password_reset_code(token, str(form.get("code") or "")) new_token = _AUTH.complete_password_reset(token, new_password=str(form.get("new_password") or "")) response = RedirectResponse("/account", status_code=303) _set_auth_cookie(request, response, new_token) return response except (AuthError, ValueError) as exc: fields = "
" return page("Password recovery error | Bean Box®", auth_form("Reset local password", fields, "/account/password-reset", str(exc), "Local synthetic accounts only."), request_owner(request), status=422) def _account_required(request: Request) -> HTMLResponse | None: if authenticated_account(request) is None: return page("Sign in required | Bean Box®", "
Sign in with an account to manage subscriptions.
SIGN IN
", request_owner(request), status=401) return None @app.get("/account/subscriptions", include_in_schema=False) async def subscription_management(request: Request) -> HTMLResponse: denied = _account_required(request) if denied: return denied owner = request_owner(request) with BACKEND.lifecycle.connection() as connection: rows = business.subscriptions(connection, owner) if not rows: content = "

No subscriptions yet

START A SUBSCRIPTION
" else: cards = [] for row in rows: draft = json.loads(row["draft_json"]) actions = "" if row["status"] == "active": actions += "" elif row["status"] == "paused": actions += "" else: actions += "" cards.append(f"

{esc(row['subscription_id'])}

Status: {esc(row['status'])} · skipped {row['skip_count']} · {esc(row['next_delivery_label'])}

{actions}
") content = "".join(cards) return page("Subscriptions | Bean Box®", f"

Manage subscriptions

{content}

View order history

", owner) @app.post("/account/subscriptions/{subscription_id}", include_in_schema=False) async def subscription_action(subscription_id: str, request: Request) -> Response: denied = _account_required(request) if denied: return denied form = await request.form() action = str(form.get("action") or "") changes = {"preparation": str(form.get("preparation") or ""), "cadence": str(form.get("cadence") or "")} if action == "modify" else None try: with BACKEND.lifecycle.connection(transaction=True) as connection: row = business.update_subscription(connection, request_owner(request), subscription_id, action, changes) if row is None: return not_found(request_owner(request)) except ValueError as exc: return page("Subscription error | Bean Box®", f"
{esc(exc)}
BACK
", request_owner(request), status=409) return RedirectResponse("/account/subscriptions", status_code=303) @app.get("/account/orders", include_in_schema=False) async def order_history(request: Request) -> HTMLResponse: denied = _account_required(request) if denied: return denied owner = request_owner(request) with BACKEND.lifecycle.connection() as connection: rows = business.orders(connection, owner) items = "".join(f"

{esc(row['order_id'])}

{esc(row['status'])} · {money(int(row['amount_minor']))} · {esc(row['created_at'])}

" for row in rows) or "
No orders yet.
" return page("Order history | Bean Box®", f"

Order history

{items}
", owner) def not_found(owner: str = "public-fixture-actor") -> HTMLResponse: return page("Page not found | Bean Box®", "

This page needs a fresh grind

We couldn't find that page.

RETURN HOME
", owner, status=404) @app.exception_handler(404) async def handle_404(request: Request, exc: Exception) -> HTMLResponse: return not_found(request_owner(request))