razorpay / razorpay-node

Razorpay node.js bindings
MIT License
182 stars 111 forks source link

Test Mode - Verification of Razorpay Signature Failed #171

Closed SHAHRUKH-KHAN closed 2 years ago

SHAHRUKH-KHAN commented 3 years ago

Hi Team,

This is related to the closed issue.

router.post("/verifypayment", function(req, res) {
    var paymentsignature = req.body;

    console.log(paymentsignature);

    var generated_signature = crypto.createHmac('sha256','my-secret')
        .update(paymentsignature.order_id + '|' + paymentsignature.razorpay_payment_id)
        .digest('hex');
    if (generated_signature == paymentsignature.razorpay_signature) {    
        console.log('matched'); 
    } else {
        console.log('not matched');
    }
});

The signature is generated which is not matched. variables used above:

  1. order_id - custom-generated (not the one returned from Razorpay)
  2. razorpay_payment_id - Returned by Razorpay Checkout.

+1 if you face the same. Razorpay Team - Please look into this.

RohitChattopadhyay commented 3 years ago

Can you try this once, to generate signature

expected_signature = razorpay_invoice_id + '|' +
                     razorpay_invoice_receipt + '|' +
                     razorpay_invoice_status + '|' +
                     razorpay_payment_id;

Ref: https://github.com/razorpay/razorpay-php/pull/147/files#diff-cec2e6ba3034a76554db4a9fbbca23051d9b3f3ed8b3bb32a7c4759d61764348R44

pvsvamsi commented 3 years ago

@umanghome Can we expect an update on this?

DibyajyotiMishra commented 3 years ago

I am getting undefined values of razorpayOrderId and razorpaySignature from the handler response while working on test mode. So the payment verification fails in the backend. I have been stuck at this for over a day now. Any help is highly appreciated.

This is my code snippet:

` const launchRazorpay = async () => { const order = await axios.post"http://localhost:8081/api/subscription/pay");

const { amount, id: order_id, currency } = order.data;

console.log(order);

const options = {
  key: "rzp_test_asdfg345dfCVBN",
  amount: amount,
  currency: currency,
  name: "Razorpay",
  description: "PAY YOUR SUBSCRIPTION FEES",
  image: `${process.env.PUBLIC_URL}/assets/images/logo.png`,
  orderId: order_id,
  handler: function (response) {
    alert(response.razorpay_payment_id);// pay_asd1234
    alert(response.razorpay_order_id);// undefined
    alert(response.razorpay_signature);// undefined
  },
  prefill: {
    name: "abc",
    email: "abc@gmail.com",
    contact: "+911234567890",
  },
};
console.log("====================================");
console.log(options.orderId === order_id); // true
console.log(response.razorpay_order_id === order_id);// false
console.log("====================================");
const paymentObject = new window.Razorpay(options);
paymentObject.open();

};

`

Tharun-b777 commented 2 years ago

@DibyajyotiMishra were you able to fix this.I am facing the same issue in live mode but works fine in test

DibyajyotiMishra commented 2 years ago

yes. The function should be async.

handler: async function (response) {
        const data = {
          orderId: order_id,
          doneBy: authState.userId,
          institutionType: authState.userType,
          razorpayPaymentId: response.razorpay_payment_id,
          razorpayOrderId: response.razorpay_order_id,
          razorpaySignature: response.razorpay_signature,
        };

console.log(data);

Hope it helps.

ankitdas13 commented 2 years ago

@SHAHRUKH-KHAN @DibyajyotiMishra Sorry for the delay . the sdk is updated now for payment signature you can use this code snippet var { validatePaymentVerification } = require('razorpay/dist/utils/razorpay-utils');

const respBody = { 'subscription_id':'sub_ID6MOhgkcoHj9I', 'payment_id':'pay_IDZNwZZFtnjyym' } const correctSignature = '601f383334975c714c91a7d97dd723eb56520318355863dcf3821c0d07a17693' secret = 'EnLs21M47BllR3X8PSFtjtbd'; validatePaymentVerification(respBody, correctSignature, secret)

you can also use this doc

ankitdas13 commented 2 years ago

I am closing this issue for now. If you are still facing the same issue, please do not hesitate to reopen it. Please feel free to add any other suggestions you may have.