drexpay-react-native 1.0.0

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.
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ export interface DrexpayCheckoutProps {
3
+ /** Whether the checkout sheet is visible. */
4
+ visible: boolean;
5
+ /** The hosted checkout URL returned by POST /api/payment-links. */
6
+ checkoutUrl: string;
7
+ /**
8
+ * Called when the customer completes payment and is redirected to your
9
+ * `redirectTo` URL. `reference` is the payment reference — also passed via
10
+ * your server-side webhook so you can verify independently.
11
+ */
12
+ onSuccess: (reference: string) => void;
13
+ /** Called when the user dismisses the sheet without completing payment. */
14
+ onClose: () => void;
15
+ /** Called on WebView or network errors. */
16
+ onError?: (error: Error) => void;
17
+ }
18
+ export declare function DrexpayCheckout({ visible, checkoutUrl, onSuccess, onClose, onError, }: DrexpayCheckoutProps): React.JSX.Element;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.DrexpayCheckout = DrexpayCheckout;
37
+ const react_1 = __importStar(require("react"));
38
+ const react_native_1 = require("react-native");
39
+ const react_native_webview_1 = require("react-native-webview");
40
+ const DREXPAY_HOSTS = new Set(['drexpay.tech', 'www.drexpay.tech']);
41
+ function isDrexpayUrl(url) {
42
+ try {
43
+ const host = new URL(url).hostname;
44
+ return DREXPAY_HOSTS.has(host);
45
+ }
46
+ catch (_a) {
47
+ return false;
48
+ }
49
+ }
50
+ function extractReference(url) {
51
+ var _a;
52
+ try {
53
+ return (_a = new URL(url).searchParams.get('reference')) !== null && _a !== void 0 ? _a : '';
54
+ }
55
+ catch (_b) {
56
+ return '';
57
+ }
58
+ }
59
+ function DrexpayCheckout({ visible, checkoutUrl, onSuccess, onClose, onError, }) {
60
+ const [loading, setLoading] = (0, react_1.useState)(true);
61
+ const completedRef = (0, react_1.useRef)(false);
62
+ const handleNavigationStateChange = (0, react_1.useCallback)((state) => {
63
+ if (completedRef.current)
64
+ return;
65
+ const { url } = state;
66
+ if (url &&
67
+ !url.startsWith('about:') &&
68
+ !url.startsWith('data:') &&
69
+ !isDrexpayUrl(url)) {
70
+ completedRef.current = true;
71
+ onSuccess(extractReference(url));
72
+ }
73
+ }, [onSuccess]);
74
+ const handleClose = (0, react_1.useCallback)(() => {
75
+ completedRef.current = false;
76
+ onClose();
77
+ }, [onClose]);
78
+ return (<react_native_1.Modal visible={visible} animationType="slide" presentationStyle={react_native_1.Platform.OS === 'ios' ? 'pageSheet' : 'overFullScreen'} onRequestClose={handleClose}>
79
+ <react_native_1.SafeAreaView style={styles.root}>
80
+ <react_native_1.View style={styles.header}>
81
+ <react_native_1.View style={styles.headerSpacer}/>
82
+ <react_native_1.Text style={styles.headerTitle}>Complete Payment</react_native_1.Text>
83
+ <react_native_1.TouchableOpacity onPress={handleClose} style={styles.closeBtn} hitSlop={{ top: 10, right: 10, bottom: 10, left: 10 }} accessibilityLabel="Close checkout">
84
+ <react_native_1.Text style={styles.closeIcon}>✕</react_native_1.Text>
85
+ </react_native_1.TouchableOpacity>
86
+ </react_native_1.View>
87
+
88
+ <react_native_1.View style={styles.webviewWrap}>
89
+ <react_native_webview_1.WebView source={{ uri: checkoutUrl }} onNavigationStateChange={handleNavigationStateChange} onLoadStart={() => setLoading(true)} onLoadEnd={() => setLoading(false)} onError={(e) => onError === null || onError === void 0 ? void 0 : onError(new Error(e.nativeEvent.description))} javaScriptEnabled domStorageEnabled style={styles.webview}/>
90
+ {loading && (<react_native_1.View style={styles.loadingOverlay}>
91
+ <react_native_1.ActivityIndicator size="large" color="#7C3AED"/>
92
+ </react_native_1.View>)}
93
+ </react_native_1.View>
94
+ </react_native_1.SafeAreaView>
95
+ </react_native_1.Modal>);
96
+ }
97
+ const styles = react_native_1.StyleSheet.create({
98
+ root: {
99
+ flex: 1,
100
+ backgroundColor: '#ffffff',
101
+ },
102
+ header: {
103
+ flexDirection: 'row',
104
+ alignItems: 'center',
105
+ justifyContent: 'space-between',
106
+ paddingHorizontal: 20,
107
+ paddingVertical: 14,
108
+ borderBottomWidth: react_native_1.StyleSheet.hairlineWidth,
109
+ borderBottomColor: '#E4E4E7',
110
+ },
111
+ headerSpacer: {
112
+ width: 32,
113
+ },
114
+ headerTitle: {
115
+ fontSize: 16,
116
+ fontWeight: '600',
117
+ color: '#09090B',
118
+ textAlign: 'center',
119
+ },
120
+ closeBtn: {
121
+ width: 32,
122
+ alignItems: 'flex-end',
123
+ },
124
+ closeIcon: {
125
+ fontSize: 16,
126
+ color: '#71717A',
127
+ },
128
+ webviewWrap: {
129
+ flex: 1,
130
+ position: 'relative',
131
+ },
132
+ webview: {
133
+ flex: 1,
134
+ },
135
+ loadingOverlay: {
136
+ ...react_native_1.StyleSheet.absoluteFillObject,
137
+ backgroundColor: '#ffffff',
138
+ alignItems: 'center',
139
+ justifyContent: 'center',
140
+ },
141
+ });
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { DrexpayCheckout } from './DrexpayCheckout';
2
+ export type { DrexpayCheckoutProps } from './DrexpayCheckout';
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DrexpayCheckout = void 0;
4
+ var DrexpayCheckout_1 = require("./DrexpayCheckout");
5
+ Object.defineProperty(exports, "DrexpayCheckout", { enumerable: true, get: function () { return DrexpayCheckout_1.DrexpayCheckout; } });
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "drexpay-react-native",
3
+ "version": "1.0.0",
4
+ "description": "React Native SDK for Drexpay payment gateway — open the hosted checkout in a WebView with one component",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "src"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "prepare": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "drexpay",
17
+ "payment",
18
+ "react-native",
19
+ "checkout",
20
+ "nigeria",
21
+ "webview"
22
+ ],
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/learnwithalex/drexpay.git",
27
+ "directory": "sdk/react-native"
28
+ },
29
+ "homepage": "https://drexpay.tech/docs#mobile-sdks",
30
+ "peerDependencies": {
31
+ "react": ">=18.0.0",
32
+ "react-native": ">=0.71.0",
33
+ "react-native-webview": ">=13.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/react": "^18.0.0",
37
+ "@types/react-native": "^0.73.0",
38
+ "react": "^18.0.0",
39
+ "react-native": "^0.73.0",
40
+ "react-native-webview": "^13.0.0",
41
+ "typescript": "^5.4.0"
42
+ }
43
+ }
@@ -0,0 +1,169 @@
1
+ import React, { useCallback, useRef, useState } from 'react';
2
+ import {
3
+ ActivityIndicator,
4
+ Modal,
5
+ Platform,
6
+ SafeAreaView,
7
+ StyleSheet,
8
+ Text,
9
+ TouchableOpacity,
10
+ View,
11
+ } from 'react-native';
12
+ import { WebView, type WebViewNavigation } from 'react-native-webview';
13
+
14
+ const DREXPAY_HOSTS = new Set(['drexpay.tech', 'www.drexpay.tech']);
15
+
16
+ function isDrexpayUrl(url: string): boolean {
17
+ try {
18
+ const host = new URL(url).hostname;
19
+ return DREXPAY_HOSTS.has(host);
20
+ } catch {
21
+ return false;
22
+ }
23
+ }
24
+
25
+ function extractReference(url: string): string {
26
+ try {
27
+ return new URL(url).searchParams.get('reference') ?? '';
28
+ } catch {
29
+ return '';
30
+ }
31
+ }
32
+
33
+ export interface DrexpayCheckoutProps {
34
+ /** Whether the checkout sheet is visible. */
35
+ visible: boolean;
36
+ /** The hosted checkout URL returned by POST /api/payment-links. */
37
+ checkoutUrl: string;
38
+ /**
39
+ * Called when the customer completes payment and is redirected to your
40
+ * `redirectTo` URL. `reference` is the payment reference — also passed via
41
+ * your server-side webhook so you can verify independently.
42
+ */
43
+ onSuccess: (reference: string) => void;
44
+ /** Called when the user dismisses the sheet without completing payment. */
45
+ onClose: () => void;
46
+ /** Called on WebView or network errors. */
47
+ onError?: (error: Error) => void;
48
+ }
49
+
50
+ export function DrexpayCheckout({
51
+ visible,
52
+ checkoutUrl,
53
+ onSuccess,
54
+ onClose,
55
+ onError,
56
+ }: DrexpayCheckoutProps) {
57
+ const [loading, setLoading] = useState(true);
58
+ const completedRef = useRef(false);
59
+
60
+ const handleNavigationStateChange = useCallback(
61
+ (state: WebViewNavigation) => {
62
+ if (completedRef.current) return;
63
+ const { url } = state;
64
+ if (
65
+ url &&
66
+ !url.startsWith('about:') &&
67
+ !url.startsWith('data:') &&
68
+ !isDrexpayUrl(url)
69
+ ) {
70
+ completedRef.current = true;
71
+ onSuccess(extractReference(url));
72
+ }
73
+ },
74
+ [onSuccess],
75
+ );
76
+
77
+ const handleClose = useCallback(() => {
78
+ completedRef.current = false;
79
+ onClose();
80
+ }, [onClose]);
81
+
82
+ return (
83
+ <Modal
84
+ visible={visible}
85
+ animationType="slide"
86
+ presentationStyle={Platform.OS === 'ios' ? 'pageSheet' : 'overFullScreen'}
87
+ onRequestClose={handleClose}
88
+ >
89
+ <SafeAreaView style={styles.root}>
90
+ <View style={styles.header}>
91
+ <View style={styles.headerSpacer} />
92
+ <Text style={styles.headerTitle}>Complete Payment</Text>
93
+ <TouchableOpacity
94
+ onPress={handleClose}
95
+ style={styles.closeBtn}
96
+ hitSlop={{ top: 10, right: 10, bottom: 10, left: 10 }}
97
+ accessibilityLabel="Close checkout"
98
+ >
99
+ <Text style={styles.closeIcon}>✕</Text>
100
+ </TouchableOpacity>
101
+ </View>
102
+
103
+ <View style={styles.webviewWrap}>
104
+ <WebView
105
+ source={{ uri: checkoutUrl }}
106
+ onNavigationStateChange={handleNavigationStateChange}
107
+ onLoadStart={() => setLoading(true)}
108
+ onLoadEnd={() => setLoading(false)}
109
+ onError={(e) => onError?.(new Error(e.nativeEvent.description))}
110
+ javaScriptEnabled
111
+ domStorageEnabled
112
+ style={styles.webview}
113
+ />
114
+ {loading && (
115
+ <View style={styles.loadingOverlay}>
116
+ <ActivityIndicator size="large" color="#7C3AED" />
117
+ </View>
118
+ )}
119
+ </View>
120
+ </SafeAreaView>
121
+ </Modal>
122
+ );
123
+ }
124
+
125
+ const styles = StyleSheet.create({
126
+ root: {
127
+ flex: 1,
128
+ backgroundColor: '#ffffff',
129
+ },
130
+ header: {
131
+ flexDirection: 'row',
132
+ alignItems: 'center',
133
+ justifyContent: 'space-between',
134
+ paddingHorizontal: 20,
135
+ paddingVertical: 14,
136
+ borderBottomWidth: StyleSheet.hairlineWidth,
137
+ borderBottomColor: '#E4E4E7',
138
+ },
139
+ headerSpacer: {
140
+ width: 32,
141
+ },
142
+ headerTitle: {
143
+ fontSize: 16,
144
+ fontWeight: '600',
145
+ color: '#09090B',
146
+ textAlign: 'center',
147
+ },
148
+ closeBtn: {
149
+ width: 32,
150
+ alignItems: 'flex-end',
151
+ },
152
+ closeIcon: {
153
+ fontSize: 16,
154
+ color: '#71717A',
155
+ },
156
+ webviewWrap: {
157
+ flex: 1,
158
+ position: 'relative',
159
+ },
160
+ webview: {
161
+ flex: 1,
162
+ },
163
+ loadingOverlay: {
164
+ ...StyleSheet.absoluteFillObject,
165
+ backgroundColor: '#ffffff',
166
+ alignItems: 'center',
167
+ justifyContent: 'center',
168
+ },
169
+ });
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { DrexpayCheckout } from './DrexpayCheckout';
2
+ export type { DrexpayCheckoutProps } from './DrexpayCheckout';