Skip to content

Commit

Permalink
✨ First steps to implement merchant authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
stracker-phil committed Dec 20, 2024
1 parent 62dda0d commit 69f6cc2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const ButtonOrPlaceholder = ( {
buttonProps.href = href;
buttonProps.target = 'PPFrame';
buttonProps[ 'data-paypal-button' ] = 'true';
buttonProps[ 'data-paypal-onboard-complete' ] = 'onOnboardComplete';
buttonProps[ 'data-paypal-onboard-button' ] = 'true';
}

Expand All @@ -48,18 +47,34 @@ const ConnectionButton = ( {
showIcon = true,
className = '',
} ) => {
const { onboardingUrl, scriptLoaded } =
useHandleOnboardingButton( isSandbox );
const {
onboardingUrl,
scriptLoaded,
setCompleteHandler,
removeCompleteHandler,
} = useHandleOnboardingButton( isSandbox );
const buttonClassName = classNames( 'ppcp-r-connection-button', className, {
'sandbox-mode': isSandbox,
'live-mode': ! isSandbox,
} );
const environment = isSandbox ? 'sandbox' : 'production';

useEffect( () => {
if ( scriptLoaded && onboardingUrl ) {
window.PAYPAL.apps.Signup.render();
setCompleteHandler( environment );
}
}, [ scriptLoaded, onboardingUrl ] );

return () => {
removeCompleteHandler();
};
}, [
scriptLoaded,
onboardingUrl,
environment,
setCompleteHandler,
removeCompleteHandler,
] );

return (
<BusyStateWrapper isBusy={ ! onboardingUrl }>
Expand Down
51 changes: 47 additions & 4 deletions modules/ppcp-settings/resources/js/hooks/useHandleConnections.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import { useState, useEffect, useCallback, useRef } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';

import { CommonHooks, OnboardingHooks } from '../data';

const PAYPAL_PARTNER_SDK_URL =
'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js';

const MESSAGES = {
CONNECTED: __( 'Connected to PayPal', 'woocommerce-paypal-payments' ),
POPUP_BLOCKED: __(
Expand Down Expand Up @@ -41,6 +44,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
const products = OnboardingHooks.useDetermineProducts();
const [ onboardingUrl, setOnboardingUrl ] = useState( '' );
const [ scriptLoaded, setScriptLoaded ] = useState( false );
const timerRef = useRef( null );

useEffect( () => {
const fetchOnboardingUrl = async () => {
Expand Down Expand Up @@ -73,8 +77,7 @@ export const useHandleOnboardingButton = ( isSandbox ) => {

const script = document.createElement( 'script' );
script.id = 'partner-js';
script.src =
'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js';
script.src = PAYPAL_PARTNER_SDK_URL;
script.onload = () => {
setScriptLoaded( true );
};
Expand Down Expand Up @@ -105,7 +108,47 @@ export const useHandleOnboardingButton = ( isSandbox ) => {
};
}, [ onboardingUrl ] );

return { onboardingUrl, scriptLoaded };
const setCompleteHandler = useCallback( ( environment ) => {
const onComplete = ( authCode, shareId ) => {
// TODO -- finish this!
console.log(
`${ environment }-boarding complete - AUTH: `,
authCode
);
console.log(
`${ environment }-boarding complete - SHARE:`,
shareId
);
};

const addHandler = () => {
const MiniBrowser = window.PAYPAL?.apps?.Signup?.MiniBrowser;
if ( ! MiniBrowser || MiniBrowser.onOnboardComplete ) {
return;
}

MiniBrowser.onOnboardComplete = onComplete;
};

// Ensure the onComplete handler is not removed by a PayPal init script.
timerRef.current = setInterval( addHandler, 250 );
}, [] );

const removeCompleteHandler = useCallback( () => {
if ( timerRef.current ) {
clearInterval( timerRef.current );
timerRef.current = null;
}

delete window.PAYPAL?.apps?.Signup?.MiniBrowser?.onOnboardComplete;
}, [] );

return {
onboardingUrl,
scriptLoaded,
setCompleteHandler,
removeCompleteHandler,
};
};

const useConnectionBase = () => {
Expand Down

0 comments on commit 69f6cc2

Please sign in to comment.