【问题标题】:Stripe subscription with 3D secure is incomplete带 3D 安全的 Stripe 订阅不完整
【发布时间】:2021-04-11 00:03:42
【问题描述】:

对于不需要 3D 安全性的卡片,它可以完美运行。但是对于那些需要它们的人,它会停留在incomplete 状态。 我的实现非常简单。 frontend 收集卡信息并向条带化的API 发出请求以获取token 并创建payment_method

async submitForm(e) {
  e.preventDefault();

  try {
    const { token } = await this.stripe.createToken(this.card);
    const { paymentMethod } = await this.stripe.createPaymentMethod({
      type: 'card',
      card: this.card,
    });

    this.stripeTokenHandler(token.id, paymentMethod.id); // handles submission to backend
  } catch (e) {
    this.cardErrorsTarget.textContent = e.error.message;
  }
}

后端部分写在rails

customer = Stripe::Customer.create(
  source: params[:stripe_token],
  plan: plan_id,
  # address of the customer
)

# Creating the setup intent
intent = Stripe::PaymentIntent.create(
  amount: some_amount,
  customer: customer.id,
  payment_method_types: ["card"],
)

# After that I need to confirm the intent
confirm_intent = Stripe::PaymentIntent.confirm(intent.id, {
  payment_method: params[:pm_token],
  return_url: app_confirm_subscription_url # After 3D secure, it redirects to this URL
})

重定向后,

def show
  intent = Stripe::PaymentIntent.retrieve(params[:payment_intent]) # stripe adds the payment_intent params
  
  raise intent
end

通过提高intent,我明白了

{
  paid: true,
  status: "succeeded",
  result: "authenticated",
  // more data
}

但是当我转到 Stripe 的仪表板时,它显示订阅不完整。这些是降序排列的events

The payment pi_1I6AVKCYHrwDIsnfiGm7bWUM for $29.00 USD has succeeded

example@example.com was charged $29.00 USD

The payment pi_1I6AVKCYHrwDIsnfiGm7bWUM for $29.00 USD requires you to take action in order to complete the payment

A new payment pi_1I6AVKCYHrwDIsnfiGm7bWUM for $29.00 USD was created

example@example.com attempted to subscribe to price_1I5vshCYHrwDIsnf981UpZEc

example@example.com added a new Visa ending in 3220

A card payment method ending in 3220 was attached to customer cus_IhZezT431x2G7Z

A draft invoice for $29.00 USD to example@example.com was finalized

A draft invoice was created

example@example.com is a new customer

PS:我用的卡是4000 0000 0000 3220,需要3D安全。如果我使用4242 4242 4242 4242,它可以正常工作。

我有什么遗漏的吗?

【问题讨论】:

标签: ruby-on-rails stripe-payments


【解决方案1】:

在这种情况下,创建一个独立的 PaymentIntent 并使用它来驱动订阅创建是错误的集成。如果我们参考使用Elements [1]开始固定价格订阅的指南,主要步骤如下:

  1. 创建客户
  2. 使用来自 Stripe.js 的 createPaymentMethod 使用卡片元素收集卡片信息
  3. 通过以下方式开始订阅:a) 将付款方式附加到客户 b) 将付款方式设置为发票设置,默认付款方式,c) 创建订阅
  4. 处理身份验证 [2]

这里的关键区别在于第 4 步。当您开始订阅时,PaymentIntent 会在后台自动创建并与订阅的第一张发票相关联。这是使用 Stripe.js 和 PaymentIntent 的客户端密码 [2] 的客户端中应该与 confirmCardPayment 结合使用的 PaymentIntent。可以通过在订阅创建后(即第一次付款尝试后)检查订阅的latest_invoice.payment_intent.status 来访问它。

还有很多其他步骤需要处理,例如当支付失败时,但指南详细介绍了您将遇到的可能流程。

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

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

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2019-06-13
    • 2020-07-20
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多