[English](./README.md) | 简体中文
# 特征 ## 全面 支持支付宝、微信、PayPal、Stripe、Payssion、空中云汇(Airwallex)、PIX(巴西即时支付)等主流支付方式,覆盖绝大部分业务场景。 ## 跨端适配 H5、PC、App跨端 场景均可用,适配多端,满足多样化业务需求。 ## TypeScript 友好 所有组件均由 TypeScript 编写,类型安全,开发体验优秀。 ## 多种承接形式 支持支付按钮、弹窗、二维码、表单、popup、modal、蒙层等多种支付承接方式,灵活组合,满足不同业务场景。 # 安装 [npm package](https://www.npmjs.com/package/pay-sdk-react) ```shell # 使用 npm npm install pay-sdk-react # 使用 yarn yarn add pay-sdk-react # 使用 pnpm pnpm add pay-sdk-react ``` ## 示例 ### 基础使用 ```typescript import React from 'react'; import ReactDOM from 'react-dom'; import { PayButton, PaymentMethod } from 'pay-sdk-react'; import 'pay-sdk-react/dist/css/index.min.css'; function App() { return ( ); } ReactDOM.render(, document.getElementById('app')); ``` ### 按需加载 使用 babel-plugin-import #### 1. 安装 ```shell npm i babel-plugin-import -D ``` #### 2. 添加配置:组件和样式的按需加载 在 babel 配置中加入: ```js plugins: [ [ 'babel-plugin-import', { libraryName: 'pay-sdk-react', libraryDirectory: 'es', camel2DashComponentName: false, style: true, // 样式按需加载 }, ], ]; ``` # 支付场景与用法 ## 支付宝 ### PC 端 #### 方式一:使用 PayButton 引入 PayButton 按钮,点击唤起支付弹窗,展示支付宝二维码。用户扫码完成支付后,支付宝会通知业务后端支付结果。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const AlipayComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { formHtml: res.htmlStr }; }; return (

PayButton-基础用法

Pay Button
); }; export default AlipayComponent; ``` #### 方式二:Alipay.Button 引入 Alipay,Alipay.Button 点击唤起支付弹窗,展示支付宝二维码。用户扫码完成支付后,支付宝会通知业务后端支付结果。 ```typescript import { Alipay } from 'pay-sdk-react'; const AlipayComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { formHtml: res.htmlStr }; }; return (

Alipay.Button - modal

alipay Button
); }; export default AlipayComponent; ``` #### 方式三:自定义按钮 使用自定义按钮,调用支付弹窗组件,展示二维码。用户扫码完成支付后,支付宝会通知业务后端支付结果。 ```typescript import { useRef, useState } from 'react'; import { Alipay, AlipayModalRef } from 'pay-sdk-react'; const formHtml = '
'; const AlipayComponent = () => { const modalRef = useRef(null); const [modalVisible, setModalVisible] = useState(false); const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); modalRef.current?.open({ formHtml: res.htmlStr }); }; return (

自定义 Button - visible 参数唤起 modal

setModalVisible(false)} alipayProps={{ formHtml }} />

自定义 Button - ref open 方法唤起 modal

); }; export default AlipayComponent; ``` #### 方式四:自定义承接 直接引入二维码组件,自定义承接方式&展示时机。用户扫码完成支付后,支付宝会通知业务后端支付结果。 ```typescript import { Alipay } from 'pay-sdk-react'; const formHtml = ''; const AlipayComponent = () => { return (
); }; export default AlipayComponent; ``` ### H5 端 #### 方式一:PayButton 引入支付按钮,点击唤起等待支付弹窗(或蒙层),自动提交 iframe 表单唤起支付宝 App,无 App 时跳转浏览器。用户完成支付后,支付宝会通知业务后端支付结果。等待结果承接:支持弹窗和蒙层两种方式。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const MAlipayComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { formHtml: res.htmlStr }; }; return (

PayButton-基础用法

Pay Button
); }; export default MAlipayComponent; ``` #### 方式二:MAlipay.Button 引入 MAlipay.Button 按钮,点击唤起等待支付弹窗(或蒙层),自动提交 iframe 表单唤起支付宝 App,无 App 时跳转浏览器。用户完成支付后,支付宝会通知业务后端支付结果。 ```typescript import { MAlipay } from "pay-sdk-react"; const MAlipayComponent = () => { const createOrder = async() => { const res = await fetch('/create-order', { method: 'POST', }); return { formHtml: res.htmlStr }; } return (

