#Yarrik_b-checkout-line_items
1 messages · Page 1 of 1 (latest)
Hello 👋
Can you edit your message to remove the example code and post that in the thread instead?
okay, give me some time please
hello again! i've got some issues with line_items one more time. i create session checkout expanding it with line_items. but when i get response from stripe with webhook, i can't see line_items, and so i can't use them.
here i create checkout session:
exports.getCheckoutSession = catchAsync(async (req, res, next) => {
// 1) Get currently booked tour
const tour = await Tour.findById(req.params.tourID);
// 2) Create checkout session
const session = await stripe.checkout.sessions.create({
expand: ['line_items'],
payment_method_types: ['card'],
// success_url: ${req.protocol}://${req.get('host')}/?tour=${ // req.params.tourID // }&user=${req.user.id}&price=${tour.price},
success_url: ${req.protocol}://${req.get('host')}/my-tours,
cancel_url: ${req.protocol}://${req.get('host')}/tour/${tour.slug},
customer_email: req.user.email,
client_reference_id: req.params.tourID,
line_items: [
{
name: ${tour.name} Tour,
description: tour.summary,
images: [
${req.protocol}://${req.get('host')}/img/tours/${tour.imageCover},
],
amount: tour.price * 100,
currency: 'usd',
quantity: 1,
},
],
});
console.log(session);
// 3) Create session as response
res.status(200).json({
status: 'success',
session,
});
});
and here i get response from stripe:
exports.webhookCheckout = (req, res, next) => {
const signature = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(
req.body,
signature,
process.env.STRIPE_WEBHOOK_SECRET
);
} catch (err) {
return res.status(400).send(Webhook error: ${err.message});
}
if (event.type === 'checkout.stripe.completed') {
createBookingCheckout(event.data.object);
}
res.status(200).json({ received: true });
};
Sorry that's a lot of code.
Where exactly are you expanding the line_items here?
when i create session checkout
That's not how expand works. You can't expand in a create request.
You need to expand responses;
https://stripe.com/docs/api/expanding_objects
So here, when you receive the event checkout.session.completed, then you need to retrieve the session object and then expand the response https://stripe.com/docs/api/checkout/sessions/retrieve
Example:
stripe.checkout.sessions.retrieve(session ID, {
expand: ['line_items'],
});