RangeError: Maximum Call Size Exceeding, Stripe session create

I’m using stripe(both server and client side using sessions) for payments. I’m using node on the backend and react on frontend. I have started building my server route for creating a new session.

Route –

router.route('/new-session/:orderId').get(protectMiddleware, async (req, res) => {
    try {
        const order = await Order.findById(req.params.orderId);
        const domain = `${req.protocol}://${req.get('host')}`;

        const session = await stripe.checkout.sessions.create({
            payment_method_types: ['card'],
            line_items: order.orderItems,
            mode: 'payment',
            success_url: `${domain}/`,
            cancel_url: `${domain}/order/${req.params.orderId}`,
            customer_email: req.user.email,
            client_reference_id: req.params.orderId,
        });

        res.status(200).json({ status: 'success', session });
    } catch (error) {
        console.log(error);
        res.send(error.message);
    }
});

The error I’m getting is that –

[0] RangeError: Maximum call stack size exceeded
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:47:35)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:109:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:109:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:109:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:109:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:109:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:109:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:109:33)
[0]     at stringify (/Users/akash/Code/Projects/ProShop/node_modules/qs/lib/stringify.js:125:33)

11 thoughts on “RangeError: Maximum Call Size Exceeding, Stripe session create”

  1. I do not directly see where the problem is but can you try to return something from this callback eg:

    router.route('/new-session/:orderId').get(protectMiddleware, async (req, res) => {
        try {
            const order = await Order.findById(req.params.orderId);
            const domain = `${req.protocol}://${req.get('host')}`;
    
            const session = await stripe.checkout.sessions.create({
                payment_method_types: ['card'],
                line_items: order.orderItems,
                mode: 'payment',
                success_url: `${domain}/`,
                cancel_url: `${domain}/order/${req.params.orderId}`,
                customer_email: req.user.email,
                client_reference_id: req.params.orderId,
            });
    
            return res.status(200).json({ status: 'success', session }); // change here
        } catch (error) {
            console.log(error);
            return res.send(error.message); // change here
        }
    });
    
    Reply
  2. Don’t try to send the whole Session back to the frontend, all you need is the ID and it’s the attempt to stringify it that’s probably giving you the error.
    Instead of

    res.status(200).json({ status: 'success', session });
    

    just do

    res.status(200).json({ status: 'success', sessionId: session.id });
    
    

    and then on the frontend you parse the JSON and call redirectToCheckout with that sessionId.

    Reply

Leave a Comment