الـ Webhooks
التحقق من التوقيع
كل webhook معاه هيدر WhatPro-Signature: الـ t هو وقت الإرسال (ثواني Unix) والـ v1 هو HMAC-SHA256 لـ "t.raw_body" بسر التوقيع بتاعك، بالـ hex.
WhatPro-Signature: t=1793782800,v1=5f2b8c0d…e91a اقرا الـ body الخام قبل ما تحوّله لـ JSON.
احسب HMAC-SHA256(
signing_secret, t + "." +raw_body) بالـ hex.قارنه بالـ v1 بمقارنة ثابتة الوقت، وارفض أي t أقدم من 5 دقايق.
import crypto from "node:crypto";
import express from "express";
const app = express();
// Read the RAW body: the signature is over the exact bytes Yudoo sent.
app.post("/yudoo/webhook", express.raw({ type: "application/json" }), (req, res) => {
const header = req.get("WhatPro-Signature") ?? "";
const { t, v1 } = Object.fromEntries(header.split(",").map((p) => p.split("=")));
const expected = crypto
.createHmac("sha256", process.env.YUDOO_SIGNING_SECRET)
.update(`${t}.${req.body}`)
.digest("hex");
const fresh = Math.abs(Date.now() / 1000 - Number(t)) < 300;
if (!fresh || !v1 || !crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(v1))) {
return res.status(400).send("bad signature");
}
const event = JSON.parse(req.body);
// event.type: "order.confirmed" | "order.cancelled" | "webhook.test"
res.sendStatus(200);
});$raw = file_get_contents("php://input");
$header = $_SERVER["HTTP_WHATPRO_SIGNATURE"] ?? "";
parse_str(str_replace(",", "&", $header), $sig); // ["t" => …, "v1" => …]
$expected = hash_hmac("sha256", $sig["t"] . "." . $raw, getenv("YUDOO_SIGNING_SECRET"));
$fresh = abs(time() - (int) $sig["t"]) < 300;
if (!$fresh || !hash_equals($expected, $sig["v1"] ?? "")) {
http_response_code(400);
exit("bad signature");
}
$event = json_decode($raw, true);
http_response_code(200);import hmac, hashlib, os, time
from flask import Flask, request, abort
app = Flask(__name__)
@app.post("/yudoo/webhook")
def yudoo_webhook():
raw = request.get_data() # the raw bytes, before any JSON parsing
parts = dict(p.split("=", 1) for p in request.headers.get("WhatPro-Signature", "").split(","))
expected = hmac.new(
os.environ["YUDOO_SIGNING_SECRET"].encode(),
f"{parts.get('t')}.".encode() + raw,
hashlib.sha256,
).hexdigest()
fresh = abs(time.time() - int(parts.get("t", 0))) < 300
if not fresh or not hmac.compare_digest(expected, parts.get("v1", "")):
abort(400)
event = request.get_json()
return "", 200 هيدرز الـ webhook لسه بالبادئة WhatPro- (WhatPro-Signature وWhatPro-Event وWhatPro-Delivery): يودوو كان اسمه وات برو، والأسماء فضلت زي ما هي عشان الربط القديم يفضل شغال.
واقف في حاجة، أو محتاج حقل مش موجود؟ فريقنا بيرد على واتساب.