omnipay-reactnative-sdk 0.2.6 → 0.2.8

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,204 +1,28 @@
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 React, { Fragment, useEffect } from 'react';
2
+ import { useOmnipay } from '../hooks/useOmnipay';
13
3
 
14
4
  type OmnipayProps = {
15
- color: string;
16
- env: 'dev' | 'prod';
17
- publicKey: string;
18
5
  phoneNumber: string;
19
- view: 'bills';
20
- onEnterFullScreen?: () => void;
21
- onExitFullScreen?: () => void;
22
6
  };
23
7
 
24
- type PostMessage = {
25
- [key: string]: unknown;
26
- };
27
-
28
- type Status = 'error' | 'loading' | 'success';
29
8
 
30
- const OmnipayView = ({
31
- color,
32
- env,
33
- publicKey,
9
+ export const Omnipay = ({
34
10
  phoneNumber,
35
- view,
36
- onEnterFullScreen,
37
- onExitFullScreen,
38
11
  }: 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
- }
12
+ const { initiateBills } = useOmnipay();
93
13
 
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
- }
14
+ useEffect(() => {
15
+ initiateBills(phoneNumber)
16
+ }, [phoneNumber]);
110
17
 
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
18
 
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
19
 
153
20
  return (
154
21
  <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>
22
+
172
23
  </Fragment>
173
24
  );
174
25
  };
175
26
 
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
27
 
204
- export default OmnipayView;
28
+
@@ -0,0 +1,49 @@
1
+ import { PermissionsAndroid, Platform } from "react-native";
2
+ import { selectContactPhone } from "react-native-select-contact";
3
+
4
+ export async function checkPermisionStatus() {
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;
21
+ }
22
+ }
23
+ return false;
24
+ }
25
+
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: '' };
40
+ }
41
+ }
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
+ }
49
+
package/src/index.tsx CHANGED
@@ -1,2 +1,3 @@
1
1
  export { OmnipayProvider } from "./components/OmnipayProvider";
2
2
  export { useOmnipay } from './hooks/useOmnipay';
3
+ export { Omnipay } from "./components/OmnipayView"