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