-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (73 loc) · 2.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { NativeAppEventEmitter, NativeModules } from 'react-native';
import promisify from 'es6-promisify';
const Alipay = NativeModules.Alipay;
function translateError(err, result) {
console.log(err, result);
if (!err) {
return this.resolve(result);
}
if (typeof err === 'object') {
if (err instanceof Error) {
return this.reject(ret);
}
return this.reject(Object.assign(new Error(err.message), { errCode: err.errCode }));
} else if (typeof err === 'string') {
return this.reject(new Error(err));
}
this.reject(Object.assign(new Error(), { origin: err }));
}
// Save callback and wait for future event.
let savedCallback = undefined;
function waitForPayResponse(type) {
return new Promise((resolve, reject) => {
if (savedCallback) {
savedCallback('User canceled.');
}
savedCallback = result => {
// if (result.type !== type) {
// //
// //if (__DEV__) {
// // throw new Error('Unsupported response type: ' + resp.type);
// //}
// return;
// }
savedCallback = undefined;
if (result.resultStatus !== '9000') {
// const err = new Error(result.errMsg);
// err.errCode = result.errCode;
reject(result);
} else {
resolve(result);
}
};
});
}
NativeAppEventEmitter.addListener('Alipay_Resp', resp => {
const callback = savedCallback;
savedCallback = undefined;
callback && callback(resp);
});
function wrapCheckApi(nativeFunc) {
if (!nativeFunc) {
return undefined;
}
const promisified = promisify(nativeFunc, translateError);
return (...args) => {
return promisified(...args);
};
}
export const isLogined = wrapCheckApi(Alipay.isLogined);
function wrapApi(nativeFunc) {
if (!nativeFunc) {
return undefined;
}
const promisified = promisify(nativeFunc, translateError);
return async function (...args) {
return await promisified(...args);
};
}
const nativePayRequest = wrapApi(Alipay.payOrder);
export function payOrder(orderStr, scheme) {
return nativePayRequest(orderStr, scheme)
.then(() => waitForPayResponse("Pay.Resp"));
}