SOLVED:
I had several errors, the first one was in question of the documentation since I wanted to implement it as if it were v3 when in v4 it is very different. I read the documentation and did several tests at the end, I will leave my pro code if someone works for him and I want to implement it in some project I could
#########################################################################
“use strict”;
/**
- order controller
*/
#########################################################################
#########################################################################
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 gametitle = "";
// let gameId2 = 0;
// let gamePrice = 0;
// let gameDiscount = 0;
// gameId2 = products.id;
// gamePrice = products.attributes.price;
// gameDiscount = products.attributes.discount;
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
`,
});
console.log(charge );
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);
// console.log(sanitizedEntity);
return this.transformResponse(sanitizedEntity);
},
}));