#TypeError: res.redirect and res.status is not a function

8 messages · Page 1 of 1 (latest)

pure kestrel
#

Dear community,

Trying to implement Stripe, it looks like it's going okay overall, but it's returning this TypeError in the console.

    if (items) {
      res.status(200).json({
        url: session.url,
      });
    } else {
      res.redirect(301, session.url ?? '');
    }
  } catch (e: any) {
    console.log(e);
    res.status(e.statusCode || 500).json({
      message: e.message,
    });
  }

I would love to if anyone else had this similar problem with the implementation of Stripe or in general.

All the help is welcome. Please let me know if you need any more source code.

Next.js 13.4.4 & TypeScript 5.0.4

Best regards.

Paddy

mental gullBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
vernal basalt
# pure kestrel Dear community, Trying to implement Stripe, it looks like it's going okay overa...

I had this same issue before. If you are using the App directory, I have found that you have to do responses along the lines of:

return NextResponse.json({ status: (statuscode), message: (message) })

That might work, however I know you are using TypeScript and that you have to give the 'res' object a type of 'NextResponse'. I wasn't using TypeScript when I ran into this same error so I had to return what's provided in the code block above.

Hope this helps.

pure kestrel
#

Thank you @vernal basalt . Really appreciate you for telling me this, I will look into this. 🙂

sacred cape
pure kestrel
#

hmm, very interesting, thanks @sacred cape You both are heros, have been struggling with this for a bit and the morning sun is already up.. :p

#

But I think I am using this correctly, I am sharing the whole file under here, so you understand how the req and res are being setup.

import type { NextApiRequest, NextApiResponse } from 'next';
import Stripe from 'stripe';

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY ?? '', {
  apiVersion: '2022-11-15',
});

console.log('outside the const handler!');

export async function POST(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    return res.status(405).end();
  }
  try {
    const { price, quantity, items } = req.body;
    const lineItems = items
      ? items.map((item: any) => ({
          price: item.id,
          quantity: item.quantity,
        }))
      : [
          {
            price,
            quantity,
          },
        ];
    console.log('logging the req.headers.origin:');

    console.log(`${req.headers.origin}`);
    const session = await stripe.checkout.sessions.create({
      payment_method_types: ['card'],
      line_items: [
        {
          price: 'price_1N2UwFHjh6rwfnNSgLo5XwJs',
          quantity: 1,
        },
      ],
      // line_items: lineItems,
      mode: 'payment',
      success_url: `http://localhost:3000/result?session_id={CHECKOUT_SESSION_ID}`,
      cancel_url: `http://localhost:3000/cancel`,
      automatic_tax: { enabled: true },
    });
    if (items) {
      res.status(200).json({
        url: session.url,
      });
    } else {
      res.redirect(301, session.url ?? '');
    }
  } catch (e: any) {
    console.log(e);
    res.status(e.statusCode || 500).json({
      message: e.message,
    });
  }
}

@sacred cape

sacred cape