You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React+Node.js集成Stripe支付显示未完成问题求助

Stripe支付集成问题:前端获取正常token但后台显示支付未完成

我使用react-stripe-checkout结合Node.js集成Stripe支付功能,前端已成功获取到支付token,但Stripe后台始终显示支付未完成,需要排查问题原因并实现正常支付。以下是相关代码及token数据:


前端代码

支付请求处理函数

const payNow = async token => {
    try {
        const response = await axios({
            url: `/mycardpay/${localStorage.getItem('slno')}`,
            method: 'post',
            data: {
                amount: this.calculateTotalAmount(),
                token,
            }
        });

        if (response.data.message == 'Success') {
            console.log(response.data)
        }

    } catch (err) {
        console.log(err)
    }
}

StripeCheckout组件配置

<StripeCheckout   
    stripeKey="pk_stripekey..."  
    label='Pay Now'  
    name='Pay With Credit Card'  
    billingAddress   
    amount={this.calculateTotalAmount()*100}   
    description={`Your Total Amount $${this.calculateTotalAmount()}`}  
    token={payNow} 
/>

后端代码

const Stripe = require('stripe')(process.env.Secret_Key); 

async function paymentCard(req, res, next){
    try{
        const {usersl} = req.params;
        const {token, amount} = req.body;
        
        try{
            console.log(amount)
            console.log(token)
            console.log(token.card.last4)
            const customer = await Stripe.customers.create({
                email: token.email, // 使用token中的邮箱
                source: token.id, // 将token ID作为支付方式来源
              });
              
            await Stripe.paymentIntents.create({
                amount: amount, // 注释标注为分,但实际未转换单位
                currency: 'inr',
                payment_method_types: ['card'],
                payment_method: token.card.id, // 使用token关联的卡ID
                customer: customer.id, 
                confirm: true,
                    
            })

获取到的token数据

{
  "id": "tok_1Ng5esSI10A4W78jlyIUAMLX",
  "object": "token",
  "card": {
    "id": "card_1Ng5erSI10A4W78j5WRiJWUm",
    "object": "card",
    "address_city": "dhaka",
    "address_country": "Bangladesh",
    "address_line1": "asdad",
    "address_line1_check": "unchecked",
    "address_line2": null,
    "address_state": null,
    "address_zip": "1244",
    "address_zip_check": "unchecked",
    "brand": "Visa",
    "country": "US",
    "cvc_check": "unchecked",
    "dynamic_last4": null,
    "exp_month": 4,
    "exp_year": 2026,
    "funding": "credit",
    "last4": "4242",
    "name": "asdad asd",
    "tokenization_method": null,
    "wallet": null
  },
  "client_ip": "103.110.161.4",
  "created": 1692276638,
  "email": "arnobsamin95@gmail.com",
  "livemode": false,
  "type": "card",
  "used": false
}

问题排查点

  1. 金额单位不匹配:前端组件中已将金额乘以100转成分,但后端直接使用前端传入的amount(元单位)作为PaymentIntent的金额,导致实际金额仅为预期的1/100,无法触发正常扣款。
  2. PaymentIntent参数错误:使用token.card.id作为payment_method是无效的,token.id才是合法的支付令牌标识;同时创建Customer时已将token.id关联为客户支付来源,无需重复指定payment_method。
  3. 缺少错误处理与响应返回:后端未捕获PaymentIntent创建过程中的错误,也未向前端返回明确的成功/失败响应,导致前端无法确认支付状态,且错误无法暴露。
  4. API用法冲突:旧版token(tok_开头)属于Stripe传统API,与PaymentIntent的payment_method参数不兼容,应使用source参数关联token。

修复后的后端代码

const Stripe = require('stripe')(process.env.Secret_Key); 

async function paymentCard(req, res, next){
    try{
        const {usersl} = req.params;
        const {token, amount} = req.body;
        
        // 将元单位金额转成分
        const amountInCents = Math.round(amount * 100);
        
        // 创建客户并关联token
        const customer = await Stripe.customers.create({
            email: token.email,
            source: token.id,
        });
        
        // 创建并确认支付意向
        const paymentIntent = await Stripe.paymentIntents.create({
            amount: amountInCents,
            currency: 'inr',
            customer: customer.id,
            confirm: true,
            automatic_payment_methods: { enabled: true },
        });
        
        // 返回成功响应
        res.status(200).json({
            message: 'Success',
            paymentId: paymentIntent.id
        });
        
    } catch (error) {
        // 捕获并返回错误信息
        console.error('支付错误:', error);
        res.status(400).json({
            message: 'Payment failed',
            error: error.message
        });
    }
}

内容的提问来源于stack exchange,提问作者Samin Arnob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 04:56:32