Alipay Button - mask

Alipay Button
); }; export default MAlipayComponent; ``` #### 方式三:自定义表单 引入 MAlipay 表单组件,自定义触发表单时机。用户完成支付后,支付宝会通知业务后端支付结果。 ```typescript import { MAlipay } from "pay-sdk-react"; const formHtml = ""; const MAlipayComponent = () => { return (

Alipay 表单

); }; export default MAlipayComponent; ``` ## 微信 ### PC 端 #### 方式一:PayButton 引入支付按钮,点击唤起支付弹窗,展示微信二维码。用户扫码完成支付后,微信会通知业务后端支付结果。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const WechatComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { wechatUrl: res.url }; }; return (

PayButton-基础用法

Pay Button
); }; export default WechatComponent; ``` #### 方式二:Wechat.Button 引入 Wechat.Button 按钮,点击唤起支付弹窗,展示微信二维码。用户扫码完成支付后,微信会通知业务后端支付结果。 ```typescript import { Wechat } from 'pay-sdk-react'; const WechatComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { wechatUrl: res.url }; }; return (

Wechat.Button - modal

Wechat Button
); }; export default WechatComponent; ``` #### 方式三:自定义按钮 使用自定义按钮,调用支付弹窗组件,展示二维码。用户扫码完成支付后,微信会通知业务后端支付结果。 ```typescript import { useRef, useState } from 'react'; import { Wechat, WechatModalRef } from 'pay-sdk-react'; const wechatUrl = 'weixin://wxpay...'; const WechatComponent = () => { const modalRef = useRef(null); const [modalVisible, setModalVisible] = useState(false); const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); modalRef.current?.open({ wechatUrl: res.url }); }; return (

自定义 Button - visible 参数唤起 modal

setModalVisible(false)} wechatProps={{ wechatUrl }} />

自定义 Button - ref open 方法唤起 modal

); }; export default WechatComponent; ``` #### 方式四:自定义承接 直接引入二维码组件,自定义承接和唤起方式。用户扫码完成支付后,微信会通知业务后端支付结果。 ```typescript import { Wechat } from 'pay-sdk-react'; const wechatUrl = 'weixin://wxpay/...'; const WechatComponent = () => { return (

直接使用 Wechat - 用户自定义承接方式&展示时机

); }; export default WechatComponent; ``` ### H5 端 #### 方式一:PayButton 引入支付按钮,点击唤起等待支付弹窗(或蒙层),自动打开微信支付链接,微信内浏览器自动唤起支付。用户完成支付后,微信会通知业务后端支付结果。等待结果承接:支持弹窗和蒙层两种方式。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const MWechatComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { wechatUrl: res.url }; }; return (

PayButton - 基础用法

Pay Button

PayButton - mask

Pay Button
); }; export default MWechatComponent; ``` #### 方式二:MWechatButton 引入 MWechatButton 按钮,点击唤起等待支付弹窗(或蒙层),自动打开微信支付链接,微信内浏览器自动唤起支付。用户完成支付后,微信会通知业务后端支付结果。 ```typescript import { MWechatButton } from 'pay-sdk-react'; const wechatUrl = 'https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?...'; const MWechatComponent = () => { return (

wechat Button - modal

Wechat Button
); }; export default MWechatComponent; ``` ## PayPal 引入支付按钮,点击唤起支付,自动打开 PayPal 支付链接,同时展示等待支付结果蒙层。PayPal 支付完成后,PayPal 官方会重定向到业务指定的结果页面,重定向地址由业务后端服务调用 PayPal 官方创建支付订单 API 时传入。 ### 方式一:PayButton 引入支付按钮,点击唤起支付,自动打开 PayPal 支付链接,同时展示等待支付结果蒙层。用户完成支付后,PayPal 会重定向到业务指定的结果页面。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const PaypalComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { wechatUrl: res.url }; }; return (

PayButton-基础用法

Pay Button

PaypalButton - 不自动打开第三方支付页面 - 新tab

Pay Button
); }; export default PaypalComponent; ``` ### 方式二:PaypalButton 引入 PaypalButton 按钮,点击唤起支付,自动打开 PayPal 支付链接,同时展示等待支付结果蒙层。用户完成支付后,PayPal 会重定向到业务指定的结果页面。 ```typescript import { PaypalButton } from 'pay-sdk-react'; const paypalUrl = 'https://www.sandbox.paypal.com/checkoutnow?...'; const PaypalComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { wechatUrl: res.url }; }; return (

