I've been working on a E-Store built on Express.js, MongoDB and Stripe. This is my first ever project handling transactions and database stuff.
I have a weird project structure where the product info is stored on MongoDB, payment stuff on Stripe and server handling with express.js.
I've gotten to the point where im able to make successful purchase, but I can't update the data on mongoDB.
When Stripe send a response that items are purchased, it only shows their Stripe ID, but I want to somehow save the MongoDB ID.
Here is the app.js snippet for Stripe.
app.post("/buy", async (req, res) => {
const id = req.body.id;
const p = await Product.find(({_id: id}));
try {
let items = [];
for(const i in p) {
const item = p[i];
items.push({
price_data: {
currency: "eur",
product_data: {
name: item.title,
},
unit_amount: item.price
},
quantity: item.in_stock
});
}
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
mode: "payment",
success_url: `${process.env.SERVER_URL}/thanks?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.SERVER_URL}/`,
line_items: items,
});
res.json({url: session.url});
} catch(err) {
res.status(500).json({ message: err.message });
}
});
app.get("/thanks", async (req, res) => {
try {
const session = await stripe.checkout.sessions.retrieve(req.query.session_id);
stripe.checkout.sessions.listLineItems(
req.query.session_id,
function(err, lineItems) {
// asynchronously called
console.log(lineItems);
}
);
} catch(err) {
res.redirect(process.env.SERVER_URL);
}
res.render("thanks", {home_url: process.env.SERVER_URL});
});
Is there some way to update or delete a purchased item?
I tried to add a custorm field to the item object, but it throws an error.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/gBhXkJi
Comments
Post a Comment