-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
96 lines (81 loc) · 3.07 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
85
86
87
88
89
90
91
92
93
94
95
96
import { Component } from 'react';
import { NativeModules, DeviceEventEmitter, Platform, NativeEventEmitter } from 'react-native';
import { PropTypes } from 'prop-types';
const { IPay88 } = NativeModules;
const iosEvent = new NativeEventEmitter(IPay88);
const isAndroid = Platform.OS === 'android';
let successSubscription;
let failedSubscription;
let cancelSubscription;
export default class IPay extends Component {
static propTypes = {
successNotify: PropTypes.func.isRequired,
failedNotify: PropTypes.func.isRequired,
cancelNotify: PropTypes.func.isRequired,
};
componentWillMount() {
if (isAndroid) {
// Android
successSubscription = DeviceEventEmitter.addListener('ipay88:success', (data) => this.onSuccess(data));
failedSubscription = DeviceEventEmitter.addListener('ipay88:failed', (data) => this.onFailed(data));
cancelSubscription = DeviceEventEmitter.addListener('ipay88:canceled', (data) => this.onCanceled(data));
} else {
// ios
successSubscription = iosEvent.addListener('ipay88:success', (data) => this.onSuccess(data));
failedSubscription = iosEvent.addListener('ipay88:failed', (data) => this.onFailed(data));
cancelSubscription = iosEvent.addListener('ipay88:canceled', (data) => this.onCanceled(data));
}
}
componentDidMount() {
successSubscription.remove();
failedSubscription.remove();
cancelSubscription.remove();
}
onSuccess = (data) => {
this.props.successNotify(data);
};
onCanceled = (data) => {
this.props.cancelNotify(data);
};
onFailed = (data) => {
this.props.failedNotify(data);
};
render() {
return null;
}
}
const Pay = (data) => {
const {
merchantKey = '',
merchantCode = '',
referenceNo = '',
amount = '',
currency = '',
productDescription = '',
userName = '',
userEmail = '',
userContact = '',
country = '',
backendUrl = '',
} = data;
const errors = {};
// if (paymentId === '') {errors.paymentId = '`paymentId` is required'; // optional
if (merchantKey === '') errors.merchantKey = '`merchantKey` is required';
if (merchantCode === '') errors.merchantCode = '`merchantCode` is required`';
if (referenceNo === '') errors.referenceNo = '`referenceNo` is required';
if (amount === '') errors.amount = '`amount` is required';
if (currency === '') errors.currency = '`currency` is required';
if (productDescription === '') errors.productDescription = '`productDescription` is required';
if (userName === '') errors.userName = '`userName` is required';
if (userEmail === '') errors.userEmail = '`userEmail` is required';
if (userContact === '') errors.userContact = '`userContact` is required';
// if (remark === '') errors.remark = '`remark` is required'; // optional
// if (utfLang === '') errors.utfLang = '`utfLang` is required'; // optional
if (country === '') errors.country = '`country` is required';
if (backendUrl === '') errors.backendUrl = '`backendUrl` is required';
if (Object.keys(errors).length > 0) {
return errors;
}
return IPay88.pay(data);
};
export { Pay };