Stripe创建客户后扣款报错‘Required Action’求助
Stripe 客户保存与后续扣款「Required Action」错误解决
问题根源
你当前直接调用paymentIntents.create并设置confirm: true+allow_redirects: 'never',当测试卡需要3DS验证时,因无法完成验证流程触发「Required Action」错误。你的需求是先保存客户信息、无需实时扣款,正确做法是通过SetupIntent预验证支付方法可用性,确保后续扣款顺畅。
解决方案步骤
- 创建客户并附加支付方法后,用SetupIntent完成支付方法验证,处理可能的3DS等验证流程
- 后续扣款时,直接使用已验证的支付方法创建PaymentIntent并确认,测试模式下即可成功支付
修改后的代码
创建客户并验证支付方法接口
app.post('/create-customer', async (req, res) => { try { // 从前端token创建PaymentMethod const paymentMethod = await stripe.paymentMethods.create({ type: 'card', card: { token: req.body.token, }, }); // 创建客户并设置默认支付方法 const customer = await stripe.customers.create({ name: 'Abxxxx kumar', email: 'xxxxx@gmail.com', payment_method: paymentMethod.id, invoice_settings: { default_payment_method: paymentMethod.id, }, }); // 创建SetupIntent验证支付方法,确保后续可扣款 const setupIntent = await stripe.setupIntents.create({ customer: customer.id, payment_method: paymentMethod.id, confirm: true, automatic_payment_methods: { enabled: true, allow_redirects: 'if_required', // 允许必要的验证跳转 }, }); // 若需额外验证(如3DS),返回客户端处理 if (setupIntent.status === 'requires_action') { return res.status(200).json({ customer, setupIntent, requiresAction: true, clientSecret: setupIntent.client_secret }); } // 验证成功,返回客户信息 res.status(200).json({ customer, setupIntent, requiresAction: false }); } catch (error) { console.error(error); res.status(500).json({ error: '创建客户或验证支付方法时出错。' }); } });
后续扣款函数
const deductAmount = async () => { try { const amount = 1000; console.log("🚀 ~ 扣款金额:", amount); const customerID = 'cus_Oas7xxxxx1BCrv'; const customer = await stripe.customers.retrieve(customerID); const defaultPaymentMethod = customer.invoice_settings.default_payment_method; if (!defaultPaymentMethod) { throw new Error('客户未设置默认支付方式。'); } // 使用已验证的支付方法创建并确认PaymentIntent const paymentIntent = await stripe.paymentIntents.create({ amount: parseInt(amount) * 100, currency: 'usd', customer: customerID, payment_method: defaultPaymentMethod, confirm: true, automatic_payment_methods: { enabled: true, }, }); if (paymentIntent.status === 'succeeded') { console.log("🚀 支付成功。PaymentIntent ID:", paymentIntent.id); } else { console.error("❌ 支付失败。PaymentIntent状态:", paymentIntent.status); } } catch (error) { console.error("❌ 错误:", error.message); } };
测试提示
- 使用无需3DS的测试卡(如
4242 4242 4242 4242,任意过期日期、CVC)可直接验证成功并完成扣款 - 若用需3DS的测试卡(如
4000 0025 0000 3155),需前端调用Stripe.js的confirmCardSetup处理验证流程,验证完成后支付方法即可用于后续扣款
内容的提问来源于stack exchange,提问作者Abdhesh kumar
相关产品推荐
相关产品推荐

