简体   繁体   English

StripeCardError:无法向没有活动卡的客户收费。 尝试使用 Stripe 付款

[英]StripeCardError: Cannot charge a customer that has no active card. Trying to make a payment using Stripe

So I am trying to make a test payment using Stripe, React and Nodejs.所以我尝试使用 Stripe、React 和 Nodejs 进行测试付款。

on the front-end I am using createPaymentMethod() and sending a post request with valid user information related to products, how many items, user and address info.在前端,我使用createPaymentMethod()并发送带有与产品相关的有效用户信息、多少项目、用户和地址信息的发布请求。 Like so:像这样:

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),
        })

So far so good...到现在为止还挺好...

On the backend I am retrieving this information and using stripe.customers.create() when the user hasn't been created yet在后端,我正在检索此信息并在尚未创建用户时使用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
      })

And finally, to create the charge I use charges.create() method:最后,为了创建费用,我使用了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
    })

However I am getting this error: StripeCardError: Cannot charge a customer that has no active card .但是我收到此错误: StripeCardError: Cannot charge a customer that has no active card

I figured it might have something to do with some source property that had to be passed like this: source: req.body.stripeToken .我认为它可能与某些必须像这样传递的source属性有关: source: req.body.stripeToken However, I have no clue where to get this stripeToken, tried everything but hasn't found anything yet.但是,我不知道从哪里获得这个 stripeToken,尝试了所有方法但还没有找到任何东西。

Can someone help me please?有人能帮助我吗? I would really appreciate it.我真的很感激。

You cannot use Payment Methods directly with Charges, you should use the newer Payment Intents API instead following this guide , which has the added benefit of supporting Strong Customer Authentication .您不能直接将 Payment Methods 与 Charges 一起使用,您应该使用较新的 Payment Intents API 而不是遵循本指南,它具有支持强客户身份验证的额外好处。

To create a payment for a Customer after already attaching a payment method, you can retrieve the payment methods for that customer and then create a payment:附加付款方式为客户创建付款,您可以检索该客户的付款方式,然后创建付款:

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}}',
  });

and then confirm that payment on the client side :然后在客户端确认付款

stripe.confirmCardPayment(intent.client_secret)

If you're previously had the Customer authenticate and prepared that card for later use , you can 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,
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM