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