我有一个条带支付意图的支付系统,我想创建一个成功的支付。但是一旦我支付了,就会在服务器端显示这个错误
C: Project_Programing_hero assainment-list assainment-12 best-tech-server node_modules 条纹 lib Error.js: 40返回new StripeInvalidRequestError(rawStripeError);^
StripeInvalidRequestError:该值必须大于等于1。
在checkoutFrom.js
useEffect(() => {
console.log(typeof (totalPrice));
获取(http://localhost: 5000/create-payment-intent, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ price: parseInt(totalPrice) }),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
const dataClientSecret = data?.clientSecret;
if (dataClientSecret) {
setClientSecret(dataClientSecret)
}
})
}, [totalPrice])
const handleSubmit = async (event) => {
event.preventDefault()
if (!stripe || !elements) {
return;
}
const card = elements.getElement(CardElement);
if (card == null) {
return;
}
const { error, paymentMethod } = await stripe.createPaymentMethod({
type: 'card',
card,
});
if (error) {
console.log('[error]', error);
setCardError(error.message)
} else {
console.log('[PaymentMethod]', paymentMethod);
setCardError('')
}
// confirm card payment
setCardSuccess('')
setProcessing(true)
const { paymentIntent, error: intentError } = await stripe.confirmCardPayment(
`${clientSecret}`,
{
payment_method: {
card: card,
billing_details: {
name: customerName,
email: email
},
},
},
);
if (intentError) {
setCardError(intentError?.message)
setProcessing(false)
} else {
setCardError('')
setTransitionId(paymentIntent.id)
console.log(paymentIntent);
setCardSuccess('Congrats,Your payment is compiled')
// store payment to database
const payment = {
order: _id,
transitionId: paymentIntent.id
}
axios.patch(`http://localhost:5000/order/${_id}`, payment)
.then(res => {
setProcessing(false)
console.log(res.data);
})
}
}
" This value must be greater or equal to 1 "错误表示在支付意图创建请求中设置的amount[0]参数小于1。在服务器的端点/create-payment-intent
中,您需要确保request.price大于或等于1,并正确地分配给amount参数。例如,
const paymentIntent = await stripe.paymentIntents.create({
amount: request.price,
currency: 'usd',
automatic_payment_methods: {enabled: true},
});
除了服务器端,您还应该确保totalPrice在传递到服务器之前,在前端大于或等于1。
body: JSON.stringify({ price: parseInt(totalPrice) }),
[0] https://stripe.com/docs/api/payment_intents/create create_payment_intent-amount