如何使用 Stripe 附加付款方式?

问题描述 投票:0回答:2

我正在努力让 Stripe 在我的服务器上运行。

所以,在客户端,我有这段代码(经过一番努力才让元素正常工作):

this.props.stripe.createPaymentMethod({ type: 'card', card: cardElement, billing_details: { name: name } }).then((paymentMethod) => {
        // server request with customer_id and paymentMethod.id);
      });

这个效果很好。现在,在服务器 (NodeJS) 上,我想为我的客户添加一个新的持续订阅,并收取固定费用。

这就是我所拥有的:

const paymentIntent = await stripe.paymentIntents.create({
          amount: 1000,
          currency: 'usd',
          payment_method_types: ['card'],
          customer: req.body.customer_id,
          metadata: { integration_check: 'accept_a_payment' },
        });

const paymentIntentConfirmation = await stripe.paymentIntents.confirm(paymentIntent.id, { payment_method: req.body.paymentMethod_id });


const newSubscription = await stripe.subscriptions.create({
          customer: req.body.customer_id,
          items: [{ plan: premium_plan.id, quantity: 1 }],
          default_payment_method: req.body.paymentMethod_id,
        });
const attachPaymentToCustomer = await stripe.paymentMethods.attach(req.body.paymentMethod_id, { customer: req.body.customer_id });


const updateCustomerDefaultPaymentMethod = await stripe.customers.update(req.body.customer_id, {
      invoice_settings: {
        default_payment_method: req.body.paymentMethod_id,
      },
    });

因此,如果我没有将付款附加给客户,我会收到以下错误消息:

'The customer does not have a payment method with the ID pm_1Gx9m1HVGJbiGjghYhrkt6j. The payment method must be attached to the customer.'

如果这样做,我会收到以下错误消息:

'This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again.'

那么,我如何添加该死的付款方式,这样当我检索我的客户时,它显示该客户已更新为他刚刚订阅的服务的新订阅,以及他的付款方式(在本例中为 CC) .

非常感谢这里为沮丧的用户提供的任何帮助!

更一般地说,到目前为止,实施 Stripe 是一次非常痛苦的经历。似乎什么都不起作用。我使用 Typescript,但有很多错误。该文档不是很有帮助,也没有很好的解释。 “创建源”,“创建令牌”,“创建支付意图”,“创建设置意图”,我应该如何理解所有这些东西之间的区别?我想添加一个该死的在线订阅,这应该是互联网服务的标准程序。为什么有这么多不同的指导方针,有代币,有来源,等等....

payment stripe-payments
2个回答
8
投票

您可以在此处进行一些更改以使其正常工作,为了启动订阅 [1],您不需要创建并确认 PaymentIntent。这是在为付款创建发票时在发票内自动创建的。所以大致的步骤是(你已经做了很多这样的事情,但只是为了有一个端到端的例子):

  1. 创建客户
  2. 使用 Stripe.js 安全地收集支付信息
  3. 将付款方式附加给客户
  4. (可选)将其保存为发票设置默认付款方式(因为您可以将 PaymentMethod 作为默认付款方式传递到订阅创建,但这是一个很好的做法,以便您可以使用相同的付款方式为该客户启动订阅)
  5. 创建订阅
  6. 提供您的服务
  7. 考虑 SCA/3DS 并处理身份验证 [2]

[1]对此进行了详细概述。这里有一些启动订阅的示例代码,当然您可以用您自己的 ID 替换创建产品和价格的调用:

const customer = await stripe.customers.create({
  name: "Foo Bartley"
});

const paymentMethod = await stripe.paymentMethods.create(
  {
    type: 'card',
    card: {
      number: '4242424242424242',
      exp_month: 6,
      exp_year: 2021,
      cvc: '314',
    },
  }
);

const product = await stripe.products.create(
  {name: 'Gold Special'}
);

const price = await stripe.prices.create(
  {
    unit_amount: 1111,
    currency: 'eur',
    recurring: {interval: 'month'},
    product: product.id,
  }
);

// Everything above here is just setting up this demo

const attachPaymentToCustomer = await stripe.paymentMethods.attach(
  paymentMethod.id,  // <-- your payment method ID collected via Stripe.js
  { customer: customer.id } // <-- your customer id from the request body  
  
); 

const updateCustomerDefaultPaymentMethod = await stripe.customers.update(
  customer.id, { // <-- your customer id from the request body
  
    invoice_settings: {
      default_payment_method: paymentMethod.id, // <-- your payment method ID collected via Stripe.js
    },
});

const newSubscription = await stripe.subscriptions.create({
  customer: customer.id, // <-- your customer id from the request body
  items: [{ plan: price.id, quantity: 1 }], // <-- plans and prices are compatible Prices is a newer API
  default_payment_method: paymentMethod.id, // <-- your payment method ID collected via Stripe.js
});

希望这有帮助!

[1] https://stripe.com/docs/billing/subscriptions/fixed-price#create-subscription

[2] https://stripe.com/docs/billing/subscriptions/fixed-price#manage- payment-authentication


0
投票

为了能够将付款意向中的付款方式附加给客户,您可以在付款意向创建选项中添加

"setup_future_usage": "off_session"
,如下所述:

https://stripe.com/docs/api/ payment_intents/create#create_ payment_intent-setup_future_usage

另请参阅:

此付款方式之前曾在未附加到客户或与客户分离的情况下使用过,并且不得再次使用

我还没有测试过实际使用该付款方式的客户,所以我不能 100% 确定它会起作用。但它似乎正确地附着在客户身上,所以我认为客户可以使用它。

© www.soinside.com 2019 - 2024. All rights reserved.