mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-15 12:44:49 +00:00
Initial commit 🚀
This commit is contained in:
75
4-natours/after-section-14/controllers/bookingController.js
Normal file
75
4-natours/after-section-14/controllers/bookingController.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
|
||||
const Tour = require('../models/tourModel');
|
||||
const User = require('../models/userModel');
|
||||
const Booking = require('../models/bookingModel');
|
||||
const catchAsync = require('../utils/catchAsync');
|
||||
const factory = require('./handlerFactory');
|
||||
|
||||
exports.getCheckoutSession = catchAsync(async (req, res, next) => {
|
||||
// 1) Get the currently booked tour
|
||||
const tour = await Tour.findById(req.params.tourId);
|
||||
// console.log(tour);
|
||||
|
||||
// 2) Create checkout session
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
// success_url: `${req.protocol}://${req.get('host')}/my-tours/?tour=${
|
||||
// req.params.tourId
|
||||
// }&user=${req.user.id}&price=${tour.price}`,
|
||||
success_url: `${req.protocol}://${req.get('host')}/my-tours?alert=booking`,
|
||||
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
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
// 3) Create session as response
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
session
|
||||
});
|
||||
});
|
||||
|
||||
const createBookingCheckout = async session => {
|
||||
const tour = session.client_reference_id;
|
||||
const user = (await User.findOne({ email: session.customer_email })).id;
|
||||
const price = session.display_items[0].amount / 100;
|
||||
await Booking.create({ tour, user, price });
|
||||
};
|
||||
|
||||
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.session.completed')
|
||||
createBookingCheckout(event.data.object);
|
||||
|
||||
res.status(200).json({ received: true });
|
||||
};
|
||||
|
||||
exports.createBooking = factory.createOne(Booking);
|
||||
exports.getBooking = factory.getOne(Booking);
|
||||
exports.getAllBookings = factory.getAll(Booking);
|
||||
exports.updateBooking = factory.updateOne(Booking);
|
||||
exports.deleteBooking = factory.deleteOne(Booking);
|
||||
Reference in New Issue
Block a user