mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
✨ Implement PayPal connection via popup
This commit is contained in:
parent
88f2ed9185
commit
5f8dda0980
2 changed files with 57 additions and 2 deletions
|
@ -8,6 +8,7 @@ import SettingsToggleBlock from '../../../ReusableComponents/SettingsToggleBlock
|
|||
import Separator from '../../../ReusableComponents/Separator';
|
||||
import DataStoreControl from '../../../ReusableComponents/DataStoreControl';
|
||||
import { CommonHooks } from '../../../../data';
|
||||
import { openPopup } from '../../../../utils/window';
|
||||
|
||||
const AdvancedOptionsForm = ( { setCompleted } ) => {
|
||||
const { isBusy } = CommonHooks.useBusyState();
|
||||
|
@ -87,9 +88,21 @@ const AdvancedOptionsForm = ( { setCompleted } ) => {
|
|||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const connectionUrl = res.data;
|
||||
const popup = openPopup( connectionUrl );
|
||||
|
||||
if ( ! popup ) {
|
||||
createErrorNotice(
|
||||
__(
|
||||
'Popup blocked. Please allow popups for this site to connect to PayPal.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const handleConnect = async () => {
|
||||
const handleManualConnect = async () => {
|
||||
if ( ! handleFormValidation() ) {
|
||||
return;
|
||||
}
|
||||
|
@ -183,7 +196,7 @@ const AdvancedOptionsForm = ( { setCompleted } ) => {
|
|||
onChange={ setClientSecret }
|
||||
type="password"
|
||||
/>
|
||||
<Button variant="secondary" onClick={ handleConnect }>
|
||||
<Button variant="secondary" onClick={ handleManualConnect }>
|
||||
{ __( 'Connect Account', 'woocommerce-paypal-payments' ) }
|
||||
</Button>
|
||||
</SettingsToggleBlock>
|
||||
|
|
42
modules/ppcp-settings/resources/js/utils/window.js
Normal file
42
modules/ppcp-settings/resources/js/utils/window.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Opens the provided URL, preferably in a popup window.
|
||||
*
|
||||
* Popups are usually only supported on desktop devices, when the browser is not in fullscreen mode.
|
||||
*
|
||||
* @param {string} url
|
||||
* @param {Object} options
|
||||
* @param {string} options.name
|
||||
* @param {number} options.width
|
||||
* @param {number} options.height
|
||||
* @param {boolean} options.resizeable
|
||||
* @return {null|Window} Popup window instance, or null.
|
||||
*/
|
||||
export const openPopup = (
|
||||
url,
|
||||
{ name = '_blank', width = 450, height = 720, resizeable = false } = {}
|
||||
) => {
|
||||
width = Math.max( 100, Math.min( window.screen.width - 40, width ) );
|
||||
height = Math.max( 100, Math.min( window.screen.height - 40, height ) );
|
||||
|
||||
const left = ( window.screen.width - width ) / 2;
|
||||
const top = ( window.screen.height - height ) / 2;
|
||||
|
||||
const features = [
|
||||
`width=${ width }`,
|
||||
`height=${ height }`,
|
||||
`left=${ left }`,
|
||||
`top=${ top }`,
|
||||
`resizable=${ resizeable ? 'yes' : 'no' }`,
|
||||
`scrollbars=yes`,
|
||||
`status=no`,
|
||||
];
|
||||
|
||||
const popup = window.open( url, name, features.join( ',' ) );
|
||||
|
||||
if ( popup && ! popup.closed ) {
|
||||
popup.focus();
|
||||
return popup;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue