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
8 changes: 8 additions & 0 deletions Api/ClientDetailsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Riskified\Decider\Api;

interface ClientDetailsInterface
{
public function getData();
public function getCleanData();
}
8 changes: 8 additions & 0 deletions Api/SessionDetailsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Riskified\Decider\Api;

interface SessionDetailsInterface
{
public function getData();
public function getCleanData();
}
10 changes: 10 additions & 0 deletions Model/Api/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ public function getDeclineNotificationContent($scopeId = 0)
);
}

/**
* @return bool
*/
public function getCustomerLoginHandleEnabled()
{
return (bool) $this->_scopeConfig->getValue(
'riskified/riskified/connect_customer'
);
}

/**
* Sets store id.
* @param $id
Expand Down
50 changes: 50 additions & 0 deletions Model/Api/Data/ClientDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Riskified\Decider\Model\Api\Data;

use Magento\Framework\HTTP\Header;
use Magento\Framework\Locale\ResolverInterface;
use Riskified\Decider\Api\ClientDetailsInterface;

class ClientDetails implements ClientDetailsInterface
{
/**
* @var ResolverInterface
*/
private $localeResolver;
/**
* @var Header
*/
private $httpHeader;

/**
* ClientDetails constructor.
* @param ResolverInterface $localeResolver
* @param Header $httpHeader
*/
public function __construct(
ResolverInterface $localeResolver,
Header $httpHeader
) {
$this->localeResolver = $localeResolver;
$this->httpHeader = $httpHeader;
}

/**
* @return array
*/
public function getData()
{
return [
'accept_language' => $this->localeResolver->getLocale(),
'user_agent' => $this->httpHeader->getHttpUserAgent()
];
}

/**
* @return array
*/
public function getCleanData()
{
return array_filter($this->getData(), 'strlen');
}
}
55 changes: 55 additions & 0 deletions Model/Api/Data/SessionDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Riskified\Decider\Model\Api\Data;

use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\HTTP\Header;
use Magento\Framework\Session\SessionManager;
use Riskified\Decider\Api\SessionDetailsInterface;
use Riskified\Decider\Model\DateFormatter;

class SessionDetails implements SessionDetailsInterface
{
/**
* @var \Magento\Framework\Session\SessionManager
*/
private $session;
private $remoteAddress;
private $mobileAgent;
private $httpHeader;

use DateFormatter;

public function __construct(
SessionManager $sessionManager,
RemoteAddress $remoteAddress,
Header $httpHeader
) {
$this->session = $sessionManager;
$this->remoteAddress = $remoteAddress;
$this->httpHeader = $httpHeader;
}

/**
* @return array
*/
public function getData()
{
$userAgent = $this->httpHeader->getHttpUserAgent();
$isMobile = \Zend_Http_UserAgent_Mobile::match($userAgent, $_SERVER);

return [
'created_at' => $this->formatDateAsIso8601(date('Y-m-d H:i:s')),
'cart_token' => $this->session->getSessionId(),
'browser_ip' => $this->remoteAddress->getRemoteAddress(),
'source' => $isMobile ? 'mobile_web' : 'desktop_web'
];
}

/**
* @return array
*/
public function getCleanData()
{
return array_filter($this->getData(), 'strlen');
}
}
14 changes: 9 additions & 5 deletions Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Magento\Checkout\Model\Session;
use Riskified\OrderWebhook\Model;
use Riskified\Decider\Model\DateFormatter;

