简体   繁体   English

如何使用 Stripe API 将付款方式附加到付款意图

[英]How do you attach a Payment Method to a Payment Intent using the Stripe API

I've read a lot of documentation on "why" my Stripe payment intents are left in a status of " The Customer has not entered a payment method " and that " We recommend that you explicitly provide the payment_method going forward " however I can't tell how do that (provide the payment_method).我已经阅读了很多关于“为什么”我的 Stripe 付款意图处于“客户尚未输入付款方式”的状态以及“我们建议您明确提供未来的 payment_method ”的文档,但是我可以' t 告诉怎么做(提供 payment_method)。

When I initialise the Payment Intent object I have "AutomaticPaymentMethods" enabled.当我初始化支付意图对象时,我启用了“AutomaticPaymentMethods”。 I only have 1 payment method enabled in my Stripe Dashboard (cards).我的 Stripe Dashboard(卡片)中仅启用了 1 种付款方式。

(Also, I'm using Blazor with JS interop for this - if that is relevant...) (另外,我为此使用 Blazor 和 JS 互操作 - 如果相关的话......)

        string customerId = await getCustomer();    // fetch existing customer from Stripe API based on current user or create a new one

        Stripe.PaymentMethodService pms = new PaymentMethodService();
        
        Stripe.PaymentIntentCreateOptions options = new PaymentIntentCreateOptions
        {
            Amount = p.UnitAmount,
            Currency = p.Currency,
            Customer = customerId,
            ReceiptEmail = "test@malinator.com",
            Description = "Test Purchase",
            StatementDescriptor = "Test Purchase",
            AutomaticPaymentMethods = new PaymentIntentAutomaticPaymentMethodsOptions
            {
                Enabled = true,
            },
        };

        Stripe.PaymentIntentService pis = new Stripe.PaymentIntentService();
        Stripe.PaymentIntent pi = pis.Create(options);

        return pi;

The Payment Method is null on the resulting Payment Intent object.生成的 Payment Intent 对象上的 Payment Method 为 null。

It is still null after the payment details have been completed in the "payment-element" on my HTML form.在我的 HTML 表单的“付款元素”中完成付款详细信息后,它仍然为空。

Is the payment_method something I can set up as the Payment Intent object is created (as I only wish to use "cards" at the moment anyway), or is it something I need to set in the confirm_payment JS call? payment_method 是我可以在创建 Payment Intent 对象时设置的东西(因为我现在只想使用“卡”),还是我需要在 confirm_payment JS 调用中设置的东西? (Either way, how to I obtain that Payment Method?) (无论如何,我如何获得该付款方式?)

Your code creates a PaymentIntent server-side for say $10 USD.您的代码以 10 美元的价格创建了一个PaymentIntent服务器端。 This represents the "state machine" of an overall payment for the customer.这代表了客户整体支付的“状态机”。 They might never pay, or they might try to pay once and get a decline or a success, or they might get declined, try another card and see a success.他们可能永远不会付款,或者他们可能尝试支付一次并获得拒绝或成功,或者他们可能被拒绝,尝试另一张卡并看到成功。 Each "payment attempt" is represented in the API as a Charge object and the corresponding payment method details associated with that are represented as a PaymentMethod .每个“付款尝试”在 API 中都表示为Charge对象,与之关联的相应付款方式详细信息表示为PaymentMethod

Now all of that "payment attempt" happens client-side where you collect payment method details and you use those to "confirm the PaymentIntent".现在所有“付款尝试”都发生在客户端,您可以在其中收集付款方式详细信息,然后使用这些信息“确认 PaymentIntent”。

You mentioned you are using PaymentElement which is Stripe's newer UI library component to collect payment method details and accept a payment client-side.您提到您正在使用 PaymentElement,它是 Stripe 较新的 UI 库组件来收集付款方式详细信息并接受付款客户端。 Since you only accept card payments, the PaymentElement would get initialized with your PaymentIntent's client_secret and render the card form to collect those card details.由于您只接受卡付款,因此 PaymentElement 将使用您的 PaymentIntent 的client_secret进行初始化并呈现卡表单以收集这些卡详细信息。

What you need to do here is have a button to "pay" and when it's clicked, your Javascript code should call the confirmPayment() method from Stripe.js.你需要做的是有一个“支付”按钮,当它被点击时,你的 Javascript 代码应该从 Stripe.js 调用confirmPayment()方法。 Doing that will use the card details entered by the customer and attempt to confirm the PaymentIntent that is for a specific amount and currency.这样做将使用客户输入的卡详细信息,并尝试确认特定金额和货币的 PaymentIntent。 This payment might succeed, in which case the customer is redirected to your return_url , or it might be declined in which case the promise completes with error details you can handle to surface an error.此付款可能会成功,在这种情况下,客户将被重定向到您的return_url ,或者可能会被拒绝,在这种情况下,承诺完成并带有您可以处理以显示错误的错误详细信息。

What you need to do here is look into your client-side code, ensure that you call confirmPayment() as expected and debug the response you get back to help narrow it down.您需要在这里做的是查看您的客户端代码,确保您按预期调用confirmPayment()并调试您返回的响应以帮助缩小范围。

After the PaymentIntent is confirmed successfully and has status: 'succeeded' then it will have payment_method: 'pm_123' that is the id of the PaymentMethod object associated with the successful confirmation.在 PaymentIntent 成功确认并具有status: 'succeeded'后,它将具有payment_method: 'pm_123' ,即与成功确认关联的PaymentMethod对象的 id。 All the information about that card would be on the successful Charge object associated with it inside payment_method_details .有关该卡的所有信息都将在payment_method_details中与其关联的成功Charge对象上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM