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.
@@ -1,226 +1,234 @@
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
 
19
30
  export type OmnipayContextType = {
20
- initiateBills: (phoneNumber: string) => void;
21
- }
31
+ initiateBills: (phoneNumber: string) => void;
32
+ };
22
33
 
23
34
  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
-
35
+ initiateBills: () => null,
36
+ };
50
37
 
51
- function getWebviewStyle() {
52
- if (!showWebview) {
53
- return { opacity: 0, height: 0, width: 0, flex: 0 };
54
- }
55
- return styles.webview;
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
- function getWebHost() {
60
- if (env === 'dev') {
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
- function postMessage(data: PostMessage) {
73
- if (!webviewRef.current) {
74
- return;
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
- 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) { }
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
- 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
- );
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
- 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
- });
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
+ });