Test Failed
Push — 3ds-authentication ( 8aad35 )
by Kiet
09:46
created

AbstractRequest::setBaseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Omnipay\IcepayPayments\Message;
4
5
use DateTimeInterface;
6
use Omnipay\Common\Message\AbstractRequest as OmnipayAbstractRequest;
7
use Psr\Http\Message\ResponseInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
10
/**
11
 * Class AbstractRequest.
12
 */
13
abstract class AbstractRequest extends OmnipayAbstractRequest
14
{
15
    /**
16
     * @var string
17
     */
18
    public const TIMESTAMP_FORMAT = 'Y-m-d\TH:i:s\Z';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 5
    public function getData(): array
24
    {
25 5
        $this->validate('contractProfileId', 'secretKey');
26
27 5
        return [];
28
    }
29
30
    /**
31
     * Send the request to the API of the Payment Service Provider.
32
     * The base url and the authentication headers are automatically added.
33
     *
34
     * @param string $method
35
     * @param string $urlPath
36
     * @param array  $data
37
     *
38
     * @return ResponseInterface
39
     */
40 4
    protected function sendRequest(string $method, string $urlPath, array $data): ResponseInterface
41
    {
42 4
        $securityHash = $this->getSecurityHash($method, $urlPath, $data);
43 4
        $headers = $this->getAuthenticationHeaders($securityHash);
44 4
        $body = null;
45
46 4
        if ($method === Request::METHOD_POST) {
47 4
            $headers['Content-Type'] = 'application/json';
48 4
            $body = json_encode($data);
49
        }
50
51 4
        $this->response = $this->httpClient->request(
52 4
            $method,
53 4
            $this->getBaseUrl().$urlPath,
54 4
            $headers,
55 4
            $body
56
        );
57
58 4
        return $this->response;
59
    }
60
61
    /**
62
     * Returns the JSON decoded response body.
63
     *
64
     * @return array
65
     */
66 4
    protected function getResponseBody(): array
67
    {
68 4
        $responseBody = json_decode($this->getResponse()->getBody()->getContents(), true);
69 4
        if (is_array($responseBody) === false) {
70 4
            $responseBody = [];
71
        }
72
73 4
        return $responseBody;
74
    }
75
76
    /**
77
     * Safety hash from icepay, to be generated after putting in all the data.
78
     *
79
     * @param string          $requestMethod
80
     * @param string          $urlPath
81
     * @param array|\stdClass $data
82
     * @param bool            $urlIsFullUrl  = false. True to have $urlPath be the absolute (full) url
83
     *
84
     * @return string
85
     */
86 4
    protected function getSecurityHash(
87
        string $requestMethod,
88
        string $urlPath,
89
        $data,
90
        bool $urlIsFullUrl = false
91
    ): string {
92 4
        $contractProfileId = $this->getContractProfileId();
93
94 4
        $fullUrl = $this->getBaseUrl().$urlPath;
95 4
        if ($urlIsFullUrl) {
96
            $fullUrl = $urlPath;
97
        }
98
99 4
        $toBeHashed = $fullUrl.$requestMethod.$contractProfileId.json_encode($data);
100
101 4
        $hash = hash_hmac(
102 4
            'sha256',
103 4
            $toBeHashed,
104 4
            base64_decode($this->getSecretKey()),
105 4
            true
106
        );
107
108 4
        return base64_encode($hash);
109
    }
110
111
    /**
112
     * Get the authentication headers information.
113
     *
114
     * @param string $securityHash
115
     *
116
     * @return array
117
     */
118 4
    private function getAuthenticationHeaders(string $securityHash): array
119
    {
120
        return [
121 4
            'CHECKSUM' => $securityHash,
122 4
            'USERID' => $this->getContractProfileId(),
123
        ];
124
    }
125
126
    /**
127
     * Returns the base URL of the API.
128
     *
129
     * @return string
130
     */
131 4
    public function getBaseUrl(): string
132
    {
133 4
        return $this->getParameter('baseUrl');
134
    }
135
136
    /**
137
     * Sets the base URL of the API.
138
     *
139
     * @param string $baseUrl
140
     *
141
     * @return self
142
     */
143 14
    public function setBaseUrl(string $baseUrl): self
144
    {
145 14
        return $this->setParameter('baseUrl', $baseUrl);
146
    }
147
148
    /**
149
     * Get Contract Profile Id (also known as the user id).
150
     *
151
     * Use the Contract Profile Id assigned by Allied wallet.
152
     *
153
     * @return string
154
     */
155 9
    public function getContractProfileId(): string
156
    {
157 9
        return $this->getParameter('contractProfileId');
158
    }
159
160
    /**
161
     * Set Contract Profile Id (also known as the user id).
162
     *
163
     * @param string $contractProfileId
164
     *
165
     * @return self
166
     */
167 14
    public function setContractProfileId(string $contractProfileId): self
168
    {
169 14
        return $this->setParameter('contractProfileId', $contractProfileId);
170
    }
171
172
    /**
173
     * Get Secret Key.
174
     *
175
     * @return string
176
     */
177 5
    public function getSecretKey(): string
178
    {
179 5
        return $this->getParameter('secretKey');
180
    }
181
182
    /**
183
     * Set Secret Key.
184
     *
185
     * @param string $secretKey
186
     *
187
     * @return self
188
     */
189 14
    public function setSecretKey($secretKey): self
190
    {
191 14
        return $this->setParameter('secretKey', $secretKey);
192
    }
193
194
    /**
195
     * Get the currency code.
196
     *
197
     * @return string
198
     */
199 3
    public function getCurrencyCode(): string
200
    {
201 3
        return $this->getParameter('currencyCode');
202
    }
203
204
    /**
205
     * Sets the currency code.
206
     *
207
     * @param string $currencyCode
208
     *
209
     * @return self
210
     */
211 7
    public function setCurrencyCode(string $currencyCode): self
212
    {
213 7
        return $this->setParameter('currencyCode', $currencyCode);
214
    }
215
216
    /**
217
     * Get the country code.
218
     *
219
     * @return string
220
     */
221
    public function getCountryCode(): string
222
    {
223
        return $this->getParameter('countryCode');
224
    }
225
226
    /**
227
     * Sets the country code.
228
     *
229
     * @param string $countryCode
230
     *
231
     * @return self
232
     */
233 4
    public function setCountryCode(string $countryCode): self
234
    {
235 4
        return $this->setParameter('countryCode', $countryCode);
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getIssuerCode(): string
242
    {
243
        return $this->getParameter('issuerCode');
244
    }
245
246
    /**
247
     * Sets the issuerCode.
248
     *
249
     * @param string $issuerCode
250
     *
251
     * @return self
252
     */
253 4
    public function setIssuerCode(string $issuerCode): self
254
    {
255 4
        return $this->setParameter('issuerCode', $issuerCode);
256
    }
257
258
    /**
259
     * Get the language code.
260
     *
261
     * @return string
262
     */
263
    public function getLanguageCode(): string
264
    {
265
        return $this->getParameter('languageCode');
266
    }
267
268
    /**
269
     * Sets the language code.
270
     *
271
     * @param string $languageCode
272
     *
273
     * @return self
274
     */
275 4
    public function setLanguageCode(string $languageCode): self
276
    {
277 4
        return $this->setParameter('languageCode', $languageCode);
278
    }
279
280
    /**
281
     * Get the reference.
282
     *
283
     * @return string
284
     */
285 3
    public function getReference(): string
286
    {
287 3
        return $this->getParameter('reference');
288
    }
289
290
    /**
291
     * Sets the reference.
292
     *
293
     * @param string $reference
294
     *
295
     * @return self
296
     */
297 7
    public function setReference(string $reference): self
298
    {
299 7
        return $this->setParameter('reference', $reference);
300
    }
301
302
    /**
303
     * Get the timestamp.
304
     *
305
     * @return DateTimeInterface
306
     */
307 3
    public function getTimestamp(): DateTimeInterface
308
    {
309 3
        return $this->getParameter('timestamp');
310
    }
311
312
    /**
313
     * Sets the timestamp as string value.
314
     *
315
     * @param DateTimeInterface $timestamp
316
     *
317
     * @return self
318
     */
319 8
    public function setTimestamp(DateTimeInterface $timestamp): self
320
    {
321 8
        return $this->setParameter('timestamp', $timestamp);
322
    }
323
324
    /**
325
     * Get the address street name.
326
     *
327
     * @return string
328
     */
329
    public function getAddressStreet(): string
330
    {
331
        return $this->getParameter('addressStreet');
332
    }
333
334
    /**
335
     * Sets the address street name as string value.
336
     *
337
     * @param string $addressStreet
338
     *
339
     * @return self
340
     */
341
    public function setAddressStreet(string $addressStreet): self
342
    {
343
        return $this->setParameter('addressStreet', $addressStreet);
344
    }
345
346
    /**
347
     * Get the address house number.
348
     *
349
     * @return string
350
     */
351
    public function getAddressHouseNumber(): string
352
    {
353
        return $this->getParameter('addressHouseNumber');
354
    }
355
356
    /**
357
     * Sets the address house number as string value.
358
     * 
359
     * @param string $addressHouseNumber
360
     * 
361
     * @return self
362
     */
363
    public function setAddressHouseNumber(string $addressHouseNumber): self
364
    {
365
        return $this->setParameter('addressHouseNumber', $addressHouseNumber);
366
    }
367
368
    /**
369
     * Get the address postal code.
370
     *
371
     * @return string
372
     */
373
    public function getAddressPostalCode(): string
374
    {
375
        return $this->getParameter('addressPostalCode');
376
    }
377
378
    /**
379
     * Sets the address postal code as string value.
380
     *
381
     * @param string $addressPostalCode
382
     *
383
     * @return self
384
     */
385
    public function setAddressPostalCode(string $addressPostalCode): self
386
    {
387
        return $this->setParameter('addressPostalCode', $addressPostalCode);
388
    }
389
390
    /**
391
     * Get the address city.
392
     *
393
     * @return string
394
     */
395
    public function getAddressCity(): string
396
    {
397
        return $this->getParameter('addressCity');
398
    }
399
400
    /**
401
     * Sets the address city as string value.
402
     *
403
     * @param string $addressCity
404
     *
405
     * @return self
406
     */
407
    public function setAddressCity(string $addressCity): self
408
    {
409
        return $this->setParameter('addressCity', $addressCity);
410
    }
411
412
    /**
413
     * Get the phone number.
414
     *
415
     * @return string
416
     */
417
    public function getPhoneNumber(): string
418
    {
419
        return $this->getParameter('phoneNumber');
420
    }
421
422
    /**
423
     * Sets the phone number as string value.
424
     *
425
     * @param string $phoneNumber
426
     *
427
     * @return self
428
     */
429
    public function setPhoneNumber(string $phoneNumber): self
430
    {
431
        return $this->setParameter('phoneNumber', $phoneNumber);
432
    }
433
434
    /**
435
     * Get the email address.
436
     *
437
     * @return string
438
     */
439
    public function getEmailAddress(): string
440
    {
441
        return $this->getParameter('emailAddress');
442
    }
443
444
    /**
445
     * Sets the email address as string value.
446
     *
447
     * @param string $emailAddress
448
     *
449
     * @return self
450
     */
451
    public function setEmailAddress(string $emailAddress): self
452
    {
453
        return $this->setParameter('emailAddress', $emailAddress);
454
    }
455
}
456