File size: 3,818 Bytes
1e92f2d | 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 | import { PayPalScriptProvider, ReactPayPalScriptOptions } from '@paypal/react-paypal-js';
import { useI18n } from '@wordpress/react-i18n';
import { useEffect, useState, createContext, PropsWithChildren, useContext, useRef } from 'react';
export interface PayPalConfigurationApiResponse {
client_id: string | undefined;
}
export interface PayPalConfiguration {
clientId: string | undefined;
}
export interface UsePayPalConfiguration {
payPalConfiguration: PayPalConfiguration | undefined;
}
const PayPalContext = createContext< PayPalConfiguration | undefined >( undefined );
const defaultConfiguration: PayPalConfiguration = {
clientId: undefined,
};
function usePayPalConfigurationInternalOnly( {
fetchPayPalConfiguration,
}: {
fetchPayPalConfiguration: () => Promise< PayPalConfigurationApiResponse >;
} ): {
payPalConfiguration: PayPalConfiguration | undefined;
error: undefined | Error;
} {
const [ configurationError, setConfigurationError ] = useState< undefined | Error >();
const [ payPalConfiguration, setConfiguration ] = useState< undefined | PayPalConfiguration >(
defaultConfiguration
);
useEffect( () => {
let isSubscribed = true;
fetchPayPalConfiguration()
.then( ( configuration ) => {
if ( ! isSubscribed ) {
return;
}
if ( ! configuration.client_id ) {
throw new Error(
'Error loading PayPal configuration. Received invalid data from the server.'
);
}
setConfiguration( { clientId: configuration.client_id } );
} )
.catch( ( error ) => {
setConfigurationError( error );
} );
return () => {
isSubscribed = false;
};
}, [ fetchPayPalConfiguration ] );
return { payPalConfiguration, error: configurationError };
}
export function usePayPalConfiguration(): UsePayPalConfiguration {
const payPalConfiguration = useContext( PayPalContext );
if ( ! payPalConfiguration ) {
throw new Error( 'usePayPalConfiguration can only be used inside a PayPalProvider' );
}
return { payPalConfiguration };
}
export function PayPalProvider( {
children,
currency,
handleError,
fetchPayPalConfiguration,
}: PropsWithChildren< {
currency: string;
handleError?: ( error: Error ) => void;
fetchPayPalConfiguration: () => Promise< PayPalConfigurationApiResponse >;
} > ) {
const { payPalConfiguration, error } = usePayPalConfigurationInternalOnly( {
fetchPayPalConfiguration,
} );
const lastError = useRef< Error | undefined >();
useEffect( () => {
if ( ! error || lastError.current === error ) {
return;
}
lastError.current = error;
const errorWithCause = new Error(
`Error fetching PayPal configuration: ${ error?.message ?? error }`,
{
cause: error,
}
);
// eslint-disable-next-line no-console
console.error( errorWithCause );
if ( handleError ) {
handleError( errorWithCause );
}
}, [ error, handleError ] );
const payPalScriptOptions: ReactPayPalScriptOptions = {
clientId: payPalConfiguration?.clientId ?? 'loading-client-id',
components: 'buttons',
currency,
commit: true,
intent: 'capture',
vault: true,
};
const isConfigurationLoaded = payPalConfiguration?.clientId ? true : false;
const { __ } = useI18n();
// Even though `PayPalScriptProvider` has a `deferLoading` option, it still
// requires the `options` prop to include a `clientId`, and it appears that
// the ID you provide is cached for the lifetime of the provider, even if
// it later changes. Therefore, we have to avoid loading the
// `PayPalScriptProvider` at all until we have the correct client ID.
if ( ! isConfigurationLoaded ) {
return <div>{ __( 'Loading…' ) }</div>;
}
return (
<PayPalScriptProvider options={ payPalScriptOptions }>
<PayPalContext.Provider value={ payPalConfiguration }>{ children }</PayPalContext.Provider>
</PayPalScriptProvider>
);
}
|