omnipay-reactnative-sdk 0.3.5 → 0.3.6
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 +24 -33
- 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 +25 -34
- 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 +1 -1
- 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.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 +206 -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,226 +1,234 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
import {
|
|
1
|
+
import React, { useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
ActivityIndicator,
|
|
4
|
+
Linking,
|
|
5
|
+
Platform,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
View,
|
|
8
|
+
Modal,
|
|
9
|
+
ScrollView,
|
|
10
|
+
Dimensions,
|
|
11
|
+
TouchableOpacity,
|
|
12
|
+
Image,
|
|
13
|
+
} from 'react-native';
|
|
3
14
|
import WebView, { WebViewMessageEvent } from 'react-native-webview';
|
|
4
15
|
import { getContact } from '../functions';
|
|
5
16
|
|
|
6
17
|
type OmnipayProviderProps = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
18
|
+
publicKey: string;
|
|
19
|
+
env: 'dev' | 'prod';
|
|
20
|
+
color: string;
|
|
21
|
+
children: React.ReactElement | React.ReactElement[];
|
|
22
|
+
};
|
|
12
23
|
|
|
13
24
|
type PostMessage = {
|
|
14
|
-
|
|
25
|
+
[key: string]: unknown;
|
|
15
26
|
};
|
|
16
27
|
|
|
17
28
|
type Status = 'error' | 'loading' | 'success';
|
|
18
29
|
|
|
19
30
|
export type OmnipayContextType = {
|
|
20
|
-
|
|
21
|
-
}
|
|
31
|
+
initiateBills: (phoneNumber: string) => void;
|
|
32
|
+
};
|
|
22
33
|
|
|
23
34
|
let defaultValue = {
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const OmnipayContext = React.createContext<OmnipayContextType | null>(defaultValue);
|
|
28
|
-
|
|
29
|
-
export const OmnipayProvider = ({ children, publicKey, env, color }: OmnipayProviderProps) => {
|
|
30
|
-
const [webviewStatus, setWebviewStatus] = useState<Status>('loading');
|
|
31
|
-
const [isVisible, setIsVisible] = useState(false);
|
|
32
|
-
const webviewRef = useRef<WebView>(null);
|
|
33
|
-
const webHost = getWebHost();
|
|
34
|
-
const [webviewUrl, setWebviewUrl] = useState(webHost);
|
|
35
|
-
const showWebview = webviewUrl.includes("view") && isVisible;
|
|
36
|
-
const [containerOffset, setContainerOffset] = useState(30);
|
|
37
|
-
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
if (!publicKey) {
|
|
40
|
-
console.warn("Omnipay error: Public key is required")
|
|
41
|
-
}
|
|
42
|
-
if (!["prod", "dev"].includes(env)) {
|
|
43
|
-
console.warn("Omnipay error: Invalid environment")
|
|
44
|
-
}
|
|
45
|
-
if (color.length < 3) {
|
|
46
|
-
console.warn("Omnipay error: Invalid color ")
|
|
47
|
-
}
|
|
48
|
-
}, [publicKey, env, color])
|
|
49
|
-
|
|
35
|
+
initiateBills: () => null,
|
|
36
|
+
};
|
|
50
37
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
38
|
+
export const OmnipayContext = React.createContext<OmnipayContextType | null>(
|
|
39
|
+
defaultValue
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
export const OmnipayProvider = ({
|
|
43
|
+
children,
|
|
44
|
+
publicKey,
|
|
45
|
+
env,
|
|
46
|
+
color,
|
|
47
|
+
}: OmnipayProviderProps) => {
|
|
48
|
+
const [webviewStatus, setWebviewStatus] = useState<Status>('loading');
|
|
49
|
+
const [isVisible, setIsVisible] = useState(false);
|
|
50
|
+
const webviewRef = useRef<WebView>(null);
|
|
51
|
+
const webHost = getWebHost();
|
|
52
|
+
const [webviewUrl, setWebviewUrl] = useState(webHost);
|
|
53
|
+
const showWebview = webviewUrl.includes('view') && isVisible;
|
|
54
|
+
const [containerOffset, setContainerOffset] = useState(30);
|
|
55
|
+
const isValidEnv = ['prod', 'dev'].includes(env);
|
|
56
|
+
const isValidColor = color.length > 2;
|
|
57
|
+
|
|
58
|
+
function getWebviewStyle() {
|
|
59
|
+
if (!showWebview) {
|
|
60
|
+
return { opacity: 0, height: 0, width: 0, flex: 0 };
|
|
56
61
|
}
|
|
62
|
+
return styles.webview;
|
|
63
|
+
}
|
|
57
64
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return 'https://omnipay-websdk.vercel.app/';
|
|
62
|
-
}
|
|
63
|
-
return 'https://sdk.omnipay.ng/';
|
|
65
|
+
function getWebHost() {
|
|
66
|
+
if (env === 'dev') {
|
|
67
|
+
return 'https://omnipay-websdk.vercel.app/';
|
|
64
68
|
}
|
|
69
|
+
return 'https://sdk.omnipay.ng/';
|
|
70
|
+
}
|
|
65
71
|
|
|
66
|
-
|
|
67
|
-
const onWebviewMount = `
|
|
72
|
+
const onWebviewMount = `
|
|
68
73
|
window.nativeOs = ${Platform.OS};
|
|
69
74
|
true;
|
|
70
75
|
`;
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
try {
|
|
77
|
-
webviewRef.current.postMessage(JSON.stringify(data));
|
|
78
|
-
} catch (error) { }
|
|
77
|
+
function postMessage(data: PostMessage) {
|
|
78
|
+
if (!webviewRef.current) {
|
|
79
|
+
return;
|
|
79
80
|
}
|
|
81
|
+
try {
|
|
82
|
+
webviewRef.current.postMessage(JSON.stringify(data));
|
|
83
|
+
} catch (error) {}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function onWebviewMessage(e: WebViewMessageEvent) {
|
|
87
|
+
try {
|
|
88
|
+
if (e.nativeEvent && e.nativeEvent.data) {
|
|
89
|
+
const eventData = JSON.parse(e.nativeEvent.data);
|
|
90
|
+
const { dataKey, dataValue } = eventData;
|
|
91
|
+
if (dataKey === 'chooseContact') {
|
|
92
|
+
const contactDetails = await getContact();
|
|
93
|
+
postMessage({
|
|
94
|
+
dataKey: 'contactSelected',
|
|
95
|
+
dataValue: contactDetails,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
80
98
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
} catch (error) { }
|
|
99
|
+
if (dataKey === 'openLink') {
|
|
100
|
+
Linking.openURL(dataValue);
|
|
101
|
+
}
|
|
102
|
+
if (dataKey === 'modalOpen') {
|
|
103
|
+
setContainerOffset(0);
|
|
104
|
+
}
|
|
105
|
+
if (dataKey === 'modalClosed') {
|
|
106
|
+
setContainerOffset(30);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const _initiateBills = (phoneNumber: string) => {
|
|
113
|
+
if (phoneNumber) {
|
|
114
|
+
if (phoneNumber.length === 11) {
|
|
115
|
+
const webUrl = `${webHost}?theme=${color}&view=bills&publicKey=${publicKey}&phoneNumber=${phoneNumber}`;
|
|
116
|
+
setWebviewUrl(webUrl);
|
|
117
|
+
setIsVisible(true);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
105
120
|
}
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
{
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
</View>
|
|
161
|
-
)}
|
|
162
|
-
</>
|
|
163
|
-
}
|
|
164
|
-
{children}
|
|
165
|
-
</OmnipayContext.Provider>
|
|
166
|
-
);
|
|
121
|
+
console.warn('Omnipay error: Invalid phone number');
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const webviewStyle = getWebviewStyle();
|
|
125
|
+
const isPropsValid = isValidColor && !!publicKey && isValidEnv;
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<OmnipayContext.Provider value={{ initiateBills: _initiateBills }}>
|
|
129
|
+
{isPropsValid && (
|
|
130
|
+
<>
|
|
131
|
+
{webviewUrl.includes('view') && (
|
|
132
|
+
<Modal visible={showWebview} style={styles.modal}>
|
|
133
|
+
<View style={styles.backdrop}>
|
|
134
|
+
<ScrollView
|
|
135
|
+
style={[styles.container, { paddingTop: containerOffset }]}
|
|
136
|
+
>
|
|
137
|
+
<WebView
|
|
138
|
+
source={{
|
|
139
|
+
uri: webviewUrl,
|
|
140
|
+
}}
|
|
141
|
+
style={webviewStyle}
|
|
142
|
+
injectedJavaScriptBeforeContentLoaded={onWebviewMount}
|
|
143
|
+
onMessage={onWebviewMessage}
|
|
144
|
+
ref={webviewRef}
|
|
145
|
+
onLoadStart={() => {
|
|
146
|
+
setWebviewStatus('loading');
|
|
147
|
+
}}
|
|
148
|
+
onLoadEnd={() => setWebviewStatus('success')}
|
|
149
|
+
/>
|
|
150
|
+
{containerOffset !== 0 && webviewStatus === 'success' && (
|
|
151
|
+
<TouchableOpacity
|
|
152
|
+
style={styles.close}
|
|
153
|
+
onPress={() => setIsVisible(false)}
|
|
154
|
+
>
|
|
155
|
+
<Image
|
|
156
|
+
source={require('../assets/cancel.png')}
|
|
157
|
+
style={styles.closeIcon}
|
|
158
|
+
/>
|
|
159
|
+
</TouchableOpacity>
|
|
160
|
+
)}
|
|
161
|
+
</ScrollView>
|
|
162
|
+
</View>
|
|
163
|
+
</Modal>
|
|
164
|
+
)}
|
|
165
|
+
{webviewStatus === 'loading' && showWebview && (
|
|
166
|
+
<View style={styles.webviewLoader}>
|
|
167
|
+
<ActivityIndicator size="small" color={color} />
|
|
168
|
+
</View>
|
|
169
|
+
)}
|
|
170
|
+
</>
|
|
171
|
+
)}
|
|
172
|
+
{children}
|
|
173
|
+
</OmnipayContext.Provider>
|
|
174
|
+
);
|
|
167
175
|
};
|
|
168
176
|
|
|
169
177
|
const styles = StyleSheet.create({
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
});
|
|
178
|
+
hide: {
|
|
179
|
+
display: 'none',
|
|
180
|
+
},
|
|
181
|
+
full: {
|
|
182
|
+
flex: 1,
|
|
183
|
+
width: '100%',
|
|
184
|
+
height: '100%',
|
|
185
|
+
},
|
|
186
|
+
webview: {
|
|
187
|
+
flex: 1,
|
|
188
|
+
width: '100%',
|
|
189
|
+
height: Dimensions.get('window').height - 110,
|
|
190
|
+
backgroundColor: 'white',
|
|
191
|
+
},
|
|
192
|
+
webviewLoader: {
|
|
193
|
+
zIndex: 3,
|
|
194
|
+
backgroundColor: 'white',
|
|
195
|
+
alignItems: 'center',
|
|
196
|
+
justifyContent: 'center',
|
|
197
|
+
flex: 1,
|
|
198
|
+
width: '100%',
|
|
199
|
+
height: '100%',
|
|
200
|
+
position: 'absolute',
|
|
201
|
+
top: 0,
|
|
202
|
+
left: 0,
|
|
203
|
+
},
|
|
204
|
+
backdrop: {
|
|
205
|
+
backgroundColor: 'rgba(0,0,0, 0.48)',
|
|
206
|
+
flex: 1,
|
|
207
|
+
justifyContent: 'flex-end',
|
|
208
|
+
position: 'relative',
|
|
209
|
+
height: '100%',
|
|
210
|
+
},
|
|
211
|
+
container: {
|
|
212
|
+
backgroundColor: 'white',
|
|
213
|
+
borderTopRightRadius: 20,
|
|
214
|
+
borderTopLeftRadius: 20,
|
|
215
|
+
maxHeight: Dimensions.get('window').height - 110,
|
|
216
|
+
flex: 1,
|
|
217
|
+
position: 'relative',
|
|
218
|
+
},
|
|
219
|
+
modal: {
|
|
220
|
+
flex: 1,
|
|
221
|
+
backgroundColor: 'white',
|
|
222
|
+
height: '100%',
|
|
223
|
+
width: '100%',
|
|
224
|
+
},
|
|
225
|
+
close: {
|
|
226
|
+
position: 'absolute',
|
|
227
|
+
top: -10,
|
|
228
|
+
right: 20,
|
|
229
|
+
},
|
|
230
|
+
closeIcon: {
|
|
231
|
+
height: 12,
|
|
232
|
+
width: 12,
|
|
233
|
+
},
|
|
234
|
+
});
|