Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
Express pass req.body data into the Stripe route to pay

In my express application I have route that gets a cake price from the client side in the req.body

app.post("/data", (req, res) => {
      let price = req.body[0].price * 1000;
      console.log(`Cake price is: £${price}`);
    });

This works fine and logs the price in the console

I have another route that I would like to pass this variable price from the req.body to Stripe and charge the customer.

app.post("/create-checkout-session", async (req, res) => {
  const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    line_items: [
      {
        price_data: {
          currency: "gbp",
          unit_amount: req.body[0].price,
          product_data: {
            name: "CakeItOrLeaveIt",
            // description: "", cake it or leave it description
            // images: [], cake it or leave it logo
          },
        },
        quantity: 1,
      },
    ],
    mode: "payment",
    success_url: `http://localhost:4242/success.html`,
    cancel_url: `http://localhost:4242/cancel.html`,
  });
  res.redirect(303, session.url);
});

When I pass the req.body[0].price into my Stripe route I get the below error.

unit_amount: req.body[0].price, TypeError: Cannot read properties of undefined (reading 'price')

Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Are you sending the price through to /create-checkout-session route on the client-side? What might be occurring is that you passing the price to the /data route on the client-side, but you aren't passing the price to the /create-checkout-session. It's hard to tell without seeing how you send your data to your API.

If you wanted to use one route - for instance the /data route you could transfer the /create-checkout-session into a function and call that function in the /data route and pass the price as a parameter to the /create-checkout-session function.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Thanks I was able to modify my code to req the cake price in the /create-checkout-session route

app.post("/create-checkout-session", async (req, res) => {

  console.log(req.body[0].price * 1000); //this works and gets the cake price

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    line_items: [
      {
        price_data: {
          currency: "gbp",
          unit_amount: req.body[0].price * 1000, //this errors with Cannot read properties of undefined (reading 'price')
          product_data: {
            name: "CakeItOrLeaveIt",
            // description: "", cake it or leave it description
            // images: [], cake it or leave it logo
          },
        },
        quantity: 1,
      },
    ],
    mode: "payment",
    success_url: `http://localhost:4242/success.html`,
    cancel_url: `http://localhost:4242/cancel.html`,
  });
  
  res.redirect(303, session.url);
});

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda