Passed
Push — master ( f4feef...3322ef )
by payever
10:36
created

PaymentsApiClient::shippingGoodsPaymentRequest()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * PHP version 5.4 and 7
5
 *
6
 * @category  Payments
7
 * @package   Payever\Payments
8
 * @author    payever GmbH <[email protected]>
9
 * @copyright 2017-2021 payever GmbH
10
 * @license   MIT <https://opensource.org/licenses/MIT>
11
 * @link      https://docs.payever.org/shopsystems/api/getting-started
12
 */
13
14
namespace Payever\ExternalIntegration\Payments;
15
16
use Payever\ExternalIntegration\Core\Authorization\OauthToken;
17
use Payever\ExternalIntegration\Core\CommonApiClient;
18
use Payever\ExternalIntegration\Core\Http\RequestBuilder;
19
use Payever\ExternalIntegration\Payments\Base\PaymentsApiClientInterface;
20
use Payever\ExternalIntegration\Payments\Http\RequestEntity\AuthorizePaymentRequest;
21
use Payever\ExternalIntegration\Payments\Http\RequestEntity\CreatePaymentRequest;
22
use Payever\ExternalIntegration\Payments\Http\RequestEntity\ListPaymentsRequest;
23
use Payever\ExternalIntegration\Payments\Http\RequestEntity\RefundPaymentRequest;
24
use Payever\ExternalIntegration\Payments\Http\RequestEntity\ShippingGoodsPaymentRequest;
25
use Payever\ExternalIntegration\Payments\Http\RequestEntity\SubmitPaymentRequest;
26
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\AuthorizePaymentResponse;
27
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\CancelPaymentResponse;
28
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\CollectPaymentsResponse;
29
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\CreatePaymentResponse;
30
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\GetTransactionResponse;
31
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\LatePaymentsResponse;
32
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ListPaymentOptionsResponse;
33
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ListPaymentOptionsWithVariantsResponse;
34
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ListPaymentsResponse;
35
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RefundPaymentResponse;
36
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RemindPaymentResponse;
37
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RetrieveApiCallResponse;
38
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RetrievePaymentResponse;
39
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ShippingGoodsPaymentResponse;
40
41
/**
42
 * Class represents Payever Payments API Connector
43
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
44
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
45
 */
46
class PaymentsApiClient extends CommonApiClient implements PaymentsApiClientInterface
47
{
48
    const SUB_URL_CREATE_PAYMENT = 'api/payment';
49
    const SUB_URL_CREATE_PAYMENT_SUBMIT = 'api/payment/submit';
50
    const SUB_URL_RETRIEVE_PAYMENT = 'api/payment/%s';
51
    const SUB_URL_LIST_PAYMENTS = 'api/payment';
52
    const SUB_URL_REFUND_PAYMENT = 'api/payment/refund/%s';
53
    const SUB_URL_AUTHORIZE_PAYMENT = 'api/payment/authorize/%s';
54
    const SUB_URL_REMIND_PAYMENT = 'api/payment/remind/%s';
55
    const SUB_URL_COLLECT_PAYMENTS = 'api/payment/collect/%s';
56
    const SUB_URL_LATE_PAYMENTS = 'api/payment/late-payment/%s';
57
    const SUB_URL_SHIPPING_GOODS_PAYMENT = 'api/payment/shipping-goods/%s';
58
    const SUB_URL_CANCEL_PAYMENT = 'api/payment/cancel/%s';
59
    const SUB_URL_RETRIEVE_API_CALL = 'api/%s';
60
    const SUB_URL_LIST_PAYMENT_OPTIONS = 'api/shop/%s/payment-options/%s';
61
    const SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS = 'api/shop/%s/payment-options/variants/%s';
62
    const SUB_URL_TRANSACTION = 'api/rest/v1/transactions/%s';
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @throws \Exception
68
     */
69 View Code Duplication
    public function createPaymentRequest(CreatePaymentRequest $createPaymentRequest)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $this->configuration->assertLoaded();
72
73
        if (!$createPaymentRequest->getChannel()) {
74
            $createPaymentRequest->setChannel(
75
                $this->configuration->getChannelSet()
76
            );
77
        }
78
79
        $request = RequestBuilder::post($this->getCreatePaymentURL())
80
            ->addRawHeader(
81
                $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString()
82
            )
83
            ->setRequestEntity($createPaymentRequest)
84
            ->setResponseEntity(new CreatePaymentResponse())
85
            ->build();
86
87
        return $this->getHttpClient()->execute($request);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * @throws \Exception
94
     */
95 View Code Duplication
    public function submitPaymentRequest(SubmitPaymentRequest $submitPaymentRequest)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $this->configuration->assertLoaded();
98
99
        if (!$submitPaymentRequest->getChannel()) {
100
            $submitPaymentRequest->setChannel(
101
                $this->configuration->getChannelSet()
102
            );
103
        }
104
105
        $request = RequestBuilder::post($this->getSubmitPaymentURL())
106
            ->addRawHeader(
107
                $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString()
108
            )
109
            ->contentTypeIsJson()
110
            ->setRequestEntity($submitPaymentRequest)
111
            ->setResponseEntity(new RetrievePaymentResponse())
112
            ->build();
113
114
        return $this->getHttpClient()->execute($request);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     *
120
     * @throws \Exception
121
     */
122
    public function retrievePaymentRequest($paymentId)
123
    {
124
        $this->configuration->assertLoaded();
125
126
        $request = RequestBuilder::get($this->getRetrievePaymentURL($paymentId))
127
            ->addRawHeader(
128
                $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString()
129
            )
130
            ->setResponseEntity(new RetrievePaymentResponse())
131
            ->build();
132
133
        return $this->getHttpClient()->execute($request);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     *
139
     * @throws \Exception
140
     */
141
    public function listPaymentsRequest(ListPaymentsRequest $listPaymentsRequest)
142
    {
143
        $this->configuration->assertLoaded();
144
145
        $request = RequestBuilder::get($this->getListPaymentsURL())
146
            ->addRawHeader(
147
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
148
            )
149
            ->setRequestEntity($listPaymentsRequest)
150
            ->setResponseEntity(new ListPaymentsResponse())
151
            ->build();
152
153
        return $this->getHttpClient()->execute($request);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     *
159
     * @throws \Exception
160
     */
161
    public function refundPaymentRequest($paymentId, $amount)
162
    {
163
        $this->configuration->assertLoaded();
164
165
        $request = RequestBuilder::post($this->getRefundPaymentURL($paymentId))
166
            ->setParams([
167
                'amount' => $amount,
168
            ])
169
            ->addRawHeader(
170
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
171
            )
172
            ->setRequestEntity(new RefundPaymentRequest())
173
            ->setResponseEntity(new RefundPaymentResponse())
174
            ->build();
175
176
        return $this->getHttpClient()->execute($request);
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     *
182
     * @throws \Exception
183
     */
184 View Code Duplication
    public function authorizePaymentRequest($paymentId, AuthorizePaymentRequest $paymentRequest = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186
        $this->configuration->assertLoaded();
187
188
        $request = RequestBuilder::post($this->getAuthorizePaymentURL($paymentId))
189
            ->addRawHeader(
190
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
191
            )
192
            ->setRequestEntity($paymentRequest ?: new AuthorizePaymentRequest())
193
            ->setResponseEntity(new AuthorizePaymentResponse())
194
            ->build();
195
196
        return $this->getHttpClient()->execute($request);
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     *
202
     * @throws \Exception
203
     *
204
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
205
     */
206
    public function remindPaymentRequest($paymentId)
207
    {
208
        $this->configuration->assertLoaded();
209
210
        $request = RequestBuilder::post($this->getRemindPaymentURL($paymentId))
211
            ->addRawHeader(
212
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
213
            )
214
            ->setResponseEntity(new RemindPaymentResponse())
215
            ->build();
216
217
        return $this->getHttpClient()->execute($request);
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     *
223
     * @throws \Exception
224
     *
225
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
226
     */
227
    public function collectPaymentsRequest($paymentId)
228
    {
229
        $this->configuration->assertLoaded();
230
231
        $request = RequestBuilder::post($this->getCollectPaymentsURL($paymentId))
232
            ->addRawHeader(
233
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
234
            )
235
            ->setResponseEntity(new CollectPaymentsResponse())
236
            ->build();
237
238
        return $this->getHttpClient()->execute($request);
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     *
244
     * @throws \Exception
245
     *
246
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
247
     */
248
    public function latePaymentsRequest($paymentId)
249
    {
250
        $this->configuration->assertLoaded();
251
252
        $request = RequestBuilder::post($this->getLatePaymentsURL($paymentId))
253
            ->addRawHeader(
254
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
255
            )
256
            ->setResponseEntity(new LatePaymentsResponse())
257
            ->build();
258
259
        return $this->getHttpClient()->execute($request);
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     *
265
     * @throws \Exception
266
     */
267 View Code Duplication
    public function shippingGoodsPaymentRequest(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
268
        $paymentId,
269
        ShippingGoodsPaymentRequest $paymentRequest = null
270
    ) {
271
        $this->configuration->assertLoaded();
272
273
        $request = RequestBuilder::post($this->getShippingGoodsPaymentURL($paymentId))
274
            ->addRawHeader(
275
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
276
            )
277
            ->setRequestEntity($paymentRequest ?: new ShippingGoodsPaymentRequest())
278
            ->setResponseEntity(new ShippingGoodsPaymentResponse())
279
            ->build();
280
281
        return $this->getHttpClient()->execute($request);
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     *
287
     * @throws \Exception
288
     */
289
    public function cancelPaymentRequest($paymentId)
290
    {
291
        $this->configuration->assertLoaded();
292
293
        $request = RequestBuilder::post($this->getCancelPaymentURL($paymentId))
294
            ->addRawHeader(
295
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
296
            )
297
            ->setResponseEntity(new CancelPaymentResponse())
298
            ->build();
299
300
        return $this->getHttpClient()->execute($request);
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     *
306
     * @throws \Exception
307
     */
308
    public function retrieveApiCallRequest($callId)
309
    {
310
        $this->configuration->assertLoaded();
311
312
        $request = RequestBuilder::get($this->getRetrieveApiCallURL($callId))
313
            ->addRawHeader(
314
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
315
            )
316
            ->setResponseEntity(new RetrieveApiCallResponse())
317
            ->build();
318
319
        return $this->getHttpClient()->execute($request);
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     *
325
     * @throws \Exception
326
     */
327 View Code Duplication
    public function listPaymentOptionsRequest($params = [], $businessUuid = '', $channel = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
328
    {
329
        $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid();
330
        $channel = $channel ?: $this->getConfiguration()->getChannelSet();
331
332
        $request = RequestBuilder::get($this->getListPaymentOptionsURL($businessUuid, $channel, $params))
333
            ->setResponseEntity(new ListPaymentOptionsResponse())
334
            ->build();
335
336
        return $this->getHttpClient()->execute($request);
337
    }
338
339
    /**
340
     * {@inheritdoc}
341
     *
342
     * @throws \Exception
343
     */
344 View Code Duplication
    public function listPaymentOptionsWithVariantsRequest($params = [], $businessUuid = '', $channel = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
345
    {
346
        $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid();
347
        $channel = $channel ?: $this->getConfiguration()->getChannelSet();
348
349
        $request = RequestBuilder::get($this->getListPaymentOptionsVariantsURL($businessUuid, $channel, $params))
350
            ->setResponseEntity(new ListPaymentOptionsWithVariantsResponse())
351
            ->build();
352
353
        return $this->getHttpClient()->execute($request);
354
    }
355
356
    /**
357
     * {@inheritdoc}
358
     *
359
     * @throws \Exception
360
     */
361
    public function getTransactionRequest($paymentId)
362
    {
363
        $this->configuration->assertLoaded();
364
365
        $request = RequestBuilder::get($this->getTransactionURL($paymentId))
366
            ->addRawHeader(
367
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
368
            )
369
            ->setResponseEntity(new GetTransactionResponse())
370
            ->build();
371
372
        return $this->getHttpClient()->execute($request);
373
    }
374
375
    /**
376
     * Returns URL for Create Payment path
377
     *
378
     * @return string
379
     */
380
    protected function getCreatePaymentURL()
381
    {
382
        return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT;
383
    }
384
385
    /**
386
     * Returns URL for Submit Payment path
387
     *
388
     * @return string
389
     */
390
    protected function getSubmitPaymentURL()
391
    {
392
        return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT_SUBMIT;
393
    }
394
395
    /**
396
     * Returns URL for Retrieve Payment path
397
     *
398
     * @param string $paymentId
399
     *
400
     * @return string
401
     */
402
    protected function getRetrievePaymentURL($paymentId)
403
    {
404
        return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_PAYMENT, $paymentId);
405
    }
406
407
    /**
408
     * Returns URL for List Payments path
409
     *
410
     * @return string
411
     */
412
    protected function getListPaymentsURL()
413
    {
414
        return $this->getBaseUrl() . self::SUB_URL_LIST_PAYMENTS;
415
    }
416
417
    /**
418
     * Returns URL for Refund Payment path
419
     *
420
     * @param string $paymentId
421
     *
422
     * @return string
423
     */
424
    protected function getRefundPaymentURL($paymentId)
425
    {
426
        return $this->getBaseUrl() . sprintf(self::SUB_URL_REFUND_PAYMENT, $paymentId);
427
    }
428
429
    /**
430
     * Returns URL for Authorize Payment path
431
     *
432
     * @param string $paymentId
433
     *
434
     * @return string
435
     */
436
    protected function getAuthorizePaymentURL($paymentId)
437
    {
438
        return $this->getBaseUrl() . sprintf(self::SUB_URL_AUTHORIZE_PAYMENT, $paymentId);
439
    }
440
441
    /**
442
     * Returns URL for Remind Payment path
443
     *
444
     * @param string $paymentId
445
     *
446
     * @return string
447
     */
448
    protected function getRemindPaymentURL($paymentId)
449
    {
450
        return $this->getBaseUrl() . sprintf(self::SUB_URL_REMIND_PAYMENT, $paymentId);
451
    }
452
453
    /**
454
     * Returns URL for Collect Payment path
455
     *
456
     * @param string $paymentId
457
     *
458
     * @return string
459
     */
460
    protected function getCollectPaymentsURL($paymentId)
461
    {
462
        return $this->getBaseUrl() . sprintf(self::SUB_URL_COLLECT_PAYMENTS, $paymentId);
463
    }
464
465
    /**
466
     * Returns URL for Late Payments path
467
     *
468
     * @param string $paymentId
469
     *
470
     * @return string
471
     */
472
    protected function getLatePaymentsURL($paymentId)
473
    {
474
        return $this->getBaseUrl() . sprintf(self::SUB_URL_LATE_PAYMENTS, $paymentId);
475
    }
476
477
    /**
478
     * Returns URL for Shipping Goods Payment path
479
     *
480
     * @param string $paymentId
481
     *
482
     * @return string
483
     */
484
    protected function getShippingGoodsPaymentURL($paymentId)
485
    {
486
        return $this->getBaseUrl() . sprintf(self::SUB_URL_SHIPPING_GOODS_PAYMENT, $paymentId);
487
    }
488
489
    /**
490
     * Returns URL for Cancel Payment path
491
     *
492
     * @param string $paymentId
493
     *
494
     * @return string
495
     */
496
    protected function getCancelPaymentURL($paymentId)
497
    {
498
        return $this->getBaseUrl() . sprintf(self::SUB_URL_CANCEL_PAYMENT, $paymentId);
499
    }
500
501
    /**
502
     * Returns URL for Retrieve API Call path
503
     *
504
     * @param string $callId
505
     *
506
     * @return string
507
     */
508
    protected function getRetrieveApiCallURL($callId)
509
    {
510
        return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_API_CALL, $callId);
511
    }
512
513
    /**
514
     * Returns URL for Available Payment Options path
515
     *
516
     * @param string $businessUuid
517
     * @param string $channel
518
     * @param array $params
519
     *
520
     * @return string
521
     */
522
    protected function getListPaymentOptionsURL($businessUuid, $channel, $params = [])
523
    {
524
        return $this->getBaseUrl()
525
            . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS, $businessUuid, $channel)
526
            . (empty($params) ? '' : '?' . http_build_query($params));
527
    }
528
529
    /**
530
     * Returns URL for Available Payment Options request
531
     *
532
     * @param string $businessUuid
533
     * @param string $channel
534
     * @param array $params
535
     *
536
     * @return string
537
     */
538
    protected function getListPaymentOptionsVariantsURL($businessUuid, $channel, $params = [])
539
    {
540
        return $this->getBaseUrl()
541
            . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS, $businessUuid, $channel)
542
            . (empty($params) ? '' : '?' . http_build_query($params));
543
    }
544
545
    /**
546
     * Returns URL to Transaction path
547
     *
548
     * @param int $paymentId
549
     *
550
     * @return string
551
     */
552
    protected function getTransactionURL($paymentId)
553
    {
554
        return $this->getBaseUrl() . sprintf(self::SUB_URL_TRANSACTION, $paymentId);
555
    }
556
}
557