This commit is contained in:
Sibunnayak 2024-03-27 10:59:29 +05:30
commit 6b1e79f319
2 changed files with 48 additions and 16 deletions

2
.env
View File

@ -14,7 +14,7 @@ WEBHOOK_SECRET_KEY="whsec_m9u7CFBCY1qWarhxq65CkII6egOBf20K"
STRIPE_SECRET_KEY="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql"
STRIPE_WEBHOOK_SECRET="whsec_dc9b9084fc764c806c8c5c06dd91de1ee809e9c8deab6d56e8e3ef2fc9c30c67"
FRONTEND_URL="http://127.0.0.1:5173"
FRONTEND_URL="https://smellika.com"
SEND_EMAIL_FROM="hello@smellika.com"

View File

@ -79,7 +79,7 @@ export const handlePayment = async (req, res) => {
shippingInfo: shipping,
user: req.user._id,
});
console.log("fffffffff", order, "llllllllll");
// console.log("fffffffff", order, "llllllllll");
const lineItems = await cart.map((item) => ({
price_data: {
currency: "inr",
@ -108,8 +108,8 @@ export const handlePayment = async (req, res) => {
// Allow only India for INR transactions
},
billing_address_collection: "required",
success_url: "http://localhost:5173/order-complete", // Provide your success URL here
cancel_url: "http://localhost:5173/cart",
success_url: `${process.env.FRONTEND_URL}/order-complete`, // Provide your success URL here
cancel_url: `${process.env.FRONTEND_URL}/cart`,
});
// res.json({ sessionId: session.id });
@ -125,6 +125,7 @@ export const handlePayment = async (req, res) => {
export const webhook = async (req, res) => {
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
const signature = req.headers["stripe-signature"];
let event;
if (webhookSecret) {
@ -142,7 +143,6 @@ export const webhook = async (req, res) => {
}
if (event.type === "checkout.session.completed") {
// console.log("dddddddddddd", event.data);
const findOrder = await Order.findById(event.data.object.metadata?.orderId);
findOrder.paypal_payer_id = event.data.object.id;
findOrder.paidAt = new Date(event.data.object.created * 1000);
@ -154,20 +154,52 @@ export const webhook = async (req, res) => {
}
findOrder.orderStatus = "new";
await findOrder.save();
// Construct the HTML for the email
const itemRows = findOrder?.orderItems
.map(
(item) =>
`<tr><td>${item?.name}</td><td>${item?.quantity}</td><td>₹${item?.price}</td></tr>`
)
.join("");
const htmlContent = `
<strong style="color: #1b03a3; font-size: 16px"> Hi ${findOrder?.shippingInfo?.first_Name},</strong>
<p style="color: #555; font-size: 15px;">Great news! Your order #${findOrder?.orderID} has been confirmed. Here are the details:</p>
<br/>
<table border="1" cellpadding="5" style="border-collapse: collapse; width: 100%;">
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
${itemRows}
</tbody>
</table>
<p style="color: #555; font-size: 15px;">Shipping Address: ${findOrder?.shippingInfo.first_Name} ${findOrder?.shippingInfo.last_Name},${findOrder?.shippingInfo.postalCode},
${findOrder?.shippingInfo.street},
${findOrder?.shippingInfo.city},
${findOrder?.shippingInfo.state},
${findOrder?.shippingInfo.country} </br> Phone number:${findOrder?.shippingInfo.phone_Number}</p>
<p style="color: #555; font-size: 15px;">Total: ${findOrder.total_amount}</p>
<br/>
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
<span style="color: #555; font-size: 13px;">Team Smellika</span>`;
// Send the email
await sendEmail({
to: `${event.data.object.customer_email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `Your Order #${findOrder?.orderID} Confirmation`,
html: ` <h1 style="color: #333; text-align: center; font-family: Arial, sans-serif;">Welcome to Smellika - Let the Shopping Begin!</h1>
<strong style="color: #1b03a3; font-size: 16px"> Hi ${findOrder?.shippingInfo?.first_Name},</strong>
<p style="color: #555; font-size: 15px;">Great news! Your order #${findOrder?.orderID} has been confirmed. Here are the details</p>
<br/>
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
<span style="color: #555; font-size: 13px;">Team Smellika</span>`,
html: htmlContent,
});
// Items: [List of Purchased Items]
@ -184,7 +216,7 @@ export const webhook = async (req, res) => {
"---------------------"
);
console.log(`💰 Payment status: ${event.data.object?.payment_status}`);
// console.log(`💰 Payment status: ${event.data.object?.payment_status}`);
// Saving the payment details in the database
// const payment = await Payment.create({