v1

Webhooks

Instant Notifications (Webhooks)

The Webhooks system (or IPN) allows us to notify your server immediately when a payment status changes, ensuring that orders are updated even if the customer closes the browser.

Webhooks are the primary fulfillment signal. For reconciliation when a notification was delayed or missed, use GET /payment/status documented in Payments.

Setup

  1. Go to the Merchant Dashboard.
  2. Navigate to the API Credentials section.
  3. Enter your Webhook URL and obtain the Webhook Secret.

Security Verification

To ensure that the notification is coming from MyPay, we send a digital signature in the X-MyPay-Signature header. You must verify this signature.

Verification Example (PHP)

$payload = file_get_contents('php://input');
$receivedSignature = $_SERVER['HTTP_X_MYPAY_SIGNATURE'];
$webhookSecret = 'your_webhook_secret_here';

// Calculate expected signature using HMAC SHA256
$calculatedSignature = hash_hmac('sha256', $payload, $webhookSecret);

if (hash_equals($calculatedSignature, $receivedSignature)) {
    // ✅ Signature is valid
    // Process the request and update order status in your database
} else {
    // ❌ Signature is invalid
    // Ignore the request
}

Data Structure (Payload)

You will receive a POST request containing JSON. Success example:

{
    "event": "payment.success",
    "type": "QR_PAY",
    "transaction_id": "ABC123XYZ789",
    "token": "ABC123XYZ789",
    "trx_ref": "REF_456789",
    "status": "success",
    "gateway": "moamalat",
    "custom": "ORDER_12345",
    "amount": 100.00,
    "currency": "LYD",
    "timestamp": "2024-01-12 14:30:00"
}

Cancelled / failed events use the same core fields, plus error details:

{
    "event": "payment.cancelled",
    "type": "QR_PAY",
    "transaction_id": "QR-ABC123",
    "token": "ABC123XYZ789",
    "trx_ref": "ABC123XYZ789",
    "status": "cancelled",
    "gateway": "masrafi-pay",
    "custom": "ORDER_12345",
    "amount": 100.00,
    "currency": "LYD",
    "timestamp": "2024-01-12 14:30:00",
    "error_code": "MITF_SOURCE_ERROR",
    "error_message": "هناك خطأ من المصدر"
}
{
    "event": "payment.failed",
    "type": "QR_PAY",
    "transaction_id": "QR-ABC123",
    "token": "ABC123XYZ789",
    "trx_ref": "ABC123XYZ789",
    "status": "failed",
    "gateway": "yasr-pay",
    "custom": "ORDER_12345",
    "amount": 100.00,
    "currency": "LYD",
    "timestamp": "2024-01-12 14:30:00",
    "error_code": "MITF_SERVICE_NOT_ACTIVATED",
    "error_message": "Payment service is not activated on merchant account"
}

Field Reference

Field Description
event Event type (payment.success, payment.cancelled, payment.failed)
type Integration type (QR_PAY)
transaction_id MyPay transaction identifier
token Public payment token
trx_ref Transaction reference (usually the same as token)
status Payment status (success, cancelled, or failed)
gateway Lowercase payment gateway alias (e.g. moamalat, masrafi-pay, yasr-pay, sahary-pay, sadad, mobicash) — not an uppercase label
custom Your order identifier from payment/create — use this to match the webhook to your order (required for integrations such as WooCommerce and Odoo)
amount Paid amount (or requested amount for cancelled/failed)
currency Currency code (e.g. LYD)
timestamp Notification timestamp
error_code / error_message Present on payment.cancelled and payment.failed only

[!NOTE] The gateway value is the lowercase alias sent in payment_gateway during payment/create, not a display name.

Events

Event Description
payment.success Payment successfully received — fulfill the order after signature verification
payment.cancelled Payment cancelled / source rejection (currently emitted on MITF reject paths such as Masrafi / Yasr / Sahary) — do not fulfill
payment.failed Payment failed (currently emitted on MITF reject paths) — do not fulfill

[!IMPORTANT] Your server must respond with status code 200 OK immediately upon receiving the notification to confirm receipt. Treat payment.cancelled and payment.failed as terminal negative outcomes. Use GET /payment/status only for reconciliation; webhooks remain the primary signal.

Automatic Retries

If your server does not acknowledge the webhook successfully, MyPay retries delivery automatically (up to 5 attempts):

Attempt Delay before retry
1 10 seconds
2 30 seconds
3 1 minute
4 5 minutes
5 10 minutes

Respond with 2xx as soon as you receive the notification. Temporary failures (for example network errors or 502 / 503 / 504) are retried; client errors (4xx) and 500 are not.