|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\Http; |
|
9
|
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\AfterPayApiResponseErrorTransfer; |
|
|
|
|
|
|
11
|
|
|
use GuzzleHttp\Client; |
|
12
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
13
|
|
|
use GuzzleHttp\Psr7\Request; |
|
14
|
|
|
use Psr\Http\Message\RequestInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Psr\Http\Message\StreamInterface; |
|
17
|
|
|
use SprykerEco\Zed\AfterPay\AfterPayConfig; |
|
18
|
|
|
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface; |
|
19
|
|
|
use SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException; |
|
20
|
|
|
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface; |
|
21
|
|
|
|
|
22
|
|
|
class Guzzle implements ClientInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public const REQUEST_METHOD_POST = 'POST'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
public const REQUEST_METHOD_GET = 'GET'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public const REQUEST_HEADER_X_AUTH_KEY = 'X-Auth-Key'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
public const REQUEST_HEADER_CONTENT_TYPE = 'Content-Type'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
public const HEADER_CONTENT_TYPE_JSON = 'application/json'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected const ERROR_KEY_ACTION_CODE = 'actionCode'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected const ERROR_KEY_CODE = 'code'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected const ERROR_KEY_TYPE = 'type'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected const ERROR_KEY_MESSAGE = 'message'; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $encodingService; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var \GuzzleHttp\ClientInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $client; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var \SprykerEco\Zed\AfterPay\AfterPayConfig |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $config; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface $encodingService |
|
86
|
|
|
* @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __construct( |
|
89
|
|
|
AfterPayToUtilEncodingServiceInterface $encodingService, |
|
90
|
|
|
AfterPayConfig $config |
|
91
|
|
|
) { |
|
92
|
|
|
$this->encodingService = $encodingService; |
|
93
|
|
|
$this->config = $config; |
|
94
|
|
|
$this->client = new Client(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $endPointUrl |
|
99
|
|
|
* @param string|null $jsonBody |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Psr\Http\Message\StreamInterface |
|
102
|
|
|
*/ |
|
103
|
|
|
public function sendPost(string $endPointUrl, ?string $jsonBody = null): StreamInterface |
|
104
|
|
|
{ |
|
105
|
|
|
$postRequest = $this->buildPostRequest($endPointUrl, $jsonBody); |
|
106
|
|
|
$response = $this->send($postRequest); |
|
107
|
|
|
|
|
108
|
|
|
return $response->getBody(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $endPointUrl |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function sendGet(string $endPointUrl): string |
|
117
|
|
|
{ |
|
118
|
|
|
$getRequest = $this->buildGetRequest($endPointUrl); |
|
119
|
|
|
$response = $this->send($getRequest); |
|
120
|
|
|
|
|
121
|
|
|
return $response->getBody()->getContents(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $endPointUrl |
|
126
|
|
|
* |
|
127
|
|
|
* @return int |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getStatus(string $endPointUrl): int |
|
130
|
|
|
{ |
|
131
|
|
|
$getRequest = $this->buildGetRequest($endPointUrl); |
|
132
|
|
|
$response = $this->send($getRequest); |
|
133
|
|
|
|
|
134
|
|
|
return $response->getStatusCode(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
|
139
|
|
|
* @param array $options |
|
140
|
|
|
* |
|
141
|
|
|
* @throws \SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException |
|
142
|
|
|
* |
|
143
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function send(RequestInterface $request, array $options = []): ResponseInterface |
|
146
|
|
|
{ |
|
147
|
|
|
try { |
|
148
|
|
|
return $this->client->send($request, $options); |
|
149
|
|
|
} catch (RequestException $requestException) { |
|
150
|
|
|
$apiHttpRequestException = new ApiHttpRequestException($requestException->getMessage()); |
|
151
|
|
|
|
|
152
|
|
|
$responseContent = $this->getExceptionResponseContent($requestException); |
|
153
|
|
|
$errorsResponseData = $this->encodingService->decodeJson($responseContent, true); |
|
154
|
|
|
|
|
155
|
|
|
if (isset($errorsResponseData[0])) { |
|
156
|
|
|
$afterPayApiResponseErrorTransfer = $this->createAfterPayApiResponseErrorTransfer($errorsResponseData[0]); |
|
157
|
|
|
|
|
158
|
|
|
$apiHttpRequestException->setError($afterPayApiResponseErrorTransfer); |
|
159
|
|
|
$apiHttpRequestException->setDetailedMessage($responseContent); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
throw $apiHttpRequestException; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param array $errorDetails |
|
168
|
|
|
* |
|
169
|
|
|
* @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer\AfterPayApiResponseErrorTransfer |
|
|
|
|
|
|
170
|
|
|
*/ |
|
171
|
|
|
protected function createAfterPayApiResponseErrorTransfer(array $errorDetails): AfterPayApiResponseErrorTransfer |
|
172
|
|
|
{ |
|
173
|
|
|
return (new AfterPayApiResponseErrorTransfer()) |
|
174
|
|
|
->setActionCode($errorDetails[static::ERROR_KEY_ACTION_CODE]) |
|
175
|
|
|
->setCode($errorDetails[static::ERROR_KEY_CODE]) |
|
176
|
|
|
->setType($errorDetails[static::ERROR_KEY_TYPE]) |
|
177
|
|
|
->setMessage($errorDetails[static::ERROR_KEY_MESSAGE]) |
|
178
|
|
|
->setIsSuccess(false); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param \GuzzleHttp\Exception\RequestException $requestException |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function getExceptionResponseContent(RequestException $requestException): string |
|
187
|
|
|
{ |
|
188
|
|
|
$response = $requestException->getResponse(); |
|
189
|
|
|
|
|
190
|
|
|
return $response instanceof ResponseInterface ? $response->getBody()->getContents() : ''; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param string $endPointUrl |
|
195
|
|
|
* @param string|null $jsonBody |
|
196
|
|
|
* |
|
197
|
|
|
* @return \Psr\Http\Message\RequestInterface |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function buildPostRequest(string $endPointUrl, ?string $jsonBody = null): RequestInterface |
|
200
|
|
|
{ |
|
201
|
|
|
return new Request( |
|
202
|
|
|
static::REQUEST_METHOD_POST, |
|
203
|
|
|
$endPointUrl, |
|
204
|
|
|
[ |
|
205
|
|
|
(string)static::REQUEST_HEADER_CONTENT_TYPE => (string)static::HEADER_CONTENT_TYPE_JSON, |
|
206
|
|
|
(string)static::REQUEST_HEADER_X_AUTH_KEY => (string)$this->config->getApiCredentialsAuthKey(), |
|
207
|
|
|
], |
|
208
|
|
|
$jsonBody, |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param string $endPointUrl |
|
214
|
|
|
* |
|
215
|
|
|
* @return \Psr\Http\Message\RequestInterface |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function buildGetRequest(string $endPointUrl): RequestInterface |
|
218
|
|
|
{ |
|
219
|
|
|
return new Request( |
|
220
|
|
|
static::REQUEST_METHOD_GET, |
|
221
|
|
|
$endPointUrl, |
|
222
|
|
|
[ |
|
223
|
|
|
(string)static::REQUEST_HEADER_CONTENT_TYPE => (string)static::HEADER_CONTENT_TYPE_JSON, |
|
224
|
|
|
(string)static::REQUEST_HEADER_X_AUTH_KEY => (string)$this->config->getApiCredentialsAuthKey(), |
|
225
|
|
|
], |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths