omnipay-reactnative-sdk 0.2.3 → 0.2.4

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