Hi, this is straight from tutorial:
//& don’t change this
“use strict”;
const stripe = require(“stripe”)(“sk_test_key_goes_here”);
const { createCoreController } = require(“@strapi/strapi”).factories;
// Create the order controller
module.exports = createCoreController(“api::order.order”, ({ strapi }) => ({
async create(ctx) {
// Get the authenticated user from the context
const user = ctx.state.user;
// Check if a user is authenticated
if (!user) {
return ctx.unauthorized("You are not authorized!");
}
// Destructure the necessary properties from the request body
const { address, amount, dishes, token, city, state } = ctx.request.body.data;
try {
// Charge the customer using Stripe
await stripe.charges.create({
amount: amount,
currency: "usd",
description: `Order ${new Date()} by ${ctx.state.user.id}`,
source: token
});
// Create the order and associate it with the authenticated user
const order = await strapi.service("api::order.order").create({
data: {
amount,
address,
dishes,
city,
state,
token,
user: ctx.state.user.id // Set the user field to the ID of the authenticated user
},
});
// Return the created order
return order;
} catch (err) {
// Handle errors, return a 500 error for simplicity in this example
console.log("err", err);
ctx.response.status = 500;
return {
error: { message: "There was a problem creating the charge" },
address,
amount,
dishes,
token,
city,
state,
};
}
},
}));