支付过程中保存付款详情
用 Payment Intents API 保存购物时输入的付款详情。有几个用例:
- 对客户的网上订单扣款并存储支付信息,用于以后的购物。
- 发起一系列经常性付款中的第一笔付款。
- 收取押金并存储详情,便于以后收取完整金额。
合规
You’re responsible for your compliance with all applicable laws, regulations, and network rules when saving a customer’s payment details. These requirements generally apply if you want to save your customer’s payment method for future use, such as displaying a customer’s payment method to them in the checkout flow for a future purchase or charging them when they’re not actively using your website or app. Add terms to your website or app that state how you plan to save payment method details and allow customers to opt in.
When you save a payment method, you can only use it for the specific usage you have included in your terms. To charge a payment method when a customer is offline and save it as an option for future purchases, make sure that you explicitly collect consent from the customer for this specific use. For example, include a “Save my payment method for future use” checkbox to collect consent.
To charge them when they’re offline, make sure your terms include the following:
- 客户同意您代其对指定的交易发起一次或一系列付款。
- 预期的付款时间和频率(例如,收款是计划的分期付款、订阅付款还是计划外充值)。
- 如何确定付款金额。
- 如果支付方式是用于支付订阅服务,那即同意您的取消政策。
请务必让客户书面同意这些条款并做好记录。
创建一个 Customer服务器端
要为将来的付款设置银行卡,就必须将它绑定到 Customer。当客户在您的公司创建账户时,创建 Customer 对象。Customer 对象允许重复使用支付方式并跟踪多笔付款。
启用支付方式
查看您的支付方式设置,启用您想支持的支付方式。您至少需要启用一个支付方式才能创建 PaymentIntent。
默认情况下,Stripe 启用了银行卡和其他常见的支付方式,可以帮助您接触更多客户,但建议您开启与您的业务和客户相关的其他支付方式。查看支付方式集成选项,了解支持的产品和支付方式,并查看我们的定价页面,了解收费情况。
创建 PaymentIntent服务器端
备注
如果您想在创建 PaymentIntent 之前先呈现 Payment Element,请参阅收集支付详情后再创建 Intent。
PaymentIntent 对象用来表示您从客户收款的意图,跟踪扣款尝试及整个过程中的状态变化情况。
创建 PaymentIntent
在您的服务器上创建 PaymentIntent,设置好金额 和货币。在最新版的 API 中,可以选择性指定 automatic_payment_methods
参数,因为 Stripe 会默认开启其功能。您可以从管理平台管理支付方式。Stripe 根据交易金额、货币和支付流程等因素处理符合条件的支付方式的退货。
在您的服务器上创建 PaymentIntent指定金额、货币和客户。在最新版的 API 中,可以选择性指定 automatic_payment_methods
参数,因为 Stripe 会默认开启其功能。启用 setup_future_usage。您在管理平台中配置的支付方式会自动添加到 Payment Intent。
如不想使用管理平台或想手动指定支付方式,可通过 payment_method_types
属性将其列出。
备注
始终在服务器端决定扣款金额,这是一个可信的环境,客户端不行。这样可防止客户自己选择价格。
检索客户端私钥
PaymentIntent 中包含的是一个客户端私钥,用于在客户端安全地完成支付流程。有不同的方法可以将客户端私钥传递到客户端。
收集付款详情客户端
用 Payment Element 在客户端收集支付信息。Payment Element 是一个预构建的用户界面组件,它简化了为各种支付方式收集支付信息的过程。
Payment Element 中包含一个 iframe,它通过一个 HTTPS 连接安全地将支付信息发送到 Stripe。避免将 Payment Element 放在另一个 iframe 中,因为有些支付方式需要重定向到另一个页面进行付款确认。
If you do choose to use an iframe and want to accept Apple Pay or Google Pay, the iframe must have the allow attribute set to equal "payment *"
.
结账页面上的地址也必须以 https://
开头,不能是 http://
,否则您的集成不能工作。您可以在不使用 HTTPS 的情况下测试您的集成,准备好进行真实收款时将它启用。
Payment Element 呈现一个动态表单,您的客户可在这里选择一个支付方式。对于每个支付方式,表单会自动请求客户填写必要的支付详情。
自定义外观
自定义 Payment Element,使其匹配您的网站设计风格,方法是在创建 Elements
提供程序时向 options
传递外观对象。
收集地址
默认情况下,Payment Element 仅收集必要的账单地址信息。收集客户完整的账单地址(例如,计算数字商品和服务的税额)或收货地址时,使用 Address Element。
申请 Apple Pay 商家令牌
如果您已经对您的集成应用进行了配置来接受 Apple Pay 付款,那么我们建议将 Apple Pay 接口配置为返回商家令牌,以支持商家发起的交易 (MIT)。在 Payment Element 中申请相关的商家令牌类型。
向 Stripe 提交付款客户端
使用 stripe.confirmPayment,用来自 Payment Element 的详情完成付款。为该函数提供一个 return_url,告诉 Stripe 在用户完成付款后将他们重定向到哪里。您的用户可能会先被重定向到一个中间站点,如银行授权页面,然后才被重定向到 return_url
。付款成功时,银行卡付款将立即重定向到 return_url
。
如果您不想在完成付款后对银行卡付款重定向,可以将 redirect 设置到 if_required
。这样就会只对使用基于重定向支付方式的客户进行重定向。
备注
stripe.confirmPayment
可能需要几秒才能完成。该时间内,禁止表单重复提交,并显示一个类似于微调器的等待指示器。如果收到错误,则向客户显示,重新启用表单,然后隐藏掉等待指示器。如果客户必须执行额外的步骤来完成付款,如身份验证,Stripe.js 将引导其完成该流程。
如果付款成功,那么卡会被保存到 Customer 对象。这将反映在 PaymentMethod 的 Customer 字段。这时,将这个 Customer 对象的 ID 关联到您自己对这个客户使用的内部名称(如果有)。到此,可以用存储的 PaymentMethod 对象对您的客户进行未来的收款了,不需要提示他们再次输入银行卡详情。
确保 return_url
对应于您网站上显示付款状态的一个页面。Stripe 将客户重定向到 return_url
时,我们会提供以下 URL 查询参数。
参数 | 描述 |
---|---|
payment_intent | PaymentIntent 的唯一标识符。 |
payment_intent_client_secret | PaymentIntent 对象的客户端私钥。 |
注意
如果您有可以用来跟踪客户浏览器会话的工具,那么您可能需要将 stripe.com
域名添加到推荐人列表。重定向会导致一些工具创建新的会话,从而阻止您跟踪完整的会话。
用以下某个查询参数检索 PaymentIntent。检查 PaymentIntent 的状态,以决定向客户显示的内容。您还可以在提供 return_url
时附加自己的查询参数,它们会在重定向过程中持续存在。
以后对保存的支付方式扣款服务器端
警告
bancontact
、ideal
和 sofort
是默认的一次性支付方式。设置为将来使用时,它们会生成一个 sepa_debit
可重复使用的支付方式类型,因此您可以用 sepa_debit
来查询保存的支付方式。
Compliance
You’re responsible for your compliance with all applicable laws, regulations, and network rules when saving a customer’s payment details. When rendering past payment methods to your end customer for future purchases, make sure you’re listing payment methods where you’ve collected consent from the customer to save the payment method details for this specific future use. To differentiate between payment methods attached to customers that can and can’t be presented to your end customer as a saved payment method for future purchases, use the allow_redisplay parameter.
When you’re ready to charge your customer off-session, use the Customer and PaymentMethod IDs to create a PaymentIntent. To find a payment method to charge, list the payment methods associated with your customer. This example lists cards but you can list any supported type.
当您有 Customer 和 PaymentMethod ID 时,创建一个包含付款金额和货币的 PaymentIntent。设置几个其他参数来进行会话外付款:
- 将 off_session 设置为
true
,以指示客户其在尝试付款时并不在您的结账流程中,并且无法完成合作伙伴(如发卡行、银行或其他支付机构)提出的身份验证请求。如果在您的结账流程中,合作伙伴要求进行验证,Stripe 将使用之前的会话内交易中的客户信息请求豁免。如果不满足豁免条件,PaymentIntent 可能会抛出一个错误。 - 将 PaymentIntent 的 confirm 属性的值设置为
true
,这样就会在创建 PaymentIntent 时立即进行确认。 - 将 payment_method 设置为 PaymentMethod 的 Id,并将 customer 设置为 Customer 的 ID。
测试集成应用
用测试付款详情和测试重定向页面验证您的集成。点击下方选项卡,查看每个支付方式的详情。
测试对保存的 SEPA 借记 PaymentMethod 的收款
用 iDEAL、Bancontact 或 Sofort 确认 PaymentIntent ,生成一个可重复使用的 SEPA 直接借记 PaymentMethod。SEPA 直接借记是一种延迟通知型支付方式,它几天后会变为 succeeded
或 requires_payment_method
状态,在此之前会变为一个中间 processing
状态。