62 lines
2.3 KiB
Python
62 lines
2.3 KiB
Python
"""
|
|
billing_status.py — route: GET /internal/billing-status/<tenant_slug>.
|
|
|
|
Read-only: subscription status, next-bill date, recent invoices, and the
|
|
reactivation Payment Link, for the billing Web Page (see
|
|
provision_client.py's "Get Billing Status" Server Script, which is the
|
|
only caller — this route is never hit directly by a browser). Not
|
|
reverse-proxied to the public internet in production, the same as every
|
|
route here except /webhook/stripe/<tenant_slug> — see
|
|
INSTALLATION_PLAYBOOK.md's billing stage.
|
|
|
|
Inbound: GET /internal/billing-status/<tenant_slug>
|
|
Response shape: see stripe_status.get_billing_status's docstring for the
|
|
success shape, plus a "trace_id"; {"error", "trace_id"}
|
|
on failure
|
|
"""
|
|
|
|
import logging
|
|
|
|
import sentry_sdk
|
|
from flask import Blueprint, current_app, g, jsonify
|
|
|
|
from .stripe_status import StripeStatusError, get_billing_status
|
|
|
|
logger = logging.getLogger("billing_status")
|
|
|
|
billing_status_bp = Blueprint("billing_status", __name__)
|
|
|
|
|
|
@billing_status_bp.route("/internal/billing-status/<tenant_slug>", methods=["GET"])
|
|
def billing_status(tenant_slug):
|
|
"""
|
|
Returns this tenant's current billing status.
|
|
|
|
Data flow:
|
|
tenant_slug (str) — URL segment identifying which client
|
|
returns (Flask response) — see module docstring for shapes
|
|
|
|
Steps:
|
|
1. Reject with 404 if tenant_slug matches no configured tenant
|
|
2. Fetch and return this tenant's billing status
|
|
"""
|
|
trace_id = g.trace_id
|
|
registry = current_app.config["TENANT_REGISTRY"]
|
|
|
|
# Step 1: an unknown tenant is rejected outright
|
|
tenant = registry.get(tenant_slug)
|
|
if tenant is None:
|
|
logger.warning("trace_id=%s rejected: unknown tenant %s", trace_id, tenant_slug)
|
|
return jsonify({"error": "unknown tenant", "trace_id": trace_id}), 404
|
|
|
|
# Step 2: fetch — a failure here never leaks a raw Stripe error to the caller
|
|
try:
|
|
status = get_billing_status(tenant)
|
|
except StripeStatusError as exc:
|
|
logger.error("trace_id=%s tenant=%s billing status fetch failed: %s", trace_id, tenant_slug, exc)
|
|
sentry_sdk.capture_exception(exc)
|
|
return jsonify({"error": "failed to fetch billing status", "trace_id": trace_id}), 502
|
|
|
|
status["trace_id"] = trace_id
|
|
return jsonify(status), 200
|