Razorpay callback url is not working, not giving, razorpay_order_id, razorpay_payment_id and signature

98 views Asked by At

Razorpay callback url is not working, it is giving get request to server while for signature verification we want post request, http url doesnt work?

above is my code and at verification url i got empty req.body and not getting razorpay_orderid, razorpay paymentid, signature...!!

1

There are 1 answers

0
Harish Kumar On

Please refer to my code. Working for me

app.get('/api/v1/payments/:vendor_code/callback/:tenantId', async (req, res) => {
    const { vendor_code, tenantId } = req.params;
    logger.info(`Vendor Code: ${vendor_code}, Tenant ID: ${tenantId}`);
    logger.info("Token from env: [SENSITIVE INFORMATION - NOT LOGGED]");
    const queryParams = req.query;
    logger.info('Payment Callback Received with Query Params:', queryParams);

    try {
        const paymentId = queryParams.razorpay_payment_id;

        // Check if payment ID has already been processed
        if (processedPaymentIds.has(paymentId)) {
            logger.warn(`Duplicate payment callback received for payment ID: ${paymentId}. Ignoring.`);
            res.status(200).send('Duplicate payment callback. Ignoring.');
            return;
        }

        logger.info("---> " + paymentId);
        const paymentDetails = await verifyPayment(paymentId);

        await savePaymentDetails(paymentDetails, vendor_code, tenantId, queryParams);

        // Mark payment ID as processed
        processedPaymentIds.add(paymentId);

        if (paymentDetails.captured) {
            logger.info('Payment was successful');
            res.status(200).send('Payment was successful.');
        } else {
            logger.info('Payment was not successful');
            res.status(200).send('Payment was not successful');
        }
    } catch (error) {
        logger.error('Error processing payment:', error.message);
        res.status(500).send('Internal Server Error');
    }
});


async function verifyPayment(paymentId) {
    logger.info("payment id is " + paymentId);
    logger.info(razorpayCredentialsBase64);
    try {
        const response = await axios.get(`https://api.razorpay.com/v1/payments/${paymentId}`, {
            headers: {
                'Authorization': `Basic ${razorpayCredentialsBase64}`
            }
        });
        return response.data;
    } catch (error) {
        logger.error('Error fetching payment details:', error.response ? error.response.data : error.message);
        throw error;
    }
}