【问题标题】:StripeCardError: Cannot charge a customer that has no active card. Trying to make a payment using StripeStripe Card 错误:无法向没有活动卡的客户收费。尝试使用 Stripe 付款
【发布时间】:2021-02-13 23:53:30
【问题描述】:

所以我正在尝试使用 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,尝试了所有方法但还没有找到任何东西。

有人可以帮帮我吗?我真的很感激。

【问题讨论】:

    标签: node.js reactjs stripe-payments


    【解决方案1】:

    您不能直接将付款方式与费用一起使用,您应该使用更新的 Payment Intents 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,
      });
    

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 2017-03-17
      • 1970-01-01
      • 2017-11-05
      • 2015-05-25
      • 2015-03-01
      相关资源
      最近更新 更多