HMAC Signature Verification
The section describes how the hmac signature sent in the callback header can be verified
Obtain the Signing Key
The signing key is an alpha-numeric string generated by our platform during your merchant account creation and it is stored against your account record. This value can be found under your account details in the merchant dashboard. EllyPay uses this value to create the HMAC signature and the same will be used when verifying the signature. It is recommended that it is copied and stored safely together with the security keys.
Below is the sample callback data to be used for the demonstration;
{
"event": "transaction.charges",
"payload": {
"id": 11832,
"merchant_reference": "MCTREFNGKLP5VQCQSBH2",
"internal_reference": "ELPREFA65BGTFR7NGUXM",
"transaction_type": "COLLECTION",
"request_currency": "UGX",
"transaction_amount": 100000,
"transaction_currency": "UGX",
"transaction_charge": 4000,
"transaction_account": "256777000001",
"charge_customer": false,
"total_credit": 96000,
"provider_code": "mtn_momo_ug",
"request_amount": 100000,
"institution_name": "MTN Mobile Money Uganda",
"customer_name": "JOHN DOE",
"transaction_status": "PENDING",
"status_message": "Collection initialized successfully. Confirm charges"
}
}
Next Steps
Obtain the value of the
hmac-signature
header. The value sent in the signature header takes the formatt=timestamp,s=hmac_hash
Form the string payload to be used in signature verification. This is obtained by concatenating values of the callback data in the format;
event:merchant_reference:internal_reference:transaction_type:transaction_status
and these values are obtained from the callback data. The string payload in this case would therefore betransaction.charges:MCTREFNGKLP5VQCQSBH2:ELPREFA65BGTFR7NGUXM:COLLECTION:PENDING
Create the hmac hash of the string payload.
Compare the resulting hash to the value in the hmac-signature header. Equality means the signature is valid.
<?php
public function isValidSignature() {
$strPayload = "transaction.charges:MCTREFNGKLP5VQCQSBH2:ELPREFA65BGTFR7NGUXM:COLLECTION:PENDING";
$signingKey = "your signing key string";
$hmacSignature = "value of hmac-signature header";
try {
$timestamp = null;
$hmacHash = null;
// Split the hmacSignature into key-value pairs
foreach (explode(",", $hmacSignature) as $sig_part) {
[$key, $value] = explode("=", $sig_part);
switch ($key) {
case "t":
$timestamp = $value;
break;
case "s":
$hmacHash = $value;
break;
}
}
// Optional timestamp check based on your logic
// Calculate the HMAC signature
$signature = hash_hmac("sha256", $strPayload, $signingKey, false);
// Compare the calculated and provided signatures
return $signature === $hmacHash;
} catch (Exception $e) {
return false;
}
}
?>
Below is a sample signature generated using the signing key: SGNKYLSPUJKZBKQH5YVU
t=1722416074424,s=a33e2d1b844fad58ab8ca41e3bda4834ef2eece4ac77d857a7c9f06b4b1a4b6b
Last updated