# Webhook description

> Understand Embedded Finance webhook headers, payload structure, acknowledgement, and HMAC-SHA256 signature verification.

Read these common rules before handling Embedded Finance webhooks. Each event has its own `data` fields and example payload; see, for example, [Deposit notification](/embedded-finance/api-reference/webhooks/deposit-notification).

## Request headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `Content-Type` | String | Yes | Fixed to `application/json;charset=UTF-8`. |
| `x-timestamp` | String | Yes | Unix timestamp in seconds. |
| `x-signature` | String | Yes | Generate the HMAC-SHA256 signature from `webhook_secret` and `x-timestamp + "." + raw_body`. See [Signature and verification](/embedded-finance/api-reference/webhook-description#signature-and-verification). |

## Payload structure

| Field | Description |
| --- | --- |
| `request_id` | Unique notification ID. Use it to deduplicate deliveries. |
| `event_type` | Event type identifier. See the individual webhook page for its fixed value. |
| `created_at` | Notification creation time in ISO 8601 format. |
| `version` | Webhook payload version. |
| `data` | Event-specific business data. Read the corresponding webhook page for its fields and handling rules. |

## Merchant response requirements

After accepting a notification, the merchant callback must return HTTP 200 with a JSON response. Onerway uses this response to decide whether the notification was received successfully.

| Field | Type | Description |
| --- | --- | --- |
| `respCode` | String | Return `20000` to confirm receipt. |
| `respMsg` | String | Response message. |

If the response code is not `20000`, Onerway retries.

## Signature and verification

Onerway signs Embedded Finance webhook notifications with HMAC-SHA256. You must verify the signature before processing an event.

Generate and verify the signature as follows:

1. Read `x-timestamp` and `x-signature` from the request headers.
2. Read the raw request body without parsing or re-serializing it.
3. Concatenate `x-timestamp`, a period (`.`), and `raw_body` to build the signed content:

```text
x-timestamp + "." + raw_body
```

1. Use the UTF-8 bytes of `webhook_secret` directly as the HMAC key. Do not Base64-decode `webhook_secret`.
2. Apply HMAC-SHA256 to the signed content and encode the result as a lowercase hexadecimal string.
3. To verify the notification, calculate the signature using the same rules and compare it securely with `x-signature`, ignoring hexadecimal letter case.

<warning>

Calculate the signature from the raw request body exactly as received. Parsing and re-serializing the JSON can change whitespace, field order, or escape sequences and cause verification to fail.

</warning>

<code-collapse name="Signature examples">
<code-group sync="embedded-finance-webhook-signing-lang">

```java [Java]
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public final class RHSecureUtil {
    private RHSecureUtil() {
    }

    public static String signHmacSHA256(String webhookSecret, String content) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(webhookSecret.getBytes(), "HmacSHA256"));
        byte[] result = mac.doFinal(content.getBytes());
        return HexUtil.encodeHexStr(result);
    }

    public static boolean verifyHmacSHA256(String webhookSecret, String content, String signed) throws Exception {
        String hmacSHA256Str = signHmacSHA256(webhookSecret, content);
        return StrUtil.equalsAnyIgnoreCase(hmacSHA256Str, signed);
    }
}
```

```python [Python]
import hashlib
import hmac

def sign_hmac_sha256(webhook_secret: str, content: str) -> str:
    return hmac.new(
        webhook_secret.encode("utf-8"),
        content.encode("utf-8"),
        hashlib.sha256,
    ).hexdigest()

def verify_hmac_sha256(webhook_secret: str, content: str, signed: str) -> bool:
    expected = sign_hmac_sha256(webhook_secret, content)
    return hmac.compare_digest(expected.lower(), signed.lower())
```

```php [PHP]
<?php

final class HmacSHA256Util
{
    public static function signHmacSHA256(string $webhookSecret, string $content): string
    {
        return hash_hmac('sha256', $content, $webhookSecret);
    }

    public static function verifyHmacSHA256(string $webhookSecret, string $content, string $signed): bool
    {
        $expected = self::signHmacSHA256($webhookSecret, $content);
        return hash_equals(strtolower($expected), strtolower($signed));
    }
}
```

</code-group>
</code-collapse>
