Passed
Push — master ( c06059...bfa84d )
by payever
03:42
created

PaymentsApiClient::collectPaymentsRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
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\CancelPaymentRequest;
22
use Payever\ExternalIntegration\Payments\Http\RequestEntity\CreatePaymentRequest;
23
use Payever\ExternalIntegration\Payments\Http\RequestEntity\CreatePaymentV2Request;
24
use Payever\ExternalIntegration\Payments\Http\RequestEntity\ListPaymentsRequest;
25
use Payever\ExternalIntegration\Payments\Http\RequestEntity\RefundPaymentRequest;
26
use Payever\ExternalIntegration\Payments\Http\RequestEntity\ShippingGoodsPaymentRequest;
27
use Payever\ExternalIntegration\Payments\Http\RequestEntity\RefundItemsPaymentRequest;
28
use Payever\ExternalIntegration\Payments\Http\RequestEntity\SubmitPaymentRequest;
29
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\AuthorizePaymentResponse;
30
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\CancelPaymentResponse;
31
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\CollectPaymentsResponse;
32
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\CreatePaymentResponse;
33
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\GetTransactionResponse;
34
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\LatePaymentsResponse;
35
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ListPaymentOptionsResponse;
36
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ListPaymentOptionsWithVariantsResponse;
37
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ListPaymentsResponse;
38
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RefundPaymentResponse;
39
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RemindPaymentResponse;
40
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RetrieveApiCallResponse;
41
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\RetrievePaymentResponse;
42
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\ShippingGoodsPaymentResponse;
43
44
/**
45
 * Class represents Payever Payments API Connector
46
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
47
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
48
 */
49
class PaymentsApiClient extends CommonApiClient implements PaymentsApiClientInterface
50
{
51
    const SUB_URL_CREATE_PAYMENT = 'api/payment';
52
    const SUB_URL_CREATE_PAYMENT_V2 = 'api/v2/payment';
53
    const SUB_URL_CREATE_PAYMENT_SUBMIT = 'api/payment/submit';
54
    const SUB_URL_RETRIEVE_PAYMENT = 'api/payment/%s';
55
    const SUB_URL_LIST_PAYMENTS = 'api/payment';
56
    const SUB_URL_REFUND_PAYMENT = 'api/payment/refund/%s';
57
    const SUB_URL_AUTHORIZE_PAYMENT = 'api/payment/authorize/%s';
58
    const SUB_URL_REMIND_PAYMENT = 'api/payment/remind/%s';
59
    const SUB_URL_COLLECT_PAYMENTS = 'api/payment/collect/%s';
60
    const SUB_URL_LATE_PAYMENTS = 'api/payment/late-payment/%s';
61
    const SUB_URL_SHIPPING_GOODS_PAYMENT = 'api/payment/shipping-goods/%s';
62
    const SUB_URL_CANCEL_PAYMENT = 'api/payment/cancel/%s';
63
    const SUB_URL_RETRIEVE_API_CALL = 'api/%s';
64
    const SUB_URL_LIST_PAYMENT_OPTIONS = 'api/shop/oauth/%s/payment-options/%s';
65
    const SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS = 'api/shop/oauth/%s/payment-options/variants/%s';
66
    const SUB_URL_TRANSACTION = 'api/rest/v1/transactions/%s';
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @throws \Exception
72
     */
73
    public function createPaymentRequest(CreatePaymentRequest $createPaymentRequest)
74
    {
75
        $this->configuration->assertLoaded();
76
77
        if (!$createPaymentRequest->getChannel()) {
78
            $createPaymentRequest->setChannel(
79
                $this->configuration->getChannelSet()
80
            );
81
        }
82
83
        $request = RequestBuilder::post($this->getCreatePaymentURL())
84
            ->addRawHeader(
85
                $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString()
86
            )
87
            ->setRequestEntity($createPaymentRequest)
88
            ->setResponseEntity(new CreatePaymentResponse())
89
            ->build();
90
91
        return $this->executeRequest($request, OauthToken::SCOPE_CREATE_PAYMENT);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @throws \Exception
98
     */
99
    public function createPaymentV2Request(CreatePaymentV2Request $createPaymentRequest)
100
    {
101
        $this->configuration->assertLoaded();
102
103
        $request = RequestBuilder::post($this->getCreatePaymentV2URL())
104
                                 ->addRawHeader(
105
                                     $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString()
106
                                 )
107
                                 ->contentTypeIsJson()
108
                                 ->setRequestEntity($createPaymentRequest)
109
                                 ->setResponseEntity(new CreatePaymentResponse())
110
                                 ->build();
111
112
        return $this->executeRequest($request, OauthToken::SCOPE_CREATE_PAYMENT);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @throws \Exception
119
     */
120
    public function submitPaymentRequest(SubmitPaymentRequest $submitPaymentRequest)
121
    {
122
        $this->configuration->assertLoaded();
123
124
        if (!$submitPaymentRequest->getChannel()) {
125
            $submitPaymentRequest->setChannel(
126
                $this->configuration->getChannelSet()
127
            );
128
        }
129
130
        $request = RequestBuilder::post($this->getSubmitPaymentURL())
131
            ->addRawHeader(
132
                $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString()
133
            )
134
            ->contentTypeIsJson()
135
            ->setRequestEntity($submitPaymentRequest)
136
            ->setResponseEntity(new RetrievePaymentResponse())
137
            ->build();
138
139
        return $this->executeRequest($request, OauthToken::SCOPE_CREATE_PAYMENT);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     *
145
     * @throws \Exception
146
     */
147
    public function retrievePaymentRequest($paymentId)
148
    {
149
        $this->configuration->assertLoaded();
150
151
        $request = RequestBuilder::get($this->getRetrievePaymentURL($paymentId))
152
            ->addRawHeader(
153
                $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString()
154
            )
155
            ->setResponseEntity(new RetrievePaymentResponse())
156
            ->build();
157
158
        return $this->executeRequest($request, OauthToken::SCOPE_PAYMENT_INFO);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     *
164
     * @throws \Exception
165
     */
166
    public function listPaymentsRequest(ListPaymentsRequest $listPaymentsRequest)
167
    {
168
        $this->configuration->assertLoaded();
169
170
        $request = RequestBuilder::get($this->getListPaymentsURL())
171
            ->addRawHeader(
172
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
173
            )
174
            ->setRequestEntity($listPaymentsRequest)
175
            ->setResponseEntity(new ListPaymentsResponse())
176
            ->build();
177
178
        return $this->executeRequest($request);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     *
184
     * @throws \Exception
185
     */
186
    public function refundPaymentRequest($paymentId, $amount)
187
    {
188
        $this->configuration->assertLoaded();
189
190
        $request = RequestBuilder::post($this->getRefundPaymentURL($paymentId))
191
            ->setParams([
192
                'amount' => $amount,
193
            ])
194
            ->addRawHeader(
195
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
196
            )
197
            ->setRequestEntity(new RefundPaymentRequest())
198
            ->setResponseEntity(new RefundPaymentResponse())
199
            ->build();
200
201
        return $this->executeRequest($request);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     *
207
     * @throws \Exception
208
     */
209
    public function refundItemsPaymentRequest($paymentId, $items, $deliveryFee = null)
210
    {
211
        $this->configuration->assertLoaded();
212
213
        $refundPaymentRequest = new RefundItemsPaymentRequest();
214
        $refundPaymentRequest->setPaymentItems($items);
215
216
        if ($deliveryFee) {
217
            $refundPaymentRequest->setDeliveryFee($deliveryFee);
218
        }
219
220
        $request = RequestBuilder::post($this->getRefundPaymentURL($paymentId))
221
            ->addRawHeader(
222
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
223
            )
224
            ->contentTypeIsJson()
225
            ->setRequestEntity($refundPaymentRequest)
226
            ->setResponseEntity(new RefundPaymentResponse())
227
            ->build();
228
229
        return $this->executeRequest($request);
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     *
235
     * @throws \Exception
236
     */
237
    public function authorizePaymentRequest($paymentId, AuthorizePaymentRequest $paymentRequest = null)
238
    {
239
        $this->configuration->assertLoaded();
240
241
        $request = RequestBuilder::post($this->getAuthorizePaymentURL($paymentId))
242
            ->addRawHeader(
243
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
244
            )
245
            ->setRequestEntity($paymentRequest ?: new AuthorizePaymentRequest())
246
            ->setResponseEntity(new AuthorizePaymentResponse())
247
            ->build();
248
249
        return $this->executeRequest($request);
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     *
255
     * @throws \Exception
256
     *
257
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
258
     */
259
    public function remindPaymentRequest($paymentId)
260
    {
261
        $this->configuration->assertLoaded();
262
263
        $request = RequestBuilder::post($this->getRemindPaymentURL($paymentId))
264
            ->addRawHeader(
265
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
266
            )
267
            ->setResponseEntity(new RemindPaymentResponse())
268
            ->build();
269
270
        return $this->executeRequest($request);
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     *
276
     * @throws \Exception
277
     *
278
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
279
     */
280
    public function collectPaymentsRequest($paymentId)
281
    {
282
        $this->configuration->assertLoaded();
283
284
        $request = RequestBuilder::post($this->getCollectPaymentsURL($paymentId))
285
            ->addRawHeader(
286
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
287
            )
288
            ->setResponseEntity(new CollectPaymentsResponse())
289
            ->build();
290
291
        return $this->executeRequest($request);
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     *
297
     * @throws \Exception
298
     *
299
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
300
     */
301
    public function latePaymentsRequest($paymentId)
302
    {
303
        $this->configuration->assertLoaded();
304
305
        $request = RequestBuilder::post($this->getLatePaymentsURL($paymentId))
306
            ->addRawHeader(
307
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
308
            )
309
            ->setResponseEntity(new LatePaymentsResponse())
310
            ->build();
311
312
        return $this->executeRequest($request);
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     *
318
     * @throws \Exception
319
     */
320
    public function shippingGoodsPaymentRequest(
321
        $paymentId,
322
        ShippingGoodsPaymentRequest $paymentRequest = null
323
    ) {
324
        $this->configuration->assertLoaded();
325
326
        $request = RequestBuilder::post($this->getShippingGoodsPaymentURL($paymentId))
327
            ->addRawHeader(
328
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
329
            )
330
            ->contentTypeIsJson()
331
            ->setRequestEntity($paymentRequest ?: new ShippingGoodsPaymentRequest())
332
            ->setResponseEntity(new ShippingGoodsPaymentResponse())
333
            ->build();
334
335
        return $this->executeRequest($request);
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     *
341
     * @throws \Exception
342
     */
343
    public function cancelPaymentRequest($paymentId, $amount = null)
344
    {
345
        $this->configuration->assertLoaded();
346
347
        $request = RequestBuilder::post($this->getCancelPaymentURL($paymentId))
348
            ->setParams([
349
                'amount' => $amount,
350
            ])
351
            ->addRawHeader(
352
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
353
            )
354
            ->setRequestEntity(new CancelPaymentRequest())
355
            ->setResponseEntity(new CancelPaymentResponse())
356
            ->build();
357
358
        return $this->executeRequest($request);
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     *
364
     * @throws \Exception
365
     */
366
    public function retrieveApiCallRequest($callId)
367
    {
368
        $this->configuration->assertLoaded();
369
370
        $request = RequestBuilder::get($this->getRetrieveApiCallURL($callId))
371
            ->addRawHeader(
372
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
373
            )
374
            ->setResponseEntity(new RetrieveApiCallResponse())
375
            ->build();
376
377
        return $this->executeRequest($request);
378
    }
379
380
    /**
381
     * {@inheritdoc}
382
     *
383
     * @throws \Exception
384
     */
385
    public function listPaymentOptionsRequest($params = [], $businessUuid = '', $channel = '')
386
    {
387
        $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid();
388
        $channel = $channel ?: $this->getConfiguration()->getChannelSet();
389
390
        $request = RequestBuilder::get($this->getListPaymentOptionsURL($businessUuid, $channel, $params))
391
            ->addRawHeader(
392
                $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString()
393
            )
394
            ->setResponseEntity(new ListPaymentOptionsResponse())
395
            ->build();
396
397
        return $this->executeRequest($request, OauthToken::SCOPE_PAYMENT_INFO);
398
    }
399
400
    /**
401
     * {@inheritdoc}
402
     *
403
     * @throws \Exception
404
     */
405
    public function listPaymentOptionsWithVariantsRequest($params = [], $businessUuid = '', $channel = '')
406
    {
407
        $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid();
408
        $channel = $channel ?: $this->getConfiguration()->getChannelSet();
409
410
        $request = RequestBuilder::get($this->getListPaymentOptionsVariantsURL($businessUuid, $channel, $params))
411
            ->addRawHeader(
412
                $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString()
413
            )
414
            ->setResponseEntity(new ListPaymentOptionsWithVariantsResponse())
415
            ->build();
416
417
        return $this->executeRequest($request, OauthToken::SCOPE_PAYMENT_INFO);
418
    }
419
420
    /**
421
     * {@inheritdoc}
422
     *
423
     * @throws \Exception
424
     */
425
    public function getTransactionRequest($paymentId)
426
    {
427
        $this->configuration->assertLoaded();
428
429
        $request = RequestBuilder::get($this->getTransactionURL($paymentId))
430
            ->addRawHeader(
431
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
432
            )
433
            ->setResponseEntity(new GetTransactionResponse())
434
            ->build();
435
436
        return $this->executeRequest($request);
437
    }
438
439
    /**
440
     * Returns URL for Create Payment path
441
     *
442
     * @return string
443
     */
444
    protected function getCreatePaymentURL()
445
    {
446
        return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT;
447
    }
448
449
    /**
450
     * Returns URL for Create Payment path
451
     *
452
     * @return string
453
     */
454
    protected function getCreatePaymentV2URL()
455
    {
456
        return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT_V2;
457
    }
458
459
    /**
460
     * Returns URL for Submit Payment path
461
     *
462
     * @return string
463
     */
464
    protected function getSubmitPaymentURL()
465
    {
466
        return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT_SUBMIT;
467
    }
468
469
    /**
470
     * Returns URL for Retrieve Payment path
471
     *
472
     * @param string $paymentId
473
     *
474
     * @return string
475
     */
476
    protected function getRetrievePaymentURL($paymentId)
477
    {
478
        return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_PAYMENT, $paymentId);
479
    }
480
481
    /**
482
     * Returns URL for List Payments path
483
     *
484
     * @return string
485
     */
486
    protected function getListPaymentsURL()
487
    {
488
        return $this->getBaseUrl() . self::SUB_URL_LIST_PAYMENTS;
489
    }
490
491
    /**
492
     * Returns URL for Refund Payment path
493
     *
494
     * @param string $paymentId
495
     *
496
     * @return string
497
     */
498
    protected function getRefundPaymentURL($paymentId)
499
    {
500
        return $this->getBaseUrl() . sprintf(self::SUB_URL_REFUND_PAYMENT, $paymentId);
501
    }
502
503
    /**
504
     * Returns URL for Authorize Payment path
505
     *
506
     * @param string $paymentId
507
     *
508
     * @return string
509
     */
510
    protected function getAuthorizePaymentURL($paymentId)
511
    {
512
        return $this->getBaseUrl() . sprintf(self::SUB_URL_AUTHORIZE_PAYMENT, $paymentId);
513
    }
514
515
    /**
516
     * Returns URL for Remind Payment path
517
     *
518
     * @param string $paymentId
519
     *
520
     * @return string
521
     */
522
    protected function getRemindPaymentURL($paymentId)
523
    {
524
        return $this->getBaseUrl() . sprintf(self::SUB_URL_REMIND_PAYMENT, $paymentId);
525
    }
526
527
    /**
528
     * Returns URL for Collect Payment path
529
     *
530
     * @param string $paymentId
531
     *
532
     * @return string
533
     */
534
    protected function getCollectPaymentsURL($paymentId)
535
    {
536
        return $this->getBaseUrl() . sprintf(self::SUB_URL_COLLECT_PAYMENTS, $paymentId);
537
    }
538
539
    /**
540
     * Returns URL for Late Payments path
541
     *
542
     * @param string $paymentId
543
     *
544
     * @return string
545
     */
546
    protected function getLatePaymentsURL($paymentId)
547
    {
548
        return $this->getBaseUrl() . sprintf(self::SUB_URL_LATE_PAYMENTS, $paymentId);
549
    }
550
551
    /**
552
     * Returns URL for Shipping Goods Payment path
553
     *
554
     * @param string $paymentId
555
     *
556
     * @return string
557
     */
558
    protected function getShippingGoodsPaymentURL($paymentId)
559
    {
560
        return $this->getBaseUrl() . sprintf(self::SUB_URL_SHIPPING_GOODS_PAYMENT, $paymentId);
561
    }
562
563
    /**
564
     * Returns URL for Cancel Payment path
565
     *
566
     * @param string $paymentId
567
     *
568
     * @return string
569
     */
570
    protected function getCancelPaymentURL($paymentId)
571
    {
572
        return $this->getBaseUrl() . sprintf(self::SUB_URL_CANCEL_PAYMENT, $paymentId);
573
    }
574
575
    /**
576
     * Returns URL for Retrieve API Call path
577
     *
578
     * @param string $callId
579
     *
580
     * @return string
581
     */
582
    protected function getRetrieveApiCallURL($callId)
583
    {
584
        return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_API_CALL, $callId);
585
    }
586
587
    /**
588
     * Returns URL for Available Payment Options path
589
     *
590
     * @param string $businessUuid
591
     * @param string $channel
592
     * @param array $params
593
     *
594
     * @return string
595
     */
596
    protected function getListPaymentOptionsURL($businessUuid, $channel, $params = [])
597
    {
598
        return $this->getBaseUrl()
599
            . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS, $businessUuid, $channel)
600
            . (empty($params) ? '' : '?' . http_build_query($params));
601
    }
602
603
    /**
604
     * Returns URL for Available Payment Options request
605
     *
606
     * @param string $businessUuid
607
     * @param string $channel
608
     * @param array $params
609
     *
610
     * @return string
611
     */
612
    protected function getListPaymentOptionsVariantsURL($businessUuid, $channel, $params = [])
613
    {
614
        return $this->getBaseUrl()
615
            . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS, $businessUuid, $channel)
616
            . (empty($params) ? '' : '?' . http_build_query($params));
617
    }
618
619
    /**
620
     * Returns URL to Transaction path
621
     *
622
     * @param int $paymentId
623
     *
624
     * @return string
625
     */
626
    protected function getTransactionURL($paymentId)
627
    {
628
        return $this->getBaseUrl() . sprintf(self::SUB_URL_TRANSACTION, $paymentId);
629
    }
630
}
631