1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @project Promopult Integra client library |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Promopult\Integra; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Client |
10
|
|
|
* |
11
|
|
|
* @method \Promopult\Integra\Response hello(array $data) |
12
|
|
|
* @method \Promopult\Integra\Response createUser(array $data) |
13
|
|
|
* @method \Promopult\Integra\Response cryptLogin(array $data) |
14
|
|
|
* @method \Promopult\Integra\Response archiveUser(array $data) |
15
|
|
|
* @method \Promopult\Integra\Response unarchiveUser(array $data) |
16
|
|
|
* @method \Promopult\Integra\Response doPayment(array $data) |
17
|
|
|
* @method \Promopult\Integra\Response confirmPayment(array $data) |
18
|
|
|
* @method \Promopult\Integra\Response declinePayment(array $data) |
19
|
|
|
* @method \Promopult\Integra\Response getUserData(array $data) |
20
|
|
|
* @method \Promopult\Integra\Response getUsersData(array $data) |
21
|
|
|
* @method \Promopult\Integra\Response getUserMessages(array $data) |
22
|
|
|
* @method \Promopult\Integra\Response getMessages(array $data) |
23
|
|
|
* @method \Promopult\Integra\Response getMessageTemplates(array $data) |
24
|
|
|
* @method \Promopult\Integra\Response readMessages(array $data) |
25
|
|
|
* @method \Promopult\Integra\Response changeUrl(array $data) |
26
|
|
|
* |
27
|
|
|
* @author Dmitry Gladyshev <[email protected]> |
28
|
|
|
* @since 1.0 |
29
|
|
|
*/ |
30
|
|
|
class Client implements \Promopult\Integra\TransportInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var \Promopult\Integra\IdentityInterface |
34
|
|
|
*/ |
35
|
|
|
protected $identity; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Promopult\Integra\CryptInterface |
39
|
|
|
*/ |
40
|
|
|
protected $crypt; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Psr\Http\Client\ClientInterface |
44
|
|
|
*/ |
45
|
|
|
protected $httpClient; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Psr\Http\Message\RequestInterface |
49
|
|
|
*/ |
50
|
|
|
protected $lastHttpRequest; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Psr\Http\Message\ResponseInterface |
54
|
|
|
*/ |
55
|
|
|
protected $lastHttpResponse; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Client constructor. |
59
|
|
|
* |
60
|
|
|
* @param \Promopult\Integra\IdentityInterface $identity |
61
|
|
|
* @param \Promopult\Integra\CryptInterface $crypt |
62
|
|
|
* @param \Psr\Http\Client\ClientInterface $httpClient |
63
|
|
|
*/ |
64
|
|
|
public function __construct( |
65
|
|
|
\Promopult\Integra\IdentityInterface $identity, |
66
|
|
|
\Promopult\Integra\CryptInterface $crypt, |
67
|
|
|
\Psr\Http\Client\ClientInterface $httpClient = null |
68
|
|
|
) { |
69
|
|
|
$this->identity = $identity; |
70
|
|
|
$this->crypt = $crypt; |
71
|
|
|
$this->httpClient = $httpClient; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $methodName |
76
|
|
|
* @param array $ars |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
79
|
|
|
*/ |
80
|
|
|
public function __call(string $methodName, array $ars = []): \Promopult\Integra\ResponseInterface |
81
|
|
|
{ |
82
|
|
|
$request = new \Promopult\Integra\Request( |
83
|
|
|
$methodName, |
84
|
|
|
$ars[0] ?? [], |
85
|
|
|
$this->identity, |
86
|
|
|
$this->crypt |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $this->send($request); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function send(\Promopult\Integra\RequestInterface $request): \Promopult\Integra\ResponseInterface |
96
|
|
|
{ |
97
|
|
|
$httpRequest = new \GuzzleHttp\Psr7\Request('POST', $request->getCryptUrl(), [ |
98
|
|
|
'Content-Type' => 'application/json' |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$this->lastHttpRequest = $httpRequest; |
102
|
|
|
|
103
|
|
|
$httpResponse = $this->getHttpClient()->sendRequest($httpRequest); |
104
|
|
|
|
105
|
|
|
$this->lastHttpResponse = $httpResponse; |
106
|
|
|
|
107
|
|
|
return \Promopult\Integra\Response::fromHttpResponse($httpResponse); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return \Psr\Http\Client\ClientInterface |
112
|
|
|
*/ |
113
|
|
|
protected function getHttpClient(): \Psr\Http\Client\ClientInterface |
114
|
|
|
{ |
115
|
|
|
if (empty($this->httpClient)) { |
116
|
|
|
$this->httpClient = new \Http\Adapter\Guzzle6\Client; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->httpClient; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/***************/ |
123
|
|
|
/* Debug stuff */ |
124
|
|
|
/***************/ |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return \Psr\Http\Message\ResponseInterface|null |
128
|
|
|
*/ |
129
|
|
|
public function getLastHttpResponse(): ?\Psr\Http\Message\ResponseInterface |
130
|
|
|
{ |
131
|
|
|
return $this->lastHttpResponse; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return \Psr\Http\Message\RequestInterface|null |
136
|
|
|
*/ |
137
|
|
|
public function getLastHttpRequest(): ?\Psr\Http\Message\RequestInterface |
138
|
|
|
{ |
139
|
|
|
return $this->lastHttpRequest; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getLastHttpResponseAsString(): string |
146
|
|
|
{ |
147
|
|
|
if ($this->lastHttpResponse instanceof \Psr\Http\Message\MessageInterface) { |
|
|
|
|
148
|
|
|
return \GuzzleHttp\Psr7\str($this->getLastHttpResponse()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return ''; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getLastHttpRequestAsString(): string |
158
|
|
|
{ |
159
|
|
|
if ($this->lastHttpRequest instanceof \Psr\Http\Message\MessageInterface) { |
|
|
|
|
160
|
|
|
return \GuzzleHttp\Psr7\str($this->getLastHttpRequest()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return ''; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|