"""Provider metadata and billing compatibility helpers for protocol checkout.""" from __future__ import annotations import os import re import uuid from typing import Any def _runtime_meta(context: dict[str, Any] | None, session_id: str) -> tuple[dict[str, str], dict[str, str]]: context = dict(context or {}) runtime = str(context.get("runtime_version") or "3") common = { "runtime_version": runtime, "guid": str(context.get("guid") or uuid.uuid4()), "muid": str(context.get("muid") or uuid.uuid4()), "sid": str(context.get("sid") or uuid.uuid4()), } attributes = { "client_session_id": str(context.get("stripe_js_id") or uuid.uuid4()), "checkout_session_id": str(context.get("checkout_session_id") or session_id), "checkout_config_id": str(context.get("elements_session_config_id") or context.get("config_id") or ""), "elements_session_id": str(context.get("elements_session_id") or ""), "elements_session_config_id": str(context.get("elements_session_config_id") or context.get("config_id") or ""), } return common, attributes def _billing_name(email: str) -> str: local = re.sub(r"[^A-Za-z ]+", " ", str(email or "").split("@", 1)[0]) words = [word.capitalize() for word in local.split() if word] return " ".join(words[:3]) or "Checkout Customer" def default_billing(country: str = "PH", email: str = "", *, real_random: bool = False) -> dict[str, Any]: country = str(country or "PH").upper() if country != "PH": raise ValueError("provider billing country must be PH") return { "name": os.getenv("PH_BILLING_NAME", "").strip() or _billing_name(email), "email": str(email or "").strip(), "phone": os.getenv("PH_BILLING_PHONE", "+6325550100").strip(), "address": { "country": "PH", "line1": os.getenv("PH_BILLING_LINE1", "Ayala Avenue").strip(), "line2": os.getenv("PH_BILLING_LINE2", "").strip(), "city": os.getenv("PH_BILLING_CITY", "Makati").strip(), "state": os.getenv("PH_BILLING_STATE", "Metro Manila").strip(), "postal_code": os.getenv("PH_BILLING_POSTAL_CODE", "1226").strip(), }, }