Skip to content

Commit

Permalink
Merge pull request #35 from sequra/fix/PAR-462
Browse files Browse the repository at this point in the history
Make base url depend on environment
  • Loading branch information
m1k3lm authored Oct 28, 2024
2 parents ded8dce + c65e6ea commit dc06bc9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 43 deletions.
69 changes: 68 additions & 1 deletion Helper/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -26,6 +36,14 @@ class UrlHelper
* @var MagentoBackendUrl
*/
private $backendUrlHelper;
/**
* @var OrderFactory
*/
private $orderFactory;
/**
* @var \Magento\Framework\UrlInterface
*/
private $urlBuilder;

/**
* UrlHelper constructor.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
17 changes: 14 additions & 3 deletions Plugin/OrderDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +25,11 @@ class OrderDetails
*/
protected $urlBuilder;

/**
* @var UrlHelper
*/
protected $urlHelper;

/**
* @var Currency
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
56 changes: 17 additions & 39 deletions Ui/Component/Listing/Column/SequraOrderLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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'])
);
}
}

Expand Down Expand Up @@ -116,27 +118,19 @@ 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(
'<a class="sequra-link" href="' . $this->getButtonLinkUrl($orderReference) . '" target="_blank" onclick="event.stopPropagation()">
'<a class="sequra-link" href="' . $url . '" target="_blank" onclick="event.stopPropagation()">
<button class="sequra-preview">
<img class="sequra-logo" src=' . $imagePath . ' alt="sequra-logo">
' . $this->translationProvider->translate("sequra.viewOnSequra") . '
</button>
</a>');
}

private function getButtonLinkUrl(string $orderReference): string
{
$connectionSettings = $this->getConnectionSettings();
$baseUrl = $connectionSettings && $connectionSettings->getEnvironment() === BaseProxy::LIVE_MODE ?
self::SEQURA_PORTAL_URL : self::SEQURA_PORTAL_SANDBOX_URL;
return $this->urlBuilder->getUrl( $baseUrl . $orderReference );
}

/**
* Returns an instance of Order service.
*
Expand All @@ -150,20 +144,4 @@ private function getOrderService(): OrderService

return $this->orderService;
}

/**
* @return ConnectionData|null
*/
private function getConnectionSettings(): ?ConnectionData
{
return $this->getConnectionService()->getConnectionData();
}

/**
* @return ConnectionService
*/
private function getConnectionService(): ConnectionService
{
return ServiceRegister::getService(ConnectionService::class);
}
}

0 comments on commit dc06bc9

Please sign in to comment.