• Stars
    star
    199
  • Rank 192,106 (Top 4 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 10 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

基于 React Native 的宝支付包,已更新到最新的支付宝 SDK 版本,支持Android/iOS。

@uiw/react-native-alipay

NPM Version David

基于 React Native 的宝支付插件,支持 iOS/Android。适用于商家在 App 应用中集成支付宝支付功能,商家 APP 调用支付宝提供的 SDK,SDK 再调用支付宝 APP 内的支付模块。如果用户已安装支付宝APP,商家APP会跳转到支付宝中完成支付,支付完后跳回到商家 APP 内,最后展示支付结果。如果用户没有安装支付宝 APP,商家 APP 内会调起支付宝网页支付收银台,用户登录支付宝账户,支付完后展示支付结果。完整实例 Example | 完整的接口文档

⚠️ 4.0+ 在 iOS 打包中报错,这是因为使用阿里云产品的 SDK 出现 UTDID 冲突的问题,在 @EatherToo 的帮助下(#44),UTDID 被剥离了。可以在 Podfile 中加上 pod 'UTDID' 解决打包失败的问题。感谢 @abing

⚠️ v5.x 在 android 需要 Gradle 7+ (#61 #60)

注意事项

  1. Android:支持2.3及以上的系统版本运行。
  2. iOS:iOS 6.0以上(包含iOS 6.0)。
  3. 支持手机系统:iOS(苹果)、Android(安卓)。
  4. 调试请注意 支付宝接入应用必须 已审核通过 状态。
  5. 支付宝开放平台-管理中心,签约 APP支付APP支付宝登录 功能。
  6. 适用于 react-native >= 0.60+ 低版本未测试。
  7. AlipaySDK 15.7.7 已更新到最新的支付宝 SDK 版本。
  8. URL Schemes 要以字母开头不能为纯数字。

安装依赖

yarn add @uiw/react-native-alipay
# react-native version >= 0.60+
$ cd ios && pod install

API

Alipay.alipay 支付

Alipay.alipay: (payInfo: string) => Promise<OrderResult>;

  • ⚠️ 注意支付成功返回结果是一个字符串,返回内容
  • ⚠️ 支付宝需要设置 Scheme 和 iOS添加原生代码,才能支持支付和回弹商家APP的功能
  • ⚠️ 支付宝 管理中心-支付宝开放平台 需要签约 APP支付
import Alipay from '@uiw/react-native-alipay';

// 设置 支付宝 URL Schemes,要表述他是宇宙唯一性,可以使用 `bundle Identifier`
// scheme = `alipay` + `APPID`,`APPID` 为支付宝分配给开发者的应用ID
Alipay.setAlipayScheme(scheme);
// ⚠️ 目前不可用,设置支付宝沙箱环境,仅 Android 支持
// Alipay.setAlipaySandbox(isSandbox);

async function aliPay() {
  // 支付宝端支付
  // payInfo 是后台拼接好的支付参数
  // return_url=
  const payInfo = 'alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2021001172656340&biz_content=%7B%22out_trade_no%22%3A%221111112222222%22%2C%22total_amount%22%3A%220.01%22%2C%22subject%22%3A%221234%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fane.boshu.ltd%2Fowner%2Fpay%2Fapi%2FownerPay%2Fcallback&sign=oUQmGtkv8mrhJ0YwHl9%2FfxMcoLACWuSFKiMTC4Id8nc%2FZVvDQ6MLQq5hhtEN03Qn1%2BAtzTAaofE8nNixdroxOek2l5YtOAcYcXVYlJIyogN%2B22erN2NpDTWJ7tQTKgYFDJLRiG0DZJaxfADhUUF6UR9kdA8omoXKLDlP17ZPUs5Jr4aKv5HJtH5C53ui7PbmyWYg934L4UDC2F%2F9pPQlRwwDeE1SAaV3HW9Dt83kK52o8%2FlChXdotbFdAvH0d4qYGhpEYU5sepj9xiOMyL9aC4pMXW9INYLLGbvtqtlRchZTAfH5yji6nqqQm9KKMmcVrWdBDLyjFVNpejq1UjbJBw%3D%3D&sign_type=RSA2&timestamp=2020-07-09+12%3A16%3A16&version=1.0';
  const resule = await Alipay.alipay(payInfo);
  console.log('alipay:resule-->>>', resule);
}

订单详情 payInfo 编码前的数据

alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=xxxxxxxxxxxxx&biz_content={ "out_trade_no":"123123123123123", "total_amount":"0.01", "subject":"1234", "product_code":"QUICK_MSECURITY_PAY" }&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http://ane.boshu.ltd/owner/pay/api/ownerPay/callback&return_url=uiwjspay://&sign=re/+2SICQggOUjfxl7MtP/qzir2e+LdH4m+02gDcw0fkByO5MqXW/9bmXw+c4RMqo835OAjMZs7s966ZuDx2PB+hO0tJ/bzdHLLqYlBeCcETkrfwRx+AFZNgzsCn75eRCA7GONH35BpfSeGkQUZ+vNXftqd6hWaa7m/MhQYrjQcV98IVJM+UR67Gj68c+LM586cnk0+rbj8zoos6tCvN8c3xx5UaCobzw4Ogf0PWZ7PZROTU9w2gtoxFfOC5d5slN3laaAXVjAxSf9JCNs8q95fDbzpbmstQOuPgGHkASkd/beH0F8eqTVv8gW1ZTo5v/d/E2wSDGV1DciaEnCroTw==&sign_type=RSA2&timestamp=2020-07-09 09:50:41&version=1.0

订单详情 payInfo 编码的数据

alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=xxxxxxxxxxxxx&biz_content=%7B+%22out_trade_no%22%3A%22123123123123123%22%2C+%22total_amount%22%3A%220.01%22%2C+%22subject%22%3A%221234%22%2C+%22product_code%22%3A%22QUICK_MSECURITY_PAY%22+%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fane.boshu.ltd%2Fowner%2Fpay%2Fapi%2FownerPay%2Fcallback&return_url=uiwjspay%3A%2F%2F&sign=re%2F%2B2SICQggOUjfxl7MtP%2Fqzir2e%2BLdH4m%2B02gDcw0fkByO5MqXW%2F9bmXw%2Bc4RMqo835OAjMZs7s966ZuDx2PB%2BhO0tJ%2FbzdHLLqYlBeCcETkrfwRx%2BAFZNgzsCn75eRCA7GONH35BpfSeGkQUZ%2BvNXftqd6hWaa7m%2FMhQYrjQcV98IVJM%2BUR67Gj68c%2BLM586cnk0%2Brbj8zoos6tCvN8c3xx5UaCobzw4Ogf0PWZ7PZROTU9w2gtoxFfOC5d5slN3laaAXVjAxSf9JCNs8q95fDbzpbmstQOuPgGHkASkd%2FbeH0F8eqTVv8gW1ZTo5v%2Fd%2FE2wSDGV1DciaEnCroTw%3D%3D&sign_type=RSA2&timestamp=2020-07-09+09%3A50%3A41&version=1.0
  • ⚠️ 后台 SDK 根据所有数据生成 sign,建议通过 API 拿到这个数据,拼接数据会报错。
  • ⚠️ out_trade_no 订单 id 和 sign 签名 是唯一的,每次不一样,需要后台生成。

支付返回结果,支付宝返回结果参数说明

{ 
  "result": "{\"alipay_trade_app_pay_response\":{\"code\":\"10000\",\"msg\":\"Success\",\"app_id\":\"2021001172656340\",\"auth_app_id\":\"2021001172656340\",\"charset\":\"UTF-8\",\"timestamp\":\"2020-07-08 21:30:14\",\"out_trade_no\":\"123123213123214\",\"total_amount\":\"0.01\",\"trade_no\":\"2020070822001414841426413774\",\"seller_id\":\"2088421915791034\"},\"sign\":\"LY7wCsNLp+QnDqCq6VelY/RvyK7ZGY8wsXoKvS+Or7JjONLDUx5P6lDgqRKkpkng7br3y6GZzfGKaZ88Tf4eMnBMKyqU+huR2Um47xUxP383njvHlxuQZsSTLQZRswy4wmb/fPkFfvyH6Or6+oj0eboePOTu63bNr+h03w0QnP4znuHpfRuoVgWpsYh/6B1DL+4xfWRKJ21zm1SV9Feo9RWqnyTaGZyFVi6IKge0dUCYs9hXju95fOUVUOx5YflOFtSEnZafY9Ls4FCRQE1ANkjaKiKIE0+c4c4sEVEf/9Dwh88N+aSQOoLT+AV4RpjMoA8hF2k+vv2OKNeqr6SYGQ==\",\"sign_type\":\"RSA2\"}",
  "resultStatus": "9000",
  "memo": ""
}

Alipay.authInfo 登录授权

Alipay.authInfo: (authInfoStr: string) => Promise<AuthResult>;

  • ⚠️ 注意授权成功返回结果是一个字符串,返回内容
  • ⚠️ 支付宝需要设置 Scheme 和 iOS添加原生代码,才能支持验证回弹商家APP的功能
  • ⚠️ 支付宝 管理中心-支付宝开放平台 需要签约 APP支付宝登录
import Alipay from '@uiw/react-native-alipay';

// 设置 支付宝 URL Schemes,要表述他是宇宙唯一性,可以使用 `bundle Identifier`
// scheme = `alipay` + `APPID`,`APPID` 为支付宝分配给开发者的应用ID
Alipay.setAlipayScheme(scheme);

async function authInfo() {
  // 支付宝端授权验证
  // authInfoStr 是后台拼接好的验证参数
  const authInfoStr = 'app_name=mc&auth_type=AUTHACCOUNT&apiname=com.alipay.account.auth&biz_type=openservice&product_id=APP_FAST_LOGIN&scope=kuaijie&pid=2088421915791034&target_id=15946456110003465&app_id=2021001172656340&sign_type=RSA2&sign=keluG28qbbLwAcSDI4VmCNOGHJoF3xgpVeqXu1nCBCYo%2FlYYGe00fTfV9L4G73Sk7%2B4IwK%2BZV8IL%2F04cVtk6SR74lKAR3rYOoUdQ09ZrZFuQoUkO0vekajhp75IDQIg6PedCyY0SjFTqrHlH%2FImscBwitxrlSc9YbN7uW0gY34K8t7v8NhDoqzKJeoIz43UxF5U1DpUA1ISBVxwO7du1t6rYltsRhReayPS3hnvmwYSKQZUEgBvJ%2BT2XdyCaz%2FdGV907lYagPp1Oxkoaj%2FvW5NjNsRnid7vH944CoFj9XtBK%2FNTk2tBPTHFxYRQTEG1PkgkBohGpAWOFGGOuapH0ag%3D%3D';
  const resule = await Alipay.authInfo(authInfoStr);
  // resule => success=true&auth_code=9c11732de44f4f1790b63978b6fbOX53&result_code=200&alipay_open_id=20881001757376426161095132517425&user_id=2088003646494707
  console.log('authInfo:resule-->>>', resule);
}

授权返回结果,支付宝返回结果参数说明

{
  "resultStatus": "9000",
  "memo": "处理成功",
  "result": "success=true&result_code=200&app_id=202100117265&auth_code=8b6e5581b85WX84&scope=kuaijie&alipay_open_id=20881029919664670&user_id=20880025&target_id=15946456110003465"
}

Alipay.getVersion 获取 SDK 版本

Alipay.getVersion: () => Promise<string>;

import Alipay from '@uiw/react-native-alipay';

async function getVersion() {
  const version = await Alipay.getVersion();
  console.log('version:', version);
}

支付宝返回应用 iOS 设置

  • ⚠️ Android 端不需要做任何设置。
  • ⚠️ 如果用户从 支付宝App 跳转到 商家APP,是通过系统功能切换,而不是通过 支付宝APP 功能键返回 商家APP,回调函数是不起作用的,可通过 AppState.addEventListener 监听事件请求后台 API,来优化这一用户体验。
  1. 在代码中设置支付宝 URL Schemes,下面实例 uiwjspay 是定义的 scheme,你也可以定义为 alipay + appidappid 为支付宝分配给开发者的应用ID,用来表述 scheme 唯一性。
Alipay.setAlipayScheme('uiwjspay');
  1. 在请求支付的 payInfo 中必须包含 return_url=uiwjspay://return_url 的值为定义的 scheme => uiwjspay://,才会返回支付宝订单支付状态结果
// payInfo 是后台拼接好的支付参数,这个参数必须包含 `return_url=uiwjspay://`
Alipay.alipay(payInfo, (res)=>console.log(res))
  1. 用的 URL Schemes 列为白名单,在 ios/<应用名称>/Info.plist 中添加
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>alipay</string>
</array>
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string></string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>uiwjspay</string>
    </array>
  </dict>
</array>
  1. 修改 ios/<应用名称>/AppDelegate.m 添加下列代码:
#import <React/RCTLinkingManager.h>

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [RCTLinkingManager application:application openURL:url
                      sourceApplication:sourceApplication annotation:annotation];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

命令测试

  • iOS: xcrun simctl openurl booted uiwjspay://
  • Android:adb shell am start -W -a android.intent.action.VIEW -d "uiwjspay://test/router" com.uiwjspay

错误处理

[NetworkInfo] Signal strength query returned error: Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied", descriptor: <CTServiceDescriptor 0x283317100, domain=1, instance=1>

Product -> Scheme -> Edit Scheme -> Run -> Arguments -> Environment Variables 添加 OS_ACTIVITY_MODE disable

其它

当前工程基于 @brodybits/create-react-native-module 初始化。

npx create-react-native-module --package-identifier com.uiwjs --object-class-name RNAlipay --generate-example Alipay --example-react-native-version 0.63.0 --module-name @uiw/react-native-alipay --github-account uiwjs --author-name "Kenny Wong" --author-email "[email protected]"

开发

cd example   # 进入实例 example 工程,根目录不需要安装,会引发错误
yarn install # 安装依赖

cd ios     # 进入 example/ios 目录安装依赖
pod instll # 安装依赖

相关连接

More Repositories

1

province-city-china

🇨🇳最全最新中国【省、市、区县、乡镇街道】json,csv,sql数据
JavaScript
2,284
star
2

react-md-editor

A simple markdown editor with preview, implemented with React.js and TypeScript.
TypeScript
2,003
star
3

react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
TypeScript
1,484
star
4

uiw

⚛️ @uiwjs A high quality UI Toolkit, A Component Library for React 16+.
TypeScript
702
star
5

react-login-page

Some `react` login pages, which can be used quickly after installation.
TypeScript
533
star
6

react-textarea-code-editor

A simple code editor with syntax highlighting.
TypeScript
459
star
7

react-amap

基于 React 封装的高德地图组件,帮助你轻松的接入地图到 React 项目中。
TypeScript
393
star
8

react-markdown-editor

A markdown editor with preview, implemented with React.js and TypeScript.
TypeScript
299
star
9

react-monacoeditor

Monaco Editor component for React.
TypeScript
284
star
10

react-markdown-preview

React component preview markdown text in web browser. The minimal amount of CSS to replicate the GitHub Markdown style. Support dark-mode/night mode.
TypeScript
248
star
11

react-color

🎨 Is a tiny color picker widget component for React apps.
TypeScript
238
star
12

react-baidu-map

基于 React 封装的百度地图组件,支持 React Hook,帮助你轻松的接入地图到 React 项目中。
TypeScript
216
star
13

react-heat-map

A lightweight calendar heatmap react component built on SVG, customizable version of GitHub's contribution graph.
TypeScript
178
star
14

react-json-view

A React component for displaying and editing javascript arrays and JSON objects.
TypeScript
154
star
15

icons

The premium icon font for @uiwjs Component Library. https://uiwjs.github.io/icons
HTML
138
star
16

npm-unpkg

A web application to view npm package files, Based on unpkg.
TypeScript
99
star
17

react-split

A piece of view can be divided into areas where the width or height can be adjusted by dragging.
TypeScript
60
star
18

react-code-preview

Code edit preview for React.
TypeScript
58
star
19

react-native-uiw

A UI component library based on React Native (Android & iOS).
TypeScript
44
star
20

json-viewer

Online JSON Viewer, JSON Beautifier to beautify and tree view of JSON data - It works as JSON Pretty Print to pretty print JSON data.
TypeScript
42
star
21

react-native-amap-geolocation

React Native 高德地图定位模块,支持 Android/iOS。
Java
41
star
22

react-signature

A signature board component for react.
TypeScript
34
star
23

react-watermark

A react component that adds a watermark to an area of a web page.
TypeScript
31
star
24

react-native-wechat

React Native 包使用微信分享、登录、收藏、支付等功能,支持Android/iOS。
Java
30
star
25

file-icons

File icons in the file tree.
HTML
29
star
26

uiw-admin

UIW-Admin Panel Framework, Powered by React and @uiwjs.
TypeScript
22
star
27

babel-plugin-transform-remove-imports

Remove the specified import declaration when you use the babel transform to build the package.
JavaScript
21
star
28

react-run-web

Online Code Editor for Rapid Web Development.
TypeScript
20
star
29

react-native-template

React Native template for react-native-uiw.
JavaScript
17
star
30

react-mac-keyboard

Macbook computer keyboard style for react component.
TypeScript
17
star
31

next-remove-imports

The default behavior is to remove all .less/.css/.scss/.sass/.styl imports from all packages in node_modules.
JavaScript
17
star
32

ui-color

Converting HEX & RGB colors to UIColor/NSColor/Color for both Objective C & Swift.
TypeScript
16
star
33

copy-to-clipboard

Copy text to the clipboard in modern browsers
JavaScript
16
star
34

react-use-online

useOnline is a tiny, zero-dependency hook for responding to online/offline changes.
TypeScript
16
star
35

reset-css

A tiny modern CSS reset.
CSS
14
star
36

keycode-info

A simple web page that responds to the pressed key and returns information about the JavaScript'on-key press' key.
TypeScript
14
star
37

react-iframe

This component allows you to wrap your entire React application or each component in an <iframe>.
TypeScript
12
star
38

react-codesandbox

A React component is provided that allows you to programmatically generate codesandbox projects from code samples on the fly.
TypeScript
11
star
39

react-github-corners

Add a Github corner to your project page, This GitHub corner for react component/web component.
TypeScript
11
star
40

react-only-when

A declarative component for conditional rendering.
TypeScript
9
star
41

bootstrap-icons

Official open source SVG icon library for Bootstrap.
8
star
42

uiwjs.github.io

The official documentation site for @uiwjs. https://uiwjs.github.io
8
star
43

react-clock

An analog clock for your React app.
TypeScript
7
star
44

vscode-uiw

Preview uiw document in vscode.
TypeScript
7
star
45

react-layout

Layout component for React. Handling the overall layout of a page.
TypeScript
7
star
46

react-csv-reader

React component that handles csv file input and its parsing.
TypeScript
7
star
47

react-use-colorscheme

useColorScheme() provides access to the devices color scheme.
TypeScript
7
star
48

react-native-transport-location

Objective-C
6
star
49

react-code-preview-layout

A react component showing the layout of `code` and `code preview example`.
TypeScript
6
star
50

date-formatter

Get a formatted date.
TypeScript
6
star
51

react-prismjs

React Component for prismjs.
TypeScript
6
star
52

react-tabs-draggable

Draggable tabs for React.
TypeScript
5
star
53

react-markdown-preview-example

Preview the markdown files and run the React examples in the documentation.
TypeScript
5
star
54

react-head

React components will manage your changes to the document head
TypeScript
4
star
55

react-shields

Shields.io for react component, Quality metadata badges for open source projects.
TypeScript
4
star
56

react-codepen

A React component is provided that allows you to programmatically generate codepen projects from code samples on the fly.
TypeScript
4
star
57

react-stackblitz

A React component is provided that allows you to programmatically generate stackblitz projects from code samples on the fly.
TypeScript
4
star
58

react-back-to-top

A minimal lightweight react component for adding a nice scroll up (back to top) button with onScroll progress.
TypeScript
4
star
59

react-monorepo-template

Simple React package development project example template.
TypeScript
3
star
60

auto-gitee-mirror

Use GitHub Actions to sync from GitHub to Gitee
3
star
61

react-keywords

Highlight a keyword in a piece of text and return a React element.
TypeScript
3
star
62

css-filter

A filter CSS generator that helps you quickly generate filter CSS declarations for your website. It comes with many options and it demonstrates instantly.
TypeScript
3
star
63

babel-plugin-transform-uiw-import

Modular import plugin for babel.
JavaScript
2
star
64

react-xml-reader

React component that handles xml file input and its parsing.
TypeScript
2
star
65

rematch-loading

Loading indicator plugin for @rematch.
TypeScript
1
star
66

logo

Source files of uiw's logo.
1
star
67

.github

Welcome to the uiwjs organization.
1
star