Webhooks (Signature)
Last updated
<?php
// This secret is not the secret of the authentication token, it is the UID
$secret = 'my-secret';
// This is the "t" value received on SmartFastPay-Signature header
$timestamp = 1681235417000;
$requestPayload = [
'callback' => true,
'value' => 'value-field'
];
$jsonPayload = json_encode($requestPayload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$paramsSignature = "{$timestamp}.{$jsonPayload}";
echo hash_hmac('sha256', $paramsSignature, $secret);
// Output: b9ffafcd16416bd11e36f877c2d7ccc71633d174f8245abc49fc2aef7e6633c8<?php
// Comparing Signatures
// Example Header of the request sent by SmartFastPay:
$headers = [
'SmartFastPay-Signature' => 't=1681235417000,v1=b9ffafcd16416bd11e36f877c2d7ccc71633d174f8245abc49fc2aef7e6633c8'
];
// Extract the value of 't' from Header 'SmartFastPay-Signature'
// '1681235417000'
$timestamp = explode("=", explode(",", $headers['SmartFastPay-Signature'])[0])[1];
// Extract the value of 'v1' from Header 'SmartFastPay-Signature'
// 'b9ffafcd16416bd11e36f877c2d7ccc71633d174f8245abc49fc2aef7e6633c8'
$signature = explode("=", explode(",", $headers['SmartFastPay-Signature'])[1])[1];
// The Signature you generated in Step 2 must be equal to the value of the "$signature" variable.
// Must return 1 (true)
echo ('yourSignature' === $signature);