"""Xynex · Python 3.9+ · Create an invoice.

    pip install requests
    XYNEX_API_KEY=cp_test_... python python-create-invoice.py
"""
from __future__ import annotations

import os
import sys
import uuid

import requests

api_key = os.environ.get("XYNEX_API_KEY")
if not api_key:
    sys.exit("Set XYNEX_API_KEY first.")

resp = requests.post(
    "https://api.xynex.app/v1/invoices",
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "reference": f"order-{uuid.uuid4().hex[:8]}",
        "amount": 49.0,
        "currency": "usd",
        "customer": {"email": "buyer@example.com"},
    },
    timeout=15,
)

body = resp.json()
if not resp.ok:
    sys.exit(f"Xynex error: {resp.status_code} {body}")

print("Checkout URL:", body["data"]["checkout_url"])
print("Invoice id:  ", body["data"]["id"])
