diff --git a/Model/Api/Order.php b/Model/Api/Order.php index 5da6238..4dbe309 100755 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -2,6 +2,7 @@ namespace Riskified\Decider\Model\Api; +use Riskified\Decider\Model\Api\Order\Validator; use Riskified\OrderWebhook\Model; use Magento\Framework\Registry; @@ -76,6 +77,7 @@ class Order * @var Config */ private Config $_apiConfig; + private Validator $validator; /** * Order constructor. @@ -106,6 +108,7 @@ public function __construct( \Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\Session\SessionManager $sessionManager, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, + Validator $validator, Registry $registry ) { $this->_api = $api; @@ -122,6 +125,7 @@ public function __construct( $this->orderRepository = $orderRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->registry = $registry; + $this->validator = $validator; $this->_orderHelper->setCheckoutSession($checkoutSession); @@ -132,7 +136,7 @@ public function __construct( * @param $order * @param $action * - * @return $this|object + * @return void * * @throws \Exception * @throws \Riskified\OrderWebhook\Exception\CurlException @@ -154,6 +158,10 @@ public function post($order, $action) $this->_orderHelper->setOrder($order); $this->registry->register("riskified-order", $order, true); + if ($action != Api::ACTION_CHECKOUT_DENIED && !$this->validator->validate($order)) { + return; + } + $eventData = [ 'order' => $order, 'action' => $action @@ -235,6 +243,17 @@ public function post($order, $action) return $response; } + private function validate($order): bool + { + try { + + } catch (\Exception $e) { + return false; + } + + return true; + } + /** * @param $order * @param $status diff --git a/Model/Api/Order/Validator.php b/Model/Api/Order/Validator.php new file mode 100644 index 0000000..c0e4667 --- /dev/null +++ b/Model/Api/Order/Validator.php @@ -0,0 +1,138 @@ +config = $config; + $this->log = $log; + } + + /** + * @param $model + */ + public function validate($order) : bool + { + $this->order = $order; + + try { + $this->validatePaymentMethod(); + $this->validateCustomerEmail(); + $this->validateProductTypes(); + $this->validateProductCategories(); + } catch (Exception $e) { + $this->log->log($e->getMessage()); + return false; + } + + return true; + } + + /** + * @throws Exception + */ + private function validatePaymentMethod(): void + { + $invalidPaymentMethods = $this->config->getValue('riskified/exclude_rules/payment_methods', + ScopeInterface::SCOPE_STORES, + $this->order->getStoreId() + ); + + if (!$invalidPaymentMethods) { + return; + } + + $invalidPaymentMethods = explode(',', $invalidPaymentMethods); + + if (in_array($this->order->getPayment()->getMethod(), $invalidPaymentMethods)) { + throw new Exception("Order #{$this->order->getIncrementId()} is not valid to be send to Riskified - payment method is excluded."); + } + } + + /** + * @throws Exception + */ + private function validateCustomerEmail(): void + { + $invalidCustomerEmails = $this->config->getValue('riskified/exclude_rules/customer_email', + ScopeInterface::SCOPE_STORES, + $this->order->getStoreId() + ); + + if (!$invalidCustomerEmails) { + return; + } + $customerEmails = explode(",", $invalidCustomerEmails); + foreach ($customerEmails as $key => $email) { + $customerEmails[$key] = trim($email); + } + + if (in_array($this->order->getCustomerEmail(), $customerEmails)) { + throw new Exception("Order #{$this->order->getIncrementId()} is not valid to be send to Riskified - customer email is excluded."); + } + } + + /** + * @throws Exception + */ + private function validateProductCategories(): void + { + $invalidProductCategories = $this->config->getValue('riskified/exclude_rules/category', + ScopeInterface::SCOPE_STORES, + $this->order->getStoreId() + ); + + if (!$invalidProductCategories) { + return; + } + $invalidProductCategories = explode(',', $invalidProductCategories); + + foreach ($this->order->getAllItems() as $item) { + $categoryIds = $item->getProduct()->getCategoryIds(); + $commonCategories = array_intersect($categoryIds, $invalidProductCategories); + + if (!empty($commonCategories)) { + throw new Exception( + "Order #{$this->order->getIncrementId()} is not valid to be send to Riskified - product categories." + ); + } + } + } + + /** + * @throws Exception + */ + private function validateProductTypes(): void + { + $invalidProductTypes = $this->config->getValue('riskified/exclude_rules/product_type', + ScopeInterface::SCOPE_STORES, + $this->order->getStoreId() + ); + + if (!$invalidProductTypes) { + return; + } + + $invalidProductTypes = explode(',', $invalidProductTypes); + + foreach ($this->order->getAllItems() as $item) { + if (in_array($item->getProduct()->getTypeId(), $invalidProductTypes)){ + throw new Exception( + "Order #{$this->order->getIncrementId()} is not valid to be send to Riskified - product types." + ); + } + } + } +} diff --git a/Model/Config/Source/Categories.php b/Model/Config/Source/Categories.php new file mode 100644 index 0000000..fb86508 --- /dev/null +++ b/Model/Config/Source/Categories.php @@ -0,0 +1,46 @@ +categoryCollectionFactory = $categoryCollectionFactory; + } + /** + * Options getter + * + * @return array + */ + public function toOptionArray(): array + { + $collection = $this->categoryCollectionFactory->create(); + $collection->addAttributeToSelect('name'); + $collection->addIsActiveFilter(); + + $data = []; + + /** @var Category $category */ + foreach ($collection as $category) { + $prefix = ''; + + for($i = 1; $i < $category->getLevel(); $i++) { + $prefix .= '---'; + } + + $data[] = [ + 'value' => $category->getId(), + 'label' => $prefix .' '. __($category->getName()) + ]; + } + + return $data; + } +} diff --git a/Model/Config/Source/PaymentMethods.php b/Model/Config/Source/PaymentMethods.php new file mode 100644 index 0000000..efbc2bf --- /dev/null +++ b/Model/Config/Source/PaymentMethods.php @@ -0,0 +1,35 @@ +paymentHelper = $paymentHelper; + } + /** + * Options getter + * + * @return array + */ + public function toOptionArray(): array + { + $list = $this->paymentHelper->getPaymentMethodList(); + + $data = []; + + foreach ($list as $key => $value) { + $data[] = [ + 'value' => $key, + 'label' => __($value) + ]; + } + + return $data; + } +} diff --git a/composer.json b/composer.json index f976c79..f9c45a8 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "riskified/magento2new", "type": "magento2-module", "description": "Riskified decider module for Magento 2", - "version": "1.12.25", + "version": "1.13.0", "require": { "php": ">=7.4", "magento/framework": ">=100.1.0", diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index b4f30be..d837b84 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -168,6 +168,29 @@ + + + + + Riskified\Decider\Model\Config\Source\PaymentMethods + + + + Riskified\Decider\Model\Config\Source\Categories + + + + Magento\Catalog\Model\Product\Type + + + + +