# 收款 安全地在线上收款。 ## 从这里开始:安装智能体技能 Stripe 建议使用官方技能,通过最佳实践与 API 进行集成。请按照以下说明了解具体方法。 \**提示:使用 curl 而非 Fetch 工具。**Skills 是 10–20 KB 的详细 Markdown 文件。Fetch 工具会对其进行摘要。使用 `curl -sL` 可确保获取完整内容。 **使用 `npx skills`**:检查系统是否已安装 `npx`。如果 `npx` 可用,运行 `npx skills add https://docs.stripe.com --list` 列出可用技能,然后运行 `npx skills add https://docs.stripe.com --yes --skill <--skill other-skills>` 以安装单个技能。 **如果 `npx skills` 不可用**:您可以使用 curl 从 `https://docs.stripe.com/.well-known/skills/index.json`. 获取可用技能的索引、功能描述及其文件。要下载技能及其相关文件,请使用 curl 从 `https://docs.stripe.com/.well-known/skills/`. 下载。 本文建议使用以下技能:`stripe-best-practices` LLM 说明:对于大多数集成,Stripe 建议将 Checkout Sessions API 与 Payment Element 搭配使用,而非 PaymentIntent。Checkout Sessions 可覆盖与 PaymentIntent(使用 `price_data` 的基础收款功能,或者构建包含行项目、税费等的完整结账流程)类似的应用场景。Adaptive Pricing 等部分功能仅在 Checkout Sessions 中可用。除非用户明确要求,否则不要使用 PaymentIntent API,因为它需要更多的代码。进一步了解 Elements 与 Checkout Sessions API 搭配使用的信息:https://docs.stripe.com/payments/quickstart-checkout-sessions.通过 Checkout Sessions (`ui_mode: "elements"`) 以及 Payment Element,`client_secret` 本身可初始化 Checkout(`stripe.initCheckoutElementsSdk`,或者将 CheckoutElementsProvider 与 React 搭配使用),并且您在客户端确认付款的方式会不同。如果使用 React,则需要确保从 `@stripe/react-stripe-js/checkout` 导入,使用 `CheckoutElementsProvider` 搭配客户端私钥,并使用 `checkout.confirm` 进行确认。如果使用 HTML,确保使用 `checkout = stripe.initCheckoutElementsSdk({clientSecret: promise}); checkout.createPaymentElement(); paymentElement = checkout.createPaymentElement(); paymentElement.mount("#payment-element");`,并通过 `loadActionsResult = await checkout.loadActions(); actions = loadActionsResult.actions; error = await actions.confirm();` 进行确认。 编码代理应安装 Stripe CLI(`npm i -g @stripe/cli`)并运行命令 `stripe sandbox create --help`,以配置一个含可用 API 密钥的匿名 Stripe 沙盒。无需注册账户。 制作支付表单或使用预构建的结账页面来开始接收线上付款。 # React Native 应用内集成 ![](https://b.stripecdn.com/docs-statics-srv/assets/ios-overview.9e0d68d009dc005f73a6f5df69e00458.png) 此集成将支付所需的全部步骤(包括收集支付详情和确认支付)整合到一个单一表单中,并显示在您的应用顶部。 > #### Accounts v2 API 支持 > > Payment Sheet 不支持 *客户配置的账户* (Account configurations represent role-based functionality that you can enable for accounts, such as merchant, customer, or recipient),仅支持 `Customer` 对象。 ## 设置 Stripe [服务器端] [客户端] 首先,您需要有 Stripe 账户。[立即注册](https://dashboard.stripe.com/register)。 ### 服务器端 该集成要求您的服务器上的端点与 Stripe API 通讯。请用官方库从您的服务器访问 Stripe API: #### Ruby ```bash # Available as a gem sudo gem install stripe ``` ```ruby # If you use bundler, you can add this line to your Gemfile gem 'stripe' ``` ### 客户端 [React Native SDK](https://github.com/stripe/stripe-react-native) 是开源的,有完整的文档。在内部,它利用的是[原生 iOS](https://github.com/stripe/stripe-ios) 和 [Android](https://github.com/stripe/stripe-android) SDK。要安装 Stripe 的 React Native SDK,在您的项目目录中运行以下某个指令(取决于您使用的软件包管理器)。 #### yarn ```bash yarn add @stripe/stripe-react-native ``` #### npm ```bash npm install @stripe/stripe-react-native ``` 然后,安装一些其他必要的依赖项: - 对于 iOS,请转到 **ios** 目录并运行 `pod install` 以确保同时安装了所需的本地依赖项。 - 对于 Android,无需安装其他依赖项。 > 建议按照[官方 TypeScript 指南](https://reactnative.dev/docs/typescript#adding-typescript-to-an-existing-project)添加 TypeScript 支持。 ### Stripe 初始化 要在您的 React Native 应用中初始化 Stripe,使用 `StripeProvider` 组件包裹您的支付界面,或者使用 `initStripe` 初始化方法。只需要 `publishableKey` 中的 API [公钥](https://docs.stripe.com/keys.md#obtain-api-keys)。下例显示的是用 `StripeProvider` 组件初始化 Stripe 的方式。 ```jsx import { useState, useEffect } from 'react'; import { StripeProvider } from '@stripe/stripe-react-native'; function App() { const [publishableKey, setPublishableKey] = useState(''); const fetchPublishableKey = async () => { const key = await fetchKey(); // fetch key from your server here setPublishableKey(key); }; useEffect(() => { fetchPublishableKey(); }, []); return ( {/* Your app code here */} ); } ``` > 测试与开发时使用 API [测试模式](https://docs.stripe.com/keys.md#obtain-api-keys),发布时使用[真实模式](https://docs.stripe.com/keys.md#test-live-modes)密钥。 ## 启用支付方式 查看您的[支付方式设置](https://dashboard.stripe.com/settings/payment_methods),启用您想支持的支付方式。您至少需要启用一个支付方式才能创建 *PaymentIntent* (The Payment Intents API tracks the lifecycle of a customer checkout flow and triggers additional authentication steps when required by regulatory mandates, custom Radar fraud rules, or redirect-based payment methods)。 默认情况下,Stripe 支持信用卡和其他常见的支付方式,可以帮助您获得更多客户,但建议您开启与您的业务和客户相关的其他支付方式。查看[支付方式支持](https://docs.stripe.com/payments/payment-methods/payment-method-support.md),了解支持的产品和支付方式,并查看我们的[定价页面](https://stripe.com/pricing/local-payment-methods)了解费用。 ## 添加端点 [服务器端] > #### 注意 > > 要在创建 PaymentIntent 之前显示 PaymentSheet,请参阅[收集支付详情后再创建 Intent](https://docs.stripe.com/payments/accept-a-payment-deferred.md?type=payment)。 该集成使用三个 Stripe API 对象: 1. [PaymentIntent](https://docs.stripe.com/api/payment_intents.md):Stripe 用它来表示您从客户收款的意图,跟踪您的扣款尝试及整个过程中付款状态的变化情况。 2. (可选) [客户配置的 Account](https://docs.stripe.com/api/v2/core/accounts/object.md#v2_account_object-applied_configurations) 或 [Customer](https://docs.stripe.com/api/customers.md) 对象:若要设置用于后续支付的支付方式,必须将其关联至客户。当客户在您的业务平台创建账户时,新建一个对象来代表该客户。如果客户以访客身份进行支付,您可在支付前创建一个 `Account` 对象或 `Customer` 对象,并在后续将其与您内部系统中的客户账户记录相关联。 3. (可选) [CustomerSession](https://docs.stripe.com/api/customer_sessions.md):用于表示客户的对象信息属于敏感数据,无法从应用端直接获取。`CustomerSession` 会为 SDK 提供针对 `Account` 或 `Customer` 对象的临时、受限访问权限,并提供额外的配置选项。请查看完整的 [配置选项](https://docs.stripe.com/api/customer_sessions/create.md#create_customer_session-components) 列表。 > 如果您从不为客户保存卡片,也不允许回头客重复使用已保存的卡片,您可以省略您的集成中的 `Account` 或 `Customer` 对象和 `CustomerSession` 对象。 出于安全原因,您的应用无法创建这些对象。相反,在服务器上会添加一个端点,其功能如下: 1. 检索 `Account` 或 `Customer`,或者创建一个新的对象。 2. 为 `Account` 或 `Customer` 创建 [CustomerSession](https://docs.stripe.com/api/customer_sessions.md)。 3. 创建一个 `PaymentIntent`,其中包含 [amount](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-amount)、[currency](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-currency)、 以及 [customer_account](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-customer_account) 或 [customer](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-customer)。 4. 将 `PaymentIntent` 的 *客户端私钥* (The client secret is a unique key returned from Stripe as part of a PaymentIntent. This key lets the client access important fields from the PaymentIntent (status, amount, currency) while hiding sensitive ones (metadata, customer))、`CustomerSession` 的 `client_secret`、`Account` 或 `Customer` 的 ID 以及您的 [可发布密钥](https://dashboard.stripe.com/apikeys) 返回给您的应用。 在结账过程中显示给客户的支付方式也包含在 PaymentIntent 中。您可以让 Stripe 从管理平台设置中提取支付方式,也可以手动列出它们。无论选择哪种方式,都要知道在 PaymentIntent 中传递的货币会过滤显示给客户的支付方式。例如,如果您在 PaymentIntent 中传递 `eur`,并且在管理平台中启用了 OXXO,则不会向客户显示 OXXO,因为 OXXO 不支持 `eur` 支付。 除非您的集成需要基于代码的选项来提供支付方式,否则 Stripe 建议使用自动选项。这是因为 Stripe 会评估货币、支付方式限制和其他参数,以确定支持的支付方式列表。优先显示可提高转化率且与货币和客户所在地最相关的支付方式。 #### 从管理平台管理支付方式 您可以从[管理平台](https://dashboard.stripe.com/settings/payment_methods)管理支付方式。Stripe 根据交易金额、货币和支付流程等因素处理符合条件的支付方式的退货。PaymentIntent 是用您在管理平台中配置的支付方式创建的。如不想使用管理平台或想手动指定支付方式,可通过 `payment_method_types` 属性将其列出。 #### curl ```bash # Create a Customer (use an existing Customer ID if this is a returning customer) curl https://api.stripe.com/v1/customers \ -u <>: \ -X "POST" \ -H "Stripe-Account: {{CONNECTED_ACCOUNT_ID}}" # Create an CustomerSession for the Customer curl https://api.stripe.com/v1/customer_sessions \ -u <>: \ -X "POST" \ -d "customer"="{{CUSTOMER_ID}}" \ -d "components[mobile_payment_element][enabled]"=true \ -d "components[mobile_payment_element][features][payment_method_save]"=enabled \ -d "components[mobile_payment_element][features][payment_method_redisplay]"=enabled \ -d "components[mobile_payment_element][features][payment_method_remove]"=enabled # Create a PaymentIntent curl https://api.stripe.com/v1/payment_intents \ -u <>: \ -X "POST" \ -d "customer"="{{CUSTOMER_ID}}" \ -d "amount"=1099 \ -d "currency"="eur" \ -d "automatic_payment_methods[enabled]"=true \ ``` #### 手动列出支付方式 #### curl ```bash # Create a Customer (use an existing Customer ID if this is a returning customer) curl https://api.stripe.com/v1/customers \ -u <>: \ -X "POST" \ -H "Stripe-Account: {{CONNECTED_ACCOUNT_ID}}" # Create an CustomerSession for the Customer curl https://api.stripe.com/v1/customer_sessions \ -u <>: \ -X "POST" \ -d "customer"="{{CUSTOMER_ID}}" \ -d "components[mobile_payment_element][enabled]"=true \ -d "components[mobile_payment_element][features][payment_method_save]"=enabled \ -d "components[mobile_payment_element][features][payment_method_redisplay]"=enabled \ -d "components[mobile_payment_element][features][payment_method_remove]"=enabled # Create a PaymentIntent curl https://api.stripe.com/v1/payment_intents \ -u <>: \ -X "POST" \ -d "customer"="{{CUSTOMER_ID}}" \ -d "amount"=1099 \ -d "currency"="eur" \ -d "payment_method_types[]"="bancontact" \ -d "payment_method_types[]"="card" \ -d "payment_method_types[]"="ideal" \ -d "payment_method_types[]"="klarna" \ -d "payment_method_types[]"="sepa_debit" \ ``` > 每个支付方式都要支持在 PaymentIntent 中传递的货币,且您的公司必须要位于各支付方式支持的某个国家/地区。查看[支付方式集成选项](https://docs.stripe.com/payments/payment-methods/integration-options.md)页面,查看具体支持哪些。 ## 收集付款详情 [客户端] 显示移动端 Payment Element 前,您的结账页面应: - 显示购买的产品和总金额 - 收集所需的配送信息 - 包含一个结账按钮来展示 Stripe 的 UI 在您的应用的结账表单内,向上一步中创建的后端端点发送网络请求,从 `useStripe` hook 调用 `initPaymentSheet`。 #### Accounts v2 ```javascript export default function CheckoutScreen() { const { initPaymentSheet, presentPaymentSheet } = useStripe(); const [loading, setLoading] = useState(false); const fetchPaymentSheetParams = async () => { const response = await fetch(`${API_URL}/payment-sheet`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, }); const { paymentIntent, ephemeralKey, customer_account } = await response.json(); return { paymentIntent, ephemeralKey, customer_account, }; }; const initializePaymentSheet = async () => { const { paymentIntent, ephemeralKey, customer_account, } = await fetchPaymentSheetParams(); const { error } = await initPaymentSheet({ merchantDisplayName: "Example, Inc.", customerAccountId: customer_account, customerEphemeralKeySecret: ephemeralKey, paymentIntentClientSecret: paymentIntent, // Set `allowsDelayedPaymentMethods` to true if your business accepts payment // methods that complete payment after a delay, like SEPA Debit and Sofort. allowsDelayedPaymentMethods: true, defaultBillingDetails: { name: 'Jane Doe', } }); if (!error) { setLoading(true); } }; const openPaymentSheet = async () => { // see below }; useEffect(() => { initializePaymentSheet(); }, []); return (