Fastjmz
Stripe responsible billing direction
Prompt
fastapi.responsesimport os from fastapi import FastAPI, HTTPException, Query from fastapi.responses import JSONResponse, RedirectResponse import stripe stripe.api_key = os.environ["STRIPE_SECRET_KEY"] DEFAULT_EMAIL = "jameschristopherfrederick7@gmail.com" DEFAULT_CONNECTED_ACCOUNT = "acct_1USSCQChExH6A2p1" DEFAULT_RETURN_URL = os.environ.get("BILLING_PORTAL_RETURN_URL", "https://example.com/billing") app = FastAPI() def _resolve_customer_id(email: str, stripe_account: str) -> str: customers = stripe.Customer.list( email=email, limit=1, stripe_account=stripe_account, ) if not customers.data: raise HTTPException( status_code=404, detail=f"No Stripe customer found for {email} on account {stripe_account}", ) return customers.data[0].id @app.post("/billing/portal") @app.get("/billing/portal") def create_billing_portal_session( email: str = Query(default=DEFAULT_EMAIL), stripe_account: str = Query(default=DEFAULT_CONNECTED_ACCOUNT), return_url: str = Query(default=DEFAULT_RETURN_URL), redirect: bool = Query(default=False), ): try: customer_id = _resolve_customer_id(email=email, stripe_account=stripe_account) session = stripe.billing_portal.Session.create( customer=customer_id, return_url=return_url, stripe_account=stripe_account, ) except HTTPException: raise except stripe.error.StripeError as exc: raise HTTPException(status_code=502, detail=exc.user_message or str(exc)) from exc if redirect: return RedirectResponse(url=session.url, status_code=303) return JSONResponse( content={ "id": session.id, "url": session.url, "customer": customer_id, "email": email, "stripe_account": stripe_account, } ) CREATE TABLE IF NOT EXISTS stripe_account_links ( stripe_account_id VARCHAR(64) NOT NULL PRIMARY KEY, account_email VARCHAR(255) NOT NULL, stripe_customer_id VARCHAR(255) NULL, stripe_subscription_id VARCHAR(255) NULL, subscription_status VARCHAR(64) NOT NULL DEFAULT 'none', updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO stripe_account_links (stripe_account_id, account_email) VALUES ('acct_1USSCQChExH6A2p1', 'jameschristopherfrederick7@gmail.com') ON DUPLICATE KEY UPDATE account_email = VALUES(account_email); CREATE TABLE IF NOT EXISTS stripe_audit_log ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, stripe_account_id VARCHAR(64) NOT NULL, account_email VARCHAR(255) NOT NULL, source_type ENUM('llms_doc', 'webhook_event', 'checkout_session') NOT NULL, external_id VARCHAR(255) NOT NULL, sha256_hash CHAR(64) NOT NULL, status VARCHAR(64) NOT NULL, payload_summary TEXT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY uq_acct_source_external (stripe_account_id, source_type, external_id), KEY idx_account_email (account_email) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; mkdir -p ~/projects/slingshot-3d cd ~/projects/slingshot-3d git init -b main git commit --allow-empty -m "Initialize slingshot-3d"