@@ -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+
3452export 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