-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpaystack.js
42 lines (35 loc) · 1.16 KB
/
paystack.js
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
const PAYSTACK_URL = 'https://js.paystack.co/v1/inline.js';
const paystackLoaded = () => window.PaystackPop && typeof window.PaystackPop.setup === 'function';
let globalConfig = {};
export const setGlobalConfig = (obj) => {
globalConfig = { ...globalConfig, ...obj };
};
const Paystack = () => {
let instanceOptions = {};
const init = ({ timeout = 10000 } = {}) => new Promise((resolve, reject) => {
if (paystackLoaded()) resolve();
const rejectPromise = setTimeout(reject, timeout, new Error('Could not fetch Paystack script'));
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
document.body.appendChild(script);
script.onload = () => {
clearTimeout(rejectPromise);
resolve();
};
script.src = PAYSTACK_URL;
});
const addOptions = (options) => {
instanceOptions = { ...globalConfig, ...instanceOptions, ...options };
};
const pay = async () => {
await init();
const instance = window.PaystackPop.setup(instanceOptions);
if (!instanceOptions.container) instance.openIframe();
};
return {
init,
addOptions,
pay,
};
};
export default Paystack;