在我的快遞應用程式中,我有一條路線可以從 req.body 中的客戶端獲取蛋糕價格
app.post("/data", (req, res) => {
let price = req.body[0].price * 1000;
console.log(`Cake price is: £${price}`);
});
這作業正常并在控制臺中記錄價格
我有另一條路線,我想將這個可變價格從 req.body 傳遞給 Stripe 并向客戶收費。
app.post("/create-checkout-session", async (req, res) => {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: [
{
price_data: {
currency: "gbp",
unit_amount: req.body[0].price,
product_data: {
name: "CakeItOrLeaveIt",
// description: "", cake it or leave it description
// images: [], cake it or leave it logo
},
},
quantity: 1,
},
],
mode: "payment",
success_url: `http://localhost:4242/success.html`,
cancel_url: `http://localhost:4242/cancel.html`,
});
res.redirect(303, session.url);
});
當我將 req.body[0].price 傳遞到我的 Stripe 路線時,我收到以下錯誤。
unit_amount: req.body[0].price, TypeError: 無法讀取未定義的屬性(讀取“價格”)
提前致謝
uj5u.com熱心網友回復:
您是否將價格發送到/create-checkout-session客戶端的路由?可能發生的情況是您將價格傳遞給/data客戶端的路線,但您沒有將價格傳遞給/create-checkout-session. 如果不了解您如何將資料發送到 API,就很難判斷。
如果您想使用一條路線 - 例如,/data您可以將路線轉移/create-checkout-session到一個函式中并在/data路線中呼叫該函式并將價格作為引數傳遞給/create-checkout-session function.
uj5u.com熱心網友回復:
謝謝我能夠修改我的代碼以在 /create-checkout-session 路線中請求蛋糕價格
app.post("/create-checkout-session", async (req, res) => {
console.log(req.body[0].price * 1000); //this works and gets the cake price
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: [
{
price_data: {
currency: "gbp",
unit_amount: req.body[0].price * 1000, //this errors with Cannot read properties of undefined (reading 'price')
product_data: {
name: "CakeItOrLeaveIt",
// description: "", cake it or leave it description
// images: [], cake it or leave it logo
},
},
quantity: 1,
},
],
mode: "payment",
success_url: `http://localhost:4242/success.html`,
cancel_url: `http://localhost:4242/cancel.html`,
});
res.redirect(303, session.url);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333151.html
標籤:javascript 节点.js 表达 条纹支付
