Merge pull request #3198 from woocommerce/PCP-4294-isu-incorrect-business-subscriptions-with-acdc-and-no-cards-flows

Use single onboarding url (4294)
This commit is contained in:
Emili Castells 2025-03-11 09:44:37 +01:00 committed by GitHub
commit 01bfbe708f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 47 deletions

View file

@ -41,45 +41,25 @@ export function refresh() {
}
/**
* Side effect. Fetches the ISU-login URL for a sandbox account.
*
* @return {Function} The thunk function.
*/
export function sandboxOnboardingUrl() {
return async () => {
try {
return apiFetch( {
path: REST_CONNECTION_URL_PATH,
method: 'POST',
data: {
useSandbox: true,
products: [ 'EXPRESS_CHECKOUT' ],
},
} );
} catch ( e ) {
return {
success: false,
error: e,
};
}
};
}
/**
* Side effect. Fetches the ISU-login URL for a production account.
* Side effect. Fetches the ISU-login URL for an account.
*
* @param {string[]} [products=[]] Which products/features to display in the ISU popup.
* @param {Object} [options={}] Options to customize the onboarding workflow.
* @param isSandbox True if is sandbox, otherwise false.
* @return {Function} The thunk function.
*/
export function productionOnboardingUrl( products = [], options = {} ) {
export function onboardingUrl(
products = [],
options = {},
isSandbox = false
) {
return async () => {
try {
return apiFetch( {
path: REST_CONNECTION_URL_PATH,
method: 'POST',
data: {
useSandbox: false,
useSandbox: isSandbox,
products,
options,
},

View file

@ -98,23 +98,23 @@ export const useStore = () => {
export const useSandbox = () => {
const { dispatch, usePersistent } = useStoreData();
const [ isSandboxMode, setSandboxMode ] = usePersistent( 'useSandbox' );
const { sandboxOnboardingUrl, persist } = dispatch;
const { onboardingUrl } = dispatch;
return {
isSandboxMode,
setSandboxMode: ( state ) => {
setSandboxMode( state );
return persist();
return dispatch.persist();
},
sandboxOnboardingUrl,
onboardingUrl,
};
};
export const useProduction = () => {
const { dispatch } = useStoreData();
const { productionOnboardingUrl } = dispatch;
const { onboardingUrl } = dispatch;
return { productionOnboardingUrl };
return { onboardingUrl };
};
export const useAuthentication = () => {

View file

@ -28,23 +28,19 @@ const ACTIVITIES = {
};
export const useHandleOnboardingButton = ( isSandbox ) => {
const { sandboxOnboardingUrl } = CommonHooks.useSandbox();
const { productionOnboardingUrl } = CommonHooks.useProduction();
const { onboardingUrl } = isSandbox
? CommonHooks.useSandbox()
: CommonHooks.useProduction();
const { products, options } = OnboardingHooks.useDetermineProducts();
const { startActivity } = CommonHooks.useBusyState();
const { authenticateWithOAuth } = CommonHooks.useAuthentication();
const [ onboardingUrl, setOnboardingUrl ] = useState( '' );
const [ onboardingUrlState, setOnboardingUrl ] = useState( '' );
const [ scriptLoaded, setScriptLoaded ] = useState( false );
const timerRef = useRef( null );
useEffect( () => {
const fetchOnboardingUrl = async () => {
let res;
if ( isSandbox ) {
res = await sandboxOnboardingUrl();
} else {
res = await productionOnboardingUrl( products, options );
}
const res = await onboardingUrl( products, options, isSandbox );
if ( res.success && res.data ) {
setOnboardingUrl( res.data );
@ -54,7 +50,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
};
fetchOnboardingUrl();
}, [ isSandbox, productionOnboardingUrl, products, sandboxOnboardingUrl ] );
}, [ isSandbox, products, options, onboardingUrl ] );
useEffect( () => {
/**
@ -62,7 +58,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
* 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 ( ! onboardingUrl ) {
if ( ! onboardingUrlState ) {
return;
}
@ -97,7 +93,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
}
} );
};
}, [ onboardingUrl ] );
}, [ onboardingUrlState ] );
const setCompleteHandler = useCallback(
( environment ) => {
@ -119,7 +115,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
await authenticateWithOAuth(
sharedId,
authCode,
'sandbox' === environment
environment === 'sandbox'
);
};
@ -148,7 +144,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
}, [] );
return {
onboardingUrl,
onboardingUrl: onboardingUrlState,
scriptLoaded,
setCompleteHandler,
removeCompleteHandler,