# QuataPay in Python Plain HTTP, no QuataPay package; works with test and live keys. Secrets come from `QUATAPAY_API_KEY` and `QUATAPAY_WEBHOOK_SECRET`. ## Create a payment ```python import os import uuid import requests BASE = "https://api.quatapay.com/api/v1" def create_payment(order_id: str, amount_xaf: int, idempotency_key: str | None = None): """Server-side only. Make ONE idempotency key per checkout attempt and reuse it on retries.""" key = os.environ["QUATAPAY_API_KEY"] path = "/gateway/sandbox/payments" if key.startswith("qpay_test_") else "/gateway/payments" resp = requests.post( BASE + path, headers={ "Authorization": "Bearer " + key, "Idempotency-Key": idempotency_key or str(uuid.uuid4()), }, json={ "amount": amount_xaf, # integer XAF, never a float "currency": "XAF", "customer_reference": order_id, "description": "Order " + order_id, "return_url": "https://your-site.example/pay/return?order=" + order_id, "cancel_url": "https://your-site.example/pay/cancel?order=" + order_id, }, timeout=(5, 30), ) body = resp.json() if not resp.ok: raise RuntimeError(f"QuataPay {resp.status_code} {body.get('code')}: {body.get('error')}") return body["data"]["checkout_url"], body["data"]["payment"]["id"] ``` ## Verify a webhook ```python import hashlib import hmac import time def valid_signature(secret: str, raw_body: bytes, timestamp: str | None, signature: str | None) -> bool: if not timestamp or not (timestamp.isascii() and timestamp.isdigit()) or not signature: return False if abs(time.time() - int(timestamp)) > 300: return False expected = "sha256=" + hmac.new( secret.encode(), timestamp.encode() + b"." + raw_body, hashlib.sha256 ).hexdigest() return hmac.compare_digest(expected, signature) # Raw body: Flask request.get_data(), Django request.body, FastAPI await request.body(). # Headers: X-QuataPay-Timestamp and X-QuataPay-Signature. # Idempotency: insert (data["id"], event) under a UNIQUE constraint; a duplicate means "already handled", answer 200. ```