This is the code in my webhook
stripeRouter.post(
“/webhook”,
express.raw({ type: “application/json” }),
async (request, response) => {
const sig = request.headers[“stripe-signature”];
let event;
try {
event = stripe.webhooks.constructEvent(
request.rawBody,
sig,
endpointSecret
);
console.log("Webhook verified");
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`);
return;
}
// Handle the event
switch (event.type) {
case "checkout.session.completed":
const checkoutSessionCompleted = event.data.object;
const {
id,
amount_subtotal,
amount_total,
created,
currency,
customer,
customer_details,
payment_method_types,
payment_status,
shipping_details,
total_details,
} = checkoutSessionCompleted;
const session = await stripe.checkout.sessions.retrieve(
checkoutSessionCompleted.id,
{
expand: ["customer", "line_items.data.price.product"],
}
);
await Order.create({
session_id: id,
user_id: session.customer.metadata.user_id,
cart_items: session.line_items.data,
amount_subtotal,
amount_total,
order_no: created,
order_date: created,
delivery_date: created + 14 * 24 * 60 * 60,
currency,
customer_id: customer,
customer_details,
payment_method_types,
payment_status,
shipping_details,
total_details,
});
async function main() {
const transporter = nodeMailer.createTransport({
host: process.env.MAILER_HOST,
secureConnection: true,
port: 587,
auth: {
user: process.env.TERHIRE_EMAIL,
pass: process.env.TERHIRE_PASSWORD,
},
});
const info = await transporter.sendMail({
from: `Terhire <${process.env.TERHIRE_EMAIL}>`,
to: customer_details.email,
subject: "Order Confirmation",
html: emailHtml({ name: customer_details.name }),
});
console.log(`Message sent: ${info.messageId}`);
}
main();
default:
console.log(`Unhandled event type ${event.type}`);
}
response.send().end();
}
);