diff --git a/modules.local/ppcp-button/services.php b/modules.local/ppcp-button/services.php index 75d18a3aa..43104aebf 100644 --- a/modules.local/ppcp-button/services.php +++ b/modules.local/ppcp-button/services.php @@ -52,6 +52,7 @@ return [ $payeeRepository = $container->get('api.repository.payee'); $identityToken = $container->get('api.endpoint.identity-token'); $payerFactory = $container->get('api.factory.payer'); + $requestData = $container->get('button.request-data'); $clientId = $container->get('button.client_id'); return new SmartButton( @@ -61,7 +62,8 @@ return [ $payeeRepository, $identityToken, $payerFactory, - $clientId + $clientId, + $requestData ); }, 'button.url' => static function (ContainerInterface $container): string { diff --git a/modules.local/ppcp-button/src/Assets/SmartButton.php b/modules.local/ppcp-button/src/Assets/SmartButton.php index c9f1d1720..98cc327c8 100644 --- a/modules.local/ppcp-button/src/Assets/SmartButton.php +++ b/modules.local/ppcp-button/src/Assets/SmartButton.php @@ -11,6 +11,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository; use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint; use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint; use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint; +use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData; use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings; @@ -23,6 +24,7 @@ class SmartButton implements SmartButtonInterface private $identityToken; private $payerFactory; private $clientId; + private $requestData; public function __construct( string $moduleUrl, @@ -31,7 +33,8 @@ class SmartButton implements SmartButtonInterface PayeeRepository $payeeRepository, IdentityToken $identityToken, PayerFactory $payerFactory, - string $clientId + string $clientId, + RequestData $requestData ) { $this->moduleUrl = $moduleUrl; @@ -41,6 +44,7 @@ class SmartButton implements SmartButtonInterface $this->identityToken = $identityToken; $this->payerFactory = $payerFactory; $this->clientId = $clientId; + $this->requestData = $requestData; } // phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong @@ -201,6 +205,7 @@ class SmartButton implements SmartButtonInterface private function localizeScript(): array { + $this->requestData->enqueueNonceFix(); $localize = [ 'script_attributes' => $this->attributes(), 'redirect' => wc_get_checkout_url(), @@ -242,6 +247,8 @@ class SmartButton implements SmartButtonInterface ], ], ]; + + $this->requestData->dequeueNonceFix(); return $localize; } diff --git a/modules.local/ppcp-button/src/Endpoint/RequestData.php b/modules.local/ppcp-button/src/Endpoint/RequestData.php index 2ad271716..4e0b38dea 100644 --- a/modules.local/ppcp-button/src/Endpoint/RequestData.php +++ b/modules.local/ppcp-button/src/Endpoint/RequestData.php @@ -9,11 +9,21 @@ use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException; class RequestData { + public function enqueueNonceFix() + { + add_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100); + } + + public function dequeueNonceFix() + { + remove_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100); + } + public function readRequest(string $nonce): array { $stream = file_get_contents('php://input'); $json = json_decode($stream, true); - add_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100); + $this->enqueueNonceFix(); if ( ! isset($json['nonce']) || !wp_verify_nonce($json['nonce'], $nonce) @@ -23,7 +33,7 @@ class RequestData __('Could not validate nonce.', 'woocommerce-paypal-commerce-gateway') ); } - remove_filter('nonce_user_logged_out', [$this, 'nonceFix'], 100); + $this->dequeueNonceFix(); return $this->sanitize($json); }