我有一个带有 Strapi 后端的 React 应用程序。我有一个购物车,正在尝试连接到 Stripe。
在我的后端,我在 Strapi 中创建了一条新路线
'use strict';
// @ts-ignore
const stripe = require("stripe")("sk_test_51H");
const { createCoreRouter } = require('@strapi/strapi').factories;
module.exports = createCoreRouter('api::order.order');
module.exports = {
routes: [
{
method: "POST",
path: "/orders/payment",
handler: "order.setUpStripe"
}
]
}
然后是setUpStripe函数
"use strict";
// @ts-ignore
const stripe = require("stripe")("sk_test_51H");
const { createCoreController } = require("@strapi/strapi").factories;
module.exports = {
...createCoreController("api::order.order"),
setUpStripe: async (ctx) => {
console.log('here')
let total = ''; // Initialize total as a numeric value
let validatedCart = [];
let receiptCart = [];
const { cart } = ctx.request.body;
await Promise.all(cart.map(async (font) => {
try {
const validatedProduct = await strapi.db.query('api::font.font').findOne({
where: { id: font.id }
});
if (validatedProduct) {
validatedCart.push(validatedProduct);
receiptCart.push({
id: font.id
});
}
} catch (error) {
}
}));
total = validatedCart.reduce((acc, { price }) => acc + price, 0);
try {
const paymentIntent = await stripe.paymentIntents.create({
amount: total,
currency: 'usd',
metadata: { cart: JSON.stringify(receiptCart) },
payment_method_types: ['card']
});
// Send a response back to the client
ctx.send({
paymentIntent,
});
} catch (error) {
ctx.send({
error: true,
message: 'Error in processing payment',
details: error.message,
});
}
}
};
这不起作用。
如果我在浏览器中尝试这条路线
http://localhost:1337/api/orders/payment
我明白了
{“数据”:空,
“错误”:{“状态”:404,“名称”:“NotFoundError”, “消息”:“未找到”,“详细信息”:{} } }
我希望能够在浏览器中查看这个地址
您的 Strapi 端点使用
POST
HTTP 方法。在浏览器中打开该 URL/路由将使用 GET
HTTP 方法,因此预计会 404
。
如果您希望能够直接在浏览器中访问该路由,则需要将方法更改为
GET
。