This commit is contained in:
Narek Zakarian 2025-03-11 11:28:15 +04:00
parent 090a140e7e
commit 9b12abbd59
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
2 changed files with 24 additions and 2 deletions

View file

@ -53,6 +53,11 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
}, [ isSandbox, products, options, onboardingUrl ] );
useEffect( () => {
/**
* The partner.js script initializes all onboarding buttons in the onload event.
* When no buttons are present, a JS error is displayed; i.e. we should load this script
* only when the button is ready (with a valid href and data-attributes).
*/
if ( ! onboardingUrlState ) {
return;
}
@ -66,6 +71,14 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
document.body.appendChild( script );
return () => {
/**
* When the component is unmounted, remove the partner.js script, as well as the
* dynamic scripts it loaded (signup-js and rampConfig-js)
*
* This is important, as the onboarding button is only initialized during the onload
* event of those scripts; i.e. we need to load the scripts again, when the button is
* rendered again.
*/
const onboardingScripts = [
'partner-js',
'signup-js',
@ -85,6 +98,15 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
const setCompleteHandler = useCallback(
( environment ) => {
const onComplete = async ( authCode, sharedId ) => {
/**
* Until now, the full page is blocked by PayPal's semi-transparent, black overlay.
* But at this point, the overlay is removed, while we process the sharedId and
* authCode via a REST call.
*
* Note: The REST response is irrelevant, since PayPal will most likely refresh this
* frame before the REST endpoint returns a value. Using "withActivity" is more of a
* visual cue to the user that something is still processing in the background.
*/
startActivity(
ACTIVITIES.OAUTH_VERIFY,
'Validating the connection details'
@ -106,6 +128,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
MiniBrowser.onOnboardComplete = onComplete;
};
// Ensure the onComplete handler is not removed by a PayPal init script.
timerRef.current = setInterval( addHandler, 250 );
},
[ authenticateWithOAuth, startActivity ]