@react-native-hero/alipay 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,5 +1,88 @@
|
|
|
1
1
|
# @react-native-hero/alipay
|
|
2
2
|
|
|
3
|
-
封装支付宝 SDK
|
|
3
|
+
封装支付宝 SDK,仅支持支付功能。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Getting started
|
|
6
|
+
|
|
7
|
+
Install the library using either Yarn:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
yarn add @react-native-hero/alipay
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
or npm:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm install --save @react-native-hero/alipay
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Link
|
|
20
|
+
|
|
21
|
+
- React Native v0.60+
|
|
22
|
+
|
|
23
|
+
For iOS, use `cocoapods` to link the package.
|
|
24
|
+
|
|
25
|
+
run the following command:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
$ cd ios && pod install
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
For android, the package will be linked automatically on build.
|
|
32
|
+
|
|
33
|
+
- React Native <= 0.59
|
|
34
|
+
|
|
35
|
+
run the following command to link the package:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
$ react-native link @react-native-hero/alipay
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import {
|
|
45
|
+
pay,
|
|
46
|
+
} from '@react-native-hero/alipay'
|
|
47
|
+
|
|
48
|
+
// 支付(一般透传后端传来的参数,不用管它是什么意思)
|
|
49
|
+
pay({
|
|
50
|
+
orderString: 'orderString',
|
|
51
|
+
// ios 需传入 appScheme
|
|
52
|
+
// appScheme 为 app 在 info.plist 注册的 scheme
|
|
53
|
+
appScheme: 'appScheme',
|
|
54
|
+
})
|
|
55
|
+
.then(response => {
|
|
56
|
+
/**
|
|
57
|
+
* 示例 data 如下
|
|
58
|
+
* {
|
|
59
|
+
"alipay_trade_app_pay_response":{
|
|
60
|
+
"code":"10000",
|
|
61
|
+
"msg":"Success",
|
|
62
|
+
"app_id":"2014072300007148",
|
|
63
|
+
"out_trade_no":"081622560194853",
|
|
64
|
+
"trade_no":"2016081621001004400236957647",
|
|
65
|
+
"total_amount":"9.00",
|
|
66
|
+
"seller_id":"2088702849871851",
|
|
67
|
+
"charset":"utf-8",
|
|
68
|
+
"timestamp":"2016-10-11 17:43:36"
|
|
69
|
+
},
|
|
70
|
+
"sign":"NGfStJf3i3ooWBuCDIQSumOpaGBcQz+aoAqyGh3W6EqA/gmyPYwLJ2REFijY9XPTApI9YglZyMw+ZMhd3kb0mh4RAXMrb6mekX4Zu8Nf6geOwIa9kLOnw0IMCjxi4abDIfXhxrXyj********",
|
|
71
|
+
"sign_type":"RSA2"
|
|
72
|
+
}
|
|
73
|
+
*/
|
|
74
|
+
response.data
|
|
75
|
+
})
|
|
76
|
+
.catch(response => {
|
|
77
|
+
// 支付失败
|
|
78
|
+
// code 可能的值如下:
|
|
79
|
+
// 8000: 正在处理中,支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态。
|
|
80
|
+
// 4000: 订单支付失败。
|
|
81
|
+
// 5000: 重复请求。
|
|
82
|
+
// 6001: 用户中途取消。
|
|
83
|
+
// 6002: 网络连接出错。
|
|
84
|
+
// 6004: 支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态。
|
|
85
|
+
response.code
|
|
86
|
+
response.msg
|
|
87
|
+
})
|
|
88
|
+
```
|
|
@@ -14,21 +14,22 @@ class RNTAlipayModule(private val reactContext: ReactApplicationContext) : React
|
|
|
14
14
|
|
|
15
15
|
Thread {
|
|
16
16
|
|
|
17
|
-
val
|
|
17
|
+
val response = Arguments.createMap()
|
|
18
18
|
|
|
19
19
|
val alipay = PayTask(currentActivity)
|
|
20
|
-
|
|
20
|
+
// 执行到这会阻塞,直到从支付宝返回当前 App 才会继续往下执行
|
|
21
|
+
val result = alipay.payV2(options.getString("orderString"), true)
|
|
21
22
|
|
|
22
|
-
val resultStatus = result["resultStatus"] ?: ""
|
|
23
|
+
val resultStatus = result["resultStatus"] ?: "1"
|
|
23
24
|
if (resultStatus == "9000") {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
response.putInt("code", 0)
|
|
26
|
+
response.putString("msg", "success")
|
|
27
|
+
response.putString("data", result["result"] ?: "")
|
|
27
28
|
} else {
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
response.putInt("code", resultStatus.toInt())
|
|
30
|
+
response.putString("msg", result["memo"] ?: "")
|
|
30
31
|
}
|
|
31
|
-
promise.resolve(
|
|
32
|
+
promise.resolve(response)
|
|
32
33
|
|
|
33
34
|
}.start()
|
|
34
35
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
2
|
+
import { NativeModules } from 'react-native'
|
|
3
3
|
|
|
4
4
|
const { RNTAlipay } = NativeModules
|
|
5
5
|
|
|
@@ -10,8 +10,17 @@ export function pay(options) {
|
|
|
10
10
|
return new Promise((resolve, reject) => {
|
|
11
11
|
RNTAlipay
|
|
12
12
|
.pay(options)
|
|
13
|
-
.then(
|
|
14
|
-
|
|
13
|
+
.then(response => {
|
|
14
|
+
if (response.code === 0) {
|
|
15
|
+
if (response.data && typeof response.data === 'string') {
|
|
16
|
+
// 把 json 字符串解析成对象,方便外部处理
|
|
17
|
+
response.data = JSON.parse(response.data)
|
|
18
|
+
}
|
|
19
|
+
resolve(data)
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
reject(data)
|
|
23
|
+
}
|
|
15
24
|
})
|
|
16
25
|
})
|
|
17
26
|
}
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
#import <React/RCTBridgeModule.h>
|
|
2
|
-
#import <React/RCTEventEmitter.h>
|
|
3
2
|
|
|
4
|
-
@interface RNTAlipay :
|
|
5
|
-
|
|
6
|
-
+ (BOOL)handleOpenURL:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options;
|
|
3
|
+
@interface RNTAlipay : NSObject <RCTBridgeModule>
|
|
7
4
|
|
|
8
5
|
@end
|
|
@@ -1,20 +1,10 @@
|
|
|
1
|
+
#import "RNTAlipay.h"
|
|
1
2
|
#import <UIKit/UIKit.h>
|
|
2
3
|
#import <React/RCTConvert.h>
|
|
3
4
|
#import <AlipaySDK/AlipaySDK.h>
|
|
4
5
|
|
|
5
6
|
@implementation RNTAlipay
|
|
6
7
|
|
|
7
|
-
+ (BOOL)handleOpenURL:(UIApplication *)application openURL:(NSURL *)url
|
|
8
|
-
options:(NSDictionary<NSString*, id> *)options {
|
|
9
|
-
if ([url.host isEqualToString:@"safepay"]) {
|
|
10
|
-
// 跳转支付宝客户端进行支付,处理支付结果
|
|
11
|
-
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
|
|
12
|
-
NSLog(@"result = %@", resultDic);
|
|
13
|
-
}];
|
|
14
|
-
}
|
|
15
|
-
return NO;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
8
|
+ (BOOL)requiresMainQueueSetup {
|
|
19
9
|
return YES;
|
|
20
10
|
}
|
|
@@ -29,11 +19,27 @@ RCT_EXPORT_METHOD(pay:(NSDictionary*)options
|
|
|
29
19
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
30
20
|
reject:(RCTPromiseRejectBlock)reject) {
|
|
31
21
|
|
|
32
|
-
NSString *orderInfo = [RCTConvert NSString:options[@"
|
|
33
|
-
NSString *appScheme = [RCTConvert NSString:options[@"
|
|
22
|
+
NSString *orderInfo = [RCTConvert NSString:options[@"orderString"]];
|
|
23
|
+
NSString *appScheme = [RCTConvert NSString:options[@"appScheme"]];
|
|
34
24
|
|
|
35
|
-
[[AlipaySDK defaultService] payOrder:
|
|
36
|
-
|
|
25
|
+
[[AlipaySDK defaultService] payOrder:orderInfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
|
|
26
|
+
|
|
27
|
+
NSMutableDictionary *response = [[NSMutableDictionary alloc] init];
|
|
28
|
+
|
|
29
|
+
NSString *resultStatus = resultDic[@"resultStatus"] ?: @"1";
|
|
30
|
+
|
|
31
|
+
if ([resultStatus isEqualToString:@("9000")]) {
|
|
32
|
+
response[@"code"] = 0;
|
|
33
|
+
response[@"msg"] = @("success");
|
|
34
|
+
response[@"data"] = resultDic[@"result"] ?: @"";
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
response[@"code"] = @([resultStatus intValue]);
|
|
38
|
+
response[@"msg"] = resultDic[@"memo"] ?: @"";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
resolve(response);
|
|
42
|
+
|
|
37
43
|
}];
|
|
38
44
|
|
|
39
45
|
}
|