1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Omnipay\IcepayPayments\Message; |
6
|
|
|
|
7
|
|
|
use DateTimeInterface; |
8
|
|
|
use Omnipay\Common\Message\AbstractRequest as OmnipayAbstractRequest; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
|
12
|
|
|
abstract class AbstractRequest extends OmnipayAbstractRequest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Timestamp format according Icepay documentation. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public const TIMESTAMP_FORMAT = 'Y-m-d\TH:i:s\Z'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
2 |
|
public function getData(): array |
25
|
|
|
{ |
26
|
2 |
|
$this->validate('contractProfileId', 'secretKey'); |
27
|
|
|
|
28
|
1 |
|
return []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the base URL of the API. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
3 |
|
public function getBaseUrl(): string |
37
|
|
|
{ |
38
|
3 |
|
return $this->getParameter('baseUrl'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the base URL of the API. |
43
|
|
|
* |
44
|
|
|
* @param string $baseUrl |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
10 |
|
public function setBaseUrl(string $baseUrl): self |
49
|
|
|
{ |
50
|
10 |
|
return $this->setParameter('baseUrl', $baseUrl); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the ContractProfileId (also known as the UserId). |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
4 |
|
public function getContractProfileId(): string |
59
|
|
|
{ |
60
|
4 |
|
return $this->getParameter('contractProfileId'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the ContractProfileId (also known as the UserId). |
65
|
|
|
* |
66
|
|
|
* @param string $contractProfileId |
67
|
|
|
* |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
9 |
|
public function setContractProfileId(string $contractProfileId): self |
71
|
|
|
{ |
72
|
9 |
|
return $this->setParameter('contractProfileId', $contractProfileId); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get Secret Key. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function getSecretKey(): string |
81
|
|
|
{ |
82
|
3 |
|
return $this->getParameter('secretKey'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the Secret Key. |
87
|
|
|
* |
88
|
|
|
* @param string $secretKey |
89
|
|
|
* |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
9 |
|
public function setSecretKey(string $secretKey): self |
93
|
|
|
{ |
94
|
9 |
|
return $this->setParameter('secretKey', $secretKey); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the currency code. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
1 |
|
public function getCurrencyCode(): string |
103
|
|
|
{ |
104
|
1 |
|
return $this->getParameter('currencyCode'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the currency code. |
109
|
|
|
* |
110
|
|
|
* @param string $currencyCode |
111
|
|
|
* |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
2 |
|
public function setCurrencyCode(string $currencyCode): self |
115
|
|
|
{ |
116
|
2 |
|
return $this->setParameter('currencyCode', $currencyCode); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the country code. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
1 |
|
public function getCountryCode(): string |
125
|
|
|
{ |
126
|
1 |
|
return $this->getParameter('countryCode'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set the country code. |
131
|
|
|
* |
132
|
|
|
* @param string $countryCode |
133
|
|
|
* |
134
|
|
|
* @return self |
135
|
|
|
*/ |
136
|
2 |
|
public function setCountryCode(string $countryCode): self |
137
|
|
|
{ |
138
|
2 |
|
return $this->setParameter('countryCode', $countryCode); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the issuer code. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
1 |
|
public function getIssuerCode(): string |
147
|
|
|
{ |
148
|
1 |
|
return $this->getParameter('issuerCode'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the issuer code. |
153
|
|
|
* |
154
|
|
|
* @param string $issuerCode |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
2 |
|
public function setIssuerCode(string $issuerCode): self |
159
|
|
|
{ |
160
|
2 |
|
return $this->setParameter('issuerCode', $issuerCode); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the language code. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
1 |
|
public function getLanguageCode(): string |
169
|
|
|
{ |
170
|
1 |
|
return $this->getParameter('languageCode'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the language code. |
175
|
|
|
* |
176
|
|
|
* @param string $languageCode |
177
|
|
|
* |
178
|
|
|
* @return self |
179
|
|
|
*/ |
180
|
2 |
|
public function setLanguageCode(string $languageCode): self |
181
|
|
|
{ |
182
|
2 |
|
return $this->setParameter('languageCode', $languageCode); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the reference. |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getReference(): string |
191
|
|
|
{ |
192
|
|
|
return $this->getParameter('reference'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set the reference. |
197
|
|
|
* |
198
|
|
|
* @param string $reference |
199
|
|
|
* |
200
|
|
|
* @return self |
201
|
|
|
*/ |
202
|
|
|
public function setReference(string $reference): self |
203
|
|
|
{ |
204
|
|
|
return $this->setParameter('reference', $reference); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the timestamp. |
209
|
|
|
* |
210
|
|
|
* @return DateTimeInterface |
211
|
|
|
*/ |
212
|
1 |
|
public function getTimestamp(): DateTimeInterface |
213
|
|
|
{ |
214
|
1 |
|
return $this->getParameter('timestamp'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set the timestamp as string value. |
219
|
|
|
* |
220
|
|
|
* @param DateTimeInterface $timestamp |
221
|
|
|
* |
222
|
|
|
* @return self |
223
|
|
|
*/ |
224
|
7 |
|
public function setTimestamp(DateTimeInterface $timestamp): self |
225
|
|
|
{ |
226
|
7 |
|
return $this->setParameter('timestamp', $timestamp); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return the Symfony HttpRequest. |
231
|
|
|
* |
232
|
|
|
* @see AbstractRequest::$httpClient |
233
|
|
|
* |
234
|
|
|
* @return Request |
235
|
|
|
*/ |
236
|
2 |
|
public function getHttpRequest(): Request |
237
|
|
|
{ |
238
|
2 |
|
return $this->httpRequest; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Send the request to the API of the payment service provider. |
243
|
|
|
* |
244
|
|
|
* @param string $requestMethod |
245
|
|
|
* @param string $requestPath |
246
|
|
|
* @param array|null $data |
247
|
|
|
* |
248
|
|
|
* @return ResponseInterface |
249
|
|
|
*/ |
250
|
3 |
|
protected function sendRequest(string $requestMethod, string $requestPath, ?array $data = null): ResponseInterface |
251
|
|
|
{ |
252
|
3 |
|
$requestUrl = sprintf('%s%s', $this->getBaseUrl(), $requestPath); |
253
|
3 |
|
$requestHeaders = $this->getAuthenticationHeaders( |
254
|
3 |
|
$this->createChecksum($requestUrl, $requestMethod, $data) |
255
|
|
|
); |
256
|
|
|
|
257
|
3 |
|
$response = $this->httpClient->request( |
258
|
3 |
|
$requestMethod, |
259
|
3 |
|
$requestUrl, |
260
|
3 |
|
$requestHeaders, |
261
|
3 |
|
json_encode($data) |
262
|
|
|
); |
263
|
|
|
|
264
|
3 |
|
return $response; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get the json response data from the request. |
269
|
|
|
* |
270
|
|
|
* @param ResponseInterface $response |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
3 |
|
protected function getResponseBody(ResponseInterface $response): array |
275
|
|
|
{ |
276
|
3 |
|
return json_decode($response->getBody()->getContents(), true) ?? []; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Create a checksum based on the request method, path, secret and data. |
281
|
|
|
* |
282
|
|
|
* @see https://documentation.icepay.com/payments/checksum/ |
283
|
|
|
* |
284
|
|
|
* @param string $requestUrl full request url to the API endpoint |
285
|
|
|
* @param string $requestMethod Request method e.g. POST or GET |
286
|
|
|
* @param array|null $data |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
3 |
|
private function createChecksum(string $requestUrl, string $requestMethod, array $data = null): string |
291
|
|
|
{ |
292
|
3 |
|
return base64_encode( |
293
|
3 |
|
hash_hmac( |
294
|
3 |
|
'sha256', |
295
|
3 |
|
sprintf( |
296
|
3 |
|
'%s%s%s%s', |
297
|
3 |
|
$requestUrl, |
298
|
3 |
|
$requestMethod, |
299
|
3 |
|
$this->getContractProfileId(), |
300
|
3 |
|
json_encode($data) |
301
|
|
|
), |
302
|
3 |
|
base64_decode($this->getSecretKey()), |
303
|
3 |
|
true |
304
|
|
|
) |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Get the authentication headers information. |
310
|
|
|
* |
311
|
|
|
* @param string $checksum |
312
|
|
|
* |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
3 |
|
private function getAuthenticationHeaders(string $checksum): array |
316
|
|
|
{ |
317
|
|
|
return [ |
318
|
3 |
|
'Content-Type' => 'application/json', |
319
|
3 |
|
'ContractProfileId' => $this->getContractProfileId(), |
320
|
3 |
|
'CHECKSUM' => $checksum, |
321
|
|
|
]; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|