Skip to content
Open
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions Block/Advert/RootEl.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ class RootEl extends AbstractAdvert implements CatalogBlock\ShortcutInterface
*/
const COUNTRY_CODE_PATH = 'general/country/default';

/**
* @var \Zip\ZipPayment\ViewModel\WidgetConfig
*/
protected $widgetConfigViewModel;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Zip\ZipPayment\Model\Config $config,
\Magento\Framework\Registry $registry,
\Zip\ZipPayment\Helper\Logger $logger,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
\Zip\ZipPayment\ViewModel\WidgetConfig $widgetConfigViewModel,
array $data = []
) {
parent::__construct($context, $config, $registry, $logger, $checkoutSession, $priceCurrency, $data);
$this->widgetConfigViewModel = $widgetConfigViewModel;
}

/**
* Get WidgetConfig ViewModel
*
* @return \Zip\ZipPayment\ViewModel\WidgetConfig
*/
public function getWidgetConfigViewModel()
{
return $this->widgetConfigViewModel;
}

/**
* Get merchant public key
*
Expand Down
33 changes: 33 additions & 0 deletions Gateway/Validator/Method/NotAllowedProductsValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Zip\ZipPayment\Gateway\Validator\Method;

class NotAllowedProductsValidator extends \Magento\Payment\Gateway\Validator\AbstractValidator
{
private \Zip\ZipPayment\Model\ResourceModel\NotAllowedProductsProvider $notAllowedProductsProvider;

public function __construct(
\Magento\Payment\Gateway\Validator\ResultInterfaceFactory $resultFactory,
\Zip\ZipPayment\Model\ResourceModel\NotAllowedProductsProvider $notAllowedProductsProvider
) {
parent::__construct($resultFactory);
$this->notAllowedProductsProvider = $notAllowedProductsProvider;
}

public function validate(array $validationSubject): \Magento\Payment\Gateway\Validator\ResultInterface
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $validationSubject['quote'];

$disallowedProductsIds = $this->notAllowedProductsProvider->provideIds((int)$quote->getStoreId());
$disallowedProductsIdsAsKeys = array_flip($disallowedProductsIds);

foreach ($quote->getItems() ?? [] as $item) {
if (isset($disallowedProductsIdsAsKeys[(int)$item->getProductId()])) {
return $this->createResult(false);
}
}
return $this->createResult(true);
}
}

22 changes: 22 additions & 0 deletions Model/Checks/CanUseForQuoteItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Zip\ZipPayment\Model\Checks;

use Magento\Payment\Model\MethodInterface;
use Magento\Quote\Model\Quote;

class CanUseForQuoteItems implements \Magento\Payment\Model\Checks\SpecificationInterface
{
public function isApplicable(MethodInterface $paymentMethod, Quote $quote): bool
{
try {
$quoteItemsValidator = $paymentMethod->getValidatorPool()->get('quote_items');
} catch (\Throwable $e) {
return true;
}

$result = $quoteItemsValidator->validate(['quote' => $quote]);
return $result->isValid();
}
}

19 changes: 19 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class Config implements ConfigInterface
*/
const PAYMENT_ZIPMONEY_ENABLE_TOKENISATION = 'enable_tokenisation';

/**
* Exclude Categories
*
* @const
*/
const PAYMENT_ZIPMONEY_EXCLUDE_CATEGORIES = 'exclude_categories';

/**
* Minimum Order Total
*
Expand Down Expand Up @@ -403,6 +410,18 @@ public function getEnvironment($storeId = null)
return $this->getConfigData(self::PAYMENT_ZIPMONEY_ENVIRONMENT, $storeId);
}

/**
* Returns Exclude Categories
*
* @return array
*/
public function getExcludeCategories($storeId = null): array
{
$excludeCategories = $this->getConfigData(self::PAYMENT_ZIPMONEY_EXCLUDE_CATEGORIES, $storeId);

return $excludeCategories ? explode(',', $excludeCategories) : [];
}

