મુખ્ય સામગ્રી પર જાઓ
શીખોમાર્ગદર્શિકાઓવેબહૂક સાથે એજન્ટ ચુકવણીઓ સંભાળો
માર્ગદર્શિકા

વેબહૂક પેટર્ન જે વિકાસકર્તાઓ સૌથી વધુ પૂછે છે.

15 મિનિટ
ટૂંકું જવાબ

એક વિશ્વસનીય વેબહુક હેન્ડલર ચાર વસ્તુઓ ક્રમમાં કરે છે: કાચા શરીર સામે સહીની પુષ્ટિ કરે છે (Nodeમાં webhooks.verify, અન્યત્ર દસ્તાવેજિત HMAC), ઇવેન્ટ id દ્વારા ડ્યુપ્લિકેટ કરે છે, કામને બેકગ્રાઉન્ડ ક્યૂમાં એન્ક્યૂ કરે છે, અને 200 પરત કરે છે. લાંબા સમય સુધી ચાલતું કામ વર્કરમાં થાય છે, પુનરાવૃત્તિઓ અને ક્યૂ સ્તરે idempotency સાથે. કારણ કે કોઈ નિષ્ફળતા ઘટના નથી, એક શેડ્યૂલ કરેલ સ્વીપ ચુકવણીની રાહ જોઈ રહેલ નોકરીઓને સમયબદ્ધ કરે છે અને તેમને સમન્વયિત કરે છે.

પૂર્વશરતો

તમે શરૂ કરવા પહેલાં.

  • એક કાર્યરત એજન્ટ પ્રોફાઇલ અને તમારા ડેશબોર્ડમાંથી સહીનો ગુપ્ત (સેટિંગ્સ - વેબહૂક્સ).
  • એક વેબ ફ્રેમવર્ક જે કાચા-શરીરની ઍક્સેસ ધરાવે છે - express.raw સાથે એક્સપ્રેસ, ફાસ્ટએપી, ફ્લાસ્ક, વગેરે. ઓટો-પાર્સિંગ JSON મિડલવેર સહીની પુષ્ટિને તોડે છે.
  • એક નોકરીની કતાર: BullMQ (Node) અથવા Celery/arq (Python). વેબહૂક ઝડપી 200 પાછું કરે છે અને કતાર ધીમું કામ કરે છે.
  • એક અપસર્ટ પ્રિમિટિવ સાથેની ડેટાબેઝ (Postgres કાર્ય કરે છે; Redis SET NX ટૂંકા સમય માટે ડ્યુપને દૂર કરવા માટે પણ કાર્ય કરે છે).
  • એક જાહેર HTTPS અંતિમ બિંદુ - વિકાસમાં, ngrok અથવા એક ડિપ્લોય પૂર્વદર્શન. મોકલનાર ખાનગી URLs પર ડિલિવર નહીં કરે.
પગલું 1 માં 4

સહીની પુષ્ટિ કરો.

The signature is HMAC-SHA256 over {t}.{rawBody} with your webhook secret, hex-encoded, in the X-Blockchain0x-Signature header (t=<unix>,v1=<hex>), inside a 5-minute replay window. In Node, webhooks.verify from @blockchain0x/node does it and returns a discriminated union; in other languages compute the same HMAC and compare in constant time. Raw-body access matters: if the bytes you sign locally differ from the bytes that arrived, it fails.

TypeScript
import express from "express";
import { webhooks } from "@blockchain0x/node";

const app = express();
// Raw body so the HMAC matches the exact bytes on the wire.
app.use(express.raw({ type: "application/json" }));

app.post("/webhooks/payment", (req, res) => {
  const result = webhooks.verify({
    headers: req.headers,
    rawBody: req.body, // Buffer, raw bytes
    secret: process.env.BLOCKCHAIN0X_WEBHOOK_SECRET!,
  });
  // Discriminated union: branch on ok, no try/catch.
  if (!result.ok) return res.status(400).json({ code: result.code });
  // result.eventType / result.eventId are now set.
  handleEvent(result);
  res.status(200).send("ok");
});
Python
import hmac, hashlib, os, time
from flask import request

SECRET = os.environ["BLOCKCHAIN0X_WEBHOOK_SECRET"].encode()

