diff --git a/Controller/Adminhtml/Configuration/PaymentMethods.php b/Controller/Adminhtml/Configuration/PaymentMethods.php index 3c65b9c..d826e21 100644 --- a/Controller/Adminhtml/Configuration/PaymentMethods.php +++ b/Controller/Adminhtml/Configuration/PaymentMethods.php @@ -6,6 +6,14 @@ use Magento\Framework\Controller\Result\Json; use Magento\Framework\Controller\Result\JsonFactory; use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI; +use SeQura\Core\BusinessLogic\AdminAPI\PaymentMethods\Responses\PaymentMethodsResponse; +use Sequra\Core\DataAccess\Entities\PaymentMethod; +use Sequra\Core\DataAccess\Entities\PaymentMethods as PaymentMethodsEntity; +use SeQura\Core\Infrastructure\Http\Exceptions\HttpRequestException; +use SeQura\Core\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException; +use SeQura\Core\Infrastructure\ORM\QueryFilter\Operators; +use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryFilter; +use SeQura\Core\Infrastructure\ORM\RepositoryRegistry; /** * Class PaymentMethods @@ -31,12 +39,52 @@ public function __construct(Context $context, JsonFactory $jsonFactory) * Returns active connection data. * * @return Json + * + * @throws HttpRequestException + * @throws RepositoryNotRegisteredException + * @throws \Exception */ protected function getPaymentMethods(): Json { $data = AdminAPI::get()->paymentMethods($this->storeId)->getPaymentMethods($this->identifier); + $this->savePaymentMethods($data); + $this->addResponseCode($data); return $this->result->setData($data->toArray()); } + + /** + * Saves payment methods to the integration database. + * + * @param PaymentMethodsResponse $data + * + * @return void + * + * @throws RepositoryNotRegisteredException + */ + private function savePaymentMethods(PaymentMethodsResponse $data): void + { + $apiPaymentMethods = PaymentMethod::fromBatch($data->toArray()); + + $paymentMethodsRepository = RepositoryRegistry::getRepository(PaymentMethodsEntity::CLASS_NAME); + + $filter = new QueryFilter(); + $filter->where('storeId', Operators::EQUALS, $this->storeId) + ->where('merchantId', Operators::EQUALS, $this->identifier); + $paymentMethods = $paymentMethodsRepository->selectOne($filter); + + if ($paymentMethods === null) { + $paymentMethods = new PaymentMethodsEntity(); + + $paymentMethods->setStoreId($this->storeId); + $paymentMethods->setMerchantId($this->identifier); + $paymentMethods->setPaymentMethods($apiPaymentMethods); + + $paymentMethodsRepository->save($paymentMethods); + } else { + $paymentMethods->setPaymentMethods($apiPaymentMethods); + $paymentMethodsRepository->update($paymentMethods); + } + } } diff --git a/DataAccess/Entities/PaymentMethod.php b/DataAccess/Entities/PaymentMethod.php new file mode 100644 index 0000000..6914dfa --- /dev/null +++ b/DataAccess/Entities/PaymentMethod.php @@ -0,0 +1,329 @@ +addStringIndex('product'); + + return new EntityConfiguration($indexMap, 'PaymentMethod'); + } + + /** + * @inheritDoc + */ + public function inflate(array $data) + { + parent::inflate($data); + + $this->product = $data['product']; + $this->title = $data['title']; + $this->longTitle = $data['longTitle']; + $this->startsAt = $data['startsAt']; + $this->endsAt = $data['endsAt']; + $this->campaign = $data['campaign']; + $this->claim = $data['claim']; + $this->description = $data['description']; + $this->icon = $data['icon']; + $this->costDescription = $data['costDescription']; + $this->minAmount = $data['minAmount']; + $this->maxAmount = $data['maxAmount']; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + $data = parent::toArray(); + + $data['product'] = $this->product; + $data['title'] = $this->title; + $data['longTitle'] = $this->longTitle; + $data['startsAt'] = $this->startsAt; + $data['endsAt'] = $this->endsAt; + $data['campaign'] = $this->campaign; + $data['claim'] = $this->claim; + $data['description'] = $this->description; + $data['icon'] = $this->icon; + $data['costDescription'] = $this->costDescription; + $data['minAmount'] = $this->minAmount; + $data['maxAmount'] = $this->maxAmount; + + return $data; + } + + /** + * @return string|null + */ + public function getProduct(): ?string + { + return $this->product; + } + + /** + * @param string $product + * @return void + */ + public function setProduct(string $product): void + { + $this->product = $product; + } + + /** + * @return string|null + */ + public function getTitle(): ?string + { + return $this->title; + } + + /** + * @param string $title + * @return void + */ + public function setTitle(string $title): void + { + $this->title = $title; + } + + /** + * @return string|null + */ + public function getLongTitle(): ?string + { + return $this->longTitle; + } + + /** + * @param string $longTitle + * @return void + */ + public function setLongTitle(string $longTitle): void + { + $this->longTitle = $longTitle; + } + + /** + * @return string|null + */ + public function getStartsAt(): ?string + { + return $this->startsAt; + } + + /** + * @param string $startsAt + * @return void + */ + public function setStartsAt(string $startsAt): void + { + $this->startsAt = $startsAt; + } + + /** + * @return string|null + */ + public function getEndsAt(): ?string + { + return $this->endsAt; + } + + /** + * @param string $endsAt + * @return void + */ + public function setEndsAt(string $endsAt): void + { + $this->endsAt = $endsAt; + } + + /** + * @return string|null + */ + public function getCampaign(): ?string + { + return $this->campaign; + } + + /** + * @param string $campaign + * @return void + */ + public function setCampaign(string $campaign): void + { + $this->campaign = $campaign; + } + + /** + * @return string|null + */ + public function getClaim(): ?string + { + return $this->claim; + } + + /** + * @param string $claim + * @return void + */ + public function setClaim(string $claim): void + { + $this->claim = $claim; + } + + /** + * @return string|null + */ + public function getDescription(): ?string + { + return $this->description; + } + + /** + * @param string $description + * @return void + */ + public function setDescription(string $description): void + { + $this->description = $description; + } + + /** + * @return string|null + */ + public function getIcon(): ?string + { + return $this->icon; + } + + /** + * @param string $icon + * @return void + */ + public function setIcon(string $icon): void + { + $this->icon = $icon; + } + + /** + * @return string|null + */ + public function getCostDescription(): ?string + { + return $this->costDescription; + } + + /** + * @param string $costDescription + * @return void + */ + public function setCostDescription(string $costDescription): void + { + $this->costDescription = $costDescription; + } + + /** + * @return int|null + */ + public function getMinAmount(): ?int + { + return $this->minAmount; + } + + /** + * @param int $minAmount + * @return void + */ + public function setMinAmount(int $minAmount): void + { + $this->minAmount = $minAmount; + } + + /** + * @return int|null + */ + public function getMaxAmount(): ?int + { + return $this->maxAmount; + } + + /** + * @param int $maxAmount + * @return void + */ + public function setMaxAmount(int $maxAmount): void + { + $this->maxAmount = $maxAmount; + } +} diff --git a/DataAccess/Entities/PaymentMethods.php b/DataAccess/Entities/PaymentMethods.php new file mode 100644 index 0000000..0a9574a --- /dev/null +++ b/DataAccess/Entities/PaymentMethods.php @@ -0,0 +1,137 @@ +addStringIndex('storeId'); + $indexMap->addStringIndex('merchantId'); + + return new EntityConfiguration($indexMap, 'PaymentMethods'); + } + + /** + * @inheritDoc + * + * @throws \Exception + */ + public function inflate(array $data) + { + parent::inflate($data); + + $this->storeId = $data['storeId'] ?? ''; + $this->merchantId = $data['merchantId'] ?? ''; + $paymentMethods = $data['paymentMethods'] ?? []; + + foreach ($paymentMethods as $paymentMethod) { + $this->paymentMethods[] = PaymentMethod::fromArray($paymentMethod); + } + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + $data = parent::toArray(); + + $data['storeId'] = $this->storeId; + $data['merchantId'] = $this->merchantId; + $data['paymentMethods'] = []; + + foreach ($this->paymentMethods as $paymentMethod) { + $data['paymentMethods'][] = [ + 'product' => $paymentMethod->getProduct(), + 'title' => $paymentMethod->getTitle() ?: '', + 'longTitle' => $paymentMethod->getLongTitle() ?: '', + 'startsAt' => $paymentMethod->getStartsAt() ?: '', + 'endsAt' => $paymentMethod->getEndsAt() ?: '', + 'campaign' => $paymentMethod->getCampaign() ?: '', + 'claim' => $paymentMethod->getClaim() ?: '', + 'description' => $paymentMethod->getDescription() ?: '', + 'icon' => $paymentMethod->getIcon() ?: '', + 'costDescription' => $paymentMethod->getCostDescription() ?: '', + 'minAmount' => $paymentMethod->getMinAmount() ?: null, + 'maxAmount' => $paymentMethod->getMaxAmount() ?: null + ]; + } + + return $data; + } + + public function getStoreId(): string + { + return $this->storeId; + } + + public function setStoreId(string $storeId): void + { + $this->storeId = $storeId; + } + + /** + * @return string|null + */ + public function getMerchantId(): ?string + { + return $this->merchantId; + } + + /** + * @param string $merchantId + * @return void + */ + public function setMerchantId(string $merchantId): void + { + $this->merchantId = $merchantId; + } + + /** + * @return PaymentMethod[] + */ + public function getPaymentMethods(): array + { + return $this->paymentMethods; + } + + /** + * @param PaymentMethod[] $paymentMethods + */ + public function setPaymentMethods(array $paymentMethods): void + { + $this->paymentMethods = $paymentMethods; + } +} diff --git a/Plugin/MiniWidgets.php b/Plugin/MiniWidgets.php index 72c70c0..7aab3e8 100644 --- a/Plugin/MiniWidgets.php +++ b/Plugin/MiniWidgets.php @@ -19,11 +19,17 @@ use SeQura\Core\BusinessLogic\Domain\GeneralSettings\Models\GeneralSettings; use SeQura\Core\BusinessLogic\Domain\GeneralSettings\Services\GeneralSettingsService; use SeQura\Core\BusinessLogic\Domain\Multistore\StoreContext; -use SeQura\Core\BusinessLogic\Domain\PaymentMethod\Models\SeQuraPaymentMethod; use SeQura\Core\BusinessLogic\Domain\PaymentMethod\Services\PaymentMethodsService; use SeQura\Core\BusinessLogic\Domain\PromotionalWidgets\Models\WidgetSettings; use SeQura\Core\BusinessLogic\Domain\PromotionalWidgets\Services\WidgetSettingsService; +use Sequra\Core\DataAccess\Entities\PaymentMethod; +use Sequra\Core\DataAccess\Entities\PaymentMethods as PaymentMethodsEntity; use SeQura\Core\Infrastructure\Http\Exceptions\HttpRequestException; +use SeQura\Core\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException; +use SeQura\Core\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException; +use SeQura\Core\Infrastructure\ORM\QueryFilter\Operators; +use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryFilter; +use SeQura\Core\Infrastructure\ORM\RepositoryRegistry; use SeQura\Core\Infrastructure\ServiceRegister; use Sequra\Core\Services\BusinessLogic\ProductService; @@ -189,7 +195,7 @@ private function isWidgetEnabledForProduct(SaleableInterface $saleable, ?General /** * @param WidgetSettings $widgetConfig * @param StoreConfigInterface $storeConfig - * @param SeQuraPaymentMethod $paymentMethod + * @param PaymentMethod $paymentMethod * @param int $amount * * @return string @@ -197,7 +203,7 @@ private function isWidgetEnabledForProduct(SaleableInterface $saleable, ?General private function getWidgetHtml( WidgetSettings $widgetConfig, StoreConfigInterface $storeConfig, - SeQuraPaymentMethod $paymentMethod, + PaymentMethod $paymentMethod, int $amount ): string { @@ -249,13 +255,27 @@ private function getCustomerIpAddress(): string /** * @param string $merchantId * - * @return SeQuraPaymentMethod[] + * @return PaymentMethod[] * - * @throws HttpRequestException + * @throws QueryFilterInvalidParamException + * @throws RepositoryNotRegisteredException */ private function getPaymentMethods(string $merchantId): array { - return $this->getPaymentMethodsService()->getMerchantsPaymentMethods($merchantId); + $paymentMethodsRepository = RepositoryRegistry::getRepository(PaymentMethodsEntity::CLASS_NAME); + + $filter = new QueryFilter(); + $filter->where('storeId', Operators::EQUALS, $this->storeManager->getStore()->getId()) + ->where('merchantId', Operators::EQUALS, $merchantId); + + /** @var PaymentMethodsEntity $paymentMethods */ + $paymentMethods = $paymentMethodsRepository->selectOne($filter); + + if ($paymentMethods === null) { + return []; + } + + return $paymentMethods->getPaymentMethods(); } /** diff --git a/Services/Bootstrap.php b/Services/Bootstrap.php index 6a12c12..dafc884 100644 --- a/Services/Bootstrap.php +++ b/Services/Bootstrap.php @@ -25,6 +25,7 @@ use SeQura\Core\BusinessLogic\Domain\StatisticalData\RepositoryContracts\StatisticalDataRepositoryInterface; use SeQura\Core\BusinessLogic\Utility\EncryptorInterface; use SeQura\Core\BusinessLogic\Webhook\Services\ShopOrderService; +use Sequra\Core\DataAccess\Entities\PaymentMethods; use SeQura\Core\Infrastructure\Configuration\ConfigEntity; use SeQura\Core\Infrastructure\Configuration\Configuration; use SeQura\Core\Infrastructure\Logger\Interfaces\ShopLoggerAdapter; @@ -285,6 +286,7 @@ protected static function initRepositories(): void RepositoryRegistry::registerRepository(SendReport::class, BaseRepository::class); RepositoryRegistry::registerRepository(StatisticalData::class, BaseRepository::class); RepositoryRegistry::registerRepository(TransactionLog::class, BaseRepository::class); + RepositoryRegistry::registerRepository(PaymentMethods::class, BaseRepository::class); ServiceRegister::registerService( OrderStatusSettingsRepositoryInterface::class, diff --git a/Services/BusinessLogic/PaymentMethodsService.php b/Services/BusinessLogic/PaymentMethodsService.php index 50555f8..6b2839a 100644 --- a/Services/BusinessLogic/PaymentMethodsService.php +++ b/Services/BusinessLogic/PaymentMethodsService.php @@ -5,7 +5,12 @@ use Exception; use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI; use SeQura\Core\BusinessLogic\Domain\Integration\Store\StoreServiceInterface; +use Sequra\Core\DataAccess\Entities\PaymentMethod; +use Sequra\Core\DataAccess\Entities\PaymentMethods as PaymentMethodsEntity; use SeQura\Core\Infrastructure\Http\Exceptions\HttpRequestException; +use SeQura\Core\Infrastructure\ORM\QueryFilter\Operators; +use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryFilter; +use SeQura\Core\Infrastructure\ORM\RepositoryRegistry; use SeQura\Core\Infrastructure\ServiceRegister; /** @@ -28,8 +33,8 @@ public function getPaymentMethods(): array $stores = $this->getStoreService()->getConnectedStores(); $result = []; - foreach ($stores as $store) { - $countryConfigurations = AdminAPI::get()->countryConfiguration($store) + foreach ($stores as $storeId) { + $countryConfigurations = AdminAPI::get()->countryConfiguration($storeId) ->getCountryConfigurations()->toArray(); $firstConfig = array_shift($countryConfigurations); @@ -37,24 +42,23 @@ public function getPaymentMethods(): array continue; } - $widgetsConfig = AdminAPI::get()->widgetConfiguration($store)->getWidgetSettings()->toArray(); + $widgetsConfig = AdminAPI::get()->widgetConfiguration($storeId)->getWidgetSettings()->toArray(); if (isset($widgetsConfig['errorCode']) || !$widgetsConfig['useWidgets']) { continue; } - $paymentProducts = AdminAPI::get()->paymentMethods($store) - ->getPaymentMethods($firstConfig['merchantId'])->toArray(); + $paymentProducts = $this->getPaymentProducts($storeId, $firstConfig['merchantId']); if (!$paymentProducts || isset($paymentProducts['errorCode'])) { continue; } foreach ($paymentProducts as $product) { - $result[$store][] = [ - 'product' => $product['product'], - 'title' => $product['title'], - 'campaign' => $product['campaign'] + $result[$storeId][] = [ + 'product' => $product->getProduct(), + 'title' => $product->getTitle(), + 'campaign' => $product->getCampaign(), ]; } } @@ -62,6 +66,35 @@ public function getPaymentMethods(): array return $result; } + /** + * Retrieves payment products for a specific store. + * + * @param string $storeId + * @param string $merchantId + * + * @return PaymentMethod[] + * + * @throws \SeQura\Core\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException + * @throws \SeQura\Core\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException + */ + private function getPaymentProducts(string $storeId, string $merchantId): array + { + $paymentMethodsRepository = RepositoryRegistry::getRepository(PaymentMethodsEntity::CLASS_NAME); + + $filter = new QueryFilter(); + $filter->where('storeId', Operators::EQUALS, $storeId) + ->where('merchantId', Operators::EQUALS, $merchantId); + + /** @var PaymentMethodsEntity $paymentMethods */ + $paymentMethods = $paymentMethodsRepository->selectOne($filter); + + if ($paymentMethods === null) { + return []; + } + + return $paymentMethods->getPaymentMethods(); + } + /** * @return StoreServiceInterface */ @@ -69,4 +102,4 @@ private function getStoreService(): StoreServiceInterface { return ServiceRegister::getService(StoreServiceInterface::class); } -} \ No newline at end of file +} diff --git a/Services/BusinessLogic/WidgetConfigService.php b/Services/BusinessLogic/WidgetConfigService.php index 8f93e5b..3ffeab7 100644 --- a/Services/BusinessLogic/WidgetConfigService.php +++ b/Services/BusinessLogic/WidgetConfigService.php @@ -19,14 +19,18 @@ use SeQura\Core\BusinessLogic\Domain\CountryConfiguration\Services\CountryConfigurationService; use SeQura\Core\BusinessLogic\Domain\Integration\Store\StoreServiceInterface; use SeQura\Core\BusinessLogic\Domain\Multistore\StoreContext; -use SeQura\Core\BusinessLogic\Domain\PaymentMethod\Models\SeQuraPaymentMethod; use SeQura\Core\BusinessLogic\Domain\PaymentMethod\Services\PaymentMethodsService; use SeQura\Core\BusinessLogic\Domain\PromotionalWidgets\Models\WidgetSettings; use SeQura\Core\BusinessLogic\Domain\PromotionalWidgets\Services\WidgetSettingsService; use SeQura\Core\BusinessLogic\Domain\Stores\Models\Store; use SeQura\Core\BusinessLogic\SeQuraAPI\BaseProxy; +use Sequra\Core\DataAccess\Entities\PaymentMethod; +use Sequra\Core\DataAccess\Entities\PaymentMethods as PaymentMethodsEntity; use SeQura\Core\Infrastructure\Http\Exceptions\HttpRequestException; +use SeQura\Core\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException; use SeQura\Core\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException; +use SeQura\Core\Infrastructure\ORM\QueryFilter\Operators; +use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryFilter; use SeQura\Core\Infrastructure\ORM\RepositoryRegistry; use SeQura\Core\Infrastructure\ServiceRegister; @@ -234,13 +238,27 @@ private function getWidgetSettings(): ?WidgetSettings /** * @param string $merchantId * - * @return SeQuraPaymentMethod[] + * @return PaymentMethod[] * - * @throws HttpRequestException + * @throws RepositoryNotRegisteredException + * @throws QueryFilterInvalidParamException */ private function getProducts(string $merchantId): array { - return $this->getPaymentMethodsService()->getMerchantsPaymentMethods($merchantId); + $paymentMethodsRepository = RepositoryRegistry::getRepository(PaymentMethodsEntity::CLASS_NAME); + + $filter = new QueryFilter(); + $filter->where('storeId', Operators::EQUALS, $this->storeManager->getStore()->getId()) + ->where('merchantId', Operators::EQUALS, $merchantId); + + /** @var PaymentMethodsEntity $paymentMethods */ + $paymentMethods = $paymentMethodsRepository->selectOne($filter); + + if ($paymentMethods === null) { + return []; + } + + return $paymentMethods->getPaymentMethods(); } private function getFormatter(): \NumberFormatter