Stripe - 仅在用户要求时保存客户付款方式

Stripe - Save customer payment method only if user ask for it

我在客户端使用条纹支付元素。它向服务器发送请求以检索支付意向 ID 和客户端密码。

我希望仅当客户选中复选框时才能保存客户的付款方式保存此付款方式以供将来购买

但我希望该方法只有在服务器端通过我的完整流程并成功后才能注册。

1- For this I set the capture method to "manual" when I create the payment intent and I capture and update the payment intent when all the server-side instructions have succeeded like this :

if (isWantSaveMethod) {
    await stripe.paymentIntents.update(paymentIntent.id, { setup_future_usage: 'off_session' })
}

await stripe.paymentIntents.capture(paymentIntent.id)

=> 问题是如果我在捕获之前更新付款意图,我会收到错误消息:You cannot provide a new payment method to a PaymentIntent when it has a status of requires_capture. You should either capture the remaining funds, or cancel this PaymentIntent and try again with a new PaymentIntent.

2- Then I tried to use an intent setup, which apparently allows us to link a payment method to a customer

if (isWantSaveMethod) {
    await stripe.setupIntents.create({
        payment_method: paymentIntent.payment_method,
        confirm: true,
        customer: paymentIntent.customer,
    })
}

await stripe.paymentIntents.capture(paymentIntent.id)

=> 但是:The provided PaymentMethod was previously used with a PaymentIntent without Customer attachment, shared with a connected account without Customer attachment, or was detached from a Customer. It may not be used again. To use a PaymentMethod multiple times, you must attach it to a Customer first.

3- Then I tried to attach the payment intent to a customer like this :

if (isWantSaveMethod) {
    await stripe.paymentMethods.attach(paymentIntent.payment_method, {
        customer: paymentIntent.customer,
    })

    await stripe.setupIntents.create({
        payment_method: paymentIntent.payment_method,
        confirm: true,
        customer: paymentIntent.customer,
    })
}

await stripe.paymentIntents.capture(paymentIntent.id)

=> 但我记得文档说:要将新的 PaymentMethod 附加到客户以供将来付款,我们建议您使用 SetupIntent 或带有 [=44 的 PaymentIntent =]。这些方法将执行任何必要的步骤以确保 PaymentMethod 可以在未来的付款中使用。

所以我终于来到这里,想知道这样做的好方法是什么?热门网站如何做这件事?

我只是想在保存付款方式之前确保我在服务器端的所有指令都已成功执行,我希望以后能够使用此方法

如何在尊重 stripe 给出的与方法附件相关的建议的同时做到这一点?

使用支付元素时,预期的用户流程是您将在创建支付意向时传入 setup_future_usage。 Stripe 想知道您是否打算提前使用 setup_future_usage,以便他们可以选择付款元素中的付款方式类型(因为并非所有付款方式都可以与 setup_future_usage 一起使用)。您应该更改您的结帐流程询问您的客户是否想在显示付款元素之前保存他们的付款方式,或者您可以使用单独的元素,如 Card 元素(每个元素都应该支持传入 setup_future_usage 和 client-side 确认电话 - 示例 here).

或者(我真的不推荐这样做),您可以考虑执行以下操作:

  1. 确保您没有启用任何不支持 setup_future_usage 的付款方式类型(您可以找到列表 here)。
  2. 更改您的 client-side 代码,首先向您的服务器发送请求以更新 Payment Intent 以设置 setup_future_usage,并等待响应成功后再继续调用 confirmPayment client-side.