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
21 changes: 20 additions & 1 deletion Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Riskified\Decider\Model\Api;

use Riskified\Decider\Model\Api\Order\Validator;
use Riskified\OrderWebhook\Model;
use Magento\Framework\Registry;

Expand Down Expand Up @@ -76,6 +77,7 @@ class Order
* @var Config
*/
private Config $_apiConfig;
private Validator $validator;

/**
* Order constructor.
Expand Down Expand Up @@ -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;
Expand All @@ -122,6 +125,7 @@ public function __construct(
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->registry = $registry;
$this->validator = $validator;

$this->_orderHelper->setCheckoutSession($checkoutSession);

Expand All @@ -132,7 +136,7 @@ public function __construct(
* @param $order
* @param $action
*
* @return $this|object
* @return void
*
* @throws \Exception
* @throws \Riskified\OrderWebhook\Exception\CurlException
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
138 changes: 138 additions & 0 deletions Model/Api/Order/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Riskified\Decider\Model\Api\Order;

use Magento\Framework\App\Config\ScopeConfigInterface;
use \Exception;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Store\Model\ScopeInterface as ScopeInterface;
use \Riskified\Decider\Model\Api\Log;

class Validator
{
private ScopeConfigInterface $config;
private Log $log;
private OrderInterface $order;

public function __construct(ScopeConfigInterface $config, Log $log)
{
$this->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."
);
}
}
}
}
46 changes: 46 additions & 0 deletions Model/Config/Source/Categories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Riskified\Decider\Model\Config\Source;

use Magento\Catalog\Model\Category;
use \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

class Categories implements \Magento\Framework\Data\OptionSourceInterface
{
private CollectionFactory $categoryCollectionFactory;

public function __construct(
CollectionFactory $categoryCollectionFactory,
) {
$this->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;
}
}
35 changes: 35 additions & 0 deletions Model/Config/Source/PaymentMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Riskified\Decider\Model\Config\Source;

use \Magento\Payment\Helper\Data as PaymentData;

class PaymentMethods implements \Magento\Framework\Data\OptionSourceInterface
{
private PaymentData $paymentHelper;
public function __construct(
PaymentData $paymentHelper,
) {
$this->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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,29 @@
</depends>
</field>
</group>
<group id="exclude_rules" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Order exclude settings</label>
<field id="payment_methods" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Payment methods</label>
<source_model>Riskified\Decider\Model\Config\Source\PaymentMethods</source_model>
</field>
<field id="category" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Categories</label>
<source_model>Riskified\Decider\Model\Config\Source\Categories</source_model>
</field>
<field id="product_type" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Product Types</label>
<source_model>Magento\Catalog\Model\Product\Type</source_model>
</field>
<field id="customer_email" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Customer Email</label>
</field>
</group>
<group id="decline_notification" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Decline notification settings</label>
Expand Down