Skip to content

Commit 07ddc2d

Browse files
committed
Merge branch 'MAGETWO-90571' into MPI-PR
2 parents 3bd8afb + c761f67 commit 07ddc2d

File tree

15 files changed

+413
-59
lines changed

15 files changed

+413
-59
lines changed

app/code/Magento/Signifyd/Block/Fingerprint.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public function getSignifydOrderSessionId()
8585
*/
8686
public function isModuleActive()
8787
{
88-
return $this->config->isActive();
88+
$storeId = $this->quoteSession->getQuote()->getStoreId();
89+
90+
return $this->config->isActive($storeId);
8991
}
9092
}

app/code/Magento/Signifyd/Model/Config.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ public function __construct(ScopeConfigInterface $scopeConfig)
3434
* If this config option set to false no Signifyd integration should be available
3535
* (only possibility to configure Signifyd setting in admin)
3636
*
37+
* @param int|null $storeId
3738
* @return bool
3839
*/
39-
public function isActive()
40+
public function isActive($storeId = null)
4041
{
4142
$enabled = $this->scopeConfig->isSetFlag(
4243
'fraud_protection/signifyd/active',
43-
ScopeInterface::SCOPE_STORE
44+
ScopeInterface::SCOPE_STORE,
45+
$storeId
4446
);
4547
return $enabled;
4648
}
@@ -51,13 +53,15 @@ public function isActive()
5153
* @see https://www.signifyd.com/docs/api/#/introduction/authentication
5254
* @see https://app.signifyd.com/settings
5355
*
56+
* @param int|null $storeId
5457
* @return string
5558
*/
56-
public function getApiKey()
59+
public function getApiKey($storeId = null)
5760
{
5861
$apiKey = $this->scopeConfig->getValue(
5962
'fraud_protection/signifyd/api_key',
60-
ScopeInterface::SCOPE_STORE
63+
ScopeInterface::SCOPE_STORE,
64+
$storeId
6165
);
6266
return $apiKey;
6367
}
@@ -66,27 +70,31 @@ public function getApiKey()
6670
* Base URL to Signifyd REST API.
6771
* Usually equals to https://api.signifyd.com/v2 and should not be changed
6872
*
73+
* @param int|null $storeId
6974
* @return string
7075
*/
71-
public function getApiUrl()
76+
public function getApiUrl($storeId = null)
7277
{
7378
$apiUrl = $this->scopeConfig->getValue(
7479
'fraud_protection/signifyd/api_url',
75-
ScopeInterface::SCOPE_STORE
80+
ScopeInterface::SCOPE_STORE,
81+
$storeId
7682
);
7783
return $apiUrl;
7884
}
7985

8086
/**
8187
* If is "true" extra information about interaction with Signifyd API are written to debug.log file
8288
*
89+
* @param int|null $storeId
8390
* @return bool
8491
*/
85-
public function isDebugModeEnabled()
92+
public function isDebugModeEnabled($storeId = null)
8693
{
8794
$debugModeEnabled = $this->scopeConfig->isSetFlag(
8895
'fraud_protection/signifyd/debug',
89-
ScopeInterface::SCOPE_STORE
96+
ScopeInterface::SCOPE_STORE,
97+
$storeId
9098
);
9199
return $debugModeEnabled;
92100
}

