I have an Express route and in the req.body I receive the below object that includes the price I wish to charge my customer.
{
UniqueId: 1636295687144,
cakeSize: 12,
cakeShape: '',
sponge: 'plain',
butterCream: 'none',
icing: false,
specialDecoration: '',
cakeMessage: '',
dateTime: '',
price: 18
}
Here is my create-checkout-session route for Stripe
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,
//errors with "TypeError: 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`,
});
console.log(req.body[0]);
//logs this obejct
// {
// UniqueId: 1636294926748,
// cakeSize: 6,
// cakeShape: '',
// sponge: 'plain',
// butterCream: 'none',
// icing: false,
// specialDecoration: '',
// cakeMessage: '',
// dateTime: '',
// price: 7
// }
console.log(req.body[0].price);
res.redirect(303, session.url);
});
The problem is when I try to pass the req.body[0].price into stripes object to charge the customer a custom amount I get the error
/Users/calumbradley/Development/cakeitorleaveit/cakeitorleaveit/server/server.js:61
console.log(req.body[0].price);
^
TypeError: Cannot read properties of undefined (reading 'price')
at /Users/calumbradley/Development/cakeitorleaveit/cakeitorleaveit/server/server.js:61:27
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Thanks in advance