woocommerce-paypal-payments/modules/ppcp-settings/resources/js/hooks/useHandleConnections.js
2024-12-05 15:02:02 +01:00

114 lines
2.7 KiB
JavaScript

import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { CommonHooks, OnboardingHooks } from '../data';
import { openPopup } from '../utils/window';
const MESSAGES = {
CONNECTED: __( 'Connected to PayPal', 'woocommerce-paypal-payments' ),
POPUP_BLOCKED: __(
'Popup blocked. Please allow popups for this site to connect to PayPal.',
'woocommerce-paypal-payments'
),
SANDBOX_ERROR: __(
'Could not generate a Sandbox login link.',
'woocommerce-paypal-payments'
),
PRODUCTION_ERROR: __(
'Could not generate a login link.',
'woocommerce-paypal-payments'
),
MANUAL_ERROR: __(
'Could not connect to PayPal. Please make sure your Client ID and Secret Key are correct.',
'woocommerce-paypal-payments'
),
};
const useConnectionBase = () => {
const { setCompleted } = OnboardingHooks.useSteps();
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
return {
handleError: ( res, genericMessage ) => {
console.error( 'Connection error', res );
createErrorNotice( res?.message ?? genericMessage );
},
handleSuccess: async () => {
createSuccessNotice( MESSAGES.CONNECTED );
return setCompleted( true );
},
createErrorNotice,
};
};
export const useSandboxConnection = () => {
const { connectToSandbox, isSandboxMode, setSandboxMode } =
CommonHooks.useSandbox();
const { handleError, createErrorNotice } = useConnectionBase();
const handleSandboxConnect = async () => {
const res = await connectToSandbox();
if ( ! res.success || ! res.data ) {
handleError( res, MESSAGES.SANDBOX_ERROR );
return;
}
const connectionUrl = res.data;
const popup = openPopup( connectionUrl );
if ( ! popup ) {
createErrorNotice( MESSAGES.POPUP_BLOCKED );
}
};
return {
handleSandboxConnect,
isSandboxMode,
setSandboxMode,
};
};
export const useManualConnection = () => {
const {
connectViaIdAndSecret,
isManualConnectionMode,
setManualConnectionMode,
clientId,
setClientId,
clientSecret,
setClientSecret,
} = CommonHooks.useManualConnection();
const { handleError, handleSuccess, createErrorNotice } =
useConnectionBase();
const handleConnectViaIdAndSecret = async ( { validation } = {} ) => {
if ( 'function' === typeof validation ) {
try {
validation();
} catch ( exception ) {
createErrorNotice( exception.message );
return;
}
}
const res = await connectViaIdAndSecret();
if ( res.success ) {
await handleSuccess();
} else {
handleError( res, MESSAGES.MANUAL_ERROR );
}
};
return {
handleConnectViaIdAndSecret,
isManualConnectionMode,
setManualConnectionMode,
clientId,
setClientId,
clientSecret,
setClientSecret,
};
};