I have been scratching my head on this one.
I am beginner with Strapi and Stripe and trying to make e-commerce site for practise.
How to set “sold” boolean to “true” after successful purchase? (in order to hide the paid products, since they are unique items)
I don’t get errors in console, but bash gives me:
“Updating sold status for product ID: Correct product ID
Error updating sold boolean: Error: Model product not found”
But there indeed is “product” model.
this is the order controller:
"use strict";
const stripe = require("stripe")(process.env.STRIPE_KEY);
const { createCoreController } = require("@strapi/strapi").factories;
module.exports = createCoreController("api::order.order", ({ strapi }) => ({
async create(ctx) {
const { products } = ctx.request.body;
try {
const lineItems = await Promise.all(
products.map(async (product) => {
const item = await strapi
.service("api::product.product")
.findOne(product.id);
return {
price_data: {
currency: "usd",
product_data: {
name: item.title,
},
unit_amount: Math.round(item.price * 100),
},
quantity: 1,
};
})
);
const session = await stripe.checkout.sessions.create({
shipping_address_collection: { allowed_countries: ["US"] },
payment_method_types: ["card"],
mode: "payment",
success_url: process.env.CLIENT_URL + "?success=true",
cancel_url: process.env.CLIENT_URL + "?success=false",
line_items: lineItems,
});
// Update the sold boolean to true for the purchased items
const updateSoldStatus = async (productId) => {
try {
await strapi
.query("product")
.update({ id: productId }, { sold: true });
} catch (error) {
console.error("Error updating sold boolean:", error);
}
};
// Update the sold status for each purchased product
for (const product of products) {
console.log(`Updating sold status for product ID: ${product.id}`);
await updateSoldStatus(product.id);
}
await strapi
.service("api::order.order")
.create({ data: { products, stripeId: session.id } });
return { stripeSession: session };
} catch (error) {
ctx.response.status = 500;
return { error };
}
},
}));
Attempted to use strapi.models.product