【发布时间】:2020-05-06 09:52:29
【问题描述】:
我以为我在回家的路上,然后砰!回到地球!而我自己的最后期限就快到了!
Final 使用条带支付表单让一切都在本地运行,并推送到现场,不知何故我从我的 api 代码中收到一条消息,说
控制台错误:
POST https://example.com/api/charge 400 (Bad Request)
{ "message":"缺少必需的参数:数量。" },
这一切都很新,但任何帮助都会很棒!代码贴在下面
我的 charge.js 文件
import Stripe from "stripe";
const stripe = new Stripe(process.env.SECRET_KEY);
export default async (req, res) => {
const { id, amount } = req.body;
try {
const payment = await stripe.paymentIntents.create({
amount,
currency: "AUD",
description: "Delicious empanadas",
payment_method: id,
confirm: true,
});
console.log(payment);
return res.status(200).json({
confirm: "abc123",
});
} catch (error) {
console.log(error);
return res.status(400).json({
message: error.message,
});
}
};
我的 checkoutform.js
const CheckoutForm = ({ success }) => {
const stripe = useStripe();
const elements = useElements();
const [isProcessing, setProcessingTo] = useState(false);
const [checkoutError, setCheckoutError] = useState();
const handleSubmit = async (event) => {
event.preventDefault();
const { error, paymentMethod } = await stripe.createPaymentMethod({
type: "card",
card: elements.getElement(CardElement),
billing_details: {
name: event.target.name.value,
address: {
city: event.target.city.value,
line1: event.target.line1.value,
line2: event.target.line2.value,
},
},
});
setProcessingTo(true);
if (!error) {
const { id } = paymentMethod;
try {
const { data } = await axios.post("/api/charge", { id, amount: 2000 });
console.log(data);
success();
} catch (error) {
console.log(error);
}
}
};
【问题讨论】:
-
您的代码示例的第 9 行附近似乎没有将金额作为
key: value传递。那应该至少可以解决该错误。 stripe.com/docs/api/payment_intents/create?lang=node
标签: javascript reactjs stripe-payments