![](/img/trans.png)
[英]Passing a req.body price value to another Express route to redirect to Stripe and charge
[英]Express pass req.body data into the Stripe route to pay
在我的快遞應用程序中,我有一條路線可以從 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: 無法讀取未定義的屬性(讀取“價格”)
提前致謝
您是否將價格發送到客戶端的/create-checkout-session
路由? 可能發生的情況是您將價格傳遞給客戶端的/data
路由,但您沒有將價格傳遞給/create-checkout-session
。 如果不了解您如何將數據發送到 API,就很難判斷。
如果您想使用一個路由 - 例如/data
路由,您可以將/create-checkout-session
轉移到一個函數中並在/data
路由中調用該函數並將價格作為參數傳遞給/create-checkout-session function
。
謝謝我能夠修改我的代碼以在 /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); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.