Skip to content

Commit

Permalink
Merge pull request #20 from sequra/fix/CS-5483
Browse files Browse the repository at this point in the history
Fix checking payment method availability
  • Loading branch information
m1k3lm authored Jun 6, 2024
2 parents 5f38345 + 9789a75 commit 2b0e612
Showing 1 changed file with 72 additions and 59 deletions.
131 changes: 72 additions & 59 deletions Model/Api/Builders/CreateOrderRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 2b0e612

Please sign in to comment.