【问题标题】:How to attach a payment method with Stripe?如何使用 Stripe 附加付款方式?
【发布时间】:2020-10-13 10:40:56
【问题描述】:

我正在努力让 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


【解决方案1】:

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

  1. 创建客户
  2. 使用 Stripe.js 安全地收集付款信息
  3. 将 PaymentMethod 附加到客户
  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

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2021-07-28
    • 1970-01-01
    • 2015-05-20
    • 2017-06-19
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多