PaypalButton - 自动打开第三方支付页面 - 当前tab

Paypal Button

PaypalButton - 不自动打开第三方支付页面 - 新tab

Paypal Button

Paypal Button - 不自动打开第三方支付页面 - 当前tab

Paypal Button
); }; export default PaypalComponent; ``` ## Stripe 引入支付组件,使用 Stripe 支付。Stripe 支付完成后,Stripe 官方会重定向到业务指定的结果页面,重定向地址由业务后端服务调用 Stripe 官方创建支付订单 API 时传入。 ### 方式一:PayButton 引入支付按钮,点击唤起支付 modal 或 popup,展示 Stripe 支付表单。用户完成支付后,Stripe 会重定向到业务指定的结果页面。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const StripeComponent = () => { const createOrder = async () => { return await fetch('/create-order', { method: 'POST', }); }; return (

PayButton-基础用法

Pay Button
); }; export default StripeComponent; ``` ### 方式二:Stripe.Button 引入 Stripe.Button 按钮,点击唤起支付 modal 或 popup,展示 Stripe 支付表单。用户完成支付后,Stripe 会重定向到业务指定的结果页面。 ```typescript import { Stripe } from 'pay-sdk-react'; const StripeComponent = () => { const createOrder = async () => { return await fetch('/create-order', { method: 'POST', }); }; return (

Stripe.Button

Stripe Button
); }; export default StripeComponent; ``` ### 方式三:自定义唤起 popup - 推荐 移动端 使用 使用自定义按钮,调用 Stripe.Popup 组件,展示 Stripe 支付表单。用户完成支付后,Stripe 会重定向到业务指定的结果页面。 ```typescript import { useRef, useState } from 'react'; import { Stripe, StripePopupRef } from 'pay-sdk-react'; const options = { stripeKey: '***', clientSecret: '***', } as const; const StripeComponent = () => { const popupRef = useRef(null); const [popupVisible, setPopupVisible] = useState(false); const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); popupRef.current?.open(res); }; return (

自定义 button-visible 参数唤起 popup

setPopupVisible(false)} stripeProps={options} />

自定义 button-ref open 方法唤起 popup

); }; export default StripeComponent; ``` ### 方式四:自定义唤起 modal - 推荐 PC 端使用 使用自定义按钮,调用 Stripe.Modal 组件,展示 Stripe 支付表单。用户完成支付后,Stripe 会重定向到业务指定的结果页面。 ```typescript import { useRef, useState } from 'react'; import { Stripe, StripeModalRef } from 'pay-sdk-react'; const options = { stripeKey: '***', clientSecret: '***', } as const; const StripeComponent = () => { const modalRef = useRef(null); const [modalVisible, setModalVisible] = useState(false); const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); modalRef.current?.open(res); }; return (

自定义 button-visible 参数唤起 modal

setModalVisible(false)} stripeProps={options} />

