|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace floor12\MindBox; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use floor12\MindBox\Exceptions\EmptyApiEndPointException; |
|
8
|
|
|
use floor12\MindBox\Exceptions\EmptyApiKeyException; |
|
9
|
|
|
use GuzzleHttp\Client; |
|
10
|
|
|
use GuzzleHttp\ClientInterface; |
|
11
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
12
|
|
|
use GuzzleHttp\Psr7\Request; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use Ramsey\Uuid\Uuid; |
|
15
|
|
|
|
|
16
|
|
|
class MindBoxClient |
|
17
|
|
|
{ |
|
18
|
|
|
const MODE_ASYNCHRONOUS = 0; |
|
19
|
|
|
const MODE_SYNCHRONOUS = 1; |
|
20
|
|
|
const ASYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/async'; |
|
21
|
|
|
const SYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/sync'; |
|
22
|
|
|
const DEFAULT_HTTP_TIMEOUT = 2; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $endpointId; |
|
26
|
|
|
/** @var ClientInterface */ |
|
27
|
|
|
private $client; |
|
28
|
|
|
/** @var string[] */ |
|
29
|
|
|
private $headers; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $secretKey; |
|
32
|
|
|
/** @var ResponseInterface */ |
|
33
|
|
|
private $response; |
|
34
|
|
|
/** @var Request */ |
|
35
|
|
|
private $httpRequest; |
|
36
|
|
|
/** @var int */ |
|
37
|
|
|
private $httpTimeOut = self::DEFAULT_HTTP_TIMEOUT; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $secretKey |
|
41
|
|
|
* @param string $endpointId |
|
42
|
|
|
* @param ClientInterface|null $client |
|
43
|
|
|
* @throws EmptyApiEndPointException |
|
44
|
|
|
* @throws EmptyApiKeyException |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function __construct( |
|
47
|
|
|
string $secretKey, |
|
48
|
|
|
string $endpointId, |
|
49
|
|
|
ClientInterface $client = null) |
|
50
|
|
|
{ |
|
51
|
3 |
|
$this->client = $client ?? new Client(); |
|
52
|
|
|
|
|
53
|
3 |
|
$this->secretKey = $secretKey; |
|
54
|
3 |
|
$this->endpointId = $endpointId; |
|
55
|
|
|
|
|
56
|
3 |
|
if (empty($this->secretKey)) |
|
57
|
1 |
|
throw new EmptyApiKeyException(); |
|
58
|
|
|
|
|
59
|
2 |
|
if (empty($this->endpointId)) |
|
60
|
1 |
|
throw new EmptyApiEndPointException(); |
|
61
|
|
|
|
|
62
|
1 |
|
$this->headers = [ |
|
63
|
1 |
|
'Content-Type' => 'application/json; charset=utf-8', |
|
64
|
1 |
|
'Accept' => 'application/json', |
|
65
|
1 |
|
'Authorization' => "Mindbox secretKey=\"{$this->secretKey}\"" |
|
66
|
|
|
]; |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ClientInterface $client |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setClient(ClientInterface $client): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->client = $client; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param MindBoxRequest $mindBoxRequest |
|
79
|
|
|
* @throws GuzzleException |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function sendData(MindBoxRequest $mindBoxRequest): void |
|
82
|
|
|
{ |
|
83
|
|
|
$httpRequestParams = [ |
|
84
|
1 |
|
'endpointId' => $this->endpointId, |
|
85
|
1 |
|
'operation' => $mindBoxRequest->getOperationName(), |
|
86
|
|
|
'transactionId' => Uuid::uuid4()->toString() |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
1 |
|
|
|
90
|
1 |
|
if ($mindBoxRequest->getDeviceUUID()) { |
|
91
|
|
|
$httpRequestParams['deviceUUID'] = $mindBoxRequest->getDeviceUUID(); |
|
92
|
|
|
} |
|
93
|
1 |
|
|
|
94
|
|
|
$baseUrl = $mindBoxRequest->isAsync() ? self::ASYNC_MINDBOX_API_URL : self::SYNC_MINDBOX_API_URL; |
|
95
|
1 |
|
|
|
96
|
1 |
|
$this->httpRequest = new Request( |
|
97
|
1 |
|
'POST', |
|
98
|
1 |
|
$baseUrl . '?' . http_build_query($httpRequestParams), |
|
99
|
1 |
|
$this->headers, |
|
100
|
|
|
$mindBoxRequest->getBodyAsJson() |
|
101
|
1 |
|
); |
|
102
|
1 |
|
$this->response = $this->client->send($this->httpRequest); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return ResponseInterface |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getResponse(): ResponseInterface |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->response; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return Request |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getHttpRequest(): Request |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->httpRequest; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param int $httpTimeOut |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setHttpTimeOut(int $httpTimeOut): void |
|
125
|
|
|
{ |
|
126
|
|
|
$this->httpTimeOut = $httpTimeOut; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|