Webhook Signature Generation
A webhook signature is a cryptographic hash used to verify the authenticity and integrity of webhook requests. It ensures that the request comes from a trusted source and has not been tampered with.
Cash App Afterpay uses webhooks to notify merchants and partners about disputes.
Webhooks need a signature for security and verification purposes. Webhook signatures help:
- Prevent spoofing – They ensure only trusted sources can send webhooks
- Detect tampering – If the payload is altered, the signature will not match
- Add security – The signature works as an additional layer of protection alongside HTTPS.
Pre-work
Before you can receive and act on webhooks, you need to do the following:
- Set up a webhook endpoint and an associated URL. The endpoint must allow POST requests with content-type = application/json.
- Contact Afterpay merchant support and give them the following information:
- The URL you set up for webhook notification in step 1
- Your unique partner ID
After we receive this information, we share an HMAC (Hash Message Authentication Code) value with you and enable our systems for notification.
Don’t confuse the HMAC shared secret key with the HMAC value that is generated using the HMAC shared secret key.
Verify the webhook event payload
To verify a webhook event payload with the HMAC value, follow these steps:
- Retrieve the webhook event signature:
- Retrieve the event signature from the
X-Afterpay-Request-Signature
header provided in the webhook.
- Retrieve the event signature from the
- Construct raw signature in canonical form:
url
: The destination URL for the webhooktime
: TheX-Afterpay-Request-Date
header value, represented as a UNIX timestamppayload
: The raw JSON body of the webhook- Concatenate raw signature with format:
${url}\n${time}\n${payload}
- Generate HMAC-SHA-256 value:
- Create HMAC value using the raw signature and the API secret corresponding to API key utilities to create the webhook
- Use a constant-time cryptographic library to generate the signature to prevent timing attacks
- Compare the generated signature with the received signature:
- Compare the computed signature against the
X-Afterpay-Request-Signature
header value - If both signatures match, then the request is verified as legitimate
- If they don’t match, reject the request
- Compare the computed signature against the
Code sample:
Manual signature verification
You can verify the webhook signature manually using the command line. This can be helpful for debugging or verifying payloads without using a backend server.
The resulting HMAC
value is the signature you should compare against the value in the X-Afterpay-Request-Signature
header.
Sample request: