diff --git a/Helper/UrlHelper.php b/Helper/UrlHelper.php index d13d4fd..7595a9a 100644 --- a/Helper/UrlHelper.php +++ b/Helper/UrlHelper.php @@ -6,6 +6,13 @@ use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\Url as MagentoUrl; +use Magento\Sales\Model\OrderFactory; +use Magento\Framework\UrlInterface; +use SeQura\Core\BusinessLogic\SeQuraAPI\BaseProxy; +use SeQura\Core\BusinessLogic\Domain\Multistore\StoreContext; +use SeQura\Core\BusinessLogic\Domain\Connection\Services\ConnectionService; +use SeQura\Core\BusinessLogic\Domain\Order\RepositoryContracts\SeQuraOrderRepositoryInterface; +use SeQura\Core\Infrastructure\ServiceRegister; /** * Class UrlHelper @@ -14,6 +21,9 @@ */ class UrlHelper { + public const SEQURA_PORTAL_SANDBOX_URL = 'https://simbox.sequrapi.com/orders/'; + public const SEQURA_PORTAL_URL = 'https://simba.sequra.com/orders/'; + /** * @var StoreManagerInterface */ @@ -26,6 +36,14 @@ class UrlHelper * @var MagentoBackendUrl */ private $backendUrlHelper; + /** + * @var OrderFactory + */ + private $orderFactory; + /** + * @var \Magento\Framework\UrlInterface + */ + private $urlBuilder; /** * UrlHelper constructor. @@ -37,12 +55,16 @@ class UrlHelper public function __construct( StoreManagerInterface $storeManager, MagentoUrl $urlHelper, - MagentoBackendUrl $backendUrlHelper + MagentoBackendUrl $backendUrlHelper, + OrderFactory $orderFactory, + UrlInterface $urlBuilder ) { $this->storeManager = $storeManager; $this->urlHelper = $urlHelper; $this->backendUrlHelper = $backendUrlHelper; + $this->orderFactory = $orderFactory; + $this->urlBuilder = $urlBuilder; } /** @@ -79,4 +101,49 @@ public function getBackendUrl(string $routePath, array $routeParams = null): str { return $this->backendUrlHelper->getUrl($routePath, $routeParams); } + + public function getBackendUrlForSequraOrder(string $orderReference): string + { + $storeId = $this->getOrderStoreId($orderReference); + if (!$storeId) { + return '#'; + } + $connectionSettings = StoreContext::doWithStore( + $storeId, + function () { + return ServiceRegister::getService(ConnectionService::class)->getConnectionData(); + } + ); + $baseUrl = $connectionSettings && $connectionSettings->getEnvironment() === BaseProxy::LIVE_MODE ? + self::SEQURA_PORTAL_URL : self::SEQURA_PORTAL_SANDBOX_URL; + return $this->urlBuilder->getUrl( $baseUrl . $orderReference ); + } + + /** + * Returns the store id by order reference. + * + * @param string $orderReference + * + * @return int|null + */ + private function getOrderStoreId($orderReference): ?int + { + $order = $this->orderFactory->create(); + $seQuraOrder = $this->getOrderRepository()->getByOrderReference($orderReference); + if (!$seQuraOrder) { + return null; + } + $order->loadByIncrementId($seQuraOrder->getOrderRef1()); + return $order ? $order->getStoreId() : null; + } + + /** + * Returns an instance of Order service. + * + * @return SeQuraOrderRepositoryInterface + */ + private function getOrderRepository(): SeQuraOrderRepositoryInterface + { + return ServiceRegister::getService(SeQuraOrderRepositoryInterface::class); + } } diff --git a/Plugin/OrderDetails.php b/Plugin/OrderDetails.php index e646e67..5a8c276 100644 --- a/Plugin/OrderDetails.php +++ b/Plugin/OrderDetails.php @@ -11,7 +11,7 @@ use SeQura\Core\BusinessLogic\Domain\Order\Service\OrderService; use SeQura\Core\Infrastructure\ServiceRegister; use Sequra\Core\Services\BusinessLogic\Utility\SeQuraTranslationProvider; -use Sequra\Core\Ui\Component\Listing\Column\SequraOrderLink; +use Sequra\Core\Helper\UrlHelper; /** * Class OrderDetails @@ -25,6 +25,11 @@ class OrderDetails */ protected $urlBuilder; + /** + * @var UrlHelper + */ + protected $urlHelper; + /** * @var Currency */ @@ -51,11 +56,17 @@ class OrderDetails * @param Currency $currencyModel * @param SeQuraTranslationProvider $translation */ - public function __construct(UrlInterface $urlBuilder, Currency $currencyModel, SeQuraTranslationProvider $translation) + public function __construct( + UrlInterface $urlBuilder, + Currency $currencyModel, + SeQuraTranslationProvider $translation, + UrlHelper $urlHelper + ) { $this->urlBuilder = $urlBuilder; $this->currencyModel = $currencyModel; $this->translation = $translation; + $this->urlHelper = $urlHelper; } /** @@ -93,7 +104,7 @@ private function getPaymentInformationHtml(SeQuraOrder $order): string $paymentAmount = $this->getPaymentAmount($order); $paymentMethodName = $order->getPaymentMethod() ? $order->getPaymentMethod()->getName() : '/'; $paymentMethodIcon = $order->getPaymentMethod() ? $order->getPaymentMethod()->getIcon() ?? '/' : '/'; - $sequraLink = $this->urlBuilder->getUrl(SequraOrderLink::SEQURA_PORTAL_URL . $order->getReference()); + $sequraLink = $this->urlHelper->getBackendUrlForSequraOrder($order->getReference()); $viewOnSeQuraButton = ''; if ($order->getState() === OrderRequestStates::CONFIRMED) { diff --git a/Ui/Component/Listing/Column/SequraOrderLink.php b/Ui/Component/Listing/Column/SequraOrderLink.php index a7db5e3..52f23a6 100644 --- a/Ui/Component/Listing/Column/SequraOrderLink.php +++ b/Ui/Component/Listing/Column/SequraOrderLink.php @@ -6,15 +6,12 @@ use Magento\Ui\Component\Listing\Columns\Column; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; -use Magento\Framework\UrlInterface; -use SeQura\Core\BusinessLogic\Domain\Connection\Models\ConnectionData; -use SeQura\Core\BusinessLogic\Domain\Connection\Services\ConnectionService; use SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\OrderRequestStates; use SeQura\Core\BusinessLogic\Domain\Order\Models\SeQuraOrder; use SeQura\Core\BusinessLogic\Domain\Order\Service\OrderService; -use SeQura\Core\BusinessLogic\SeQuraAPI\BaseProxy; use SeQura\Core\Infrastructure\ServiceRegister; use Sequra\Core\Services\BusinessLogic\Utility\SeQuraTranslationProvider; +use Sequra\Core\Helper\UrlHelper; /** * Class SequraOrderLink @@ -23,17 +20,20 @@ */ class SequraOrderLink extends Column { - protected $assetRepository; - protected $urlBuilder; - protected $translationProvider; - public const SEQURA_PORTAL_SANDBOX_URL = 'https://simbox.sequrapi.com/orders/'; - public const SEQURA_PORTAL_URL = 'https://simba.sequra.com/orders/'; + private $assetRepository; + /** + * @var SeQuraTranslationProvider + */ + private $translationProvider; + /** + * @var UrlHelper + */ + private $urlHelper; /** * @param ContextInterface $context * @param UiComponentFactory $uiComponentFactory * @param Repository $assetRepository - * @param UrlInterface $urlBuilder * @param SeQuraTranslationProvider $translationProvider * @param array $components * @param array $data @@ -42,15 +42,15 @@ public function __construct( ContextInterface $context, UiComponentFactory $uiComponentFactory, Repository $assetRepository, - UrlInterface $urlBuilder, SeQuraTranslationProvider $translationProvider, + UrlHelper $urlHelper, array $components = [], array $data = [] ) { $this->assetRepository = $assetRepository; - $this->urlBuilder = $urlBuilder; $this->translationProvider = $translationProvider; + $this->urlHelper = $urlHelper; parent::__construct($context, $uiComponentFactory, $components, $data); } @@ -83,7 +83,9 @@ public function prepareDataSource(array $dataSource): array if (isset($item['entity_id'], $referenceMap[$item['increment_id']])) { $orderInfo = $referenceMap[$item['increment_id']]; $item[$this->getData('name') . '_reference'] = $orderInfo['ref']; - $orderInfo['isApproved'] && $item[$this->getData('name')] = $this->getButtonLink($orderInfo['ref']); + $orderInfo['isApproved'] && $item[$this->getData('name')] = $this->getButtonLink( + $this->urlHelper->getBackendUrlForSequraOrder($orderInfo['ref']) + ); } } @@ -116,12 +118,12 @@ private function createReferenceMap(array $orders): array * * @return string */ - private function getButtonLink(string $orderReference): string + private function getButtonLink(string $url): string { $imagePath = $this->assetRepository->getUrl('Sequra_Core::images/sequra-logo.png'); return html_entity_decode( - ' + '