Stripe v3 - SetupIntents 和订阅 [英] Stripe v3 - SetupIntents and Subscriptions

查看:29
本文介绍了Stripe v3 - SetupIntents 和订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SetupIntents 验证并保存付款方式,然后使用它创建订阅以向客户收取所需金额(立即然后每月).

I'm trying to use SetupIntents to validate and save a Payment Method and then create a Subscription using it to charge the customer (inmediately and then monthly) for the required amount.

这似乎工作正常:

  • 该卡已通过验证(如果需要,包括 SCA)
  • 付款方式已创建,默认附加到客户并启用以供将来使用,状态为 SUCEEDED.
  • 订阅已创建并使用上述付款方式

问题是,当所提供的卡需要安全客户授权 (SCA) 时,Stripe 会生成相应的发票和付款意图,但后者的状态为requires_action",即使正确的付款方式(已启用以供将来使用)是正在使用并且已经进行了卡验证.

The problem is that Stripe then generates the corresponding Invoice and Payment Intent but the latter with the status "requires_action" whenever the provided Card requires Secure Customer Authorization (SCA), even though the right payment method (enabled for future usage) is being used and the card validation has been already performed.

我认为使用 SetupIntents 的全部意义在于预先验证付款方式,然后能够向客户收费.

I thought the whole point of using SetupIntents was precisely to validate the payment method beforehand and be able to charge the customer afterwards.

我的假设是完全错误的还是可能的,我可能只是遗漏了什么?

Is my assumption simply wrong or this is actually possible and I might just be missing something?

提前致谢

EDIT:这是后端的订阅创建代码:

EDIT: This is the subscription creation code in the backend:

# Set the default payment method on the customer
      Stripe::Customer.update(
        stripe_customer_id,
        invoice_settings: {
          default_payment_method: @requested_source
        }
      )

subscription = Stripe::Subscription.create({
       "customer" => stripe_customer_id,
       "proration_behavior" => 'create_prorations',
       "items" => [
         [
           "plan" => "#{@stripe_plan_api_id}",
         ],
       ],
       'default_tax_rates' => [
         "#{@stripe_tax_rate_id}",
       ],
       "expand" => ["latest_invoice.payment_intent"]
     });

推荐答案

感谢 Eduardo 的提问.

Thanks for the question, Eduardo.

有几种方法可以创建订阅,同时获得客户 SCA 身份验证和稍后对卡收费的许可.假设我们已经创建了一个 Stripe Customer 对象,并且他们的 ID 存储在 stripe_customer_id 中.根据您现在的流程,有几个步骤:

There are a couple ways to create a Subscription while gaining the Customers SCA authentication and permission to charge the card later. Assuming we already have a Stripe Customer object created and their ID is stored in stripe_customer_id. With the flow you have now there are a couple steps:

  1. 创建SetupIntent.请注意,如果您使用 usage: 'off_session' 和 Customer 创建它,它会在确认后附加生成的 PaymentMethod.

  1. Create the SetupIntent. Note that if you create it with usage: 'off_session' and the Customer it'll attach the resulting PaymentMethod when confirmed.

setup_intent = Stripe::SetupIntent.create({
  customer: stripe_customer_id,
  usage: 'off_session',
})

  • 收集付款详细信息并在客户端上使用其 client_secret 确认 SetupIntent,这会将 PaymentMethod 附加到客户.请注意,它会附加但不会默认设置为 invoice_settings.default_payment_method,因此您需要稍后进行单独的 API 调用以更新 Customer(请参阅步骤 3).

  • Collect payment details and confirm the SetupIntent using its client_secret on the client which will attach the PaymentMethod to the customer. Note that it will attach but will not be set as the invoice_settings.default_payment_method by default, so you'll need to make a separate API call later to update the Customer (see step 3).

     stripe.confirmCardSetup(
       '{{setup_intent.client_secret}}', 
       {
         payment_method: {
           card: cardElement,
         },
       }
     ).then(function(result) {
       // Handle result.error or result.setupIntent
     });
    

  • 更新Customer 并将其invoice_settings.default_payment_method 设置为成功确认的SetupIntent 上PaymentMethod 的ID.

  • Update the Customer and set its invoice_settings.default_payment_method equal to the ID of the PaymentMethod on the successfully confirmed SetupIntent.

    Stripe::Customer.update(
      stripe_customer_id, {
      invoice_settings: {
        default_payment_method: 'pm_xxxx', # passed to server from client. On the client this is available on the result of confirmCardSetup 
      }
    })
    

  • 使用 off_session: true 创建 Subscription

    subscription = Stripe::Subscription.create({
      customer: stripe_customer_id,
      proration_behavior: 'create_prorations',
      items: [{
        plan: "#{@stripe_plan_api_id}",
      }],
      default_tax_rates: [
        "#{@stripe_tax_rate_id}",
      ],
      expand: ["latest_invoice.payment_intent"],
      off_session: true,
    })
    

  • 这使用所谓的商家发起的交易"(MIT) 进行订阅的第一笔付款.如果订阅是在客户离开之后创建的,并且技术上应该可行,那么这在技术上是可以的.

    This uses what's called a "Merchant Initiated Transaction" (MIT) for the Subscription's first payment. This is technically okay if the Subscription is created later after the Customer leaves and should technically work.

    如果在您创建 Subscription 时客户在您的网站/应用程序上,还有另一个流程更正确,不需要使用 SCA 的 MIT 豁免.订阅 没有试用的流程如下:

    If the customer is on your site/app when you create the Subscription, there's another flow that is a bit more correct and doesn't require using a MIT exemption for SCA. The flow is the following for a Subscription without a trial:

    1. 在客户端使用 createPaymentMethod 收集卡详细信息(无 SetupIntent)

    stripe.createPaymentMethod({
      type: 'card',
      card: cardElement,
    }).then(function(result) {
      //pass result to your server.
    })
    

  • 将这些卡的详细信息附加到 Customer

    Stripe::PaymentMethod.attach(
      "pm_xxx", {
        customer: stripe_customer_id
      }
    )
    

  • 更新Customerinvoice_settings.default_payment_method

    Stripe::Customer.update(
      stripe_customer_id,
      invoice_settings: {
        default_payment_method: @requested_source
      }
    )
    

  • 创建订阅(没有 off_session: true)

    subscription = Stripe::Subscription.create(
      customer: data['customerId'],
      items: [
        {
          price: 'price_H1NlVtpo6ubk0m'
        }
      ],
      expand: ['latest_invoice.payment_intent']
    )
    

  • 使用Subscriptionlatest_invoicepayment_intentclient_secret来收集支付细节并在客户端确认.

  • Use the Subscription's latest_invoice's payment_intent's client_secret to collect payment details and confirm on the client.

    stripe.confirmCardPayment(
      '{{subscription.latest_invoice.payment_intent.client_secret}}', { ......
    

  • 从 SCA 的角度来看,第二个支付流程更正确,以获得授权以对该卡收费.此处的指南概述了第二种方法:https://stripe.com/docs/billing/订阅/固定价格

    This second payment flow is a bit more correct from an SCA standpoint for getting authorization to charge the card. The second approach is outlined in the guide here: https://stripe.com/docs/billing/subscriptions/fixed-price

    我们还有一个条纹示例,您可以在这里进行实验:https://github.com/stripe-samples/subscription-use-cases/tree/master/fixed-price-subscriptions

    We also have a Stripe Sample you can use to experiment here: https://github.com/stripe-samples/subscription-use-cases/tree/master/fixed-price-subscriptions

    这篇关于Stripe v3 - SetupIntents 和订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