-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathPaystackProvider.tsx
More file actions
132 lines (120 loc) · 4.83 KB
/
Copy pathPaystackProvider.tsx
File metadata and controls
132 lines (120 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React, { createContext, useCallback, useMemo, useState } from 'react';
import { Modal, ActivityIndicator } from 'react-native';
import { WebView, WebViewMessageEvent } from 'react-native-webview';
import { SafeAreaView } from 'react-native-safe-area-context';
import {
PaystackParams,
PaystackProviderProps,
TransactionMethod
} from './types';
import { validateParams, paystackHtmlContent, generatePaystackParams, handlePaystackMessage, shouldHandleExternally, openExternalUrl } from './utils';
import { styles } from './styles';
export const DEFAULT_DEEP_LINK_HOSTS: string[] = [
'https://joinzap.com/app/',
];
export const PaystackContext = createContext<{
popup: {
checkout: (params: PaystackParams) => void;
newTransaction: (params: PaystackParams) => void;
resumeTransaction: (params: PaystackParams) => void;
};
} | null>(null);
export const PaystackProvider: React.FC<PaystackProviderProps> = ({
publicKey,
currency,
defaultChannels = ['card'],
deepLinkHosts = [],
debug = false,
children,
onGlobalSuccess,
onGlobalCancel,
}) => {
const [visible, setVisible] = useState(false);
const [params, setParams] = useState<PaystackParams | null>(null);
const [method, setMethod] = useState<TransactionMethod>(TransactionMethod.CHECKOUT);
const fallbackRef = useMemo(() => `ref_${Date.now()}`, []);
const resolvedDeepLinkHosts = useMemo(
() => [...DEFAULT_DEEP_LINK_HOSTS, ...deepLinkHosts],
[deepLinkHosts]
);
const open = useCallback(
(params: PaystackParams, selectedMethod: TransactionMethod) => {
if (debug) console.log(`[Paystack] Opening modal with method: ${selectedMethod}`);
if (!validateParams(params, debug)) return;
setParams(params);
setMethod(selectedMethod);
setVisible(true);
},
[debug]
);
const checkout = (params: PaystackParams) => open(params, TransactionMethod.CHECKOUT);
const newTransaction = (params: PaystackParams) => open(params, TransactionMethod.NEW_TRANSACTION);
const resumeTransaction = (params: PaystackParams) => open(params, TransactionMethod.RESUME_TRANSACTION);
const close = () => {
setVisible(false);
setParams(null);
}
const handleMessage = (event: WebViewMessageEvent) => {
handlePaystackMessage({
event,
debug,
params,
onGlobalSuccess,
onGlobalCancel,
close,
});
};
const paystackHTML = useMemo(() => {
if (!params) return '';
return paystackHtmlContent(
generatePaystackParams({
publicKey,
email: params.email,
amount: params.amount,
reference: params.reference || fallbackRef,
metadata: params.metadata,
...(currency && { currency }),
channels: defaultChannels,
plan: params.plan,
invoice_limit: params.invoice_limit,
subaccount: params.subaccount,
split: params.split,
split_code: params.split_code,
accessCode: params.accessCode,
}),
method
);
}, [params, method]);
if (debug && visible) {
console.log('[Paystack] HTML Injected:', paystackHTML);
}
return (
<PaystackContext.Provider value={{ popup: { checkout, newTransaction, resumeTransaction } }}>
{children}
<Modal visible={visible} transparent animationType="slide">
<SafeAreaView style={styles.container}>
<WebView
originWhitelist={["*"]}
source={{ html: paystackHTML }}
onMessage={handleMessage}
onShouldStartLoadWithRequest={(request) => {
const url = request.url ?? '';
if (!shouldHandleExternally(url, resolvedDeepLinkHosts)) {
return true;
}
if (debug) console.log('[Paystack] Opening external/deep link via OS:', url);
void openExternalUrl(url, debug);
return false;
}}
javaScriptEnabled
domStorageEnabled
startInLoadingState
onLoadStart={() => debug && console.log('[Paystack] WebView Load Start')}
onLoadEnd={() => debug && console.log('[Paystack] WebView Load End')}
renderLoading={() => <ActivityIndicator size="large" />}
/>
</SafeAreaView>
</Modal>
</PaystackContext.Provider>
);
};