/**
* Returns Default Merchant Private Key
*
Expand Down
18 changes: 18 additions & 0 deletions Model/Config/CategorySourceRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Zip\ZipPayment\Model\Config;

class CategorySourceRegistry
{
private bool $showAllCategories = false;

public function getShowAllCategories(): bool
{
return $this->showAllCategories;
}

public function setShowAllCategories(bool $value): void
{
$this->showAllCategories = $value;
}
}
98 changes: 98 additions & 0 deletions Model/Config/Source/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php declare(strict_types=1);

namespace Zip\ZipPayment\Model\Config\Source;

class Category implements \Magento\Framework\Data\OptionSourceInterface
{
private \Magento\Store\Model\StoreManagerInterface $storeManager;
private \Magento\Framework\App\RequestInterface $request;
private \Magento\Catalog\Helper\Category $categoryHelper;
private \Zip\ZipPayment\Model\Config\CategorySourceRegistry $categorySourceRegistry;

public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\RequestInterface $request,
\Magento\Catalog\Helper\Category $categoryHelper,
\Zip\ZipPayment\Model\Config\CategorySourceRegistry $categorySourceRegistry
) {
$this->storeManager = $storeManager;
$this->request = $request;
$this->categoryHelper = $categoryHelper;
$this->categorySourceRegistry = $categorySourceRegistry;
}

public function toOptionArray(): array
{
$options = [];
foreach ($this->getCategoriesTree() as $categoryData) {
$this->renderSubCategory($categoryData, $options);
}
return array_reverse($options);
}

private function renderSubCategory(array $categoryData, array &$optionsResult): void
{
if (isset($categoryData['children'])) {
foreach ($categoryData['children'] as $subCatData) {
$this->renderSubCategory($subCatData, $optionsResult);
}
}

if (!isset($categoryData['level']) || !is_numeric($categoryData['level']) || $categoryData['level'] < 2) {
return;
}

$optionsResult[] = [
'label' => str_repeat('―', $categoryData['level'] - 2) . $categoryData['label'],
'value' => $categoryData['id']
];
}

private function getCategoriesTree(): array
{
$currentStoreId = $this->storeManager->getStore()->getId();

$this->storeManager->setCurrentStore($this->getStoreIdByRequest() ?? $currentStoreId);
$this->categorySourceRegistry->setShowAllCategories(true);
/** @var \Magento\Framework\Data\Collection $categories */
$categories = $this->categoryHelper->getStoreCategories(false, true);
$this->categorySourceRegistry->setShowAllCategories(false);
$this->storeManager->setCurrentStore($currentStoreId);

return $this->convertToTree($categories);
}

private function convertToTree(\Magento\Framework\Data\Collection $categories): array
{
$categoryById = [];
foreach ($categories as $category) {
foreach ([$category->getId(), $category->getParentId()] as $categoryId) {
if (!isset($categoryById[$categoryId])) {
$categoryById[$categoryId] = ['id' => $categoryId, 'children' => []];
}
}
$categoryById[$category->getId()]['label'] = $category->getName();
$categoryById[$category->getId()]['level'] = $category->getLevel();
$categoryById[$category->getParentId()]['children'][] = & $categoryById[$category->getId()];
}
foreach ($categoryById as $categoryData) {
if (!isset($categoryData['label']) && isset($categoryData['children'])) {
return $categoryData['children'];
}
}
return [];
}

private function getStoreIdByRequest(): ?int
{
if ($storeId = $this->request->getParam('store')) {
return (int)$storeId;
}
if ($websiteId = $this->request->getParam('website')) {
/** @var \Magento\Store\Model\Website $website */
$website = $this->storeManager->getWebsite($websiteId);
return (int)$website->getDefaultStore()->getId();
}
return null;
}
}
61 changes: 61 additions & 0 deletions Model/ResourceModel/NotAllowedProductsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace Zip\ZipPayment\Model\ResourceModel;

class NotAllowedProductsProvider
{
private \Zip\ZipPayment\Model\Config $config;
private \Magento\Framework\App\ResourceConnection $resourceConnection;

public function __construct(
\Zip\ZipPayment\Model\Config $config,
\Magento\Framework\App\ResourceConnection $resourceConnection
) {
$this->config = $config;
$this->resourceConnection = $resourceConnection;
}

public function provideIds(?int $storeId = null): array
{
$excludedCategoriesIds = $this->config->getExcludeCategories($storeId);
if (empty($excludedCategoriesIds)) {
return [];
}

$connection = $this->resourceConnection->getConnection();
$select = $connection->select()->from(
['cat' => $this->resourceConnection->getTableName('catalog_category_product')],
'cat.product_id'
)->where($connection->prepareSqlCondition('cat.category_id', ['in' => $excludedCategoriesIds]));

return $connection->fetchCol($select);
}

/**
* Check if a single product is not allowed based on excluded categories
*
* @param int $productId
* @param int|null $storeId
* @return bool True if product is in excluded categories (not allowed), false otherwise
*/
public function isProductNotAllowed(int $productId, ?int $storeId = null): bool
{
$excludedCategoriesIds = $this->config->getExcludeCategories($storeId);
if (empty($excludedCategoriesIds)) {
return false;
}

$connection = $this->resourceConnection->getConnection();
$select = $connection->select()
->from(
['cat' => $this->resourceConnection->getTableName('catalog_category_product')],
'COUNT(*)'
)
->where('cat.product_id = ?', $productId)
->where($connection->prepareSqlCondition('cat.category_id', ['in' => $excludedCategoriesIds]));

$count = (int)$connection->fetchOne($select);

return $count > 0;
}
}
Loading
Loading