[英]Strapi v4 with stripe
我遇到的問題是我無法使用該數據創建訂單,它沒有在strapi(BACKEND)中注冊任何內容,如果它進入條帶但在strapi v4中它沒有注冊內容類型順序,所以我想知道我做錯了什么,因為strapi v4是最近的,我還沒有找到如何用next和stripe來實現它; 我還留下了如何從客戶端(前端)strapi:v4.2.0 node:v16.11.0 BACKEND 發出請求
path: order/controller/order.js
const { createCoreController } = require("@strapi/strapi").factories;
const stripe = require("stripe")(
"sk_test_keyxxxxxxxxxxxxxxxx"
);
module.exports = createCoreController("api::order.order", ({ strapi }) => ({
async create(ctx, { files } = {}) {
const ctxData = ctx.request.body;
const user = ctx.state.user;
const {
data: { token, game, idUser, addressShipping },
} = ctxData;
let Payment = 0;
let gameId = 0;
game.forEach((product) => {
Payment =
Payment +
product.attributes.price -
Math.floor(product.attributes.price * product.attributes?.discount) /
100;
gameId = product.id;
});
const totalPayment = Math.floor(Payment * 100);
console.log(gameId);
console.log(ctxData);
const charge = await stripe.charges.create({
amount: totalPayment,
currency: "usd",
source: token.id,
description: `ID usuario: ${idUser}, Username:${
user.username
}, Nombre Completo:${" " + user.name + "," + user.lastname}`,
}); const entity = await strapi.service("api::order.order").create({
data: {
game: gameId,
user: user.id,
totalPayment,
idPayment: charge.id,
addressShipping,
},
});
const entry = await strapi.query("order").create(entity);
const sanitizedEntity = await this.sanitizeOutput(entry, ctx);
return this.transformResponse(sanitizedEntity); },
}));
################################################# ########################################### 從這里我有問題訂單不是在strapi中創建的,我只使用stripe注冊,但使用strapiv4它不會創建訂單,所以我想知道我在做什么錯誤的問候。 ################################################# ############################################
const entity = await strapi.service("api::order.order").create({
data: {
game: gameId,
user: user.id,
totalPayment,
idPayment: charge.id,
addressShipping,
},
});
const entry = await strapi.query("order").create(entity);
const sanitizedEntity = await this.sanitizeOutput(entry, ctx);
return this.transformResponse(sanitizedEntity);
},
}));
################################################# ############################################
################################################# ########################################## 前端:Api 請求# ################################################# ##########################################
export async function paymentCardApi(token, products, idUser, address, logout) {
try {
const addressShipping = address.attributes;
delete addressShipping.user;
delete addressShipping.createdAt;
const url = `${BASE_PATH}/api/orders`;
const params = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
data: { token, game: products, user: idUser, addressShipping },
}),
};
const result = await authFetch(url, params, logout);
return result;
} catch (error) {
console.log(error);
}
}
################################################# ################################################# ################################################# ######################################
我希望有人能指導我,告訴我為什么我錯了,解決方案比什么都重要,因為我是一名大三學生; 在此,我開始進入這個全棧編程的世界,在此先感謝############################### ################################################# ############ ##################################### ################################################# ######在此處輸入圖片描述
已解決:我有幾個錯誤,第一個是文檔問題,因為我想像 v3 一樣實現它,而在 v4 中它是非常不同的。 我閱讀了文檔並在最后做了幾個測試,如果有人為他工作,我會留下我的專業代碼並且我想在一些我可以的項目中實現它
################################################# ################################################ ################################################# ############“使用嚴格”;
/**
* order controller
*/
################################################# ################################################ ################################################# ############
const { createCoreController } = require("@strapi/strapi").factories;
const stripe = require("stripe")(
"sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
module.exports = createCoreController("api::order.order", ({ strapi }) => ({
async create(ctx, { files } = {}) {
const ctxData = ctx.request.body;
const dataUser = ctx.state.user;
const {
data: { token, game, user, addressShipping },
} = ctxData;
let Payment = 0;
let gameId = 0;
game.forEach((product) => {
Payment =
Payment +
product.attributes.price -
Math.floor(product.attributes.price * product.attributes?.discount) /
100;
gameId = product.id;
});
let gameData2 = game.map(function (products) {
return `
Nombre del Producto: ${products.attributes.title},
Precio normal: ${products.attributes.price},
Descuento: %${products.attributes.discount},
Precio final: ${
products.attributes.price -
(products.attributes.price * products.attributes.discount) / 100
}; `;
});
const totalPayment = Math.floor(Payment * 100);
const idusuario = JSON.stringify(ctxData.data.user);
const charge = await stripe.charges.create({
amount: totalPayment,
currency: "usd",
source: token.id,
description: `ID usuario: ${idusuario}, Username: ${
dataUser.username
}, Nombre Completo: ${" " + dataUser.name + "," + dataUser.lastname},
Orden:
${gameData2}
Precio Total de la orden: $ ${Payment.toFixed(2)} usd
`,
});
let createOrder = [];
for await (const product of game) {
const datos = {
data: {
game: product.id,
user: idusuario,
totalPayment: Payment.toFixed(2),
idPayment: charge.id,
adressesShipping: addressShipping,
},
};
const validData = await strapi.service("api::order.order").create(datos);
createOrder.push(validData);
}
const sanitizedEntity = await this.sanitizeOutput(createOrder, ctx);
return this.transformResponse(sanitizedEntity);
},
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.