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,160 +1,155 @@
1
1
  import React, { Fragment, useRef, useState } from 'react';
2
2
  import {
3
- StyleSheet,
4
- View,
5
- Platform,
6
- ActivityIndicator,
7
- Text,
8
- Linking,
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 "../functions"
11
+ import { getContact, getWebHost } from '../functions';
12
12
 
13
13
  type OmnipayProps = {
14
- color: string;
15
- env: 'dev' | 'prod';
16
- publicKey: string;
17
- phoneNumber: string;
18
- view: 'bills';
19
- onEnterFullScreen?: () => void;
20
- onExitFullScreen?: () => void;
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
- [key: string]: unknown;
24
+ [key: string]: unknown;
25
25
  };
26
26
 
27
27
  type Status = 'error' | 'loading' | 'success';
28
28
 
29
29
  export const Omnipay = ({
30
- color,
31
- env,
32
- publicKey,
33
- phoneNumber,
34
- view,
35
- onEnterFullScreen,
36
- onExitFullScreen,
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
- const webviewRef = useRef<WebView>(null);
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
- function postMessage(data: PostMessage) {
52
- if (!webviewRef.current) {
53
- return;
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
- if (view !== 'bills') {
91
- return <Text>Invalid view</Text>;
92
- }
93
-
94
- if (!publicKey.includes('OMNIPUBKEY_')) {
95
- return <Text>Invalid public key</Text>;
96
- }
97
-
98
- if (phoneNumber.length !== 11) {
99
- return <Text>Invalid phone number</Text>;
100
- }
101
-
102
- if (color.length < 3) {
103
- return <Text>Invalid color</Text>;
104
- }
105
-
106
- if (!['dev', 'prod'].includes(env)) {
107
- return <Text>Invalid environment</Text>;
108
- }
109
-
110
- return (
111
- <Fragment>
112
- <View style={styles.full}>
113
- <WebView
114
- source={{
115
- uri: webUrl,
116
- }}
117
- style={styles.webview}
118
- injectedJavaScriptBeforeContentLoaded={onWebviewMount}
119
- onMessage={onWebviewMessage}
120
- ref={webviewRef}
121
- onLoadEnd={() => setWebviewStatus('success')}
122
- />
123
- {webviewStatus === 'loading' && (
124
- <View style={styles.webviewLoader}>
125
- <ActivityIndicator size="small" color={color} />
126
- </View>
127
- )}
128
- </View>
129
- </Fragment>
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
- hide: {
135
- display: 'none',
136
- },
137
- full: {
138
- flex: 1,
139
- width: '100%',
140
- height: '100%',
141
- },
142
- webview: {
143
- flex: 1,
144
- width: '100%',
145
- height: '100%',
146
- },
147
- webviewLoader: {
148
- zIndex: 3,
149
- backgroundColor: 'white',
150
- alignItems: 'center',
151
- justifyContent: 'center',
152
- flex: 1,
153
- width: '100%',
154
- height: '100%',
155
- position: 'absolute',
156
- top: 0,
157
- left: 0,
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 "react-native";
2
- import { selectContactPhone } from "react-native-select-contact";
1
+ import { PermissionsAndroid, Platform } from 'react-native';
2
+ import { selectContactPhone } from 'react-native-select-contact';
3
3
 
4
4
  export async function checkPermisionStatus() {
5
- if (Platform.OS === 'ios') {
6
- return true;
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
- 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;
21
- }
22
- }
23
- return false;
22
+ }
23
+ return false;
24
24
  }
25
25
 
26
26
  export async function getContact() {
27
- try {
28
- const isPermissionGranted = await checkPermisionStatus();
29
- if (!isPermissionGranted) {
30
- return { name: '', phoneNumber: '' };
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
- export function getWebHost(env: "prod" | "dev") {
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
- return 'https://sdk.omnipay.ng/';
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
+ }
@@ -1,9 +1,12 @@
1
1
  import { useContext } from 'react';
2
- import { OmnipayContext, OmnipayContextType } from '../components/OmnipayProvider';
2
+ import {
3
+ OmnipayContext,
4
+ OmnipayContextType,
5
+ } from '../components/OmnipayProvider';
3
6
 
4
7
  export function useOmnipay() {
5
- const { initiateBills } = useContext(OmnipayContext) as OmnipayContextType;
6
- return {
7
- initiateBills
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 "./components/OmnipayProvider";
1
+ export { OmnipayProvider } from './components/OmnipayProvider';
2
2
  export { useOmnipay } from './hooks/useOmnipay';
3
- export { Omnipay } from "./components/OmnipayView"
3
+ export { Omnipay } from './components/OmnipayView';