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