Here are some sample code snippets that demonstrate how to calculate the secure hash in different programming languages.
function calcHash(obj: any, secret: string) {
try {
let objSorted = Object.keys(obj)
.sort()
.reduce(
(acc, key) => ({
...acc,
[key]: obj[key],
}),
{},
);
let dataPreparedForHashing = Object.entries(objSorted)
.map(([key, value]) => `${key}=${value}`)
.join('&');
// console.log(dataPreparedForHashing) should be like this
amount=0.111&billerRefNumber=¤cy=512&emailNotificationV
alue=test@amwalpay.com&expireDateTime=&maxNumberOfPayment=100&merchantI
d=7921¬ificationMethod=1&payerName=Amr&paymentMethod=9&paymentViewTy
pe=1&redirectUrl=&terminalId=221143
const hmac = crypto.createHmac('sha256', Buffer.from(secret, 'hex'));
const hashValue =hmac.update(dataPreparedForHashing, 'utf8').digest('hex');
return hashValue.toUpperCase();
}
catch (error)
{
return '';
}
}
let hash = calcHash(paramsObj, "YOUR_SECRET_KEY")
PHP Example #
<?php
function encryptWithSHA256($input, $hexKey) {
// Convert the hex key to binary
$binaryKey = hex2bin($hexKey);
// Calculate the SHA-256 hash using hash_hmac
$hash = hash_hmac('sha256', $input, $binaryKey);
return $hash;
}
// Provided input text
$inputText="amount=0.111&billerRefNumber=¤cy=512&emailNotificationV
alue=test@amwalpay.com&expireDateTime=&maxNumberOfPayment=100&merchantId=7921¬ificationMethod
=1&payerName=Amr&paymentMethod=9&paymentViewType=1&redirectUrl=&terminalId=221143";
// Provided hex key
$hexKey =
"9FFA1F36D6E8A136482DF921E856709226DE5A974DB2673F84DB79DA788F7E19";
// Calculate SHA-256 hash
$result = encryptWithSHA256($inputText, $hexKey);
?>
Verification By Amwal #
- Amwal Payment Gateway receives the data and hash.
- The system verifies the authenticity of the request by regenerating the SHA-256 hash using the received data and the merchant’s stored secure key.
- If the calculated hash matches the received hash, the transaction is considered secure and processed further.