omnipay-reactnative-sdk 0.3.5 → 0.3.7
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 +10 -8
- package/lib/commonjs/components/OmnipayProvider.js +32 -40
- package/lib/commonjs/components/OmnipayProvider.js.map +1 -1
- package/lib/commonjs/components/OmnipayView.js.map +1 -1
- package/lib/commonjs/functions.js.map +1 -1
- package/lib/commonjs/hooks/useOmnipay.js +0 -1
- package/lib/commonjs/hooks/useOmnipay.js.map +1 -1
- package/lib/module/components/OmnipayProvider.js +33 -41
- package/lib/module/components/OmnipayProvider.js.map +1 -1
- package/lib/module/components/OmnipayView.js +1 -1
- package/lib/module/components/OmnipayView.js.map +1 -1
- package/lib/module/functions.js +2 -2
- package/lib/module/functions.js.map +1 -1
- package/lib/module/hooks/useOmnipay.js +0 -1
- package/lib/module/hooks/useOmnipay.js.map +1 -1
- package/lib/module/index.js +2 -2
- package/lib/typescript/components/OmnipayProvider.d.ts +5 -2
- package/lib/typescript/components/OmnipayProvider.d.ts.map +1 -1
- package/lib/typescript/components/OmnipayView.d.ts.map +1 -1
- package/lib/typescript/functions.d.ts +1 -1
- package/lib/typescript/hooks/useOmnipay.d.ts +3 -1
- package/lib/typescript/hooks/useOmnipay.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +2 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/components/OmnipayProvider.tsx +208 -198
- package/src/components/OmnipayView.tsx +130 -135
- package/src/functions.ts +38 -39
- package/src/hooks/useOmnipay.tsx +9 -6
- package/src/index.tsx +2 -2
|
@@ -1,160 +1,155 @@
|
|
|
1
1
|
import React, { Fragment, useRef, useState } from 'react';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
StyleSheet,
|
|
4
|
+
View,
|
|
5
|
+
Platform,
|
|
6
|
+
ActivityIndicator,
|
|
7
|
+
Text,
|
|
8
|
+
Linking,
|
|
9
9
|
} from 'react-native';
|
|
10
10
|
import { WebView, WebViewMessageEvent } from 'react-native-webview';
|
|
11
|
-
import { getContact, getWebHost } from
|
|
11
|
+
import { getContact, getWebHost } from '../functions';
|
|
12
12
|
|
|
13
13
|
type OmnipayProps = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
color: string;
|
|
15
|
+
env: 'dev' | 'prod';
|
|
16
|
+
publicKey: string;
|
|
17
|
+
phoneNumber: string;
|
|
18
|
+
view: 'bills';
|
|
19
|
+
onEnterFullScreen?: () => void;
|
|
20
|
+
onExitFullScreen?: () => void;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
type PostMessage = {
|
|
24
|
-
|
|
24
|
+
[key: string]: unknown;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
type Status = 'error' | 'loading' | 'success';
|
|
28
28
|
|
|
29
29
|
export const Omnipay = ({
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
color,
|
|
31
|
+
env,
|
|
32
|
+
publicKey,
|
|
33
|
+
phoneNumber,
|
|
34
|
+
view,
|
|
35
|
+
onEnterFullScreen,
|
|
36
|
+
onExitFullScreen,
|
|
37
37
|
}: OmnipayProps): JSX.Element => {
|
|
38
|
+
const webviewRef = useRef<WebView>(null);
|
|
39
|
+
const [webviewStatus, setWebviewStatus] = useState<Status>('loading');
|
|
40
|
+
const webHost = getWebHost(env);
|
|
41
|
+
const webUrl = `${webHost}?theme=${color}&view=${view}&publicKey=${publicKey}&phoneNumber=${phoneNumber}`;
|
|
38
42
|
|
|
39
|
-
|
|
40
|
-
const [webviewStatus, setWebviewStatus] = useState<Status>('loading');
|
|
41
|
-
const webHost = getWebHost(env);
|
|
42
|
-
const webUrl = `${webHost}?theme=${color}&view=${view}&publicKey=${publicKey}&phoneNumber=${phoneNumber}`;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const onWebviewMount = `
|
|
43
|
+
const onWebviewMount = `
|
|
47
44
|
window.nativeOs = ${Platform.OS};
|
|
48
45
|
true;
|
|
49
46
|
`;
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
webviewRef.current.postMessage(JSON.stringify(data));
|
|
57
|
-
} catch (error) { }
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async function onWebviewMessage(e: WebViewMessageEvent) {
|
|
61
|
-
try {
|
|
62
|
-
if (e.nativeEvent && e.nativeEvent.data) {
|
|
63
|
-
const eventData = JSON.parse(e.nativeEvent.data);
|
|
64
|
-
const { dataKey, dataValue } = eventData;
|
|
65
|
-
if (dataKey === 'chooseContact') {
|
|
66
|
-
const contactDetails = await getContact();
|
|
67
|
-
postMessage({
|
|
68
|
-
dataKey: 'contactSelected',
|
|
69
|
-
dataValue: contactDetails,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
if (dataKey === 'modalOpen') {
|
|
73
|
-
if (onEnterFullScreen) {
|
|
74
|
-
onEnterFullScreen();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (dataKey === 'modalClosed') {
|
|
78
|
-
if (onExitFullScreen) {
|
|
79
|
-
onExitFullScreen();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (dataKey === 'openLink') {
|
|
83
|
-
Linking.openURL(dataValue);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
} catch (error) { }
|
|
48
|
+
function postMessage(data: PostMessage) {
|
|
49
|
+
if (!webviewRef.current) {
|
|
50
|
+
return;
|
|
87
51
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
52
|
+
try {
|
|
53
|
+
webviewRef.current.postMessage(JSON.stringify(data));
|
|
54
|
+
} catch (error) {}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function onWebviewMessage(e: WebViewMessageEvent) {
|
|
58
|
+
try {
|
|
59
|
+
if (e.nativeEvent && e.nativeEvent.data) {
|
|
60
|
+
const eventData = JSON.parse(e.nativeEvent.data);
|
|
61
|
+
const { dataKey, dataValue } = eventData;
|
|
62
|
+
if (dataKey === 'chooseContact') {
|
|
63
|
+
const contactDetails = await getContact();
|
|
64
|
+
postMessage({
|
|
65
|
+
dataKey: 'contactSelected',
|
|
66
|
+
dataValue: contactDetails,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (dataKey === 'modalOpen') {
|
|
70
|
+
if (onEnterFullScreen) {
|
|
71
|
+
onEnterFullScreen();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (dataKey === 'modalClosed') {
|
|
75
|
+
if (onExitFullScreen) {
|
|
76
|
+
onExitFullScreen();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (dataKey === 'openLink') {
|
|
80
|
+
Linking.openURL(dataValue);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (view !== 'bills') {
|
|
87
|
+
return <Text>Invalid view</Text>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!publicKey.includes('OMNIPUBKEY_')) {
|
|
91
|
+
return <Text>Invalid public key</Text>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (phoneNumber.length !== 11) {
|
|
95
|
+
return <Text>Invalid phone number</Text>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (color.length < 3) {
|
|
99
|
+
return <Text>Invalid color</Text>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!['dev', 'prod'].includes(env)) {
|
|
103
|
+
return <Text>Invalid environment</Text>;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<Fragment>
|
|
108
|
+
<View style={styles.full}>
|
|
109
|
+
<WebView
|
|
110
|
+
source={{
|
|
111
|
+
uri: webUrl,
|
|
112
|
+
}}
|
|
113
|
+
style={styles.webview}
|
|
114
|
+
injectedJavaScriptBeforeContentLoaded={onWebviewMount}
|
|
115
|
+
onMessage={onWebviewMessage}
|
|
116
|
+
ref={webviewRef}
|
|
117
|
+
onLoadEnd={() => setWebviewStatus('success')}
|
|
118
|
+
/>
|
|
119
|
+
{webviewStatus === 'loading' && (
|
|
120
|
+
<View style={styles.webviewLoader}>
|
|
121
|
+
<ActivityIndicator size="small" color={color} />
|
|
122
|
+
</View>
|
|
123
|
+
)}
|
|
124
|
+
</View>
|
|
125
|
+
</Fragment>
|
|
126
|
+
);
|
|
131
127
|
};
|
|
132
128
|
|
|
133
129
|
const styles = StyleSheet.create({
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
130
|
+
hide: {
|
|
131
|
+
display: 'none',
|
|
132
|
+
},
|
|
133
|
+
full: {
|
|
134
|
+
flex: 1,
|
|
135
|
+
width: '100%',
|
|
136
|
+
height: '100%',
|
|
137
|
+
},
|
|
138
|
+
webview: {
|
|
139
|
+
flex: 1,
|
|
140
|
+
width: '100%',
|
|
141
|
+
height: '100%',
|
|
142
|
+
},
|
|
143
|
+
webviewLoader: {
|
|
144
|
+
zIndex: 3,
|
|
145
|
+
backgroundColor: 'white',
|
|
146
|
+
alignItems: 'center',
|
|
147
|
+
justifyContent: 'center',
|
|
148
|
+
flex: 1,
|
|
149
|
+
width: '100%',
|
|
150
|
+
height: '100%',
|
|
151
|
+
position: 'absolute',
|
|
152
|
+
top: 0,
|
|
153
|
+
left: 0,
|
|
154
|
+
},
|
|
159
155
|
});
|
|
160
|
-
|
package/src/functions.ts
CHANGED
|
@@ -1,49 +1,48 @@
|
|
|
1
|
-
import { PermissionsAndroid, Platform } from
|
|
2
|
-
import { selectContactPhone } from
|
|
1
|
+
import { PermissionsAndroid, Platform } from 'react-native';
|
|
2
|
+
import { selectContactPhone } from 'react-native-select-contact';
|
|
3
3
|
|
|
4
4
|
export async function checkPermisionStatus() {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
if (Platform.OS === 'ios') {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
if (PermissionsAndroid.PERMISSIONS.READ_CONTACTS) {
|
|
9
|
+
const granted = await PermissionsAndroid.request(
|
|
10
|
+
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
|
|
11
|
+
{
|
|
12
|
+
title: 'Allow us access your contact list',
|
|
13
|
+
message:
|
|
14
|
+
'This will enable you choose a phone number to buy airtime or data for from your contact list',
|
|
15
|
+
buttonNegative: 'Cancel',
|
|
16
|
+
buttonPositive: 'OK',
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
|
|
20
|
+
return true;
|
|
7
21
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
|
|
11
|
-
{
|
|
12
|
-
title: 'Allow us access your contact list',
|
|
13
|
-
message:
|
|
14
|
-
'This will enable you choose a phone number to buy airtime or data for from your contact list',
|
|
15
|
-
buttonNegative: 'Cancel',
|
|
16
|
-
buttonPositive: 'OK',
|
|
17
|
-
}
|
|
18
|
-
);
|
|
19
|
-
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export async function getContact() {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
const result = await selectContactPhone();
|
|
33
|
-
if (!result) {
|
|
34
|
-
return { name: '', phoneNumber: '' };
|
|
35
|
-
}
|
|
36
|
-
let { contact, selectedPhone } = result;
|
|
37
|
-
return { name: contact.name, phoneNumber: selectedPhone.number };
|
|
38
|
-
} catch (error) {
|
|
39
|
-
return { name: '', phoneNumber: '' };
|
|
27
|
+
try {
|
|
28
|
+
const isPermissionGranted = await checkPermisionStatus();
|
|
29
|
+
if (!isPermissionGranted) {
|
|
30
|
+
return { name: '', phoneNumber: '' };
|
|
40
31
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (env === 'dev') {
|
|
45
|
-
return 'https://omnipay-websdk.vercel.app/';
|
|
32
|
+
const result = await selectContactPhone();
|
|
33
|
+
if (!result) {
|
|
34
|
+
return { name: '', phoneNumber: '' };
|
|
46
35
|
}
|
|
47
|
-
|
|
36
|
+
let { contact, selectedPhone } = result;
|
|
37
|
+
return { name: contact.name, phoneNumber: selectedPhone.number };
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return { name: '', phoneNumber: '' };
|
|
40
|
+
}
|
|
48
41
|
}
|
|
49
42
|
|
|
43
|
+
export function getWebHost(env: 'prod' | 'dev') {
|
|
44
|
+
if (env === 'dev') {
|
|
45
|
+
return 'https://omnipay-websdk.vercel.app/';
|
|
46
|
+
}
|
|
47
|
+
return 'https://sdk.omnipay.ng/';
|
|
48
|
+
}
|
package/src/hooks/useOmnipay.tsx
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { useContext } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
OmnipayContext,
|
|
4
|
+
OmnipayContextType,
|
|
5
|
+
} from '../components/OmnipayProvider';
|
|
3
6
|
|
|
4
7
|
export function useOmnipay() {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
8
|
+
const { initiateBills } = useContext(OmnipayContext) as OmnipayContextType;
|
|
9
|
+
return {
|
|
10
|
+
initiateBills,
|
|
11
|
+
};
|
|
12
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { OmnipayProvider } from
|
|
1
|
+
export { OmnipayProvider } from './components/OmnipayProvider';
|
|
2
2
|
export { useOmnipay } from './hooks/useOmnipay';
|
|
3
|
-
export { Omnipay } from
|
|
3
|
+
export { Omnipay } from './components/OmnipayView';
|