webkook test
This commit is contained in:
parent
f4313fcef5
commit
7c5cc17475
@ -11,7 +11,7 @@ import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
|
||||
import sendEmail from "../../Utils/sendEmail.js";
|
||||
// const endpointSecret = STRIPE_SECRET_KEY;
|
||||
//generate unique order id
|
||||
const generateUniqueOrderId = async () => {
|
||||
export const generateUniqueOrderId = async () => {
|
||||
const currentYear = new Date().getFullYear();
|
||||
// Find the latest order to get the last serial number
|
||||
const latestOrder = await Order.findOne({}, {}, { sort: { orderID: -1 } });
|
||||
@ -36,6 +36,7 @@ export const handlePayment = async (req, res) => {
|
||||
if (!email)
|
||||
return res.status(400).send({ message: "Please enter the email" });
|
||||
const { address, cart, subtotal } = req.body;
|
||||
|
||||
if (cart.length < 1)
|
||||
return res.status(400).json({ message: "cart is empty!" });
|
||||
switch (true) {
|
||||
@ -48,7 +49,7 @@ export const handlePayment = async (req, res) => {
|
||||
}
|
||||
}
|
||||
let addss = await shippingAddress.findById(address);
|
||||
console.log(addss?.postalCode);
|
||||
|
||||
let shipping = {
|
||||
first_Name: addss.first_Name,
|
||||
last_Name: addss.last_Name,
|
||||
@ -102,12 +103,19 @@ export const handlePayment = async (req, res) => {
|
||||
|
||||
// Add any other key-value pairs as needed
|
||||
},
|
||||
success_url: `${process.env.FRONTEND_URL}/cart`,
|
||||
cancel_url: `${process.env.FRONTEND_URL}/error`,
|
||||
shipping_address_collection: {
|
||||
allowed_countries: ["IN"],
|
||||
// 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",
|
||||
});
|
||||
// res.json({ sessionId: session.id });
|
||||
|
||||
res.status(200).send({ message: "order created", url: session.url });
|
||||
res
|
||||
.status(200)
|
||||
.send({ message: "order created", url: session.url, id: session.id });
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
|
@ -1,3 +1,4 @@
|
||||
import sendEmail from "../../Utils/sendEmail.js";
|
||||
import { Order } from "./orderModel.js";
|
||||
|
||||
export const getAllOrder = async (req, res) => {
|
||||
@ -44,7 +45,7 @@ export const getSingleOrder = async (req, res) => {
|
||||
const order = await Order.findById(req.params.id)
|
||||
.populate({
|
||||
path: "user",
|
||||
select: "name -_id",
|
||||
select: "name email -_id",
|
||||
})
|
||||
.populate({
|
||||
path: "shippingInfo.addressId",
|
||||
@ -67,13 +68,13 @@ export const getSingleOrder = async (req, res) => {
|
||||
|
||||
//get self User Order
|
||||
export const getUserSelf = async (req, res) => {
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
try {
|
||||
const order = await Order.find({
|
||||
user: req.user._id,
|
||||
user: req.user?._id,
|
||||
payment_status: "success",
|
||||
})
|
||||
.populate("shippingInfo.addressId")
|
||||
.sort({ createdAt: -1 });
|
||||
}).sort({ createdAt: -1 });
|
||||
|
||||
if (order) {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
@ -117,6 +118,7 @@ export const deleteOneOrder = async (req, res) => {
|
||||
export const updateOrderStatusById = async (req, res) => {
|
||||
try {
|
||||
let body = { orderStatus: req.body.status };
|
||||
|
||||
const currentDate = new Date();
|
||||
body["status_timeline." + req.body.status] = currentDate;
|
||||
// if (req.body?.package_weight) body.package_weight = req.body.package_weight;
|
||||
@ -151,6 +153,21 @@ export const updateOrderStatusById = async (req, res) => {
|
||||
body["courier_name"] = req.body.courierName;
|
||||
body["courier_tracking_id"] = req.body.TrackingID;
|
||||
await Order.findByIdAndUpdate(order._id, body);
|
||||
await sendEmail({
|
||||
to: `${req.body.sendemail}`, // Change to your recipient
|
||||
|
||||
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||
|
||||
subject: `Your Order is On Its Way!`,
|
||||
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 ${req.body?.customerName},</strong>
|
||||
|
||||
<p style="color: #555; font-size: 15px;">Great news! Your order 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>`,
|
||||
});
|
||||
return res
|
||||
.status(200)
|
||||
.json({ status: "ok", message: "Order status updated successfully!" });
|
||||
|
@ -117,7 +117,7 @@ const orderSchema = new mongoose.Schema(
|
||||
"cancelled",
|
||||
"returned",
|
||||
],
|
||||
// default: "new",
|
||||
default: "new",
|
||||
},
|
||||
|
||||
// paypal_payer_id: { type: String },
|
||||
|
@ -28,9 +28,7 @@ router.route("/:orderID/capture/payment").post(captureOrderPayment);
|
||||
// ----------------------stripe checkOut-----------------//
|
||||
|
||||
// app.post("/webhook", express.raw({ type: "application/json" }), webhook);
|
||||
router
|
||||
.route("/stripe-checkout-session")
|
||||
.post(isAuthenticatedUser, handlePayment);
|
||||
router.route("/stripe-checkout").post(isAuthenticatedUser, handlePayment);
|
||||
router
|
||||
.route("/webhook")
|
||||
.post(express.raw({ type: "application/json" }), webhook);
|
||||
|
@ -1,12 +1,11 @@
|
||||
import { createCheckoutSession } from './stripeModel.js';
|
||||
import { createCheckoutSession } from "./stripeModel.js";
|
||||
|
||||
export async function createCheckoutSessionController(req, res) {
|
||||
try {
|
||||
const body = req.body;
|
||||
const sessionId = await createCheckoutSession(body);
|
||||
res.json({ id: sessionId });
|
||||
} catch (error) {
|
||||
console.error("Error creating checkout session:", error);
|
||||
res.status(500).json({ error: "Failed to create checkout session" });
|
||||
}
|
||||
try {
|
||||
const sessionId = await createCheckoutSession(req.body);
|
||||
res.json({ id: sessionId });
|
||||
} catch (error) {
|
||||
console.error("Error creating checkout session:", error);
|
||||
res.status(500).json({ error: "Failed to create checkout session" });
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +1,27 @@
|
||||
import Stripe from 'stripe';
|
||||
import Stripe from "stripe";
|
||||
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET);
|
||||
|
||||
export async function createCheckoutSession(body) {
|
||||
const lineItems = body.products.map(({ product, quantity }) => ({
|
||||
price_data: {
|
||||
currency: "usd",
|
||||
product_data: {
|
||||
name: product.name,
|
||||
images: [product.image[0].url] // assuming you want to use the first image URL
|
||||
},
|
||||
unit_amount: Math.round(product.price * 100), // Ensure proper conversion to cents
|
||||
},
|
||||
quantity: quantity
|
||||
}));
|
||||
const lineItems = body.products.map(({ product, quantity }) => ({
|
||||
price_data: {
|
||||
currency: "usd",
|
||||
product_data: {
|
||||
name: product.name,
|
||||
images: [product.image[0].url], // assuming you want to use the first image URL
|
||||
},
|
||||
unit_amount: Math.round(product.price * 100), // Ensure proper conversion to cents
|
||||
},
|
||||
quantity: quantity,
|
||||
}));
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
payment_method_types: ["card"],
|
||||
line_items: lineItems,
|
||||
mode: "payment",
|
||||
success_url: "http://localhost:5173/order-complete", // Provide your success URL here
|
||||
cancel_url: "http://localhost:5173/cart", // Provide your cancel URL here
|
||||
});
|
||||
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
payment_method_types: ["card"],
|
||||
line_items: lineItems,
|
||||
mode: "payment",
|
||||
success_url: "http://localhost:5173/order-complete", // Provide your success URL here
|
||||
cancel_url: "http://localhost:5173/cart" // Provide your cancel URL here
|
||||
});
|
||||
|
||||
return session.id;
|
||||
return session.id;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user