diff --git a/Model/Api/Builders/CreateOrderRequestBuilder.php b/Model/Api/Builders/CreateOrderRequestBuilder.php index 9df8d2c..2071a9c 100644 --- a/Model/Api/Builders/CreateOrderRequestBuilder.php +++ b/Model/Api/Builders/CreateOrderRequestBuilder.php @@ -22,8 +22,10 @@ use SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\CreateOrderRequest; use SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\Item\ItemType; use SeQura\Core\BusinessLogic\Domain\UIState\Services\UIStateService; +use SeQura\Core\Infrastructure\Logger\Logger; use SeQura\Core\Infrastructure\ServiceRegister; use Sequra\Core\Services\BusinessLogic\ProductService; +use Throwable; /** * Class CreateOrderRequestBuilder @@ -82,19 +84,18 @@ class CreateOrderRequestBuilder implements \SeQura\Core\BusinessLogic\Domain\Ord private $orderFactory; public function __construct( - CartRepositoryInterface $quoteRepository, + CartRepositoryInterface $quoteRepository, ProductMetadataInterface $productMetadata, - ResourceInterface $moduleResource, - DeploymentConfig $deploymentConfig, - SqlVersionProvider $sqlVersionProvider, - ScopeConfigInterface $scopeConfig, - UrlInterface $urlBuilder, - string $cartId, - string $storeId, - ProductService $productService, - OrderFactory $orderFactory - ) - { + ResourceInterface $moduleResource, + DeploymentConfig $deploymentConfig, + SqlVersionProvider $sqlVersionProvider, + ScopeConfigInterface $scopeConfig, + UrlInterface $urlBuilder, + string $cartId, + string $storeId, + ProductService $productService, + OrderFactory $orderFactory + ) { $this->quoteRepository = $quoteRepository; $this->productMetadata = $productMetadata; $this->moduleResource = $moduleResource; @@ -115,56 +116,72 @@ public function build(): CreateOrderRequest return $this->generateCreateOrderRequest(); } + /** + * Returns true if SeQura payment methods are available for current checkout. Otherwise it returns false. + * + * @param GeneralSettingsResponse $generalSettingsResponse + * + * @return bool + */ public function isAllowedFor(GeneralSettingsResponse $generalSettingsResponse): bool { - $generalSettings = $generalSettingsResponse->toArray(); - $stateService = ServiceRegister::getService(UIStateService::class); - $isOnboarding = StoreContext::doWithStore($this->storeId, [$stateService, 'isOnboardingState'], [true]); - - if ($isOnboarding) { - return false; - } - - if ( - !empty($generalSettings['allowedIPAddresses']) && - !empty($ipAddress = $this->getCustomerIpAddress()) && - !in_array($ipAddress, $generalSettings['allowedIPAddresses'], true) - ) { - return false; - } - - if ( - empty($generalSettings['excludedProducts']) && - empty($generalSettings['excludedCategories']) - ) { - return true; - } - - $this->quote = $this->quoteRepository->getActive($this->cartId); - foreach ($this->quote->getAllVisibleItems() as $item) { - if ( - !empty($generalSettings['excludedProducts']) && - !empty($item->getSku()) && - (in_array($item->getProduct()->getData('sku'), $generalSettings['excludedProducts'], true) || - in_array($item->getProduct()->getSku(), $generalSettings['excludedProducts'], true)) - ) { + try { + $generalSettings = $generalSettingsResponse->toArray(); + $stateService = ServiceRegister::getService(UIStateService::class); + $isOnboarding = StoreContext::doWithStore($this->storeId, [$stateService, 'isOnboardingState'], [true]); + $this->quote = $this->quoteRepository->getActive($this->cartId); + $merchantId = $this->getMerchantId(); + + if (!$merchantId || $isOnboarding) { return false; } - if ($item->getIsVirtual()) { + if ( + !empty($generalSettings['allowedIPAddresses']) && + !empty($ipAddress = $this->getCustomerIpAddress()) && + !in_array($ipAddress, $generalSettings['allowedIPAddresses'], true) + ) { return false; } if ( - !empty($generalSettings['excludedCategories']) && - !empty(array_intersect($generalSettings['excludedCategories'], - $this->productService->getAllProductCategories($item->getProduct()->getCategoryIds()))) + empty($generalSettings['excludedProducts']) && + empty($generalSettings['excludedCategories']) ) { - return false; + return true; } - } - return true; + $this->quote = $this->quoteRepository->getActive($this->cartId); + foreach ($this->quote->getAllVisibleItems() as $item) { + if ( + !empty($generalSettings['excludedProducts']) && + !empty($item->getSku()) && + (in_array($item->getProduct()->getData('sku'), $generalSettings['excludedProducts'], true) || + in_array($item->getProduct()->getSku(), $generalSettings['excludedProducts'], true)) + ) { + return false; + } + + if ($item->getIsVirtual()) { + return false; + } + + if ( + !empty($generalSettings['excludedCategories']) && + !empty(array_intersect($generalSettings['excludedCategories'], + $this->productService->getAllProductCategories($item->getProduct()->getCategoryIds()))) + ) { + return false; + } + } + + return true; + } catch (Throwable $exception) { + Logger::logWarning('Unexpected error occurred while checking if SeQura payment methods are available. + Reason: ' . $exception->getMessage() . ' . Stack trace: ' . $exception->getTraceAsString()); + + return false; + } } private function generateCreateOrderRequest(): CreateOrderRequest @@ -208,7 +225,7 @@ private function getMerchantData(): array } return [ - 'id' => $this->getMerchantId(), + 'id' => (string)$this->getMerchantId(), 'notify_url' => $webhookUrl, 'return_url' => $this->urlBuilder->getUrl('sequra/comeback', ['cartId' => $this->cartId]), 'notification_parameters' => [ @@ -468,14 +485,14 @@ private function getPlatform(): array } /** - * @return string + * @return string|null */ - private function getMerchantId(): string + private function getMerchantId(): ?string { $shippingCountry = $this->quote->getShippingAddress()->getCountryId(); $data = AdminAPI::get()->countryConfiguration($this->storeId)->getCountryConfigurations(); if (!$data->isSuccessful()) { - throw new \RuntimeException('Unable to find merchant configuration for selling country ' . $shippingCountry); + return null; } $merchantId = null; @@ -485,11 +502,7 @@ private function getMerchantId(): string } } - if (!$merchantId) { - throw new \RuntimeException('Unable to find merchant configuration for selling country ' . $shippingCountry); - } - - return (string)$merchantId; + return $merchantId; } private function getSignature(): string