Resilient Engineering

Webhook Idempotency & Safe Retries

Ensure your commerce stack processes payments exactly once, even when network delays trigger automated webhook retries.

Why Idempotency Matters in Web3 Payments

Because blockchain confirmation times vary and internet routing may encounter transient spikes, XPayr's automated dispatcher retries unacknowledged webhooks up to 5 times. An idempotent listener guarantees that your store never fulfills an order twice.

Key Header Specifications

  • X-XPayr-Delivery: Unique UUID assigned to each webhook event (e.g. deliv_9f8a2c1e4d). Always identical across retry attempts for the same transaction event.
  • X-XPayr-Signature: HMAC SHA-256 hash preventing replay tampering.
  • X-XPayr-Event: Event identifier (payment.confirmed, payment.failed).

Production-Ready Listener Pattern (PHP)

<?php
// Secure Webhook Listener with Idempotency Protection
$rawPayload = file_get_contents('php://input');
$deliveryId = $_SERVER['HTTP_X_XPAYR_DELIVERY'] ?? '';
$signature = $_SERVER['HTTP_X_XPAYR_SIGNATURE'] ?? '';

// 1. Verify HMAC Signature
$expectedSignature = 'sha256=' . hash_hmac('sha256', $rawPayload, $webhookSecret);
if (!hash_equals($expectedSignature, $signature)) {
    http_response_code(401);
    exit('Invalid signature');
}

$event = json_decode($rawPayload, true);
$orderId = $event['data']['order_id'] ?? null;

// 2. Check if this delivery or order was already processed (Atomic DB Lock)
$stmt = $pdo->prepare("INSERT INTO processed_webhooks (delivery_id, order_id, created_at) VALUES (?, ?, NOW())");
try {
    $stmt->execute([$deliveryId, $orderId]);
} catch (PDOException $e) {
    // Duplicate event detected (Unique constraint violation)
    // Return HTTP 200 OK immediately so XPayr stops retrying!
    http_response_code(200);
    echo json_encode(['status' => 'already_processed']);
    exit;
}

// 3. Fulfill Order (Guaranteed to execute exactly ONCE)
fulfillCustomerOrder($orderId, $event['data']);

http_response_code(200);
echo json_encode(['status' => 'success']);
Official NPM SDKs (v0.2.0) & Remote MCP Server

Integrate Faster with Official XPayr Packages & Zero-Install MCP

Install @xpayr/node-sdk (v0.2.0), @xpayr/payment-evidence (v0.2.0), @xpayr/extension-kit (v0.2.0), or @xpayr/mcp-server (v1.0.0) from NPM, or execute live JSON-RPC 2.0 tool calls via POST https://xpayr.com/api/mcp.

Live MCP Playground (/tools/mcp-playground) ES256 License Verifier AI Agent Policy Simulator HMAC-SHA256 Webhook Tester mcp.xpayr.com docs.xpayr.com api-docs.xpayr.com