Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 1x 1x 1x 1x 1x 1x 1x 1x | import * as api from '@pay-api/api';
import { useEffect } from 'react';
const CONNECT_URI = 'https://connect.affixapi.com';
// const CONNECT_URI = 'http://localhost:3000'; // for local testing
const DEFAULT_REDIRECT_URI = 'https://affixapi.com';
const CONNECT_IFRAME_ID = 'connect-iframe';
const AUTH_MESSAGE_NAME = 'auth-message';
const noop = () => {}; // eslint-disable-line @typescript-eslint/no-empty-function
export type SdkProps = {
clientId: api.ClientId;
scopes: api.Scopes;
mode: api.Mode;
provider?: api.Provider;
sandbox?: boolean;
state?: string;
onSuccess?: Function; // eslint-disable-line @typescript-eslint/ban-types
onError?: Function; // eslint-disable-line @typescript-eslint/ban-types
onClose?: Function; // eslint-disable-line @typescript-eslint/ban-types
zIndex?: number;
};
export const useConnect = (props: SdkProps) => {
const {
clientId,
scopes = [],
mode = 'retail',
provider,
state,
// provider = 'sandbox',
sandbox = false,
onSuccess = noop,
onError = noop,
onClose = noop,
zIndex = 999, // how much priority to give the modal
} = props;
const _constructAuthUrl = ({
clientId,
scopes,
}: {
clientId: api.ClientId;
scopes: api.Scopes;
}) => {
const authUrl = new URL(CONNECT_URI);
Iif (clientId) authUrl.searchParams.append('client_id', clientId);
authUrl.searchParams.append('scope', scopes.join(' '));
authUrl.searchParams.append('redirect_uri', DEFAULT_REDIRECT_URI);
authUrl.searchParams.append('mode', mode);
authUrl.searchParams.append('app_type', 'spa');
Iif (state) authUrl.searchParams.append('state', state);
Iif (provider) authUrl.searchParams.append('provider', provider);
Iif (sandbox) authUrl.searchParams.append('sandbox', sandbox.toString());
return authUrl.href;
};
const open = (): null | void => {
Iif (document.getElementById(CONNECT_IFRAME_ID)) return null;
const iframe = document.createElement('iframe');
iframe.src = _constructAuthUrl({ clientId, scopes });
iframe.frameBorder = '0';
iframe.id = CONNECT_IFRAME_ID;
iframe.style.position = 'fixed';
iframe.style.zIndex = zIndex.toString();
iframe.style.height = '100%';
iframe.style.width = '100%';
iframe.style.top = '0';
iframe.style.backgroundColor = 'none transparent';
iframe.style.border = 'none';
document.body.prepend(iframe);
document.body.style.overflow = 'hidden';
};
const close = () => {
const frameToRemove = document.getElementById(CONNECT_IFRAME_ID);
Iif (frameToRemove) {
frameToRemove?.parentNode?.removeChild(frameToRemove);
document.body.style.overflow = 'inherit';
}
};
useEffect(() => {
const handleAuth = (event: MessageEvent) => {
const handleAuthSuccess = ({
authorizationCode,
state,
}: {
authorizationCode: string;
state?: string;
}) => onSuccess({ authorizationCode, state });
const handleAuthError = (error: MessageEvent) =>
onError({ errorMessage: error });
const handleAuthClose = () => onClose();
Iif (!event.data) return;
Iif (event.data.name !== AUTH_MESSAGE_NAME) return;
Iif (!event.origin.startsWith(CONNECT_URI)) return;
const { authorizationCode, error, closed, state } = event.data;
close();
Iif (authorizationCode) handleAuthSuccess({ authorizationCode, state });
Iif (error) handleAuthError(error);
Iif (closed) handleAuthClose();
};
window.addEventListener('message', handleAuth);
return () => window.removeEventListener('message', handleAuth);
}, [onClose, onError, onSuccess]);
return {
open,
};
};
|