8
8
9
9
namespace CM \Payments \Controller \Adminhtml \Action ;
10
10
11
- use CM \Payments \Api \Service \ApiTestServiceInterfaceFactory ;
12
11
use CM \Payments \Api \Service \ApiTestServiceInterface ;
12
+ use GuzzleHttp \Exception \GuzzleException ;
13
13
use Magento \Backend \App \Action ;
14
14
use Magento \Backend \App \Action \Context ;
15
15
use Magento \Framework \Controller \Result \Json as JsonResult ;
16
16
use Magento \Framework \Controller \Result \JsonFactory as JsonResultFactory ;
17
17
use Magento \Framework \Exception \NoSuchEntityException ;
18
- use GuzzleHttp \Exception \GuzzleException ;
19
18
20
19
class CheckApiConnection extends Action
21
20
{
@@ -25,26 +24,26 @@ class CheckApiConnection extends Action
25
24
private $ jsonResultFactory ;
26
25
27
26
/**
28
- * @var ApiTestServiceInterfaceFactory
27
+ * @var ApiTestServiceInterface
29
28
*/
30
- private $ apiTestServiceFactory ;
29
+ private $ apiTestService ;
31
30
32
31
/**
33
32
* CheckApiConnection constructor
34
33
*
35
34
* @param Context $context
36
35
* @param JsonResultFactory $jsonResultFactory
37
- * @param ApiTestServiceInterfaceFactory $apiTestServiceFactory
36
+ * @param ApiTestServiceInterface $apiTestService
38
37
*/
39
38
public function __construct (
40
39
Context $ context ,
41
40
JsonResultFactory $ jsonResultFactory ,
42
- ApiTestServiceInterfaceFactory $ apiTestServiceFactory
41
+ ApiTestServiceInterface $ apiTestService
43
42
) {
44
43
parent ::__construct ($ context );
45
44
46
45
$ this ->jsonResultFactory = $ jsonResultFactory ;
47
- $ this ->apiTestServiceFactory = $ apiTestServiceFactory ;
46
+ $ this ->apiTestService = $ apiTestService ;
48
47
}
49
48
50
49
/**
@@ -54,79 +53,35 @@ public function execute(): JsonResult
54
53
{
55
54
/** @var JsonResult $jsonResult */
56
55
$ jsonResult = $ this ->jsonResultFactory ->create ();
56
+ $ success = true ;
57
57
58
- $ apiConnectionData = $ this ->getRequest ()->getParams ();
59
- $ errors = $ this ->validateData ($ apiConnectionData );
60
-
61
- if (!$ errors ) {
62
- $ success = true ;
58
+ try {
59
+ $ resultData = $ this ->apiTestService ->testApiConnection ();
63
60
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 ' ])) {
91
68
$ data = [
92
69
'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. " )
94
76
];
95
77
}
96
- } else {
97
- array_unshift ($ errors , __ ("The connection was unsuccessful. " ));
78
+ } catch (GuzzleException | NoSuchEntityException $ e ) {
98
79
$ data = [
99
80
'success ' => false ,
100
- 'connectionResult ' => implode ( ' <br /> ' , $ errors )
81
+ 'connectionResult ' => $ e -> getMessage ( )
101
82
];
102
83
}
103
84
104
85
return $ jsonResult ->setData (['result ' => $ data ]);
105
86
}
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
- }
132
87
}
0 commit comments