# pip install requests flask import hmac, hashlib, time, requests from flask import Flask, request, abort GATEWAY, API_KEY, API_SECRET = "https://pay.yourdomain.com", "PASTE_API_KEY", "PASTE_API_SECRET" def create_invoice(order_id: str, amount: str, network: str | None = None) -> dict: f = {"api_key": API_KEY, "order_id": order_id, "amount": amount, "callback_url": "https://yourshop.com/gateway/callback"} if network: f["network"] = network f["sign"] = hmac.new(API_SECRET.encode(), f"{API_KEY}|{order_id}|{amount}".encode(), hashlib.sha256).hexdigest() j = requests.post(f"{GATEWAY}/api/create.php", data=f, timeout=20).json() if not j.get("success"): raise RuntimeError(j.get("error")) return j app = Flask(__name__) @app.post("/gateway/callback") def callback(): ts = request.headers.get("X-Gateway-Timestamp", ""); sig = request.headers.get("X-Gateway-Signature", "") body = request.get_data(as_text=True) exp = hmac.new(API_SECRET.encode(), f"{ts}.{body}".encode(), hashlib.sha256).hexdigest() if abs(time.time() - int(ts or 0)) > 600 or not hmac.compare_digest(sig, exp): abort(403) d = request.get_json() if d["event"] == "invoice.paid": pass # order paid mark करें (idempotent) return "OK"