# In Node, webhooks.verify does this. In Python, verify by hand against the
# documented algorithm: HMAC-SHA256 over "{t}.{rawBody}", 300s replay window.
def verify_signature(raw_body: bytes) -> bool:
    sig = request.headers.get("X-Blockchain0x-Signature", "")
    ts = request.headers.get("X-Blockchain0x-Timestamp", "")
    parts = dict(p.split("=", 1) for p in sig.split(",") if "=" in p)
    t, v1 = parts.get("t", ts), parts.get("v1", sig)
    want = hmac.new(SECRET, t.encode() + b"." + raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(want, v1) and abs(time.time() - int(t)) <= 300
પગલું 2 માં 4

હેન્ડલર ને આઇડેમ્પોટન્ટ બનાવો.

Webhooks retry on any non-2xx response, and the same event will arrive multiple times under load even when nothing has gone wrong. Dedupe on the event's id using a database upsert. If the row already exists, skip; if it does not, insert and proceed. Postgres makes this a single statement.

TypeScript
// Pseudocode for a Postgres-backed dedupe table. Replace with your DB of choice.
async function processEventOnce(eventId: string, body: object) {
  // INSERT ... ON CONFLICT DO NOTHING returns rowCount === 0 on duplicate.
  const inserted = await db.query(
    "INSERT INTO webhook_events(id) VALUES ($1) ON CONFLICT DO NOTHING",
    [eventId],
  );
  if (inserted.rowCount === 0) return;          // Already processed.
  await handleEvent(body);
}
Python
async def process_event_once(event_id: str, body: dict):
    # INSERT ... ON CONFLICT DO NOTHING returns 0 rows on duplicate.
    inserted = await db.execute(
        "INSERT INTO webhook_events (id) VALUES ($1) ON CONFLICT DO NOTHING",
        event_id,
    )
    if inserted == "INSERT 0 0":   # asyncpg-style status
        return                     # Already processed.
    await handle_event(body)
પગલું 3 માં 4

એન્ક્યૂ અને 200 ઝડપથી પાછું આપો.

વેબહૂક અંતિમ બિંદુએ એક સેકંડની અંદર પ્રતિસાદ આપવો જોઈએ. કશુંપણ ધીમીને ટાઇમઆઉટ અને પુનરાવૃત્તિઓને આમંત્રણ આપે છે. પેટર્ન છે: ચકાસો, એન્ક્યૂ કરો, પ્રતિસાદ આપો. ક્યૂ વાસ્તવિક ડિલિવરીને એક કાર્યકરામાં પુનરાવૃત્તિઓ, વ્યાખ્યાયિત પાછા અને તેની પોતાની આઈડેમ્પોટન્સી સાથે ચલાવે છે. BullMQ અને Celery બંને પ્રતિ-જોબ IDને સપોર્ટ કરે છે, જે સમાન ઇવેન્ટની દુર્બળ એન્ક્યૂિંગને રોકે છે.

TypeScript (BullMQ)
// Express handler: verify, enqueue, return 200 fast.
import { Queue } from "bullmq";
import { webhooks } from "@blockchain0x/node";

const paymentQueue = new Queue("payments");

app.post(
  "/webhooks/payment",
  express.raw({ type: "application/json" }),
  async (req, res) => {
    const result = webhooks.verify({
      headers: req.headers,
      rawBody: req.body,
      secret: process.env.BLOCKCHAIN0X_WEBHOOK_SECRET!,
    });
    if (!result.ok) return res.status(400).json({ code: result.code });
    await paymentQueue.add(result.eventType, { raw: req.body.toString() }, {
      jobId: result.eventId,            // Idempotency key.
      removeOnComplete: true,
      attempts: 5,
      backoff: { type: "exponential", delay: 1000 },
    });
    res.status(200).send("ok");
  },
);

// Worker file:
import { Worker } from "bullmq";
new Worker("payments", async (job) => {
  await handleEvent(job.data);
});
Python (Celery)
# Flask handler enqueues to Celery (or arq) and returns 200 quickly.
from celery import Celery
from flask import request

celery = Celery("payments", broker=os.environ["REDIS_URL"])

@celery.task(bind=True, max_retries=5)
def handle_payment_event(self, event_type, raw):
    try:
        process_event_once(event_type, raw)
    except Exception as exc:
        raise self.retry(exc=exc, countdown=2 ** self.request.retries)

@app.post("/webhooks/payment")
def webhook():
    raw = request.get_data()
    if not verify_signature(raw):
        abort(401)
    event_id = request.headers.get("X-Blockchain0x-Event-Id", "")
    event_type = request.headers.get("X-Blockchain0x-Event-Type", "")
    handle_payment_event.apply_async(args=[event_type, raw.decode()], task_id=event_id)
    return "ok", 200

arq પાયથન બાજુએ સમાન આકારનું અનુસરણ કરે છે - નક્કી કરેલ નોકરી id સાથે કાર્ય નોંધણી કરો અને ક્યૂને પુનરાવર્તન સંભાળવા દો. મુખ્ય બંધન એ છે કે એન્ક્યૂ પોતે ઝડપી હોવું જોઈએ (Redis માટે એક જ રાઉન્ડ ટ્રિપ); દૂરના કોલ્સ પર વેબહૂકને ક્યારેય બ્લોક ન કરો.

પગલું 4 માં 4

એક ચુકવણીને સંભાળો જે ક્યારેય લેન્ડ નથી થતી.

કોઈ નિષ્ફળતા વેબહૂક નથી - જો એક ખરીદનાર છોડી દે છે, તો કોઈ ઇવેન્ટ નથી આવે, અને એજન્ટ 'ચુકવણીની રાહ જોઈ રહ્યો છે' માં અટકાઈ જાય છે. તેથી તેને તમારી જાતે શોધો: લાંબા સમય સુધી રાહ જોઈ રહેલા નોકરીઓ પર એક શેડ્યૂલ સ્વીપ ચલાવો, જો તે ખરેખર સેટલ થયું હોય તો transactions.get સાથે ચેઇન સામે સમન્વયિત કરો, પછી રાખેલ સંસાધનોને મુક્ત કરો, નોકરીને એક ટર્મિનલ બિનચુકવણી રાજ્યમાં ખસેડો, અને (જો યોગ્ય હોય) પરિણામને વપરાશકર્તાને સપાટી પર લાવો.

TypeScript
async function sweepStaleAwaitingPayment() {
  for (const job of await findJobsAwaitingPaymentOlderThan("1h")) {
    // Reconcile against the chain before giving up.
    const tx = job.txHash ? await client.transactions.get(job.txHash) : null;
    if (tx) { markJobPaid(job.id); continue; }   // It actually settled.

    // 1. Release any held resources tied to the job.
    releaseHeldResources(job.id);
    // 2. Move it out of 'awaiting_payment' into a terminal 'unpaid' state.
    markJobUnpaid(job.id);
    // 3. (Optional) Notify the user, with a fresh payment link.
    notifyUser(job.userId, { template: "agent_payment_unpaid", jobId: job.id });
  }
}
Python
# Run on a schedule - there is no failure webhook to wait for.
def sweep_stale_awaiting_payment():
    for job in find_jobs_awaiting_payment_older_than("1h"):
        tx = client.transactions.get(job["tx_hash"]) if job.get("tx_hash") else None
        if tx:
            mark_job_paid(job["id"])   # It actually settled.
            continue

        release_held_resources(job["id"])
        mark_job_unpaid(job["id"])
        notify_user(job["user_id"], template="agent_payment_unpaid", job_id=job["id"])
સામાન્ય ખોટા પગલાં

પાંચ ભૂલો જે ઇવેન્ટને છોડે છે અથવા નકલ કરે છે.

સહીની પુષ્ટિ કરતા પહેલા બોડીનું પાર્શિંગ

HMAC ને તે કાચા બાઇટ્સ પર ગણવામાં આવવું જોઈએ જે મોકલનારે સાઇન કર્યા હતા. જો તમારું ફ્રેમવર્ક તમારા હેન્ડલર ચાલે તે પહેલાં JSON ને આપોઆપ પાર્સ કરે છે, તો તમે સ્થાનિક રીતે સાઇન કરેલા બાઇટ્સ મોકલનાર દ્વારા સાઇન કરેલા બાઇટ્સ સાથે મેળ ખાતા નથી (વિભિન્ન whitespace, કી ક્રમ, એનકોડિંગ) અને દરેક સાઇન ખોટું લાગે છે. કાચા શરીર પ્રાપ્ત કરવા માટે માર્ગને રૂપરેખાંકિત કરો (Express: express.raw, Flask: request.get_data), પહેલા ચકાસો, પછી પાર્સ કરો.

વેબહૂક હેન્ડલરમાં વાસ્તવિક કામ કરવું

Webhooksમાં આક્રમક પુનરાવર્તન નીતિઓ છે. જો તમારા હેન્ડલર કામ પહોંચાડવામાં 30 સેકન્ડ લે છે, તો મોકલનારનો ટાઇમઆઉટ ફાયર થાય છે અને વેબહૂક ફરીથી મોકલવામાં આવે છે - હવે તમારી પાસે સમાન ચુકવણી માટે ફ્લાઇટમાં બે ડિલિવરીઓ છે. હંમેશા: ચકાસો, એનક્યૂ કરો, 2xx પરત કરો. વાસ્તવિક કાર્ય એક બેકગ્રાઉન્ડ વર્કરમાં ચાલે છે જે જેટલો સમય જોઈએ તેટલો સમય લઈ શકે છે.

વ્યાપારની તર્કને સંચાર કરવા માટે HTTP સ્થિતિનો ઉપયોગ કરવો

જો તમારું હેન્ડલર 4xx પાછું આપે છે જ્યારે વપરાશકર્તા તમારા સિસ્ટમમાં હવે અસ્તિત્વમાં નથી, તો મોકલનાર તેને 'ખોટી વિનંતી' તરીકે માન્ય રાખે છે અને ફરીથી પ્રયાસ કરવાનું બંધ કરે છે. જો તે સમાન સ્થિતિ માટે 5xx પાછું આપે છે, તો મોકલનાર ક્યારેય ફરીથી પ્રયાસ કરે છે અને તમારી ક્યૂ ભરાઈ જાય છે. જ્યારે તમે ઇવેન્ટને સુરક્ષિત રીતે સ્થિર કરી લીધું હોય (અથવા તેને ડુપ તરીકે ઓળખી લીધું હોય) ત્યારે 200 પાછું આપો; વ્યવસાયિક નિર્ણયો વ્યક્ત કરવા માટે ક્યૂ લોજિકનો ઉપયોગ કરો, HTTP સ્થિતિ નહીં.

ઇવેન્ટ ID ની જગ્યાએ પેલોડ હેશ પર આઈડેમ્પોટન્સી

એક જ એજન્ટ વિશેના બે અલગ અલગ ઇવેન્ટ (ચુકવણી પ્રાપ્ત અને પછીની ચુકવણી મોકલવામાં આવી) અલગ અલગ બોડી ધરાવે છે અને યોગ્ય રીતે અલગ પ્રક્રિયાની જરૂર છે. જો તમારી ડેડુપ્લિકેટ બોડી હેશ પર છે તો તમે તેમાંના એકને છોડીને જઈ શકો છો. X-Blockchain0x-Event-Id (ડિલિવરી માટે અનન્ય) પર ડેડુપ્લિકેટ કરો, અને ઇવેન્ટ પ્રકારને તમારા હેન્ડલર શું કરે છે તે ચલાવવા દો.

અલગ પુષ્ટિ ઇવેન્ટની અપેક્ષા રાખી રહ્યા છે

મોકલવામાં આવેલા ઇવેન્ટ્સ છે payment.received, payment.sent, wallet.deployed, અને webhook.test - ત્યાં કોઈ અલગ પુષ્ટિ ઇવેન્ટ નથી. payment.received ત્યારે ફાયર થાય છે જ્યારે પરિવહન એક બ્લોકમાં હોય છે, જે તમારા માટે મોટા ભાગના કામ માટે સંકેત છે. કંઈક ખર્ચાળ અથવા અપરિવર્તનીય માટે, transactions.get ને પોલ કરો અને તમારી પોતાની પુષ્ટિ થ્રેશોલ્ડ લાગુ કરો; એવા ઇવેન્ટની રાહ ન જુઓ જે અસ્તિત્વમાં નથી.

આગળના પગલાં

જ્યારે વેબહૂક બુલેટપ્રૂફ હોય છે.

વેબહૂક કઠિન ભાગ છે. ઉપરના ચાર પેટર્ન સાથે, બાકીનું કામ મુખ્યત્વે કાર્યાત્મક છે: એક પરીક્ષણ પર્યાવરણ જે નિષ્ફળતા માર્ગોને વ્યાયામ કરે છે, ખર્ચ નિયંત્રણો જેથી એક ઉપરવટ એજન્ટ તમારા હેન્ડલરને પૂરતું ન કરે, અને અંતિમ સુરક્ષા સમીક્ષા.

પૂર્ણ સંદર્ભ docs.blockchain0x.com પર છે. વેબહૂક શબ્દકોશ: ચુકવણી મંડેટ. ઉત્પાદન સપાટી: ચુકવણી API.

છેલ્લી સમીક્ષા: 2026-05-15. CC BY 4.0 હેઠળ પ્રકાશિત.

વેબહૂક જે તમે ભાર હેઠળ વિશ્વાસ કરી શકો છો.

સહી કરેલી, પુનરાવૃત્તિ કરેલી, idempotent. શરૂ કરવા માટે મફત.