1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Postpay\Http; |
4
|
|
|
|
5
|
|
|
use Postpay\Postpay; |
6
|
|
|
|
7
|
|
|
class Request |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @const string API URL. |
11
|
|
|
*/ |
12
|
|
|
const API_URL = 'https://<subdomain>.postpay.io'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @const string Accept HTTP header. |
16
|
|
|
*/ |
17
|
|
|
const ACCEPT_HEADER = 'application/vnd.postpay+json; version='; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string The HTTP method. |
21
|
|
|
*/ |
22
|
|
|
protected $method; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string The path URL. |
26
|
|
|
*/ |
27
|
|
|
protected $path; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array The basic auth credentials. |
31
|
|
|
*/ |
32
|
|
|
protected $auth = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array The headers to send with the request. |
36
|
|
|
*/ |
37
|
|
|
protected $headers = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array The parameters to send with the request. |
41
|
|
|
*/ |
42
|
|
|
protected $params = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string API version. |
46
|
|
|
*/ |
47
|
|
|
protected $apiVersion; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool Set to true for sandbox request. |
51
|
|
|
*/ |
52
|
|
|
protected $sandbox; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Creates a new Request entity. |
56
|
|
|
* |
57
|
|
|
* @param string $method |
58
|
|
|
* @param string|null $path |
59
|
|
|
* @param array $params |
60
|
|
|
* @param array $auth |
61
|
|
|
* @param string|null $apiVersion |
62
|
|
|
* @param bool $sandbox |
63
|
|
|
*/ |
64
|
31 |
|
public function __construct( |
65
|
|
|
$method, |
66
|
|
|
$path = null, |
67
|
|
|
array $params = [], |
68
|
|
|
array $auth = [], |
69
|
|
|
$apiVersion = null, |
70
|
|
|
$sandbox = false |
71
|
|
|
) { |
72
|
31 |
|
$this->setMethod($method); |
73
|
31 |
|
$this->path = $path; |
74
|
31 |
|
$this->params = $params; |
75
|
31 |
|
$this->auth = $auth; |
76
|
31 |
|
$this->apiVersion = $apiVersion ?: Postpay::DEFAULT_API_VERSION; |
77
|
31 |
|
$this->sandbox = $sandbox; |
78
|
31 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the HTTP method. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
10 |
|
public function getMethod() |
86
|
|
|
{ |
87
|
10 |
|
return $this->method; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the HTTP method. |
92
|
|
|
* |
93
|
|
|
* @param string |
94
|
|
|
*/ |
95
|
31 |
|
public function setMethod($method) |
96
|
|
|
{ |
97
|
31 |
|
$this->method = strtoupper($method); |
98
|
31 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the path URL. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1 |
|
public function getPath() |
106
|
|
|
{ |
107
|
1 |
|
return $this->path; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the basic auth credentials. |
112
|
|
|
* |
113
|
|
|
* @return array|null |
114
|
|
|
*/ |
115
|
4 |
|
public function getAuth() |
116
|
|
|
{ |
117
|
4 |
|
return $this->auth; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the basic auth credentials. |
122
|
|
|
* |
123
|
|
|
* @param array $auth |
124
|
|
|
*/ |
125
|
1 |
|
public function setAuth(array $auth) |
126
|
|
|
{ |
127
|
1 |
|
$this->auth = $auth; |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Generate and return HTTP headers. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
5 |
|
public function getHeaders() |
136
|
|
|
{ |
137
|
5 |
|
return array_merge($this->headers, $this->getDefaultHeaders()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the headers. |
142
|
|
|
* |
143
|
|
|
* @param array $headers |
144
|
|
|
*/ |
145
|
2 |
|
public function setHeaders(array $headers) |
146
|
|
|
{ |
147
|
2 |
|
$this->headers = array_merge($this->headers, $headers); |
148
|
2 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return the default headers. |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
5 |
|
public function getDefaultHeaders() |
156
|
|
|
{ |
157
|
|
|
return [ |
158
|
5 |
|
'Accept-Encoding' => '*', |
159
|
5 |
|
'Accept' => static::ACCEPT_HEADER . $this->apiVersion, |
160
|
5 |
|
'Content-Type' => 'application/json', |
161
|
5 |
|
'User-Agent' => 'postpay-php:' . Postpay::VERSION, |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return the params. |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
1 |
|
public function getParams() |
171
|
|
|
{ |
172
|
1 |
|
return $this->params; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set the params. |
177
|
|
|
* |
178
|
|
|
* @param array $params |
179
|
|
|
*/ |
180
|
2 |
|
public function setParams(array $params) |
181
|
|
|
{ |
182
|
2 |
|
$this->params = $params; |
183
|
2 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return JSON params on POST requests. |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
3 |
|
public function json() |
191
|
|
|
{ |
192
|
3 |
|
if ($this->method !== 'GET') { |
193
|
2 |
|
return $this->params; |
194
|
|
|
} |
195
|
2 |
|
return []; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* The API version. |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
1 |
|
public function getApiVersion() |
204
|
|
|
{ |
205
|
1 |
|
return $this->apiVersion; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set the API version. |
210
|
|
|
* |
211
|
|
|
* @param string $apiVersion |
212
|
|
|
*/ |
213
|
2 |
|
public function setApiVersion($apiVersion) |
214
|
|
|
{ |
215
|
2 |
|
$this->apiVersion = $apiVersion; |
216
|
2 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Return true if it's a GraphQL request. |
220
|
|
|
* |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
21 |
|
public function isGraphQL() |
224
|
|
|
{ |
225
|
21 |
|
return $this->apiVersion === Postpay::GRAPHQL_VERSION; |
226
|
|
|
} |
227
|
|
|
|
228
|
4 |
|
public function getApiUrl() |
229
|
|
|
{ |
230
|
4 |
|
$subdomain = $this->sandbox ? 'sandbox' : 'api'; |
231
|
4 |
|
return str_replace('<subdomain>', $subdomain, static::API_URL); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Generate and return the URL. |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
4 |
|
public function getUrl() |
240
|
|
|
{ |
241
|
4 |
|
$path = Url::slashPrefix($this->path); |
242
|
|
|
|
243
|
4 |
|
if ($this->method === 'GET') { |
244
|
3 |
|
$path = Url::addParamsToUrl($path, $this->params); |
245
|
|
|
} |
246
|
4 |
|
return $this->getApiUrl() . $path; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|