Hello everyone,
I want to integrate Binance Pay into my app, and I’m using this document for reference.
And I am using Postman to test the API, but it is giving the error “The signature is not valid for this request.”
But I have followed everything mentioned in the docs
Here is my code example as I wrote it in Postman:
Body (as a raw) :
{
"env": {
"terminalType": "WEB"
},
"merchantTradeNo": "98253829372",
"orderAmount": 1,
"currency": "USDT",
"description": "very good Ice Cream",
"goodsDetails": [
{
"goodsType": "01",
"goodsCategory": "D000",
"referenceGoodsId": "7876763A3B2312",
"goodsName": "Ice Cream"
}
]
}
Headers that I am passing, and some headers are auto generated by postman :
and for this I am using postman script (pre-request)
Note this code contain 2 body variable I have tried with both of them
const crypto = require('crypto-js')
const secretKeyValue = 'my_secrate_key provide by Binance pay'
pm.environment.set("secret_key", secretKeyValue);
const body = "env[terminalType]=WEB&merchantTradeNo=98253829372&orderAmount=1¤cy=USDT&description=very%20good%20Ice%20Cream&goodsDetails[0][goodsType]=01&goodsDetails[0][goodsCategory]=D000&goodsDetails[0][referenceGoodsId]=7876763A3B2312&goodsDetails[0][goodsName]=Ice%20Cream"
// const body = {
// "env": {
// "terminalType": "WEB"
// },
// "merchantTradeNo": "98253829372",
// "orderAmount": 1,
// "currency": "USDT",
// "description": "very good Ice Cream",
// "goodsDetails": [
// {
// "goodsType": "01",
// "goodsCategory": "D000",
// "referenceGoodsId": "7876763A3B2312",
// "goodsName": "Ice Cream"
// }
// ]
// }
function generateRandomString() {
let randomString = '';
for (let i = 0; i < 32; i++) {
const randomCharCode = Math.floor(Math.random() * 52);
const charCode = randomCharCode < 26 ? randomCharCode + 65 : randomCharCode + 71;
randomString += String.fromCharCode(charCode);
}
return randomString;
}
function createSignature(timestamp, nonce, body, secretKey) {
const payload = `${timestamp}\n${nonce}\n${body}\n`;
const hmac = crypto.HmacSHA512(payload, secretKey);
const signature = hmac.toString(crypto.enc.Hex).toUpperCase();
return signature;
}
function generateRandomString(length) {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const randomBytes = crypto.lib.WordArray.random(length);
let result = "";
for (let i = 0; i < length; i++) {
const byte = randomBytes.words[i] & 0xFF;
const index = byte % charset.length;
result += charset[index];
}
return result;
}
const randomNonce = generateRandomString(32)
pm.globals.set("random-nonce", randomNonce);
const unixTimestamp = new Date().getTime()
pm.globals.set("unixTimestamp", unixTimestamp);
const secretKey = pm.environment.get("secret_key");
console.log(secretKey)
const hashValue = createSignature(unixTimestamp, randomNonce, body, secretKey)
// // console.log(hashValue)
pm.globals.set("paysignature", hashValue);
Can anyone please help me to find a solution for this error.
Thanks