Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checking payment method availability #20

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading