所以我正在尝试使用Stripe,React和Nodejs进行测试付款。
在前端,我正在使用createPaymentMethod()
,并发送带有与产品相关的有效用户信息,多少项,用户和地址信息的发帖请求。像这样:
const purchase = {
...paymentMethod,
address: {
line1: `${user.address.number} ${user.address.street}`,
postal_code: user.address.zipcode,
city: user.address.city,
state: user.address.state,
country: 'Brazil'
},
customer: {
email: user.email,
name: user.name,
},
product,
quantity,
}
let data = await fetch('http://localhost:3002/checkout', {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(purchase),
})
到目前为止一切都很好...
在后端,我正在检索此信息,并在尚未创建用户时使用stripe.customers.create()
const customerPurchase = await stripe.customers.create({
email: customer.email,
name: customer.name,
address,
payment_method: payment_method_id //response from paymentMethod on the front end
})
最后,要创建费用,我使用charges.create()
方法:
const idempotencyKey = uuid()
return stripe.charges.create({
amount: product.price * quantity * 100,
currency: 'brl',
customer: customerStripeId,
receipt_email: customer.email,
description: `Congratulations! You just purchased the item: ${product.name}!`,
shipping: {
address: {
line1: address.line1,
city: 'Manaus',
country: 'Brazil',
postal_code: address.zipcode,
}
}
},{
idempotencyKey
})
但是我遇到此错误:StripeCardError: Cannot charge a customer that has no active card
。
我认为这可能与必须通过以下方式传递的source
属性有关:source: req.body.stripeToken
。但是,我不知道从何处获取该stripeToken,尝试了所有操作,但尚未找到任何东西。
有人可以帮我吗?我真的很感激。
答案 0 :(得分:0)
您不能直接将“付款方式”与“费用”结合使用,您应该使用更新的“付款意图” API来代替this guide,后者具有支持Strong Customer Authentication的额外好处。
要为已经附加付款方式的客户 创建付款,您可以检索该客户的付款方式,然后创建付款:
const paymentMethods = await stripe.paymentMethods.list({
customer: '{{CUSTOMER_ID}}',
type: 'card',
});
const paymentIntent = await stripe.paymentIntents.create({
amount: 1099,
currency: 'usd',
customer: '{{CUSTOMER_ID}}',
payment_method: '{{PAYMENT_METHOD_ID}}',
});
然后是confirm that payment on the client side:
stripe.confirmCardPayment(intent.client_secret)
如果您之前已获得客户身份验证并prepared that card for later use,则可以confirm on the server side when charging later:
const paymentIntent = await stripe.paymentIntents.create({
amount: 1099,
currency: 'usd',
customer: '{{CUSTOMER_ID}}',
payment_method: '{{PAYMENT_METHOD_ID}}',
off_session: true,
confirm: true,
});