mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Retrieve current script url params (client id, ...) in editor
This commit is contained in:
parent
2ecbf089f5
commit
7e8881dc9d
5 changed files with 80 additions and 8 deletions
|
@ -43,6 +43,6 @@
|
||||||
"html": false
|
"html": false
|
||||||
},
|
},
|
||||||
"textdomain": "woocommerce-paypal-payments",
|
"textdomain": "woocommerce-paypal-payments",
|
||||||
"editorScript": "file:./assets/js/paylater-block.js",
|
"editorScript": "ppcp-paylater-block",
|
||||||
"editorStyle": "file:./assets/css/edit.css"
|
"editorStyle": "file:./assets/css/edit.css"
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { useState, useEffect } from '@wordpress/element';
|
||||||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
|
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
|
||||||
import { PanelBody, SelectControl, Spinner } from '@wordpress/components';
|
import { PanelBody, SelectControl, Spinner } from '@wordpress/components';
|
||||||
import { PayPalScriptProvider, PayPalMessages } from "@paypal/react-paypal-js";
|
import { PayPalScriptProvider, PayPalMessages } from "@paypal/react-paypal-js";
|
||||||
|
import { useScriptParams } from "./hooks/script-params";
|
||||||
|
|
||||||
export default function Edit( { attributes, clientId, setAttributes } ) {
|
export default function Edit( { attributes, clientId, setAttributes } ) {
|
||||||
const { layout, logo, position, color, flexColor, flexRatio, placement, id } = attributes;
|
const { layout, logo, position, color, flexColor, flexRatio, placement, id } = attributes;
|
||||||
|
@ -23,12 +24,29 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const props = useBlockProps({className: ['ppcp-paylater-block-preview', 'ppcp-overlay-parent']});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
setAttributes({id: 'ppcp-' + clientId});
|
setAttributes({id: 'ppcp-' + clientId});
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const scriptParams = useScriptParams(PcpPayLaterBlock.ajax.cart_script_params);
|
||||||
|
if (scriptParams === null) {
|
||||||
|
return (<div {...props}><Spinner/></div>)
|
||||||
|
}
|
||||||
|
|
||||||
|
const urlParams = scriptParams === false ? {
|
||||||
|
clientId: 'test',
|
||||||
|
components: 'messages',
|
||||||
|
} : {
|
||||||
|
...scriptParams.url_params,
|
||||||
|
...{
|
||||||
|
components: 'messages',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<InspectorControls>
|
<InspectorControls>
|
||||||
|
@ -116,13 +134,10 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
|
||||||
/>
|
/>
|
||||||
</PanelBody>
|
</PanelBody>
|
||||||
</InspectorControls>
|
</InspectorControls>
|
||||||
<div {...useBlockProps({className: ['ppcp-paylater-block-preview', 'ppcp-overlay-parent']})}>
|
<div {...props}>
|
||||||
<div className={'ppcp-overlay-child'}>
|
<div className={'ppcp-overlay-child'}>
|
||||||
<PayPalScriptProvider
|
<PayPalScriptProvider
|
||||||
options={{
|
options={urlParams}
|
||||||
clientId: "test",
|
|
||||||
components: "messages",
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<PayPalMessages
|
<PayPalMessages
|
||||||
style={previewStyle}
|
style={previewStyle}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
import { useState, useEffect } from '@wordpress/element';
|
||||||
|
|
||||||
|
export const useScriptParams = (requestConfig) => {
|
||||||
|
const [data, setData] = useState(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(requestConfig.endpoint);
|
||||||
|
const json = await response.json();
|
||||||
|
if (json.success && json?.data?.url_params) {
|
||||||
|
setData(json.data);
|
||||||
|
} else {
|
||||||
|
setData(false);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setData(false);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, [requestConfig]);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
|
@ -11,4 +11,16 @@ namespace WooCommerce\PayPalCommerce\PayLaterBlock;
|
||||||
|
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
return array();
|
return array(
|
||||||
|
'paylater-block.url' => static function ( ContainerInterface $container ): string {
|
||||||
|
/**
|
||||||
|
* Cannot return false for this path.
|
||||||
|
*
|
||||||
|
* @psalm-suppress PossiblyFalseArgument
|
||||||
|
*/
|
||||||
|
return plugins_url(
|
||||||
|
'/modules/ppcp-paylater-block/',
|
||||||
|
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace WooCommerce\PayPalCommerce\PayLaterBlock;
|
namespace WooCommerce\PayPalCommerce\PayLaterBlock;
|
||||||
|
|
||||||
|
use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
||||||
|
@ -34,7 +35,27 @@ class PayLaterBlockModule implements ModuleInterface {
|
||||||
public function run( ContainerInterface $c ): void {
|
public function run( ContainerInterface $c ): void {
|
||||||
add_action(
|
add_action(
|
||||||
'init',
|
'init',
|
||||||
function (): void {
|
function () use ( $c ): void {
|
||||||
|
$script_handle = 'ppcp-paylater-block';
|
||||||
|
wp_register_script(
|
||||||
|
$script_handle,
|
||||||
|
$c->get( 'paylater-block.url' ) . '/assets/js/paylater-block.js',
|
||||||
|
array(),
|
||||||
|
$c->get( 'ppcp.asset-version' ),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
wp_localize_script(
|
||||||
|
$script_handle,
|
||||||
|
'PcpPayLaterBlock',
|
||||||
|
array(
|
||||||
|
'ajax' => array(
|
||||||
|
'cart_script_params' => array(
|
||||||
|
'endpoint' => \WC_AJAX::get_endpoint( CartScriptParamsEndpoint::ENDPOINT ),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cannot return false for this path.
|
* Cannot return false for this path.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue