Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
"""Creem: checkout creation + webhook signature verification."""
|
|
|
|
import hashlib
|
|
import hmac
|
|
|
|
import httpx
|
|
|
|
from ..config import get_settings
|
|
from .payment_providers import get_payment_provider
|
|
|
|
|
|
async def create_checkout() -> dict:
|
|
s = get_settings()
|
|
provider = get_payment_provider("creem")
|
|
api_key = (provider.get("secrets") or {}).get("api_key") or s.creem_api_key
|
|
product_id = (provider.get("secrets") or {}).get("product_id") or s.creem_product_id
|
|
api_base = "https://test-api.creem.io" if str(api_key).startswith("creem_test_") else "https://api.creem.io"
|
|
async with httpx.AsyncClient() as client:
|
|
resp = await client.post(
|
|
f"{api_base}/v1/checkouts",
|
|
headers={"x-api-key": api_key, "Content-Type": "application/json"},
|
|
json={
|
|
"product_id": product_id,
|
|
"success_url": f"{s.site_url}/purchase/thanks",
|
|
"request_id": f"ebook-{int(__import__('time').time() * 1000)}",
|
|
},
|
|
)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
|
|
if not data.get("checkout_url"):
|
|
raise ValueError(data.get("message", "checkout creation failed"))
|
|
return {"id": data.get("id", ""), "checkout_url": data["checkout_url"]}
|
|
|
|
|
|
def verify_webhook_signature(raw_body: str, signature: str | None) -> bool:
|
|
s = get_settings()
|
|
provider = get_payment_provider("creem")
|
|
webhook_secret = (provider.get("secrets") or {}).get("webhook_secret") or s.creem_webhook_secret
|
|
|
|
if not webhook_secret or not signature:
|
|
return False
|
|
expected = hmac.new(
|
|
webhook_secret.encode(),
|
|
raw_body.encode(),
|
|
hashlib.sha256,
|
|
).hexdigest()
|
|
return hmac.compare_digest(expected, signature)
|