class Order
{
/**
* @var Api
*/
private $_api;
private $_apiConfig;

/**
* @var Order\Helper
Expand Down Expand Up @@ -67,6 +69,8 @@ class Order
*/
private $session;

use DateFormatter;

/**
* Order constructor.
*
Expand Down Expand Up @@ -166,7 +170,7 @@ public function post($order, $action)
case Api::ACTION_FULFILL:
$this->_orderHelper->setOrder($order->getOrder());
$orderForTransport = $this->_orderHelper->getOrderFulfillments($order);

$order = $order->getOrder();
$eventData['order'] = $order->getOrder();

Expand All @@ -185,7 +189,7 @@ public function post($order, $action)
'riskified_decider_post_order_success',
$eventData
);

} catch (\Riskified\OrderWebhook\Exception\CurlException $curlException) {
$this->_raiseOrderUpdateEvent($order, 'error', null, 'Error transferring order data to Riskified');
$this->scheduleSubmissionRetry($order, $action);
Expand Down Expand Up @@ -284,9 +288,9 @@ private function load($model)
'id' => $this->_orderHelper->getOrderOrigId(),
'name' => $model->getIncrementId(),
'email' => $model->getCustomerEmail(),
'created_at' => $this->_orderHelper->formatDateAsIso8601($model->getCreatedAt()),
'created_at' => $this->formatDateAsIso8601($model->getCreatedAt()),
'currency' => $model->getOrderCurrencyCode(),
'updated_at' => $this->_orderHelper->formatDateAsIso8601($model->getUpdatedAt()),
'updated_at' => $this->formatDateAsIso8601($model->getUpdatedAt()),
'gateway' => $gateway,
'browser_ip' => $this->_orderHelper->getRemoteIp(),
'note' => $model->getCustomerNote(),
Expand All @@ -297,7 +301,7 @@ private function load($model)
'taxes_included' => true,
'total_tax' => $model->getBaseTaxAmount(),
'total_weight' => $model->getWeight(),
'cancelled_at' => $this->_orderHelper->formatDateAsIso8601($this->_orderHelper->getCancelledAt()),
'cancelled_at' => $this->formatDateAsIso8601($this->_orderHelper->getCancelledAt()),
'financial_status' => $model->getState(),
'fulfillment_status' => $model->getStatus(),
'vendor_id' => strval($model->getStoreId()),
Expand Down
32 changes: 12 additions & 20 deletions Model/Api/Order/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Riskified\Decider\Model\Api\Order;

use Riskified\Decider\Api\ClientDetailsInterface;
use Riskified\Decider\Model\Api\ClientDetails;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\AbstractPayment;
use Riskified\OrderWebhook\Model;
use Magento\Catalog\Api\CategoryRepositoryInterface;
Expand All @@ -16,6 +18,7 @@
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Model\ResourceModel\GroupRepository;
use Riskified\Decider\Model\DateFormatter;

class Helper
{
Expand Down Expand Up @@ -99,6 +102,9 @@ class Helper
* @var CustomerGroupFactory
*/
private $_groupRepository;
private $clientDetials;

use DateFormatter;

/**
* Helper constructor.
Expand All @@ -113,8 +119,7 @@ class Helper
* @param CategoryRepositoryInterface $categoryRepository
* @param PaymentProcessorFactory $paymentProcessorFactory
* @param State $state
* @param ResolverInterface $localeResolver
* @param Header $httpHeader
* @param ClientDetailsInterface $clientDetails
* @param Registry $registry
*/
public function __construct(
Expand All @@ -130,8 +135,7 @@ public function __construct(
CategoryRepositoryInterface $categoryRepository,
PaymentProcessorFactory $paymentProcessorFactory,
State $state,
ResolverInterface $localeResolver,
Header $httpHeader,
ClientDetailsInterface $clientDetails,
Registry $registry
) {
$this->_customerFactory = $customerFactory;
Expand All @@ -146,8 +150,7 @@ public function __construct(
$this->categoryRepository = $categoryRepository;
$this->paymentProcessorFactory = $paymentProcessorFactory;
$this->state = $state;
$this->localeResolver = $localeResolver;
$this->httpHeader = $httpHeader;
$this->clientDetials = $clientDetails;
$this->registry = $registry;
}

Expand Down Expand Up @@ -227,10 +230,9 @@ public function getBillingAddress()
*/
public function getClientDetails()
{
return new Model\ClientDetails(array_filter(array(
'accept_language' => $this->localeResolver->getLocale(),
'user_agent' => $this->httpHeader->getHttpUserAgent()
), 'strlen'));
return new Model\ClientDetails(
$this->clientDetials->getCleanData()
);
}

/**
Expand Down Expand Up @@ -674,16 +676,6 @@ public function getRemoteIp()
return $remoteIp;
}

/**
* @param $dateStr
*
* @return false|null|string
*/
public function formatDateAsIso8601($dateStr)
{
return ($dateStr == null) ? null : date('c', strtotime($dateStr));
}

/**
* @return bool
*/
Expand Down
15 changes: 15 additions & 0 deletions Model/DateFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Riskified\Decider\Model;

trait DateFormatter
{
/**
* @param $dateStr
*
* @return false|null|string
*/
public function formatDateAsIso8601($dateStr)
{
return ($dateStr == null) ? null : date('c', strtotime($dateStr));
}
}
Loading