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);
  1. Import the crypto module in Node.js to access the cryptographic functionality.
  2. Define a function generateSignature that takes orderId, razorpayPaymentId, and razorpaySignatureSecret as parameters.
  3. Create an HMAC (Hash-based Message Authentication Code) using the crypto.createHmac method with 'sha256' algorithm and the razorpaySignatureSecret.
  4. Update the HMAC with the concatenation of orderId and razorpayPaymentId using the hmac.update method.
  5. Generate the signature by digesting the HMAC in hexadecimal format using hmac.digest('hex').
  6. Call the generateSignature function with the required parameters to obtain the signature for a specific order and payment.
  7. 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.