I have completed a shopping cart and can pay to stripe, but I want to be able to know who bought it, so I want to add a feature that allows me to see the user in the strapi backend
I hope to know how to combine those that two functions.
I have tried their functions separately I will work for the different page
../controllers/order.js
const stripe = require('stripe')('xxxvxx');
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
module.exports = {
create: async ctx => {
const {
address,
amount,
product,
token,
} = ctx.request.body;
// Charge the customer
try {
// stripe charge
await stripe.customers
.create({
email: ctx.state.user.email,
source: token
})
.then((customer) => {
return stripe.charges.create({
// Transform cents to dollars.
amount: amount * 100,
currency: 'usd',
description: `Order ${new Date()} by ${ctx.state.user.id}`,
customer: customer.id
});
});
// Register the order in the database
try {
const order = await strapi.services.order.create({
user: ctx.state.user.id,
address,
amount,
product,
});
//email
if (order.id){
await strapi.plugins['email'].services.email.send({
to: ctx.state.user.email,
subject: 'Thank you for your purchase',
text: `
usd${order.amount} is charged from your credit card on Stripe.
usd${order.address} is charged from your credit card on Stripe.
`,
});
}
return order;
} catch (err) {
console.log(err)
}
} catch (err) {
console.log(err)
}
}
};
Want to combine it
async create(ctx) {
let entity;
if (ctx.is('multipart')) {
const { data, files } = parseMultipartData(ctx);
data.users_permissions_user = ctx.state.user.id;
entity = await strapi.services.about.create(data, { files });
} else {
ctx.request.body.users_permissions_user = ctx.state.user.id;
entity = await strapi.services.about.create(ctx.request.body);
}
return sanitizeEntity(entity, { model: strapi.models.about });
In the new strapi
user => users_permissions_user
the answer is // Register the order in the database
user: ctx.state.user.id,
try
users_permissions_user: ctx.state.user.id,