我在嘗試將資料傳遞到“金額”引數時遇到問題。
我正在將“價格”從客戶端發送到服務器。
當我將資料輸出到控制臺時,我得到了正確的資訊。
當我檢查“typeOf”時,我得到“number”,這是預期的輸出。
但是,當我運行應用程式時,我收到了錯誤
“UnhandledPromiseRejectionWarning:錯誤:無效整數:NaN”
似乎“paymentIntents.create”方法沒有讀取資料。your text
我在這里做錯了什么?
如何將價格欄位傳遞給金額引數。
這是我的代碼:
`
const express = require("express");
const app = express();
const { resolve } = require("path");
const stripe = require("stripe")(process.env.secret_key); // https://stripe.com/docs/keys#obtain-api-keys
app.use(express.static("."));
app.use(express.json());
const calculateOrderAmount = price =>{
const total = price * 100
return total
}
// An endpoint for your checkout
app.post("/payment-sheet", async (req, res) => {
const price = req.body.price
console.log(typeof calculateOrderAmount(price))
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(price),
currency: "usd",
//customer: customer.id,
automatic_payment_methods: {
enabled: true,
},
});
// Send the object keys to the client
res.json({
publishableKey: process.env.publishable_key, // https://stripe.com/docs/keys#obtain-api-keys
paymentIntent: paymentIntent.client_secret,
//customer: customer.id,
//ephemeralKey: ephemeralKey.secret
});
});
app.listen(process.env.PORT, () =>
console.log(`Node server listening on port ${process.env.PORT}!`)
);
`
我嘗試直接發送“價格”欄位而不進行計算,回傳的錯誤是:
Error: Missing required param: amount.
uj5u.com熱心網友回復:
問題是我從客戶端向我的服務器發送了 2 個 POST 請求。第一個是發送客戶意圖,另一個是發送金額,因此金額未在支付意圖中發送。
一旦我按照@bismarcks 的建議修復并編輯了我的代碼,它就可以正常作業了。
uj5u.com熱心網友回復:
您正在嘗試將amount引數的值設定為函式。該函式在發送到 Stripe API 之前從未真正運行過,因此是 NaN。
相反,請嘗試在您之前運行該功能const paymentIntent = await stripe.paymentIntents.create({:
app.post("/payment-sheet", async (req, res) => {
const price = req.body.price
const amountToCharge = calculateOrderAmount(price)
const paymentIntent = await stripe.paymentIntents.create({
amount: amountToCharge,
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/530894.html