app/code/Magento/Signifyd/Model/SignifydGateway/ApiClient.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ public function __construct(
3636
*
3737
* @param string $url
3838
* @param string $method
39-
* @param array $params
39+
* @param array $params
40+
* @param int|null $storeId
4041
* @return array
42+
* @throws ApiCallException
43+
* @throws \Zend_Http_Client_Exception
4144
*/
42-
public function makeApiCall($url, $method, array $params = [])
45+
public function makeApiCall($url, $method, array $params = [], $storeId = null)
4346
{
44-
$result = $this->requestBuilder->doRequest($url, $method, $params);
47+
$result = $this->requestBuilder->doRequest($url, $method, $params, $storeId);
4548

4649
return $result;
4750
}

app/code/Magento/Signifyd/Model/SignifydGateway/Client/HttpClientFactory.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ public function __construct(
7373
* @param string $url
7474
* @param string $method
7575
* @param array $params
76+
* @param int|null $storeId
7677
* @return ZendClient
78+
* @throws \Zend_Http_Client_Exception
7779
*/
78-
public function create($url, $method, array $params = [])
80+
public function create($url, $method, array $params = [], $storeId = null)
7981
{
80-
$apiKey = $this->getApiKey();
81-
$apiUrl = $this->buildFullApiUrl($url);
82+
$apiKey = $this->getApiKey($storeId);
83+
$apiUrl = $this->buildFullApiUrl($url, $storeId);
8284

8385
$client = $this->createNewClient();
8486
$client->setHeaders(
@@ -107,22 +109,24 @@ private function createNewClient()
107109
* Signifyd API key for merchant account.
108110
*
109111
* @see https://www.signifyd.com/docs/api/#/introduction/authentication
112+
* @param int|null $storeId
110113
* @return string
111114
*/
112-
private function getApiKey()
115+
private function getApiKey($storeId)
113116
{
114-
return $this->config->getApiKey();
117+
return $this->config->getApiKey($storeId);
115118
}
116119

117120
/**
118121
* Full URL for Singifyd API based on relative URL.
119122
*
120123
* @param string $url
124+
* @param int|null $storeId
121125
* @return string
122126
*/
123-
private function buildFullApiUrl($url)
127+
private function buildFullApiUrl($url, $storeId)
124128
{
125-
$baseApiUrl = $this->getBaseApiUrl();
129+
$baseApiUrl = $this->getBaseApiUrl($storeId);
126130
$fullUrl = $baseApiUrl . self::$urlSeparator . ltrim($url, self::$urlSeparator);
127131

128132
return $fullUrl;
@@ -131,11 +135,12 @@ private function buildFullApiUrl($url)
131135
/**
132136
* Base Sigifyd API URL without trailing slash.
133137
*
138+
* @param int|null $storeId
134139
* @return string
135140
*/
136-
private function getBaseApiUrl()
141+
private function getBaseApiUrl($storeId)
137142
{
138-
$baseApiUrl = $this->config->getApiUrl();
143+
$baseApiUrl = $this->config->getApiUrl($storeId);
139144

140145
return rtrim($baseApiUrl, self::$urlSeparator);
141146
}

app/code/Magento/Signifyd/Model/SignifydGateway/Client/RequestBuilder.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Signifyd\Model\SignifydGateway\Client;
77

8-
use Magento\Framework\HTTP\ZendClient;
8+
use Magento\Signifyd\Model\SignifydGateway\ApiCallException;
99

1010
/**
1111
* Class RequestBuilder
@@ -50,13 +50,16 @@ public function __construct(
5050
*
5151
* @param string $url
5252
* @param string $method
53-
* @param array $params
53+
* @param array $params
54+
* @param int|null $storeId
5455
* @return array
56+
* @throws ApiCallException
57+
* @throws \Zend_Http_Client_Exception
5558
*/
56-
public function doRequest($url, $method, array $params = [])
59+
public function doRequest($url, $method, array $params = [], $storeId = null)
5760
{
58-
$client = $this->clientCreator->create($url, $method, $params);
59-
$response = $this->requestSender->send($client);
61+
$client = $this->clientCreator->create($url, $method, $params, $storeId);
62+
$response = $this->requestSender->send($client, $storeId);
6063
$result = $this->responseHandler->handle($response);
6164

6265
return $result;

app/code/Magento/Signifyd/Model/SignifydGateway/Client/RequestSender.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ public function __construct(
3939
* debug information is recorded to debug.log.
4040
*
4141
* @param ZendClient $client
42+
* @param int|null $storeId
4243
* @return \Zend_Http_Response
4344
* @throws ApiCallException
4445
*/
45-
public function send(ZendClient $client)
46+
public function send(ZendClient $client, $storeId = null)
4647
{
4748
try {
4849
$response = $client->request();
4950

50-
$this->debuggerFactory->create()->success(
51+
$this->debuggerFactory->create($storeId)->success(
5152
$client->getUri(true),
5253
$client->getLastRequest(),
5354
$response->getStatus() . ' ' . $response->getMessage(),
@@ -56,7 +57,7 @@ public function send(ZendClient $client)
5657

5758
return $response;
5859
} catch (\Exception $e) {
59-
$this->debuggerFactory->create()->failure(
60+
$this->debuggerFactory->create($storeId)->failure(
6061
$client->getUri(true),
6162
$client->getLastRequest(),
6263
$e

app/code/Magento/Signifyd/Model/SignifydGateway/Debugger/DebuggerFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ public function __construct(
4444
/**
4545
* Create debugger instance
4646
*
47+
* @param int|null $storeId
4748
* @return DebuggerInterface
4849
*/
49-
public function create()
50+
public function create($storeId = null)
5051
{
51-
if (!$this->config->isDebugModeEnabled()) {
52+
if (!$this->config->isDebugModeEnabled($storeId)) {
5253
return $this->objectManager->get(BlackHole::class);
5354
}
5455

app/code/Magento/Signifyd/Model/SignifydGateway/Gateway.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
*/
66
namespace Magento\Signifyd\Model\SignifydGateway;
77

8+
use Magento\Sales\Api\OrderRepositoryInterface;
9+
use Magento\Signifyd\Api\CaseRepositoryInterface;
810
use Magento\Signifyd\Model\SignifydGateway\Request\CreateCaseBuilderInterface;
9-
use Magento\Signifyd\Model\SignifydGateway\ApiClient;
1011

1112
/**
1213
* Signifyd Gateway.
@@ -53,18 +54,34 @@ class Gateway
5354
*/
5455
private $apiClient;
5556

57+
/**
58+
* @var OrderRepositoryInterface
59+
*/
60+
private $orderRepository;
61+
62+
/**
63+
* @var CaseRepositoryInterface
64+
*/
65+
private $caseRepository;
66+
5667
/**
5768
* Gateway constructor.
5869
*
5970
* @param CreateCaseBuilderInterface $createCaseBuilder
6071
* @param ApiClient $apiClient
72+
* @param OrderRepositoryInterface $orderRepository
73+
* @param CaseRepositoryInterface $caseRepository
6174
*/
6275
public function __construct(
6376
CreateCaseBuilderInterface $createCaseBuilder,
64-
ApiClient $apiClient
77+
ApiClient $apiClient,
78+
OrderRepositoryInterface $orderRepository,
79+
CaseRepositoryInterface $caseRepository
6580
) {
6681
$this->createCaseBuilder = $createCaseBuilder;
6782
$this->apiClient = $apiClient;
83+
$this->orderRepository = $orderRepository;
84+
$this->caseRepository = $caseRepository;
6885
}
6986

7087
/**
@@ -74,15 +91,18 @@ public function __construct(
7491
* @param int $orderId
7592
* @return int Signifyd case (investigation) identifier
7693
* @throws GatewayException
94+
* @throws \Zend_Http_Client_Exception
7795
*/
7896
public function createCase($orderId)
7997
{
8098
$caseParams = $this->createCaseBuilder->build($orderId);
99+
$storeId = $this->getStoreIdFromOrder($orderId);
81100

82101
$caseCreationResult = $this->apiClient->makeApiCall(
83102
'/cases',
84103
'POST',
85-
$caseParams
104+
$caseParams,
105+
$storeId
86106
);
87107

88108
if (!isset($caseCreationResult['investigationId'])) {
@@ -99,15 +119,18 @@ public function createCase($orderId)
99119
* @param int $signifydCaseId
100120
* @return string
101121
* @throws GatewayException
122+
* @throws \Zend_Http_Client_Exception
102123
*/
103124
public function submitCaseForGuarantee($signifydCaseId)
104125
{
126+
$storeId = $this->getStoreIdFromCase($signifydCaseId);
105127
$guaranteeCreationResult = $this->apiClient->makeApiCall(
106128
'/guarantees',
107129
'POST',
108130
[
109131
'caseId' => $signifydCaseId,
110-
]
132+
],
133+
$storeId
111134
);
112135

113136
$disposition = $this->processDispositionResult($guaranteeCreationResult);
@@ -121,15 +144,18 @@ public function submitCaseForGuarantee($signifydCaseId)
121144
* @param int $caseId
122145
* @return string
123146
* @throws GatewayException
147+
* @throws \Zend_Http_Client_Exception
124148
*/
125149
public function cancelGuarantee($caseId)
126150
{
151+
$storeId = $this->getStoreIdFromCase($caseId);
127152
$result = $this->apiClient->makeApiCall(
128153
'/cases/' . $caseId . '/guarantee',
129154
'PUT',
130155
[
131156
'guaranteeDisposition' => self::GUARANTEE_CANCELED
132-
]
157+
],
158+
$storeId
133159
);
134160

135161
$disposition = $this->processDispositionResult($result);
@@ -172,4 +198,31 @@ private function processDispositionResult(array $result)
172198

173199
return $disposition;
174200
}
201+
202+
/**
203+
* Returns store id by case.
204+
*
205+
* @param int $caseId
206+
* @return int|null
207+
*/
208+
private function getStoreIdFromCase(int $caseId)
209+
{
210+
$case = $this->caseRepository->getByCaseId($caseId);
211+
$orderId = $case->getOrderId();
212+
213+
return $this->getStoreIdFromOrder($orderId);
214+
}
215+
216+
/**
217+
* Returns store id from order.
218+
*
219+
* @param int $orderId
220+
* @return int|null
221+
*/
222+
private function getStoreIdFromOrder(int $orderId)
223+
{
224+
$order = $this->orderRepository->get($orderId);
225+
226+
return $order->getStoreId();
227+
}
175228
}

0 commit comments

Comments
 (0)