npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@db-rn/react-native-alipay

v1.0.0

Published

React Native 支付宝 SDK 原生模块,支持支付、授权登录,兼容新架构 (TurboModule) 和旧架构 (Bridge)

Readme

@db-rn/react-native-alipay

React Native 支付宝 SDK 原生模块,支持支付授权登录功能。

兼容 React Native 新架构(TurboModule / JSI)和旧架构(Bridge),适配 iOS 和 Android 双平台。

特性

  • 支付宝 App 支付
  • 支付宝授权登录(获取 auth_code)
  • 沙箱环境切换(Android)
  • 自动适配新架构 (TurboModule) / 旧架构 (Bridge)
  • iOS 模拟器安全降级(SDK 不可用时返回模拟数据)
  • TypeScript 类型完整支持
  • 零配置即用 —— autolink 自动注册,__has_include 自动检测 SDK

环境要求

| 依赖 | 最低版本 | | -------------- | --------- | | React Native | >= 0.72.0 | | React | >= 18.0.0 | | iOS | >= 13.4 | | Android minSdk | 24 | | Node.js | >= 18 |

安装

npm install @db-rn/react-native-alipay
# 或
yarn add @db-rn/react-native-alipay

iOS 配置

安装 Pods

插件通过 podspec 自动声明了 AlipaySDK-iOS 依赖,pod install 后即可在真机上调用真实 SDK。

cd ios

# 真机构建(默认,自动引入 AlipaySDK)
bundle exec pod install

# 模拟器开发(跳过 AlipaySDK,支付/授权返回模拟数据)
SIMULATOR=1 bundle exec pod install

两种模式对比:

| | 真机模式(默认) | 模拟器模式 | | ---------------- | ------------------------------ | ------------------------- | | 安装命令 | pod install | SIMULATOR=1 pod install | | AlipaySDK | ✅ 自动引入 | ❌ 跳过 | | SDK 检测方式 | __has_include 编译时自动检测 | 同左,检测不到则走 stub | | 支付/授权 | 调用真实支付宝 SDK | 返回模拟数据 | | 适用场景 | 真机调试、发布 | 模拟器 UI 开发、CI |

注意:真机和模拟器之间切换时,需要重新执行对应的 pod install 命令并 Clean Build(Cmd + Shift + K)。

配置 URL Scheme

支付宝支付/授权完成后需要通过 URL Scheme 跳回 App,需在 Xcode 中配置:

  1. 打开 Xcode → 选择项目 Target → InfoURL Types
  2. 点击 + 添加一条,URL Schemes 填写 alipay{你的支付宝AppId}(例如 alipayxxx

配置 Info.plist(可选)

如果需要跳转到支付宝 App,需要在 Info.plist 添加白名单:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>alipay</string>
    <string>alipays</string>
</array>

Android 配置

Android 通过 autolink 自动注册原生模块,无需额外配置

支付宝 SDK 依赖(com.alipay.sdk:alipaysdk-android:15.8.40)已在库的 build.gradle 中声明,会自动传递给宿主 App。

使用

基础用法

import { Alipay, AlipayResultStatus } from "@db-rn/react-native-alipay"

// 1. 检查模块是否可用
if (!Alipay.isAvailable()) {
  console.warn("支付宝模块不可用")
}

// 2. iOS 必须设置 URL Scheme(在支付/授权前调用一次)
import { Platform } from "react-native"
if (Platform.OS === "ios") {
  Alipay.setScheme("alipayxxxxx")
}

发起支付

import { Alipay, AlipayResultStatus } from "@db-rn/react-native-alipay"

// orderInfo 由服务端签名生成,前端直接透传
const orderInfo = "后端返回的签名订单字符串"

try {
  const result = await Alipay.pay(orderInfo)

  switch (result.resultStatus) {
    case AlipayResultStatus.SUCCESS:
      console.log("支付成功")
      break
    case AlipayResultStatus.PROCESSING:
      console.log("支付处理中,请稍后查询")
      break
    case AlipayResultStatus.CANCELLED:
      console.log("用户取消支付")
      break
    default:
      console.log("支付失败:", result.memo)
  }
} catch (e) {
  console.error("支付异常:", e)
}

授权登录

import { Alipay } from "@db-rn/react-native-alipay"

// authInfo 由服务端签名生成
const authInfo = "后端返回的签名授权字符串"

try {
  const result = await Alipay.auth(authInfo)

  if (result.resultStatus === "9000") {
    // 解析 auth_code
    // result.result 格式: "auth_code=xxx&result_code=200&alipay_open_id=xxx"
    const params = new URLSearchParams(result.result)
    const authCode = params.get("auth_code")
    // 将 authCode 发送给后端换取登录 token
  }
} catch (e) {
  console.error("授权异常:", e)
}

沙箱模式(仅 Android)

import { Alipay } from "@db-rn/react-native-alipay"

// 开发环境使用沙箱
if (__DEV__) {
  Alipay.setSandbox(true)
}

iOS 端沙箱切换由后端签名时的网关地址控制,客户端 setSandbox() 为空实现。

获取 SDK 版本

const version = await Alipay.getVersion()
console.log("AlipaySDK:", version)
// 真机: '15.8.40'
// 模拟器/SDK不可用: 'sdk_not_available'

API

Alipay.isAvailable(): boolean

检查支付宝原生模块是否可用。

Alipay.setScheme(scheme: string): void

设置 URL Scheme(iOS 必须在支付/授权前调用,Android 无效果)。

Alipay.setSandbox(isSandbox: boolean): void

设置沙箱/线上环境(仅 Android 有效)。

Alipay.pay(orderInfo: string): Promise<AlipayResultType>

发起支付宝支付。orderInfo 为后端签名后的订单信息字符串。

Alipay.auth(authInfo: string): Promise<AlipayResultType>

发起支付宝授权登录。authInfo 为后端签名后的授权信息字符串。

Alipay.getVersion(): Promise<string>

获取支付宝 SDK 版本号。

类型定义

AlipayResultType

interface AlipayResultType {
  /** 状态码 */
  resultStatus: string
  /** 支付宝返回的原始结果字符串 */
  result: string
  /** 备注信息 */
  memo: string
}

AlipayResultStatus

| 枚举值 | 状态码 | 说明 | | ---------------- | ------ | -------------- | | SUCCESS | 9000 | 支付/授权成功 | | PROCESSING | 8000 | 正在处理中 | | FAILED | 4000 | 支付/授权失败 | | REPEAT | 5000 | 重复请求 | | CANCELLED | 6001 | 用户取消 | | NETWORK_ERROR | 6002 | 网络连接出错 | | ACCOUNT_FROZEN | 6004 | 支付账户被冻结 |

项目结构

react-native-alipay/
├── src/                          # 库的 TypeScript 代码
│   ├── index.ts                  # 入口,导出 Alipay API
│   ├── NativeAlipay.ts           # TurboModule Spec
│   └── types.ts                  # 类型定义
├── ios/                          # iOS 原生代码
│   ├── RNAlipay.h / .m           # 旧架构 Bridge 模块
│   └── AlipayTurboModule.h / .mm # 新架构 TurboModule
├── android/                      # Android 原生代码 (library module)
│   ├── build.gradle
│   └── src/main/java/.../alipay/
│       ├── RNAlipayModule.java   # 旧架构 Bridge 模块
│       ├── RNAlipayPackage.java
│       ├── AlipayTurboModule.kt  # 新架构 TurboModule
│       └── AlipayTurboPackage.kt
├── example/                      # 示例 App(独立 RN 项目)
│   ├── App.tsx
│   ├── package.json
│   ├── ios/
│   └── android/
├── package.json
├── react-native-alipay.podspec
├── react-native.config.js
└── README.md

架构说明

本模块同时实现了两种 RN 架构的原生绑定:

| 架构 | iOS | Android | | -------------------- | ---------------------------- | ---------------------------- | | 旧架构 (Bridge) | RNAlipay (ObjC) | RNAlipayModule (Java) | | 新架构 (TurboModule) | AlipayTurboModule (ObjC++) | AlipayTurboModule (Kotlin) |

JS 层自动检测并选择可用的模块:

  • Android:优先 TurboModule,回退 Bridge
  • iOS:优先 Bridge(稳定),回退 TurboModule

iOS SDK 检测机制

原生代码通过编译器内置指令 __has_include(<AlipaySDK/AlipaySDK.h>) 在编译时自动检测 SDK 是否可用:

  • SDK 存在(真机 pod install)→ 编译真实 SDK 调用路径
  • SDK 不存在SIMULATOR=1 pod install)→ 编译 stub 路径,返回模拟数据

开发者无需手动注入宏或做任何额外配置。

本地开发

运行示例 App

# 1. 安装库依赖
npm install

# 2. 安装示例 App 依赖
cd example && npm install

# 3. iOS(真机)
cd ios && bundle exec pod install && cd ..
npx react-native run-ios

# 3. iOS(模拟器)
cd ios && SIMULATOR=1 bundle exec pod install && cd ..
npx react-native run-ios --simulator="iPhone 16"

# 4. Android
npx react-native run-android

或者在根目录使用快捷命令:

npm run example:install    # 安装示例 App 依赖
npm run example:start      # 启动 Metro
npm run example:ios        # 运行 iOS
npm run example:android    # 运行 Android

License

MIT