Skip to content

Commit 25442f9

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-91102' into 2.3-develop-pr23
2 parents afa280c + 6655382 commit 25442f9

File tree

16 files changed

+425
-83
lines changed

16 files changed

+425
-83
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): bool
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): string
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): string
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): bool
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ 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
4142
*/
42-
public function makeApiCall($url, $method, array $params = [])
43+
public function makeApiCall($url, $method, array $params = [], $storeId = null): array
4344
{
44-
$result = $this->requestBuilder->doRequest($url, $method, $params);
45+
$result = $this->requestBuilder->doRequest($url, $method, $params, $storeId);
4546

4647
return $result;
4748
}

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

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

8384
$client = $this->createNewClient();
8485
$client->setHeaders(
@@ -107,22 +108,24 @@ private function createNewClient()
107108
* Signifyd API key for merchant account.
108109
*
109110
* @see https://www.signifyd.com/docs/api/#/introduction/authentication
111+
* @param int|null $storeId
110112
* @return string
111113
*/
112-
private function getApiKey()
114+
private function getApiKey($storeId): string
113115
{
114-
return $this->config->getApiKey();
116+
return $this->config->getApiKey($storeId);
115117
}
116118

117119
/**
118120
* Full URL for Singifyd API based on relative URL.
119121
*
120122
* @param string $url
123+
* @param int|null $storeId
121124
* @return string
122125
*/
123-
private function buildFullApiUrl($url)
126+
private function buildFullApiUrl($url, $storeId): string
124127
{
125-
$baseApiUrl = $this->getBaseApiUrl();
128+
$baseApiUrl = $this->getBaseApiUrl($storeId);
126129
$fullUrl = $baseApiUrl . self::$urlSeparator . ltrim($url, self::$urlSeparator);
127130

128131
return $fullUrl;
@@ -131,11 +134,12 @@ private function buildFullApiUrl($url)
131134
/**
132135
* Base Sigifyd API URL without trailing slash.
133136
*
137+
* @param int|null $storeId
134138
* @return string
135139
*/
136-
private function getBaseApiUrl()
140+
private function getBaseApiUrl($storeId): string
137141
{
138-
$baseApiUrl = $this->config->getApiUrl();
142+
$baseApiUrl = $this->config->getApiUrl($storeId);
139143

140144
return rtrim($baseApiUrl, self::$urlSeparator);
141145
}

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

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

8-
use Magento\Framework\HTTP\ZendClient;
9-
108
/**
119
* Class RequestBuilder
1210
* Creates HTTP client, sends request to Signifyd and handles response
@@ -50,13 +48,14 @@ public function __construct(
5048
*
5149
* @param string $url
5250
* @param string $method
53-
* @param array $params
51+
* @param array $params
52+
* @param int|null $storeId
5453
* @return array
5554
*/
56-
public function doRequest($url, $method, array $params = [])
55+
public function doRequest($url, $method, array $params = [], $storeId = null): array
5756
{
58-
$client = $this->clientCreator->create($url, $method, $params);
59-
$response = $this->requestSender->send($client);
57+
$client = $this->clientCreator->create($url, $method, $params, $storeId);
58+
$response = $this->requestSender->send($client, $storeId);
6059
$result = $this->responseHandler->handle($response);
6160

6261
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): \Zend_Http_Response
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): DebuggerInterface
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: 55 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
/**
@@ -78,11 +95,13 @@ public function __construct(
7895
public function createCase($orderId)
7996
{
8097
$caseParams = $this->createCaseBuilder->build($orderId);
98+
$storeId = $this->getStoreIdFromOrder($orderId);
8199

82100
$caseCreationResult = $this->apiClient->makeApiCall(
83101
'/cases',
84102
'POST',
85-
$caseParams
103+
$caseParams,
104+
$storeId
86105
);
87106

88107
if (!isset($caseCreationResult['investigationId'])) {
@@ -102,12 +121,14 @@ public function createCase($orderId)
102121
*/
103122
public function submitCaseForGuarantee($signifydCaseId)
104123
{
124+
$storeId = $this->getStoreIdFromCase($signifydCaseId);
105125
$guaranteeCreationResult = $this->apiClient->makeApiCall(
106126
'/guarantees',
107127
'POST',
108128
[
109129
'caseId' => $signifydCaseId,
110-
]
130+
],
131+
$storeId
111132
);
112133

113134
$disposition = $this->processDispositionResult($guaranteeCreationResult);
@@ -124,12 +145,14 @@ public function submitCaseForGuarantee($signifydCaseId)
124145
*/
125146
public function cancelGuarantee($caseId)
126147
{
148+
$storeId = $this->getStoreIdFromCase($caseId);
127149
$result = $this->apiClient->makeApiCall(
128150
'/cases/' . $caseId . '/guarantee',
129151
'PUT',
130152
[
131153
'guaranteeDisposition' => self::GUARANTEE_CANCELED
132-
]
154+
],
155+
$storeId
133156
);
134157

135158
$disposition = $this->processDispositionResult($result);
@@ -172,4 +195,31 @@ private function processDispositionResult(array $result)
172195

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

app/code/Magento/Signifyd/Observer/PlaceOrder.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ public function __construct(
5555
*/
5656
public function execute(Observer $observer)
5757
{
58-
if (!$this->signifydIntegrationConfig->isActive()) {
59-
return;
60-
}
61-
6258
$orders = $this->extractOrders(
6359
$observer->getEvent()
6460
);
@@ -68,7 +64,10 @@ public function execute(Observer $observer)
6864
}
6965

7066
foreach ($orders as $order) {
71-
$this->createCaseForOrder($order);
67+
$storeId = $order->getStoreId();
68+
if ($this->signifydIntegrationConfig->isActive($storeId)) {
69+
$this->createCaseForOrder($order);
70+
}
7271
}
7372
}
7473

0 commit comments

Comments
 (0)