Test Failed
Push — query-string ( c17fc7...166cc4 )
by
unknown
01:37
created

AbstractRequest::setSecretKey()   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 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 3
    protected function sendRequest(string $method, string $urlPath, array $data): ResponseInterface
41
    {
42 3
        $securityHash = $this->getSecurityHash($method, $urlPath, $data);
43 3
        $headers = $this->getAuthenticationHeaders($securityHash);
44 3
        $body = null;
45
46 3
        if ($method === Request::METHOD_POST) {
47 3
            $headers['Content-Type'] = 'application/json';
48 3
            $body = json_encode($data);
49
        }
50
51 3
        $this->response = $this->httpClient->request(
52 3
            $method,
53 3
            $this->getBaseUrl().$urlPath,
54 3
            $headers,
55 3
            $body
56
        );
57
58 3
        return $this->response;
59
    }
60
61
    /**
62
     * Returns the JSON decoded response body.
63
     *
64
     * @return array
65
     */
66 3
    protected function getResponseBody(): array
67
    {
68 3
        $responseBody = json_decode($this->getResponse()->getBody()->getContents(), true);
69 3
        if (is_array($responseBody) === false) {
70 3
            $responseBody = [];
71
        }
72
73 3
        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  $data
82
     * @param bool   $urlIsFullUrl = false. True to have $urlPath be the absolute (full) url
83
     *
84
     * @return string
85
     */
86 3
    protected function getSecurityHash(
87
        string $requestMethod,
88
        string $urlPath,
89
        array $data,
90
        bool $urlIsFullUrl = false
91
    ): string {
92 3
        $contractProfileId = $this->getContractProfileId();
93 3
        if (isset($data['ContractProfileId'])) {
94 1
            $contractProfileId = $data['ContractProfileId'];
95
        }
96
97 3
        $fullUrl = $this->getBaseUrl() . $urlPath;
98 3
        if ($urlIsFullUrl) {
99
            $fullUrl = $urlPath;
100
        }
101
102 3
        $toBeHashed = $fullUrl . $requestMethod . $contractProfileId . json_encode($data);
103
104 3
        $hash = hash_hmac(
105 3
            'sha256',
106 3
            $toBeHashed,
107 3
            base64_decode($this->getSecretKey()),
108 3
            true
109
        );
110
111 3
        return base64_encode($hash);
112
    }
113
114
    /**
115
     * Get the authentication headers information.
116
     *
117
     * @param string $securityHash
118
     *
119
     * @return array
120
     */
121 3
    private function getAuthenticationHeaders(string $securityHash): array
122
    {
123
        return [
124 3
            'CHECKSUM' => $securityHash,
125 3
            'USERID' => $this->getContractProfileId(),
126
        ];
127
    }
128
129
    /**
130
     * Returns the base URL of the API.
131
     *
132
     * @return string
133
     */
134 3
    public function getBaseUrl(): string
135
    {
136 3
        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 14
    public function setBaseUrl(string $baseUrl): self
147
    {
148 14
        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 9
    public function getContractProfileId(): string
159
    {
160 9
        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 14
    public function setContractProfileId(string $contractProfileId): self
171
    {
172 14
        return $this->setParameter('contractProfileId', $contractProfileId);
173
    }
174
175
    /**
176
     * Get Secret Key.
177
     *
178
     * @return string
179
     */
180 4
    public function getSecretKey(): string
181
    {
182 4
        return $this->getParameter('secretKey');
183
    }
184
185
    /**
186
     * Set Secret Key.
187
     *
188
     * @param string $secretKey
189
     *
190
     * @return self
191
     */
192 14
    public function setSecretKey($secretKey): self
193
    {
194 14
        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