|
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\AdyenApi\Business\Adapter; |
|
9
|
|
|
|
|
10
|
|
|
use GuzzleHttp\Client; |
|
11
|
|
|
use GuzzleHttp\RequestOptions; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use SprykerEco\Zed\AdyenApi\AdyenApiConfig; |
|
14
|
|
|
use SprykerEco\Zed\AdyenApi\Dependency\Service\AdyenApiToUtilEncodingServiceInterface; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractAdapter implements AdyenApiAdapterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected const DEFAULT_TIMEOUT = 45; |
|
19
|
|
|
protected const HEADER_CONTENT_TYPE_KEY = 'Content-Type'; |
|
20
|
|
|
protected const HEADER_CONTENT_TYPE_VALUE = 'application/json'; |
|
21
|
|
|
protected const HEADER_X_API_KEY = 'X-API-Key'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \SprykerEco\Zed\AdyenApi\AdyenApiConfig |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \SprykerEco\Zed\AdyenApi\Dependency\Service\AdyenApiToUtilEncodingServiceInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $encodingService; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \GuzzleHttp\Client |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $client; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract protected function getUrl(): string; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param \SprykerEco\Zed\AdyenApi\AdyenApiConfig $config |
|
45
|
|
|
* @param \SprykerEco\Zed\AdyenApi\Dependency\Service\AdyenApiToUtilEncodingServiceInterface $encodingService |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
AdyenApiConfig $config, |
|
49
|
|
|
AdyenApiToUtilEncodingServiceInterface $encodingService |
|
50
|
|
|
) { |
|
51
|
|
|
$this->client = new Client([ |
|
52
|
|
|
RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT, |
|
53
|
|
|
RequestOptions::HEADERS => [ |
|
54
|
|
|
static::HEADER_CONTENT_TYPE_KEY => static::HEADER_CONTENT_TYPE_VALUE, |
|
55
|
|
|
static::HEADER_X_API_KEY => $config->getApiKey(), |
|
56
|
|
|
], |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$this->config = $config; |
|
60
|
|
|
$this->encodingService = $encodingService; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $data |
|
65
|
|
|
* |
|
66
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public function sendRequest(array $data): ResponseInterface |
|
69
|
|
|
{ |
|
70
|
|
|
$options[RequestOptions::BODY] = $this->encodingService->encodeJson($data); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return $this->send($options); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $options |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function send(array $options = []): ResponseInterface |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->client->post( |
|
83
|
|
|
$this->getUrl(), |
|
84
|
|
|
$options |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|