from __future__ import annotations from types import SimpleNamespace from quart import ( render_template, make_response, Blueprint, g, request ) from shared.infrastructure.data_client import fetch_data from shared.infrastructure.actions import call_action from shared.browser.app.authz import require_admin from shared.browser.app.utils.htmx import is_htmx_request def register(): bp = Blueprint("payments", __name__, url_prefix='/payments') @bp.context_processor async def inject_root(): return {} async def _load_payment_ctx(): """Load PageConfig SumUp data for the current page (from blog).""" post = (getattr(g, "post_data", None) or {}).get("post", {}) post_id = post.get("id") if not post_id: return {} raw_pc = await fetch_data( "blog", "page-config", params={"container_type": "page", "container_id": post_id}, required=False, ) pc = SimpleNamespace(**raw_pc) if raw_pc else None return { "sumup_configured": bool(pc and getattr(pc, "sumup_api_key", None)), "sumup_merchant_code": (getattr(pc, "sumup_merchant_code", None) or "") if pc else "", "sumup_checkout_prefix": (getattr(pc, "sumup_checkout_prefix", None) or "") if pc else "", } @bp.get("/") @require_admin async def home(**kwargs): ctx = await _load_payment_ctx() if not is_htmx_request(): html = await render_template("_types/payments/index.html", **ctx) else: html = await render_template("_types/payments/_oob_elements.html", **ctx) return await make_response(html) @bp.put("/") @require_admin async def update_sumup(**kwargs): """Update SumUp credentials for this page (writes to blog's db_blog).""" post = (getattr(g, "post_data", None) or {}).get("post", {}) post_id = post.get("id") if not post_id: return await make_response("Post not found", 404) form = await request.form merchant_code = (form.get("merchant_code") or "").strip() api_key = (form.get("api_key") or "").strip() checkout_prefix = (form.get("checkout_prefix") or "").strip() payload = { "container_type": "page", "container_id": post_id, "sumup_merchant_code": merchant_code, "sumup_checkout_prefix": checkout_prefix, } if api_key: payload["sumup_api_key"] = api_key await call_action("blog", "update-page-config", payload=payload) ctx = await _load_payment_ctx() html = await render_template("_types/payments/_main_panel.html", **ctx) return await make_response(html) return bp