Skip to content

Commit

Permalink
Refactor checking payment method availability
Browse files Browse the repository at this point in the history
ISSUE: CS-5483
  • Loading branch information
MarijaIv committed Jun 4, 2024
1 parent b9ff534 commit 9789a75
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions Model/Api/Builders/CreateOrderRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -115,15 +116,23 @@ 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
{
try {
$generalSettings = $generalSettingsResponse->toArray();
$stateService = ServiceRegister::getService(UIStateService::class);
$isOnboarding = StoreContext::doWithStore($this->storeId, [$stateService, 'isOnboardingState'], [true]);
$this->quote = $this->quoteRepository->getActive($this->cartId);
$this->getMerchantId();
if ($isOnboarding) {
$merchantId = $this->getMerchantId();

if (!$merchantId || $isOnboarding) {
return false;
}

Expand Down Expand Up @@ -168,6 +177,9 @@ public function isAllowedFor(GeneralSettingsResponse $generalSettingsResponse):

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;
}
}
Expand Down Expand Up @@ -213,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 @@ -473,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 @@ -490,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 9789a75

Please sign in to comment.