Skip to content

Commit bb1bba6

Browse files
committed
Connect to correct environment when sandbox selected in wizard
1 parent c987486 commit bb1bba6

2 files changed

Lines changed: 59 additions & 38 deletions

File tree

modules/ppcp-settings/resources/js/Components/Screens/Onboarding/Components/ConnectionButton.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { useEffect, useCallback } from '@wordpress/element';
33
import { __ } from '@wordpress/i18n';
44
import classNames from 'classnames';
55
import { OpenSignup } from '@ppcp-settings/Components/ReusableComponents/Icons';
6-
import { useHandleOnboardingButton } from '@ppcp-settings/hooks/useHandleConnections';
6+
import {
7+
useHandleOnboardingButton,
8+
setClickedEnvironment,
9+
} from '@ppcp-settings/hooks/useHandleConnections';
710
import { OnboardingHooks } from '@ppcp-settings/data/onboarding/hooks';
811
import BusyStateWrapper from '@ppcp-settings/Components/ReusableComponents/BusyStateWrapper';
912
import { Notice } from '@ppcp-settings/Components/ReusableComponents/Elements';
@@ -88,7 +91,11 @@ const ConnectionButton = ( {
8891

8992
const handleButtonClick = useCallback( () => {
9093
setConnectionButtonClicked( true );
91-
}, [ setConnectionButtonClicked ] );
94+
95+
// Record which environment the merchant clicked so the shared
96+
// onOnboardComplete handler authenticates against the right account.
97+
setClickedEnvironment( environment );
98+
}, [ setConnectionButtonClicked, environment ] );
9299

93100
// Reset button clicked state when onboardingUrl becomes available.
94101
useEffect( () => {
@@ -100,7 +107,7 @@ const ConnectionButton = ( {
100107
useEffect( () => {
101108
if ( scriptLoaded && onboardingUrl ) {
102109
window.PAYPAL.apps.Signup.render();
103-
setCompleteHandler( environment );
110+
setCompleteHandler();
104111
}
105112

106113
return () => {
@@ -109,7 +116,6 @@ const ConnectionButton = ( {
109116
}, [
110117
scriptLoaded,
111118
onboardingUrl,
112-
environment,
113119
setCompleteHandler,
114120
removeCompleteHandler,
115121
] );

modules/ppcp-settings/resources/js/hooks/useHandleConnections.js

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ const ACTIVITIES = {
3131
API_VERIFY: 'auth/verify-login',
3232
};
3333

34+
/**
35+
* Environment ('sandbox' | 'production') of the connection button the merchant
36+
* actually clicked.
37+
*
38+
* PayPal's partner.js exposes a single global onOnboardComplete callback, but the
39+
* onboarding step mounts a live and a sandbox button at the same time. This
40+
* module-level value lets the shared completion handler authenticate against the
41+
* environment the merchant chose, rather than whichever button registered its
42+
* handler first.
43+
*
44+
* @type {?string}
45+
*/
46+
let clickedEnvironment = null;
47+
48+
export const setClickedEnvironment = ( environment ) => {
49+
clickedEnvironment = environment;
50+
};
51+
3452
export const useHandleOnboardingButton = ( isSandbox ) => {
3553
const { onboardingUrl } = isSandbox
3654
? CommonHooks.useSandbox()
@@ -110,44 +128,41 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
110128
};
111129
}, [ onboardingUrlState ] );
112130

113-
const setCompleteHandler = useCallback(
114-
( environment ) => {
115-
const onComplete = async ( authCode, sharedId ) => {
116-
/**
117-
* Until now, the full page is blocked by PayPal's semi-transparent, black overlay.
118-
* But at this point, the overlay is removed, while we process the sharedId and
119-
* authCode via a REST call.
120-
*
121-
* Note: The REST response is irrelevant, since PayPal will most likely refresh this
122-
* frame before the REST endpoint returns a value. Using "withActivity" is more of a
123-
* visual cue to the user that something is still processing in the background.
124-
*/
125-
startActivity(
126-
ACTIVITIES.OAUTH_VERIFY,
127-
'Validating the connection details'
128-
);
131+
const setCompleteHandler = useCallback( () => {
132+
const onComplete = async ( authCode, sharedId ) => {
133+
/**
134+
* Until now, the full page is blocked by PayPal's semi-transparent, black overlay.
135+
* But at this point, the overlay is removed, while we process the sharedId and
136+
* authCode via a REST call.
137+
*
138+
* Note: The REST response is irrelevant, since PayPal will most likely refresh this
139+
* frame before the REST endpoint returns a value. Using "withActivity" is more of a
140+
* visual cue to the user that something is still processing in the background.
141+
*/
142+
startActivity(
143+
ACTIVITIES.OAUTH_VERIFY,
144+
'Validating the connection details'
145+
);
129146

130-
await authenticateWithOAuth(
131-
sharedId,
132-
authCode,
133-
environment === 'sandbox'
134-
);
135-
};
147+
await authenticateWithOAuth(
148+
sharedId,
149+
authCode,
150+
clickedEnvironment === 'sandbox'
151+
);
152+
};
136153

137-
const addHandler = () => {
138-
const MiniBrowser = window.PAYPAL?.apps?.Signup?.MiniBrowser;
139-
if ( ! MiniBrowser || MiniBrowser.onOnboardComplete ) {
140-
return;
141-
}
154+
const addHandler = () => {
155+
const MiniBrowser = window.PAYPAL?.apps?.Signup?.MiniBrowser;
156+
if ( ! MiniBrowser || MiniBrowser.onOnboardComplete ) {
157+
return;
158+
}
142159

143-
MiniBrowser.onOnboardComplete = onComplete;
144-
};
160+
MiniBrowser.onOnboardComplete = onComplete;
161+
};
145162

146-
// Ensure the onComplete handler is not removed by a PayPal init script.
147-
timerRef.current = setInterval( addHandler, 250 );
148-
},
149-
[ authenticateWithOAuth, startActivity ]
150-
);
163+
// Ensure the onComplete handler is not removed by a PayPal init script.
164+
timerRef.current = setInterval( addHandler, 250 );
165+
}, [ authenticateWithOAuth, startActivity ] );
151166

152167
const removeCompleteHandler = useCallback( () => {
153168
if ( timerRef.current ) {

0 commit comments

Comments
 (0)