const params = {
amount: "100",
currency: "INR",
callback_url: "https://httpbin.org/post",
cname: "John Doe",
email: "johndoe@email.com",
key: "YOUR_API_KEY",
phone: "8930395227",
receipt_id: "TXN0438400150988993",
notes: {
udf1: "udf1",
udf2: "udf2"
},
};
// Step 1: Sort the object
const sortedParams = Object.keys(params).sort().reduce((accumulator, key) => {
accumulator[key] = params[key];
return accumulator;
}, {});
// Step 2: Join all string values with |
let valueString = "";
const allValues = Object.values(sortedParams);
for (let i = 0; i < allValues.length; i++) {
if (typeof allValues[i] !== "object") {
valueString += allValues[i] + "|";
}
}
// Step 3: Append key secret
valueString += "API_SECRET";
// Step 4: Create hash and add to the params
const hash = CryptoJS.SHA512(valueString).toString();
params.hash = hash;