簡體   English   中英

Stripe v3 - SetupIntents 和訂閱

[英]Stripe v3 - SetupIntents and Subscriptions

我正在嘗試使用 SetupIntents 來驗證和保存付款方式,然后使用它創建訂閱來向客戶(立即然后每月)收取所需的金額。

這似乎工作正常:

  • 卡經過驗證(如果需要,包括 SCA)
  • 付款方式已創建,默認附加到客戶並啟用以供將來使用,狀態為 SUCEEDED。
  • 訂閱已創建並使用上述付款方式

問題是,Stripe 隨后會生成相應的發票和付款意圖,但只要提供的卡需要安全客戶授權 (SCA),后者的狀態為“requires_action”,即使正在使用正確的付款方式(啟用以供將來使用)並且卡驗證已執行。

我認為使用 SetupIntents 的全部意義在於事先驗證付款方式,然后能夠向客戶收費。

我的假設是完全錯誤的還是這實際上是可能的,我可能只是遺漏了一些東西?

提前致謝

編輯:這是后端的訂閱創建代碼:

# 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"]
     });

謝謝你的問題,愛德華多。

有幾種方法可以創建Subscription ,同時獲得客戶 SCA 身份驗證和稍后向卡收費的權限。 假設我們已經創建了一個 Stripe 客戶 object 並且他們的 ID 存儲在stripe_customer_id中。 有了你現在的流程,有幾個步驟:

  1. 創建SetupIntent 請注意,如果您使用 usage: 'off_session' 和Customer創建它,它會在確認后附加生成的 PaymentMethod。

     setup_intent = Stripe::SetupIntent.create({ customer: stripe_customer_id, usage: 'off_session', })
  2. 收集付款詳細信息並使用客戶端上的client_secret確認 SetupIntent,這會將 PaymentMethod 附加到客戶。 請注意,它將附加但不會默認設置為invoice_settings.default_payment_method ,因此您需要稍后單獨調用 API 來更新Customer (參見步驟 3)。

     stripe.confirmCardSetup( '{{setup_intent.client_secret}}', { payment_method: { card: cardElement, }, } ).then(function(result) { // Handle result.error or result.setupIntent });
  3. 更新Customer並將其invoice_settings.default_payment_method設置為與成功確認的PaymentMethod上的 PaymentMethod 的 ID 相等。

     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 } })
  4. 使用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) 進行訂閱的首次付款。 如果訂閱是在客戶離開后創建的並且在技術上應該可以工作,這在技術上是可以的。

如果在您創建Subscription時客戶在您的站點/應用程序上,則還有另一個更正確的流程,不需要對 SCA 使用 MIT 豁免。 試用Subscription的流程如下:

  1. 在客戶端使用 createPaymentMethod 收集卡詳細信息(無SetupIntent

     stripe.createPaymentMethod({ type: 'card', card: cardElement, }).then(function(result) { //pass result to your server. })
  2. 將這些卡詳細信息附加到Customer

     Stripe::PaymentMethod.attach( "pm_xxx", { customer: stripe_customer_id } )
  3. 更新Customerinvoice_settings.default_payment_method

     Stripe::Customer.update( stripe_customer_id, invoice_settings: { default_payment_method: @requested_source } )
  4. 創建Subscription不帶off_session: true

     subscription = Stripe::Subscription.create( customer: data['customerId'], items: [ { price: 'price_H1NlVtpo6ubk0m' } ], expand: ['latest_invoice.payment_intent'] )
  5. 使用Subscriptionlatest_invoicepayment_intentclient_secret收集付款詳情並在客戶端確認。

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

從 SCA 的角度來看,第二個支付流程更正確一些,用於獲得對卡收費的授權。 此處的指南中概述了第二種方法: https://stripe.com/docs/billing/subscriptions/fixed-price

我們還有一個 Stripe 示例,您可以在這里進行實驗: https://github.com/stripe-samples/subscription-use-cases/tree/master/fixed-price-subscriptions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM