For Security Purpose, Binance will add signature for webhook notification. Partner needs to verify the signature using the public key issued from Binance Pay.
BinancePay-Certificate-SN, BinancePay-Signature and
Query Certificate API are deprecated and will be retired soon. Please
migrate to BinancePay-Certificate-SN-New, BinancePay-Signature-New and
Query New Certificate API ASAP, see
Verify the New Signature.
Protocol Rules
The following specifies the rules for calling the Webhook Notification from Binance payment.
| Rule | Description |
|---|---|
| Transfer Mode | Use HTTPS for secure transactions. |
| Submit Mode | POST/GET, depends on the API. |
| Data Format | Data submitted and response are both in application/json format. |
| Char Encoding | Use UTF-8 character encoding. |
| Signature Algorithm | RSA, asymmetric cryptographic algorithm |
| Signature Requirement | Signature-checking is required for requesting and receiving data. |
| Logic Judgment | Determine protocol field, service field and transaction status. |
Request Header
Binance Pay Webhook notification will contain the following in the header, which is used to verify the notification authenticity.
| Attributes | Type | Required | Limitation | Description |
|---|---|---|---|---|
| BinancePay-Certificate-SN (Deprecated) | long | Y | - | Deprecated, will be retired soon, use BinancePay-Certificate-SN-New. MD5 hash value of public key |
| BinancePay-Nonce | string | Y | must be 32 digits | A random string with 32 bytes, e.g. random ascii decimal within a-z and A-Z and loop 32 times to form a random string |
| BinancePay-Timestamp | string | Y | - | UnixTimestamp in milliseconds |
| BinancePay-Signature (Deprecated) | string | Y | - | Deprecated, will be retired soon, use BinancePay-Signature-New. signature, generated by Binance Pay |
| BinancePay-Certificate-SN-New | string | N | - | Sent once Binance Pay enables the new certificate, see Verify the New Signature. Serial number of the new certificate, 32-character uppercase hexadecimal. Query the certificate list from Query New Certificate API and use the certPublic whose certSerial equals this value. Do not hard-code the public key. |
| BinancePay-Signature-New | string | N | - | Sent once Binance Pay enables the new certificate. Signature generated by Binance Pay with the new certificate. Verify it with the public key found by BinancePay-Certificate-SN-New in Query New Certificate API, see Verify the New Signature. |
Verify the Signature
This section verifies the deprecated BinancePay-Signature. For BinancePay-Signature-New, see
Verify the New Signature.
Build the payload
Java
Code
PHP
Code
Note:
- ‘\n’ is LF, ASCII value is '0x0A'
Decode the Signature with Base64
Java
Code
PHP
Code
Verify the content with public key
Public key to verify BinancePay-Signature (deprecated) can be queried from
Query Certificate API (deprecated) - "certPublic". For
BinancePay-Signature-New, find the public key in
Query New Certificate API by BinancePay-Certificate-SN-New.
Note:
- Hash algorithm should use SHA256
Sample Java code is here, together with org.bouncycastle toolkit.
Code
PHP
Code
Verify the New Signature
Binance Pay is introducing a new platform certificate. Binance Pay enables it for all merchants; you
do not need to apply. Once it is enabled, every webhook carries two more headers,
BinancePay-Certificate-SN-New and BinancePay-Signature-New, and
Query New Certificate API returns a non-empty list. Call that API
to check whether the new certificate is enabled before you switch your verification. The existing
headers and BinancePay-Signature stay unchanged, so an integration that ignores the new headers
keeps working.
Find the public key by BinancePay-Certificate-SN-New
Query New Certificate API
(POST /binancepay/openapi/certificates-new) returns a list of certificates, each with a
certSerial and a certPublic. The public key for a webhook is the certPublic of the element
whose certSerial equals the webhook's BinancePay-Certificate-SN-New header.
- Call Query New Certificate API and cache the list as a map from
certSerialtocertPublic. - Look up the webhook's
BinancePay-Certificate-SN-Newin the map. Never pick a key by its position in the list. - If the value is not in the map and the map was rebuilt less than a minute ago, reject the webhook without calling the API. Otherwise call the API again, rebuild the map, and reject the webhook if the value is still not found. Anyone who can reach your webhook URL can send an unknown value, so never call the API once per webhook. Binance Pay retries a rejected webhook, so a genuine webhook signed by a certificate you have not cached yet passes on a later retry.
Verify BinancePay-Signature-New
Verify BinancePay-Signature-New exactly like BinancePay-Signature, with the public key found
above:
- The payload is the same:
BinancePay-Timestamp,BinancePay-Nonceand the body, built as in Build the payload. - The signature to decode is the
BinancePay-Signature-Newheader, Base64. - The algorithm is the same: SHA256 with RSA.
If a webhook carries BinancePay-Signature-New and it fails this verification, reject the webhook.
Do not fall back to BinancePay-Signature, otherwise the new certificate adds no protection.
Note:
- HTTP header names are case-insensitive, and frameworks normalize them differently (the PHP sample
above reads
Binancepay-Signature). ReadBinancePay-Certificate-SN-NewandBinancePay-Signature-Newthe same case-insensitive way. - Always get the new public key from Query New Certificate API and
select it by
certSerial. Do not hard-code the public key in your code or configuration: Binance Pay can replace the certificate, and a hard-coded key stops verifying from that moment. - Refresh the cached list at least once a day.
- Until the new certificate is enabled, webhooks carry no
BinancePay-Signature-New; verifyBinancePay-Signatureas described above in the meantime.BinancePay-Certificate-SN,BinancePay-Signatureand Query Certificate API are deprecated and will be retired soon, so switch to the new headers as soon as they arrive.