From e1c86bad3923b0a744333aefdf660ad66f1ba9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20TISSOT?= Date: Wed, 8 Oct 2025 19:49:02 +0200 Subject: [PATCH 1/2] MAG-565: WIP --- Block/Formjs.php | 24 ++++++++++-------------- Service/GetApiRouteByKey.php | 34 ++++++++++++++++++++++++++++++++++ composer.json | 1 + 3 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 Service/GetApiRouteByKey.php diff --git a/Block/Formjs.php b/Block/Formjs.php index 7fa74a61..3baddd2d 100644 --- a/Block/Formjs.php +++ b/Block/Formjs.php @@ -7,40 +7,36 @@ use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use Magento\Store\Model\ScopeInterface; -use Payplug\Payments\Helper\Config; +use Payplug\Payments\Service\GetApiRouteByKey; class Formjs extends Template { + private const PAYPLUG_SECURE_URL = 'https://secure.payplug.com'; + private const APPLEPAY_SDK_URL = 'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js'; + public function __construct( + private readonly GetApiRouteByKey $getApiRouteByKey, Context $context, - private Config $helper, array $data = [] ) { parent::__construct($context, $data); } - /** - * Get list of external js to include in checkout - * - * @return array - */ public function getJsUrls(): array { $urls = []; if ($this->_scopeConfig->getValue('payment/payplug_payments_apple_pay/active', ScopeInterface::SCOPE_STORE)) { - $urls[] = 'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js'; + $urls[] = self::APPLEPAY_SDK_URL; } return $urls; } - /** - * Get PayPlug js url - * - * @return string - */ public function getPayplugSecureUrl(): string { - return $this->getRequest()->getServer('PAYPLUG_SECURE_URL', 'https://secure.payplug.com'); + return $this->getApiRouteByKey->execute( + 'PAYPLUG_SECURE_URL', + self::PAYPLUG_SECURE_URL + ); } } diff --git a/Service/GetApiRouteByKey.php b/Service/GetApiRouteByKey.php new file mode 100644 index 00000000..b1710efc --- /dev/null +++ b/Service/GetApiRouteByKey.php @@ -0,0 +1,34 @@ +directoryList->getRoot(); + $envPath = $rootPath . DIRECTORY_SEPARATOR . '.env.qa'; + + $dotenv = $this->dotenvFactory->create(); + + try { + $dotenv->usePutenv()->load($envPath); + } catch (Throwable) { + return $fallbackValue; + } + + return getenv($routeKey) ?: $fallbackValue; + } +} diff --git a/composer.json b/composer.json index 4641cc42..a23e8eae 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "magento/module-sales": "102.0.*|103.0.*", "payplug/payplug-php": "^4.0.0", "giggsey/libphonenumber-for-php": "^8.10", + "symfony/dotenv": "*", "ext-openssl": "*" }, "autoload": { From 51570ad95484ce989ea029a4c1d42930bac83b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20TISSOT?= Date: Wed, 29 Oct 2025 16:07:59 +0100 Subject: [PATCH 2/2] MAG-565: Dynamic QA Routes - Squashed --- Block/Formjs.php | 42 -------------- Cron/AutoCaptureDeferredPayments.php | 6 +- Cron/CheckOrderConsistency.php | 18 +++--- Observer/InitEnvQaOnFrontDispatch.php | 22 ++++++++ Plugin/InitEnvQaOnRestDispatch.php | 24 ++++++++ Service/GetApiRouteByKey.php | 34 ------------ Service/InitEnvQa.php | 40 ++++++++++++++ ViewModel/UrlProvider.php | 55 +++++++++++++++++++ etc/adminhtml/system.xml | 7 +++ etc/events.xml | 1 + etc/webapi_rest/di.xml | 6 ++ .../layout/catalog_product_view_applepay.xml | 6 +- view/frontend/layout/checkout_cart_index.xml | 6 +- view/frontend/layout/checkout_index_index.xml | 6 +- .../layout/onestepcheckout_index_index.xml | 6 +- view/frontend/requirejs-config.js | 5 +- view/frontend/templates/checkout/formjs.phtml | 24 ++++++-- 17 files changed, 210 insertions(+), 98 deletions(-) delete mode 100644 Block/Formjs.php create mode 100644 Observer/InitEnvQaOnFrontDispatch.php create mode 100644 Plugin/InitEnvQaOnRestDispatch.php delete mode 100644 Service/GetApiRouteByKey.php create mode 100644 Service/InitEnvQa.php create mode 100644 ViewModel/UrlProvider.php create mode 100644 etc/webapi_rest/di.xml diff --git a/Block/Formjs.php b/Block/Formjs.php deleted file mode 100644 index 3baddd2d..00000000 --- a/Block/Formjs.php +++ /dev/null @@ -1,42 +0,0 @@ -_scopeConfig->getValue('payment/payplug_payments_apple_pay/active', ScopeInterface::SCOPE_STORE)) { - $urls[] = self::APPLEPAY_SDK_URL; - } - - return $urls; - } - - public function getPayplugSecureUrl(): string - { - return $this->getApiRouteByKey->execute( - 'PAYPLUG_SECURE_URL', - self::PAYPLUG_SECURE_URL - ); - } -} diff --git a/Cron/AutoCaptureDeferredPayments.php b/Cron/AutoCaptureDeferredPayments.php index 635314f9..355760d2 100644 --- a/Cron/AutoCaptureDeferredPayments.php +++ b/Cron/AutoCaptureDeferredPayments.php @@ -24,6 +24,7 @@ use Payplug\Payments\Gateway\Config\Standard; use Payplug\Payments\Helper\Config; use Payplug\Payments\Logger\Logger; +use Payplug\Payments\Service\InitEnvQa; class AutoCaptureDeferredPayments { @@ -41,7 +42,8 @@ public function __construct( private readonly Transaction $transaction, private readonly Config $config, private readonly TransportBuilder $transportBuilder, - private readonly OrderIdentity $orderIdentity + private readonly OrderIdentity $orderIdentity, + private readonly InitEnvQa $initEnvQa ) { } @@ -53,6 +55,8 @@ public function execute(): void { $this->logger->info('Running the AutoCaptureDeferredPayments cron'); + $this->initEnvQa->execute(); + // All the invoiceable order will populate this array $orderToInvoiceIds = []; diff --git a/Cron/CheckOrderConsistency.php b/Cron/CheckOrderConsistency.php index f1f08cde..3bb8a777 100644 --- a/Cron/CheckOrderConsistency.php +++ b/Cron/CheckOrderConsistency.php @@ -6,14 +6,15 @@ use Magento\Framework\Api\SearchCriteriaBuilderFactory; use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Sales\Api\Data\OrderPaymentInterface; use Magento\Sales\Api\Data\OrderInterface; +use Magento\Sales\Api\Data\OrderPaymentInterface; use Magento\Sales\Api\OrderPaymentRepositoryInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Store\Model\ScopeInterface; use Payplug\Exception\PayplugException; use Payplug\Payments\Helper\Data; use Payplug\Payments\Logger\Logger; +use Payplug\Payments\Service\InitEnvQa; use Payplug\Resource\Payment as ResourcePayment; class CheckOrderConsistency @@ -24,11 +25,12 @@ class CheckOrderConsistency public const PAST_HOURS_TO_CHECK = 4; public function __construct( - private Logger $logger, - private SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory, - private OrderPaymentRepositoryInterface $paymentRepository, - private OrderRepositoryInterface $orderRepository, - private Data $payplugHelper + private readonly Logger $logger, + private readonly SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory, + private readonly OrderPaymentRepositoryInterface $paymentRepository, + private readonly OrderRepositoryInterface $orderRepository, + private readonly Data $payplugHelper, + private readonly InitEnvQa $initEnvQa ) { } @@ -39,6 +41,8 @@ public function execute(): void { $this->logger->info('Running the CheckOrderConsistency cron'); + $this->initEnvQa->execute(); + $magentoOrdersPayments = $this->getCheckablePayplugOrderPaymentsList(); if (count($magentoOrdersPayments) >= 0) { @@ -86,7 +90,7 @@ public function execute(): void $this->logger->info( sprintf( 'No payplug payment found for the magento order %s.', - $magentoOrder->getEntityId() + $magentoOrder->getEntityId() ) ); } diff --git a/Observer/InitEnvQaOnFrontDispatch.php b/Observer/InitEnvQaOnFrontDispatch.php new file mode 100644 index 00000000..1700fe89 --- /dev/null +++ b/Observer/InitEnvQaOnFrontDispatch.php @@ -0,0 +1,22 @@ +initEnvQa->execute(); + } +} diff --git a/Plugin/InitEnvQaOnRestDispatch.php b/Plugin/InitEnvQaOnRestDispatch.php new file mode 100644 index 00000000..792a00d2 --- /dev/null +++ b/Plugin/InitEnvQaOnRestDispatch.php @@ -0,0 +1,24 @@ +initEnvQa->execute(); + + return [$request]; + } +} diff --git a/Service/GetApiRouteByKey.php b/Service/GetApiRouteByKey.php deleted file mode 100644 index b1710efc..00000000 --- a/Service/GetApiRouteByKey.php +++ /dev/null @@ -1,34 +0,0 @@ -directoryList->getRoot(); - $envPath = $rootPath . DIRECTORY_SEPARATOR . '.env.qa'; - - $dotenv = $this->dotenvFactory->create(); - - try { - $dotenv->usePutenv()->load($envPath); - } catch (Throwable) { - return $fallbackValue; - } - - return getenv($routeKey) ?: $fallbackValue; - } -} diff --git a/Service/InitEnvQa.php b/Service/InitEnvQa.php new file mode 100644 index 00000000..c97deb5b --- /dev/null +++ b/Service/InitEnvQa.php @@ -0,0 +1,40 @@ +scopeConfig->getValue(self::XML_PATH_PAYPLUG_API_URL_KEY); + $serviceUrl = $this->scopeConfig->getValue(self::XML_PATH_PAYPLUG_SERVICE_URL_KEY); + + if ($this->isQaEnabled() === false || empty($apiUrl) || empty($serviceUrl)) { + return false; + } + + BaseAPIRoutes::setApiBaseUrl($apiUrl); + BaseAPIRoutes::setServiceBaseUrl($serviceUrl); + + return true; + } + + public function isQaEnabled(): bool + { + return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLE_QA); + } +} diff --git a/ViewModel/UrlProvider.php b/ViewModel/UrlProvider.php new file mode 100644 index 00000000..920d601a --- /dev/null +++ b/ViewModel/UrlProvider.php @@ -0,0 +1,55 @@ +scopeConfig->getValue(self::XML_PATH_PAYPLUG_SECURE_URL_KEY); + + if ($this->initEnvQa->isQaEnabled() === true && $customSecureUrl) { + return $customSecureUrl; + } + + return self::DEFAULT_SECURE_URL; + } + + public function getPayplugIntegratedPaymentJsUrl(): string + { + $customIntegratedPaymentJsUrl = $this->scopeConfig->getValue(self::XML_PATH_PAYPLUG_INTEGRATED_PAYMENT_JS_URL_KEY); + + if ($this->initEnvQa->isQaEnabled() === true && $customIntegratedPaymentJsUrl) { + return $customIntegratedPaymentJsUrl; + } + + return self::DEFAULT_INTEGRATED_PAYMENT_JS_URL; + } + + public function isApplePayEnabled(): bool + { + return $this->scopeConfig->isSetFlag('payment/payplug_payments_apple_pay/active', ScopeInterface::SCOPE_STORE); + } + + public function getApplePaySdkUrl(): string + { + return self::APPLEPAY_SDK_URL; + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index d79f738b..b7e914b7 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -78,6 +78,13 @@ + + + + + + +
diff --git a/etc/events.xml b/etc/events.xml index e143b657..92bc028e 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -4,6 +4,7 @@ + diff --git a/etc/webapi_rest/di.xml b/etc/webapi_rest/di.xml new file mode 100644 index 00000000..f9fe5493 --- /dev/null +++ b/etc/webapi_rest/di.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/view/frontend/layout/catalog_product_view_applepay.xml b/view/frontend/layout/catalog_product_view_applepay.xml index d78aa717..dfd5a2e8 100644 --- a/view/frontend/layout/catalog_product_view_applepay.xml +++ b/view/frontend/layout/catalog_product_view_applepay.xml @@ -3,7 +3,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> - + + + Payplug\Payments\ViewModel\UrlProvider + + diff --git a/view/frontend/layout/checkout_cart_index.xml b/view/frontend/layout/checkout_cart_index.xml index b194391a..960e345a 100644 --- a/view/frontend/layout/checkout_cart_index.xml +++ b/view/frontend/layout/checkout_cart_index.xml @@ -2,7 +2,11 @@ - + + + Payplug\Payments\ViewModel\UrlProvider + + diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index 41c27027..fabd957f 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -2,7 +2,11 @@ - + + + Payplug\Payments\ViewModel\UrlProvider + + diff --git a/view/frontend/layout/onestepcheckout_index_index.xml b/view/frontend/layout/onestepcheckout_index_index.xml index a5617101..89579f86 100644 --- a/view/frontend/layout/onestepcheckout_index_index.xml +++ b/view/frontend/layout/onestepcheckout_index_index.xml @@ -2,7 +2,11 @@ - + + + Payplug\Payments\ViewModel\UrlProvider + + diff --git a/view/frontend/requirejs-config.js b/view/frontend/requirejs-config.js index d44cd211..59b987d9 100644 --- a/view/frontend/requirejs-config.js +++ b/view/frontend/requirejs-config.js @@ -3,8 +3,5 @@ const config = { '*': { 'oneyPopin': 'Payplug_Payments/js/view/oney/popin' } - }, - paths: { - 'payplugIntegrated': 'https://cdn.payplug.com/js/integrated-payment/v1/index' } -}; \ No newline at end of file +}; diff --git a/view/frontend/templates/checkout/formjs.phtml b/view/frontend/templates/checkout/formjs.phtml index 0d2418d3..36e3ae7e 100644 --- a/view/frontend/templates/checkout/formjs.phtml +++ b/view/frontend/templates/checkout/formjs.phtml @@ -3,17 +3,29 @@ declare(strict_types=1); use Magento\Framework\Escaper; +use Magento\Framework\View\Element\Template; use Magento\Framework\View\Helper\SecureHtmlRenderer; -use Payplug\Payments\Block\Formjs; +use Payplug\Payments\ViewModel\UrlProvider; /** * @var Escaper $escaper - * @var Formjs $block + * @var Template $block * @var SecureHtmlRenderer $secureRenderer */ + +/** @var UrlProvider $urlProvider */ +$urlProvider = $block->getData('urlProviderViewModel'); +?> +isApplePayEnabled()) : ?> + renderTag('script', ['src' => $escaper->escapeUrl($urlProvider->getApplePaySdkUrl())], '', false); ?> + +escapeUrl($urlProvider->getPayplugSecureUrl())}'; +require.config({ + paths: { + payplugIntegrated: '{$escaper->escapeUrl($urlProvider->getPayplugIntegratedPaymentJsUrl())}' + } +}); +JS; ?> -getJsUrls() as $url): ?> - renderTag('script', ['src' => $escaper->escapeUrl($url)], '', false); ?> - -escapeUrl($block->getPayplugSecureUrl()) . '\';' ?> renderTag('script', [], $scriptContent, false); ?>