To ensure the integrity and authenticity of the webhook notifications sent to the merchant, Ottu employs a signing mechanism based on HMAC (Hash-based Message Authentication Code). By leveraging HMAC, Ottu can guarantee that the webhook's content remains untampered during transmission.
This is the backbone of the signing and verification process. Merchants can retrieve their unique HMAC Key from the Webhook Configuration panel within Ottu's admin dashboard here.
It's paramount that this key remains confidential. Always store it securely and avoid exposing it to the public.
The signature is not derived from every field in the webhook payload. See payload example here. Only specific fields are considered. These are:
amount
currency_code
customer_first_name
customer_last_name
customer_email
customer_phone
customer_address_line1
customer_address_line2
customer_address_city
customer_address_state
customer_address_country
customer_address_postal_code
gateway_name
gateway_account
order_no
reference_number
result
state
Key Considerations:
Fields not present in the webhook payload or those with an empty string value are not considered when constructing the signature.
Only fields present in the above list and in the payload with valid non-empty values are considered for signature generation.
This update ensures that developers understand the significance of field presence and their values in the payload when constructing the HMAC signature.
Fields from the payload are extracted based on the aforementioned list, sorted alphabetically by key name, and then concatenated to form a unique message string.
This string, combined with the HMAC Key, is used to create the HMAC-SHA256 signature. This resultant signature is then dispatched with the webhook notification.
Ensuring the integrity and authenticity of webhook payloads is paramount for the security of both the service provider and the merchants. To achieve this, an HMAC (Hash-Based Message Authentication Code) signature is generated and sent along with the payload. This signature needs to be validated at the merchant's end to confirm that the data has not been tampered with. For the convenience of developers working with different programming languages, we provide ready-to-use code snippets in various popular languages to generate and verify this HMAC signature. This section showcases how to compute the HMAC signature for the payload in languages like Python, PHP, Java, .NET (C#), Node.js,Ruby, and Go.
Python function example for generating the HMAC signature given a payload and an HMAC key:
import hmacimport hashlibdefgenerate_hmac_signature(payload,hmac_key):# List of fields that are considered for the HMAC signature keys = ["amount","currency_code","customer_first_name","customer_last_name","customer_email","customer_phone","customer_address_line1","customer_address_line2","customer_address_city","customer_address_state","customer_address_country","customer_address_postal_code","gateway_name","gateway_account","order_no","reference_number","result","state", ]# Extract and sort the payload keys based on the 'keys' list, and ignore any missing or empty string values message = [(k, payload[k]) for k insorted(payload)if k in keys and payload[k]]# Concatenate the key-value pairs message_str ="".join([f"{k}{v}"for (k, v) in message])# Compute the HMAC signature digest = hmac.new(bytes(hmac_key, encoding="utf8"),bytes(message_str, encoding="utf8"), digestmod=hashlib.sha256 ).hexdigest()return digest# Testpayload ={"amount":"86.000","currency_code":"KWD","customer_first_name":"example-customer"}hmac_key ="pu9MpX3yPR"print(generate_hmac_signature(payload, hmac_key))
When you run this code, the printed result should match the provided HMAC signature: 6143b8ad4bd283540721ab000f6de746e722231aaaa90bc38f639081d3ff9f67.
PHP
<?phpfunctiongenerateHmacSignature($payload, $hmacKey) { $keys = ["amount","currency_code","customer_first_name","customer_last_name","customer_email","customer_phone",// ... [add all the other keys here] ..."reference_number","result","state" ]; $message ="";foreach ($keys as $key) {if (isset($payload[$key])&& $payload[$key] !=="") { $message .= $key . $payload[$key]; } }returnhash_hmac('sha256', $message, $hmacKey);}// Test$payload = ["amount"=>"86.000","currency_code"=>"KWD","customer_first_name"=>"example-customer"];$hmacKey ="pu9MpX3yPR";echogenerateHmacSignature($payload, $hmacKey);?>
The above examples provide a way for developers in different languages to generate the HMAC signature from a payload using the provided HMAC key.
Need Further Assistance? Our dedicated support team is always on hand to help. Reach out to us at support@ottu.com.
Your Feedback Matters: We continually strive to improve, and your feedback is invaluable to us. Please let us know if you found this guide helpful or if there are areas you feel could benefit from more detail.