自定义 button-ref open 方法唤起 modal

); }; export default StripeComponent; ``` ### 方式五:Stripe 表单 引入 Stripe 表单组件,自定义承接和展示时机。用户完成支付后,Stripe 会重定向到业务指定的结果页面。 ```typescript import { Stripe } from 'pay-sdk-react'; const options = { stripeKey: '***', clientSecret: '***', } as const; const StripeComponent = () => { return (

Stripe表单

); }; export default StripeComponent; ``` ## 空中云汇(Airwallex) 引入支付组件,使用空中云汇支付。空中云汇支付完成后,空中云汇支付组件会回调 onSuccess 方法,业务执行后续逻辑。 ### 方式一:PayButton 引入支付按钮,点击唤起 popup 承接表单(推荐移动端),PC 端推荐唤起 modal。用户完成支付后,空中云汇支付组件会回调 onSuccess 方法,业务执行后续逻辑。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const AirwallexComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { type: 'fullFeaturedCard', initOptions: { env: 'demo', locale: 'en', }, options: { currency: res.currency, intent: { id: res.intentId, client_secret: res.client_secret, }, client_secret: res.client_secret, }, onSuccess: () => { window.location.replace('支付成功结果页'); }, }; }; return (

PayButton-基础用法

Pay Button
); }; export default AirwallexComponent; ``` ### 方式二:自定义唤起 popup 使用自定义按钮,调用 Airwallex.Popup 组件,展示空中云汇支付表单。用户完成支付后,空中云汇支付组件会回调 onSuccess 方法,业务执行后续逻辑。 ```typescript import { useRef, useState } from 'react'; import { Airwallex, AirwallexPopupRef } from 'pay-sdk-react'; const intentId = '***'; const clientSecret = '***'; const options = { type: 'fullFeaturedCard', initOptions: { env: 'demo', locale: 'en', }, options: { currency: 'USD', intent: { id: intentId, client_secret: clientSecret, }, client_secret: clientSecret, }, onSuccess: () => { window.location.replace('支付成功结果页'); }, } as const; const AirwallexComponent = () => { const [popupVisible, setPopupVisible] = useState(false); const popupRef = useRef(null); const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); popupRef.current?.open({ type: 'fullFeaturedCard', initOptions: { env: 'demo', locale: 'en', }, options: { currency: res.currency, intent: { id: res.intentId, client_secret: res.client_secret, }, client_secret: res.client_secret, }, onSuccess: () => { window.location.replace('支付成功结果页'); }, }); }; return (

自定义 button - visible 参数唤起 popup

setPopupVisible(false)} airwallexProps={options} />

自定义 button - ref open 方法唤起 popup

); }; export default AirwallexComponent; ``` ### 方式三:唤起 modal 使用自定义按钮,调用 Airwallex.Modal 组件,展示空中云汇支付表单。用户完成支付后,空中云汇支付组件会回调 onSuccess 方法,业务执行后续逻辑。 ```typescript import { useRef, useState } from 'react'; import { Airwallex, AirwallexModalRef } from 'pay-sdk-react'; const intentId = '***'; const clientSecret = '***'; const options = { type: 'fullFeaturedCard', initOptions: { env: 'demo', locale: 'en', }, options: { currency: 'USD', intent: { id: intentId, client_secret: clientSecret, }, client_secret: clientSecret, }, onSuccess: () => { window.location.replace('支付成功结果页'); }, } as const; const AirwallexComponent = () => { const [modalVisible, setModalVisible] = useState(false); const modalRef = useRef(null); const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); modalRef.current?.open({ type: 'fullFeaturedCard', initOptions: { env: 'demo', locale: 'en', }, options: { currency: res.currency, intent: { id: res.intentId, client_secret: res.client_secret, }, client_secret: res.client_secret, }, onSuccess: () => { window.location.replace('支付成功结果页'); }, }); }; return (

自定义 button-visible 参数唤起 modal

setModalVisible(false)} airwallexProps={options} />

Airwallex.button - ref open 方法唤起 modal

); }; export default AirwallexComponent; ``` ### 方式四:自定义承接 引入 Airwallex 表单组件,自定义承接和展示时机。用户完成支付后,空中云汇支付组件会回调 onSuccess 方法,业务执行后续逻辑。 ```typescript import { Airwallex } from 'pay-sdk-react'; const intentId = '***'; const clientSecret = '***'; const options = { type: 'fullFeaturedCard', initOptions: { env: 'demo', locale: 'en', }, options: { currency: 'USD', intent: { id: intentId, client_secret: clientSecret, }, client_secret: clientSecret, }, onSuccess: () => { window.location.replace('支付成功结果页'); }, } as const; const AirwallexComponent = () => { return (

Airwallex表单

); }; export default AirwallexComponent; ``` ### 方式五:Airwallex.Button 引入支付按钮,点击打开第三方支付链接,同时唤起等待支付结果蒙层。用户完成支付后,空中云汇会重定向到业务指定的结果页面。 ```typescript import { Airwallex } from 'pay-sdk-react'; const AirwallexComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { airwallexUrl: res.url }; }; return (

Airwallex.Button

Airwallex Button
); }; export default AirwallexComponent; ``` ## PIX(巴西即时支付) 引入支付组件,使用 PIX 巴西即时支付。用户扫码完成支付后,业务后端会收到支付通知。 ### PC 端 #### 方式一:PayButton 引入 PayButton 按钮,点击唤起支付弹窗,展示 PIX 二维码。用户扫码完成支付后,业务后端会收到支付通知。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const PixComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { pixCode: res.pixCode }; }; return (

PayButton-基础用法

Pay Button
); }; export default PixComponent; ``` #### 方式二:Pix.Button 引入 Pix.Button,点击唤起支付弹窗,展示 PIX 二维码。用户扫码完成支付后,业务后端会收到支付通知。 ```typescript import { Pix } from 'pay-sdk-react'; const PixComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { pixCode: res.pixCode }; }; return (

