How to get the query params inside getServerSideProps in nextjs

Viewed 1328

My URL is something like this:

http://localhost:3000/success?merchant=xxxxxx&order_id=xxxxx&payment_ref_id=xxxxxxx&status=Aborted&status_code=9999&message=Not%20a%20Nagad%20account

I need to get the payment_ref_id from the URL inside getServerSideProps. How can I do that? I tried something like context.query.payment_ref_id but didn't work. My code :

export const getServerSideProps: GetServerSideProps = async (context) => {
    try {
        const payment_reference_id = context.query.payment_ref_id;
        const response = await axios.get(`${BASE_URL}/remote-payment-gateway-1.0/api/dfs/verify/payment/${payment_reference_id}`,
            {
                headers: {
                    "X-KM-IP-V4": IP_ADDRESS,
                    "X-KM-Client-Type": "PC_WEB",
                    "X-KM-Api-Version": "v-0.2.0",
                    "Content-Type": "application/json",
                }
            });

        const payment_verify_res = await response.data;
        const paymentStatus = await payment_verify_res.status;

        return {
            props: {
                StatusProps: payment_verify_res || null,
                PaymentStatus: paymentStatus || null,

            },


        }

    } catch (error) {
        console.log(error);
        return { props: {} }
    }

}

1 Answers

This might sound silly, but check if you have this code in /pages/success.tsx or /pages/success/index.tsx
Also after getting the response you are not calling the data() method. Your code should probably look something like this :

const payment_verify_res = await response.data();
const paymentStatus = payment_verify_res.status;
Related