Razorpay generate Signature in the node js
Generating Signature in Node.js for Razorpay
const crypto = require('crypto');
function generateSignature(orderId, razorpayPaymentId, razorpaySignatureSecret) {
const hmac = crypto.createHmac('sha256', razorpaySignatureSecret);
hmac.update(orderId + "|" + razorpayPaymentId);
return hmac.digest('hex');
}
const orderId = 'order123';
const razorpayPaymentId = 'pay123';
const razorpaySignatureSecret = 'your_razorpay_signature_secret';
const signature = generateSignature(orderId, razorpayPaymentId, razorpaySignatureSecret);
console.log("Generated Signature:", signature);
- Import the
crypto
module in Node.js to access the cryptographic functionality. - Define a function
generateSignature
that takesorderId
,razorpayPaymentId
, andrazorpaySignatureSecret
as parameters. - Create an HMAC (Hash-based Message Authentication Code) using the
crypto.createHmac
method with 'sha256' algorithm and therazorpaySignatureSecret
. - Update the HMAC with the concatenation of
orderId
andrazorpayPaymentId
using thehmac.update
method. - Generate the signature by digesting the HMAC in hexadecimal format using
hmac.digest('hex')
. - Call the
generateSignature
function with the required parameters to obtain the signature for a specific order and payment. - Log the generated signature to the console for verification.
This code snippet demonstrates how to generate a signature in Node.js for Razorpay by using the crypto
module to create an HMAC with the provided order and payment details.