Passed
Push — master ( 61e5a6...e6cb61 )
by
unknown
01:05 queued 17s
created

AbstractRequest   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 309
Duplicated Lines 0 %

Test Coverage

Coverage 98.61%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 52
c 4
b 0
f 1
dl 0
loc 309
ccs 71
cts 72
cp 0.9861
rs 10
wmc 26

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseBody() 0 8 2
A sendRequest() 0 19 2
A getData() 0 5 1
A setContractProfileId() 0 3 1
A setIssuerCode() 0 3 1
A setCountryCode() 0 3 1
A setSecretKey() 0 3 1
A setBaseUrl() 0 3 1
A getCountryCode() 0 3 1
A setCurrencyCode() 0 3 1
A getReference() 0 3 1
A getLanguageCode() 0 3 1
A setReference() 0 3 1
A getContractProfileId() 0 3 1
A getTimestamp() 0 3 1
A getBaseUrl() 0 3 1
A getIssuerCode() 0 3 1
A getAuthenticationHeaders() 0 5 1
A setTimestamp() 0 3 1
A getSecurityHash() 0 23 2
A setLanguageCode() 0 3 1
A getSecretKey() 0 3 1
A getCurrencyCode() 0 3 1
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  $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 15
    public function setBaseUrl(string $baseUrl): self
144
    {
145 15
        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 10
    public function getContractProfileId(): string
156
    {
157 10
        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 15
    public function setContractProfileId(string $contractProfileId): self
168
    {
169 15
        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 15
    public function setSecretKey($secretKey): self
190
    {
191 15
        return $this->setParameter('secretKey', $secretKey);
192
    }
193
194
    /**
195
     * Get the currency code.
196
     *
197
     * @return string
198
     */
199 4
    public function getCurrencyCode(): string
200
    {
201 4
        return $this->getParameter('currencyCode');
202
    }
203
204
    /**
205
     * Sets the currency code.
206
     *
207
     * @param string $currencyCode
208
     *
209
     * @return self
210
     */
211 8
    public function setCurrencyCode(string $currencyCode): self
212
    {
213 8
        return $this->setParameter('currencyCode', $currencyCode);
214
    }
215
216
    /**
217
     * Get the country code.
218
     *
219
     * @return string
220
     */
221 1
    public function getCountryCode(): string
222
    {
223 1
        return $this->getParameter('countryCode');
224
    }
225
226
    /**
227
     * Sets the country code.
228
     *
229
     * @param string $countryCode
230
     *
231
     * @return self
232
     */
233 5
    public function setCountryCode(string $countryCode): self
234
    {
235 5
        return $this->setParameter('countryCode', $countryCode);
236
    }
237
238
    /**
239
     * @return string
240
     */
241 1
    public function getIssuerCode(): string
242
    {
243 1
        return $this->getParameter('issuerCode');
244
    }
245
246
    /**
247
     * Sets the issuerCode.
248
     *
249
     * @param string $issuerCode
250
     *
251
     * @return self
252
     */
253 5
    public function setIssuerCode(string $issuerCode): self
254
    {
255 5
        return $this->setParameter('issuerCode', $issuerCode);
256
    }
257
258
    /**
259
     * Get the language code.
260
     *
261
     * @return string
262
     */
263 1
    public function getLanguageCode(): string
264
    {
265 1
        return $this->getParameter('languageCode');
266
    }
267
268
    /**
269
     * Sets the language code.
270
     *
271
     * @param string $languageCode
272
     *
273
     * @return self
274
     */
275 5
    public function setLanguageCode(string $languageCode): self
276
    {
277 5
        return $this->setParameter('languageCode', $languageCode);
278
    }
279
280
    /**
281
     * Get the reference.
282
     *
283
     * @return string
284
     */
285 4
    public function getReference(): string
286
    {
287 4
        return $this->getParameter('reference');
288
    }
289
290
    /**
291
     * Sets the reference.
292
     *
293
     * @param string $reference
294
     *
295
     * @return self
296
     */
297 8
    public function setReference(string $reference): self
298
    {
299 8
        return $this->setParameter('reference', $reference);
300
    }
301
302
    /**
303
     * Get the timestamp.
304
     *
305
     * @return DateTimeInterface
306
     */
307 4
    public function getTimestamp(): DateTimeInterface
308
    {
309 4
        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 9
    public function setTimestamp(DateTimeInterface $timestamp): self
320
    {
321 9
        return $this->setParameter('timestamp', $timestamp);
322
    }
323
}
324