Skip to content

Commit d97fd2f

Browse files
author
Ivan Tagil
committed
CMP-41 - Code improvements, bug fixing
1 parent 3806190 commit d97fd2f

File tree

5 files changed

+128
-156
lines changed

5 files changed

+128
-156
lines changed

Controller/Adminhtml/Action/CheckApiConnection.php

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88

99
namespace CM\Payments\Controller\Adminhtml\Action;
1010

11-
use CM\Payments\Api\Service\ApiTestServiceInterfaceFactory;
1211
use CM\Payments\Api\Service\ApiTestServiceInterface;
12+
use GuzzleHttp\Exception\GuzzleException;
1313
use Magento\Backend\App\Action;
1414
use Magento\Backend\App\Action\Context;
1515
use Magento\Framework\Controller\Result\Json as JsonResult;
1616
use Magento\Framework\Controller\Result\JsonFactory as JsonResultFactory;
1717
use Magento\Framework\Exception\NoSuchEntityException;
18-
use GuzzleHttp\Exception\GuzzleException;
1918

2019
class CheckApiConnection extends Action
2120
{
@@ -25,26 +24,26 @@ class CheckApiConnection extends Action
2524
private $jsonResultFactory;
2625

2726
/**
28-
* @var ApiTestServiceInterfaceFactory
27+
* @var ApiTestServiceInterface
2928
*/
30-
private $apiTestServiceFactory;
29+
private $apiTestService;
3130

3231
/**
3332
* CheckApiConnection constructor
3433
*
3534
* @param Context $context
3635
* @param JsonResultFactory $jsonResultFactory
37-
* @param ApiTestServiceInterfaceFactory $apiTestServiceFactory
36+
* @param ApiTestServiceInterface $apiTestService
3837
*/
3938
public function __construct(
4039
Context $context,
4140
JsonResultFactory $jsonResultFactory,
42-
ApiTestServiceInterfaceFactory $apiTestServiceFactory
41+
ApiTestServiceInterface $apiTestService
4342
) {
4443
parent::__construct($context);
4544

4645
$this->jsonResultFactory = $jsonResultFactory;
47-
$this->apiTestServiceFactory = $apiTestServiceFactory;
46+
$this->apiTestService = $apiTestService;
4847
}
4948

5049
/**
@@ -54,79 +53,35 @@ public function execute(): JsonResult
5453
{
5554
/** @var JsonResult $jsonResult */
5655
$jsonResult = $this->jsonResultFactory->create();
56+
$success = true;
5757

58-
$apiConnectionData = $this->getRequest()->getParams();
59-
$errors = $this->validateData($apiConnectionData);
60-
61-
if (!$errors) {
62-
$success = true;
58+
try {
59+
$resultData = $this->apiTestService->testApiConnection();
6360

64-
try {
65-
/** @var ApiTestServiceInterface $apiTestService */
66-
$apiTestService = $this->apiTestServiceFactory->create(
67-
[
68-
'apiConnectionData' => [
69-
'mode' => $apiConnectionData['mode'],
70-
'merchantName' => $apiConnectionData['merchant_name'],
71-
'merchantPassword' => $apiConnectionData['merchant_password'],
72-
'merchantKey' => $apiConnectionData['merchant_key']
73-
]
74-
]
75-
);
76-
77-
$result = $apiTestService->testApiConnection();
78-
79-
if (!is_array($result)) {
80-
$data = [
81-
'success' => false,
82-
'connectionResult' => __("The connection was unsuccessful. Please, check your credentials.")
83-
];
84-
} else {
85-
$data = [
86-
'success' => $success,
87-
'connectionResult' => __("The connection was successful. Your credentials are valid.")
88-
];
89-
}
90-
} catch (GuzzleException|NoSuchEntityException $e) {
61+
if (!empty($resultData['errors'])) {
62+
array_unshift($resultData['errors'], __("The connection was unsuccessful."));
63+
$data = [
64+
'success' => false,
65+
'connectionResult' => implode('<br />', $resultData['errors'])
66+
];
67+
} elseif (!is_array($resultData['result'])) {
9168
$data = [
9269
'success' => false,
93-
'connectionResult' => $e->getMessage()
70+
'connectionResult' => __("The connection was unsuccessful. Please, check your credentials.")
71+
];
72+
} else {
73+
$data = [
74+
'success' => $success,
75+
'connectionResult' => __("The connection was successful. Your credentials are valid.")
9476
];
9577
}
96-
} else {
97-
array_unshift($errors, __("The connection was unsuccessful."));
78+
} catch (GuzzleException | NoSuchEntityException $e) {
9879
$data = [
9980
'success' => false,
100-
'connectionResult' => implode('<br />', $errors)
81+
'connectionResult' => $e->getMessage()
10182
];
10283
}
10384

10485
return $jsonResult->setData(['result' => $data]);
10586
}
106-
107-
/**
108-
* @param array $apiConnectionData
109-
* @return array
110-
*/
111-
private function validateData(array $apiConnectionData): array
112-
{
113-
$errors = [];
114-
if (empty($apiConnectionData['mode'])) {
115-
$errors[] = __("The 'Api mode' was not recognized. Please, check the data.");
116-
}
117-
118-
if (empty($apiConnectionData['merchant_name'])) {
119-
$errors[] = __("The 'Merchant Name' was not recognized. Please, check the data.");
120-
}
121-
122-
if (empty($apiConnectionData['merchant_password'])) {
123-
$errors[] = __("The 'Merchant Password' was not recognized. Please, check the data.");
124-
}
125-
126-
if (empty($apiConnectionData['merchant_key'])) {
127-
$errors[] = __("The 'Merchant Key' was not recognized. Please, check the data.");
128-
}
129-
130-
return $errors;
131-
}
13287
}

Service/ApiTestService.php

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use CM\Payments\Client\ApiClient;
1414
use CM\Payments\Model\Adminhtml\Source\Mode;
1515
use GuzzleHttp\Client as HttpClient;
16+
use CM\Payments\Api\Config\ConfigInterface;
17+
use Magento\Framework\Exception\NoSuchEntityException;
1618

1719
class ApiTestService implements ApiTestServiceInterface
1820
{
@@ -21,33 +23,59 @@ class ApiTestService implements ApiTestServiceInterface
2123
*/
2224
private $apiConnectionData;
2325

26+
/**
27+
* @var ConfigInterface
28+
*/
29+
private $config;
30+
2431
/**
2532
* ApiTestService constructor
2633
*
27-
* @param array $apiConnectionData
34+
* @param ConfigInterface $config
2835
*/
2936
public function __construct(
30-
array $apiConnectionData
37+
ConfigInterface $config
3138
) {
32-
$this->apiConnectionData = $apiConnectionData;
39+
$this->config = $config;
40+
41+
try {
42+
$this->apiConnectionData = [
43+
'mode' => $this->config->getMode(),
44+
'merchantName' => $this->config->getMerchantName(),
45+
'merchantPassword' => $this->config->getMerchantPassword(),
46+
'merchantKey' => $this->config->getMerchantKey()
47+
];
48+
} catch (NoSuchEntityException $e) {
49+
$this->apiConnectionData = [];
50+
}
3351
}
3452

3553
/**
3654
* @inheritDoc
3755
*/
3856
public function testApiConnection(): array
3957
{
58+
$resultData = ['errors' => [], 'result' => null];
4059
$options = [
4160
'json' => ['date' => date('Y-m-d')]
4261
];
4362

44-
$guzzleResponse = $this->getClient()->request(
45-
RequestInterface::HTTP_GET,
46-
'orders',
47-
$options
48-
);
63+
if (!empty($this->apiConnectionData)) {
64+
$errors = $this->validateData($this->apiConnectionData);
65+
$resultData['errors'] = $errors;
4966

50-
return \GuzzleHttp\json_decode($guzzleResponse->getBody()->getContents(), true);
67+
if (empty($errors)) {
68+
$guzzleResponse = $this->getClient()->request(
69+
RequestInterface::HTTP_GET,
70+
'orders',
71+
$options
72+
);
73+
74+
$resultData['result'] = \GuzzleHttp\json_decode($guzzleResponse->getBody()->getContents(), true);
75+
}
76+
}
77+
78+
return $resultData;
5179
}
5280

5381
/**
@@ -62,6 +90,7 @@ private function getClient(): HttpClient
6290
$this->apiConnectionData['merchantName'] .
6391
':' . $this->apiConnectionData['merchantPassword']
6492
);
93+
6594
return new HttpClient(
6695
[
6796
'base_uri' => $baseApiUrl,
@@ -71,4 +100,29 @@ private function getClient(): HttpClient
71100
]
72101
);
73102
}
103+
104+
/**
105+
* @return array
106+
*/
107+
private function validateData(): array
108+
{
109+
$errors = [];
110+
if (empty($this->apiConnectionData['mode'])) {
111+
$errors[] = __("The 'Api mode' was not recognized. Please, check the data.");
112+
}
113+
114+
if (empty($this->apiConnectionData['merchantName'])) {
115+
$errors[] = __("The 'Merchant Name' was not recognized. Please, check the data.");
116+
}
117+
118+
if (empty($this->apiConnectionData['merchantPassword'])) {
119+
$errors[] = __("The 'Merchant Password' was not recognized. Please, check the data.");
120+
}
121+
122+
if (empty($this->apiConnectionData['merchantKey'])) {
123+
$errors[] = __("The 'Merchant Key' was not recognized. Please, check the data.");
124+
}
125+
126+
return $errors;
127+
}
74128
}

view/adminhtml/templates/system/config/button/check_api_connection.phtml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
"сmpayments-check-api-connection": {
2626
"component": "CM_Payments/js/system/action/check-api-connection",
2727
"config": {
28-
"wrapperContainerSelector": ".cmpayments-result-check-api-connection-wrapper",
29-
"modeContainerSelector": "#cm_payments_general_api_details_mode",
3028
"checkApiConnectionUrl": "<?= $escaper->escapeUrl($block->getApiConnectionCheckUrl()) ?>",
3129
"resultContainerSelector": ".cmpayments-check-api-connection-result"
3230
}

view/adminhtml/web/js/system/action/api-details-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ define([
3232
*/
3333
changeApiDetailsMode: function () {
3434
let modeContainerId = $(this).closest('tr').attr('id'),
35-
parentTable = $(this).parents().find('table'),
35+
parentTable = $(this).closest('table'),
3636
mode = $(this).val();
3737
parentTable.find('tr').each(function () {
3838
let containerId = $(this).attr('id');

0 commit comments

Comments
 (0)