[英]StripeCardError: Cannot charge a customer that has no active card. Trying to make a payment using Stripe
所以我嘗試使用 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,嘗試了所有方法但還沒有找到任何東西。
有人能幫助我嗎? 我真的很感激。
您不能直接將 Payment Methods 與 Charges 一起使用,您應該使用較新的 Payment Intents API 而不是遵循本指南,它具有支持強客戶身份驗證的額外好處。
要在附加付款方式后為客戶創建付款,您可以檢索該客戶的付款方式,然后創建付款:
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}}',
});
然后在客戶端確認付款:
stripe.confirmCardPayment(intent.client_secret)
如果您之前已讓客戶驗證並准備好該卡以備后用,您可以在稍后充電時在服務器端確認:
const paymentIntent = await stripe.paymentIntents.create({
amount: 1099,
currency: 'usd',
customer: '{{CUSTOMER_ID}}',
payment_method: '{{PAYMENT_METHOD_ID}}',
off_session: true,
confirm: true,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.