Pix.Button - modal

PIX Button
); }; export default PixComponent; ``` #### 方式三:自定义按钮 使用自定义按钮,调用支付弹窗组件,展示 PIX 二维码。用户扫码完成支付后,业务后端会收到支付通知。 ```typescript import { useRef, useState } from 'react'; import { Pix, PixModalRef } from 'pay-sdk-react'; const pixCode = '00020126580014br.gov.bcb.pix...'; const PixComponent = () => { const modalRef = useRef(null); const [modalVisible, setModalVisible] = useState(false); const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); modalRef.current?.open({ pixCode: res.pixCode }); }; return (

自定义 Button - visible 参数唤起 modal

setModalVisible(false)} pixProps={{ pixCode }} />

自定义 Button - ref open 方法唤起 modal

); }; export default PixComponent; ``` #### 方式四:自定义承接 直接引入 PIX 二维码组件,自定义承接方式&展示时机。用户扫码完成支付后,业务后端会收到支付通知。 ```typescript import { Pix } from 'pay-sdk-react'; const pixCode = '00020126580014br.gov.bcb.pix...'; const PixComponent = () => { return (

Pix 表单 - 自定义承接

); }; export default PixComponent; ``` ### H5 端 #### 方式一:PayButton 引入支付按钮,点击唤起等待支付弹窗(或蒙层),自动打开 PIX 支付链接。用户完成支付后,业务后端会收到支付通知。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const MPixComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { pixCode: res.pixCode }; }; return (

PayButton - 基础用法

Pay Button

PayButton - mask

Pay Button
); }; export default MPixComponent; ``` #### 方式二:MPix.Button 引入 MPix.Button 按钮,点击唤起等待支付弹窗(或蒙层),自动打开 PIX 支付链接。用户完成支付后,业务后端会收到支付通知。 ```typescript import { MPixButton } from 'pay-sdk-react'; const pixCode = '00020126580014br.gov.bcb.pix...'; const MPixComponent = () => { return (

PIX Button - modal

PIX Button

PIX Button - mask

PIX Button
); }; export default MPixComponent; ``` ## Payssion 引入支付组件,使用 Payssion 支付。Payssion 支付完成后,Payssion 官方会重定向到业务指定的结果页面,重定向地址由业务后端服务调用 Payssion 官方创建支付订单 API 时传入。 ### 方式一:PayButton 引入支付按钮,点击打开第三方支付链接,同时唤起等待支付结果蒙层。用户完成支付后,Payssion 会重定向到业务指定的结果页面。 ```typescript import { PayButton, PaymentMethod } from 'pay-sdk-react'; const PayssionComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { payssionUrl: res.url }; }; return (

PayButton-基础用法

Pay Button

Pay Button - 不自动打开第三方支付页面

Pay Button
); }; export default PayssionComponent; ``` ### 方式二:PayssionButton 引入 PayssionButton 按钮,点击打开第三方支付链接,同时唤起等待支付结果蒙层。用户完成支付后,Payssion 会重定向到业务指定的结果页面。 ```typescript import { PayssionButton } from 'pay-sdk-react'; const PayssionComponent = () => { const createOrder = async () => { const res = await fetch('/create-order', { method: 'POST', }); return { payssionUrl: res.url }; }; return (

Payssion Button - 自动打开第三方支付页面 - 当前tab

Payssion Button

PaypalButton - 不自动打开第三方支付页面 - 新tab

Payssion Button

PayssionButton - 不自动打开第三方支付页面 - 当前tab

Payssion Button
); }; export default PayssionComponent; ```