[英]Stripe v3 - SetupIntents and Subscriptions
我正在嘗試使用 SetupIntents 來驗證和保存付款方式,然后使用它創建訂閱來向客戶(立即然后每月)收取所需的金額。
這似乎工作正常:
問題是,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
中。 有了你現在的流程,有幾個步驟:
創建SetupIntent
。 請注意,如果您使用 usage: 'off_session' 和Customer
創建它,它會在確認后附加生成的 PaymentMethod。
setup_intent = Stripe::SetupIntent.create({ customer: stripe_customer_id, usage: 'off_session', })
收集付款詳細信息並使用客戶端上的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 });
更新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 } })
使用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
的流程如下:
在客戶端使用 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 } )
更新Customer
的invoice_settings.default_payment_method
Stripe::Customer.update( stripe_customer_id, invoice_settings: { default_payment_method: @requested_source } )
創建Subscription
(不帶off_session: true
)
subscription = Stripe::Subscription.create( customer: data['customerId'], items: [ { price: 'price_H1NlVtpo6ubk0m' } ], expand: ['latest_invoice.payment_intent'] )
使用Subscription
的latest_invoice
的payment_intent
的client_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.