有一个问题。付款后,它重定向到这里 'http://localhost:5173/order/$%7Border._id%7D' order?._id 不是未定义的。但是页面出现错误,例如网站无法访问达到了。 但是如果我复制这个网址并打开另一个选项卡并粘贴它,那么它就可以工作。请帮助我..提前致谢。
这是我的代码
export const createPayment = async (req, res) => {
if (!req.body) {
return res.status(400).json({ message: "No request body", success: false });
}
if (req.user.isAdmin) {
return res
.status(503)
.json({ message: "Admins cannot make payments", success: false });
}
// console.log("payment", data);
try {
const order = await Order.findById(req.params.id);
console.log("order", order);
if (!order) {
return res
.status(404)
.json({ message: "Order not found", success: false });
}
const tran_id = uuidv4();
const data = {
total_amount: order.totalPrice,
currency: "BDT",
tran_id,
success_url: `http://localhost:5173/order/${order._id}`, // Redirect here on success
fail_url: `http://localhost:5173/order/${order._id}`, // Redirect here on failure
cancel_url: `http://localhost:5173/order/${order._id}`,
ipn_url: "http://localhost:3030/ipn",
shipping_method: "Courier",
product_name: "something",
product_category: "something",
product_profile: order.orderItems
.map((item) => item.imagesUrls[0])
.join(", "),
cus_name: order.deliveryAddress.name,
cus_email: order.deliveryAddress.email,
cus_address: order.deliveryAddress.address,
cus_country: "Bangladesh",
cus_phone: order.deliveryAddress.phone,
// Add the missing field
ship_name: order.deliveryAddress.name, // Shipping name
ship_add1: order.deliveryAddress.address,
ship_city: "Dhaka",
ship_state: "Dhaka",
ship_postcode: 1000,
ship_country: "Bangladesh",
};
const sslcz = new SSLCommerzPayment(store_id, store_password, isLive);
const apiResponse = await sslcz.init(data);
// console.log(apiResponse.status);
// Redirect the user to the payment gateway
let GatewayPageURL = apiResponse.GatewayPageURL;
// console.log(GatewayPageURL);
res.send(GatewayPageURL);
if (apiResponse.status === "SUCCESS") {
order.isPaid = true;
order.isAvailable = false;
order.paidAt = Date.now();
order.transactionId = tran_id;
// order.user = req.user.id;
const bookIds = order.orderItems.map((item) => item.product);
const orderType = order.orderItems.map((item) => item.orderType);
await Promise.all(
order.orderItems.map(async (item) => {
await Book.updateOne(
{ _id: item.product },
{ $set: { bookStatus: item.orderType, isAvailable: false } }
);
})
);
// order.paidAt = Date.now();
await order.save();
// console.log("new Order", order);
}
} catch (error) {
return res.status(500).json({ message: error.message, success: false });
}
};
```
const handlePay = async () => {
Swal.fire({
title: "Are you sure ?",
icon: "success",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, continue it!",
}).then(async (result) => {
if (result.isConfirmed) {
// console.log(order, orderId);
const res = await paymentServer(order, orderId);
if (res?.data) {
console.log("Opening URL: ", res.data);
window.open(res.data, "_blank");
} else {
console.error("Invalid URL");
console.log(res);
}
// console.log(res);
setPolling(true);
}
});
}; const paymentServer = async (data, id) => {
setLoading(true);
try {
// console.log("f", data, id);
const res = await axiosPublic.put(
`/api/order/createPayment/${id}`,
data,
{
withCredentials: true,
}
);
return res;
} catch (error) {
console.log(error);
return error;
} finally {
setLoading(false);
}
};
i want after success payment it will redirect my target page or url
当我们在浏览器的网址栏中输入一个网址时,它会发出 GET 请求。正如您所说,当您在浏览器选项卡中复制 URL 时,它就会起作用。这意味着您的路线被定义为 GET 路线。
但是,sslcommerz 使用 POST 方法进行重定向。您需要提供 POST 路由 URL 作为回调 URL。
理想情况下,您应该提供后端 URL 作为回调 URL,在相应的处理程序方法中完成业务逻辑,然后从那里重定向到前端。