Strapi 如何在控制器中增加 api
Strapi how to extra api in controller
我已经完成了一个购物车,可以支付给 stripe,但是我想知道是谁买了它,所以我想添加一个功能,让我可以在 strapi 后端看到用户
我希望知道如何将这两个功能结合起来。
我已经分别尝试过它们的功能我会为不同的页面工作
../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)
}
}
};
想合并
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 });
在新的strapi
用户 => users_permissions_user
答案是 // 在数据库中注册订单
user: ctx.state.user.id,
尝试
users_permissions_user: ctx.state.user.id,
我已经完成了一个购物车,可以支付给 stripe,但是我想知道是谁买了它,所以我想添加一个功能,让我可以在 strapi 后端看到用户
我希望知道如何将这两个功能结合起来。
我已经分别尝试过它们的功能我会为不同的页面工作
../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)
}
}
};
想合并
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 });
在新的strapi
用户 => users_permissions_user
答案是 // 在数据库中注册订单
user: ctx.state.user.id,
尝试
users_permissions_user: ctx.state.user.id,