Passed
Push — customer-address ( a089f6 )
by Jelle
07:52
created

AbstractRequest::setPostalCode()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 6
    public function getData(): array
24
    {
25 6
        $this->validate('contractProfileId', 'secretKey');
26
27 6
        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|null $data
37
     *
38
     * @return ResponseInterface
39
     */
40 4
    protected function sendRequest(string $method, string $urlPath, ?array $data = null): 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 2
            $headers['Content-Type'] = 'application/json';
48 2
            $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|null $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;
100 4
        if ($data !== null) {
101 2
            $toBeHashed .= json_encode($data);
102
        }
103
104 4
        $hash = hash_hmac(
105 4
            'sha256',
106 4
            $toBeHashed,
107 4
            base64_decode($this->getSecretKey()),
108 4
            true
109
        );
110
111 4
        return base64_encode($hash);
112
    }
113
114
    /**
115
     * Get the authentication headers information.
116
     *
117
     * @param string $securityHash
118
     *
119
     * @return array
120
     */
121 4
    private function getAuthenticationHeaders(string $securityHash): array
122
    {
123
        return [
124 4
            'CHECKSUM' => $securityHash,
125 4
            'USERID' => $this->getContractProfileId(),
126
        ];
127
    }
128
129
    /**
130
     * Returns the base URL of the API.
131
     *
132
     * @return string
133
     */
134 4
    public function getBaseUrl(): string
135
    {
136 4
        return $this->getParameter('baseUrl');
137
    }
138
139
    /**
140
     * Sets the base URL of the API.
141
     *
142
     * @param string $baseUrl
143
     *
144
     * @return self
145
     */
146 15
    public function setBaseUrl(string $baseUrl): self
147
    {
148 15
        return $this->setParameter('baseUrl', $baseUrl);
149
    }
150
151
    /**
152
     * Get Contract Profile Id (also known as the user id).
153
     *
154
     * Use the Contract Profile Id assigned by Allied wallet.
155
     *
156
     * @return string
157
     */
158 10
    public function getContractProfileId(): string
159
    {
160 10
        return $this->getParameter('contractProfileId');
161
    }
162
163
    /**
164
     * Set Contract Profile Id (also known as the user id).
165
     *
166
     * @param string $contractProfileId
167
     *
168
     * @return self
169
     */
170 15
    public function setContractProfileId(string $contractProfileId): self
171
    {
172 15
        return $this->setParameter('contractProfileId', $contractProfileId);
173
    }
174
175
    /**
176
     * Get Secret Key.
177
     *
178
     * @return string
179
     */
180 5
    public function getSecretKey(): string
181
    {
182 5
        return $this->getParameter('secretKey');
183
    }
184
185
    /**
186
     * Set Secret Key.
187
     *
188
     * @param string $secretKey
189
     *
190
     * @return self
191
     */
192 15
    public function setSecretKey($secretKey): self
193
    {
194 15
        return $this->setParameter('secretKey', $secretKey);
195
    }
196
197
    /**
198
     * Get the currency code.
199
     *
200
     * @return string
201
     */
202 4
    public function getCurrencyCode(): string
203
    {
204 4
        return $this->getParameter('currencyCode');
205
    }
206
207
    /**
208
     * Sets the currency code.
209
     *
210
     * @param string $currencyCode
211
     *
212
     * @return self
213
     */
214 8
    public function setCurrencyCode(string $currencyCode): self
215
    {
216 8
        return $this->setParameter('currencyCode', $currencyCode);
217
    }
218
219
    /**
220
     * Get the country code.
221
     *
222
     * @return string
223
     */
224 1
    public function getCountryCode(): string
225
    {
226 1
        return $this->getParameter('countryCode');
227
    }
228
229
    /**
230
     * Sets the country code.
231
     *
232
     * @param string $countryCode
233
     *
234
     * @return self
235
     */
236 5
    public function setCountryCode(string $countryCode): self
237
    {
238 5
        return $this->setParameter('countryCode', $countryCode);
239
    }
240
241
    /**
242
     * @return string
243
     */
244 1
    public function getIssuerCode(): string
245
    {
246 1
        return $this->getParameter('issuerCode');
247
    }
248
249
    /**
250
     * Sets the issuerCode.
251
     *
252
     * @param string $issuerCode
253
     *
254
     * @return self
255
     */
256 5
    public function setIssuerCode(string $issuerCode): self
257
    {
258 5
        return $this->setParameter('issuerCode', $issuerCode);
259
    }
260
261
    /**
262
     * Get the language code.
263
     *
264
     * @return string
265
     */
266 1
    public function getLanguageCode(): string
267
    {
268 1
        return $this->getParameter('languageCode');
269
    }
270
271
    /**
272
     * Sets the language code.
273
     *
274
     * @param string $languageCode
275
     *
276
     * @return self
277
     */
278 5
    public function setLanguageCode(string $languageCode): self
279
    {
280 5
        return $this->setParameter('languageCode', $languageCode);
281
    }
282
283
    /**
284
     * Get the reference.
285
     *
286
     * @return string
287
     */
288 4
    public function getReference(): string
289
    {
290 4
        return $this->getParameter('reference');
291
    }
292
293
    /**
294
     * Sets the reference.
295
     *
296
     * @param string $reference
297
     *
298
     * @return self
299
     */
300 8
    public function setReference(string $reference): self
301
    {
302 8
        return $this->setParameter('reference', $reference);
303
    }
304
305
    /**
306
     * Get the timestamp.
307
     *
308
     * @return DateTimeInterface
309
     */
310 4
    public function getTimestamp(): DateTimeInterface
311
    {
312 4
        return $this->getParameter('timestamp');
313
    }
314
315
    /**
316
     * Sets the timestamp as string value.
317
     *
318
     * @param DateTimeInterface $timestamp
319
     *
320
     * @return self
321
     */
322 9
    public function setTimestamp(DateTimeInterface $timestamp): self
323
    {
324 9
        return $this->setParameter('timestamp', $timestamp);
325
    }
326
327
    /**
328
     * Get the city.
329
     *
330
     * @return string
331
     */
332 1
    public function getCity(): string
333
    {
334 1
        return $this->getParameter('city');
335
    }
336
337
    /**
338
     * Sets the city.
339
     *
340
     * @param string $city
341
     *
342
     * @return self
343
     */
344 1
    public function setCity(string $city): self
345
    {
346 1
        return $this->setParameter('city', $city);
347
    }
348
349
    /**
350
     * Gets the postal code.
351
     *
352
     * @return string
353
     */
354 1
    public function getPostalCode(): string
355
    {
356 1
        return $this->getParameter('postalCode');
357
    }
358
359
    /**
360
     * Sets the postal code.
361
     *
362
     * @param string $postalCode
363
     *
364
     * @return self
365
     */
366 1
    public function setPostalCode(string $postalCode): self
367
    {
368 1
        return $this->setParameter('postalCode', $postalCode);
369
    }
370
371
    /**
372
     * Gets the street.
373
     *
374
     * @return string
375
     */
376 1
    public function getStreet(): string
377
    {
378 1
        return $this->getParameter('street');
379
    }
380
381
    /**
382
     * Sets the street.
383
     *
384
     * @param string $street
385
     *
386
     * @return self
387
     */
388 1
    public function setStreet(string $street): self
389
    {
390 1
        return $this->setParameter('street', $street);
391
    }
392
}
393