Passed
Push — master ( f8ce69...b14078 )
by payever
03:35
created

PaymentsApiClient::listPaymentsRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Class represents Payever Payments API Connector
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Payments
8
 * @package   Payever\Payments
9
 * @author    payever GmbH <[email protected]>
10
 * @copyright 2017-2019 payever GmbH
11
 * @license   MIT <https://opensource.org/licenses/MIT>
12
 * @link      https://getpayever.com/developer/api-documentation/ Documentation
13
 */
14
15
namespace Payever\ExternalIntegration\Payments;
16
17
use Payever\ExternalIntegration\Core\Authorization\OauthToken;
18
use Payever\ExternalIntegration\Core\CommonApiClient;
19
use Payever\ExternalIntegration\Core\Http\RequestBuilder;
20
use Payever\ExternalIntegration\Payments\Base\PaymentsApiClientInterface;
21
use Payever\ExternalIntegration\Payments\Http\RequestEntity\AuthorizePaymentRequest;
22
use Payever\ExternalIntegration\Payments\Http\RequestEntity\CreatePaymentRequest;
23
use Payever\ExternalIntegration\Payments\Http\RequestEntity\ListPaymentsRequest;
24
use Payever\ExternalIntegration\Payments\Http\RequestEntity\RefundPaymentRequest;
25
use Payever\ExternalIntegration\Payments\Http\RequestEntity\ShippingGoodsPaymentRequest;
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
 *
44
 * PHP version 5.4
45
 *
46
 * @category  Payments
47
 * @package   Payever\Payments
48
 * @author    Andrey Puhovsky <[email protected]>
49
 * @copyright 2017-2019 payever GmbH
50
 * @license   MIT <https://opensource.org/licenses/MIT>
51
 * @link      https://getpayever.com/developer/api-documentation/ Documentation
52
 */
53
class PaymentsApiClient extends CommonApiClient implements PaymentsApiClientInterface
54
{
55
    const SUB_URL_CREATE_PAYMENT = 'api/payment';
56
    const SUB_URL_RETRIEVE_PAYMENT = 'api/payment/%s';
57
    const SUB_URL_LIST_PAYMENTS = 'api/payment';
58
    const SUB_URL_REFUND_PAYMENT = 'api/payment/refund/%s';
59
    const SUB_URL_AUTHORIZE_PAYMENT = 'api/payment/authorize/%s';
60
    const SUB_URL_REMIND_PAYMENT = 'api/payment/remind/%s';
61
    const SUB_URL_COLLECT_PAYMENTS = 'api/payment/collect/%s';
62
    const SUB_URL_LATE_PAYMENTS = 'api/payment/late-payment/%s';
63
    const SUB_URL_SHIPPING_GOODS_PAYMENT = 'api/payment/shipping-goods/%s';
64
    const SUB_URL_CANCEL_PAYMENT = 'api/payment/cancel/%s';
65
    const SUB_URL_RETRIEVE_API_CALL = 'api/%s';
66
    const SUB_URL_LIST_PAYMENT_OPTIONS = 'api/shop/%s/payment-options/%s';
67
    const SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS = 'api/shop/%s/payment-options/variants/%s';
68
    const SUB_URL_TRANSACTION = 'api/rest/v1/transactions/%s';
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @throws \Exception
74
     */
75
    public function createPaymentRequest(CreatePaymentRequest $createPaymentRequest)
76
    {
77
        $this->configuration->assertLoaded();
78
79
        if (!$createPaymentRequest->getChannel()) {
80
            $createPaymentRequest->setChannel(
81
                $this->configuration->getChannelSet()
82
            );
83
        }
84
85
        $request = RequestBuilder::post($this->getCreatePaymentURL())
86
            ->addRawHeader(
87
                $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString()
88
            )
89
            ->setRequestEntity($createPaymentRequest)
90
            ->setResponseEntity(new CreatePaymentResponse())
91
            ->build();
92
93
        return $this->getHttpClient()->execute($request);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     *
99
     * @throws \Exception
100
     */
101
    public function retrievePaymentRequest($paymentId)
102
    {
103
        $this->configuration->assertLoaded();
104
105
        $request = RequestBuilder::get($this->getRetrievePaymentURL($paymentId))
106
            ->addRawHeader(
107
                $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString()
108
            )
109
            ->setResponseEntity(new RetrievePaymentResponse())
110
            ->build();
111
112
        return $this->getHttpClient()->execute($request);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @throws \Exception
119
     */
120
    public function listPaymentsRequest(ListPaymentsRequest $listPaymentsRequest)
121
    {
122
        $this->configuration->assertLoaded();
123
124
        $request = RequestBuilder::get($this->getListPaymentsURL())
125
            ->addRawHeader(
126
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
127
            )
128
            ->setRequestEntity($listPaymentsRequest)
129
            ->setResponseEntity(new ListPaymentsResponse())
130
            ->build();
131
132
        return $this->getHttpClient()->execute($request);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     *
138
     * @throws \Exception
139
     */
140
    public function refundPaymentRequest($paymentId, $amount)
141
    {
142
        $this->configuration->assertLoaded();
143
144
        $request = RequestBuilder::post($this->getRefundPaymentURL($paymentId))
145
            ->setParams(
146
                array(
147
                    'amount' => $amount,
148
                )
149
            )
150
            ->addRawHeader(
151
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
152
            )
153
            ->setRequestEntity(new RefundPaymentRequest())
154
            ->setResponseEntity(new RefundPaymentResponse())
155
            ->build();
156
157
        return $this->getHttpClient()->execute($request);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     *
163
     * @throws \Exception
164
     */
165
    public function authorizePaymentRequest($paymentId, AuthorizePaymentRequest $authorizePaymentRequest = null)
166
    {
167
        $this->configuration->assertLoaded();
168
169
        $request = RequestBuilder::post($this->getAuthorizePaymentURL($paymentId))
170
            ->addRawHeader(
171
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
172
            )
173
            ->setRequestEntity($authorizePaymentRequest ?: new AuthorizePaymentRequest())
174
            ->setResponseEntity(new AuthorizePaymentResponse())
175
            ->build();
176
177
        return $this->getHttpClient()->execute($request);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     *
183
     * @throws \Exception
184
     *
185
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
186
     */
187
    public function remindPaymentRequest($paymentId)
188
    {
189
        $this->configuration->assertLoaded();
190
191
        $request = RequestBuilder::post($this->getRemindPaymentURL($paymentId))
192
            ->addRawHeader(
193
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
194
            )
195
            ->setResponseEntity(new RemindPaymentResponse())
196
            ->build();
197
198
        return $this->getHttpClient()->execute($request);
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     *
204
     * @throws \Exception
205
     *
206
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
207
     */
208
    public function collectPaymentsRequest($paymentId)
209
    {
210
        $this->configuration->assertLoaded();
211
212
        $request = RequestBuilder::post($this->getCollectPaymentsURL($paymentId))
213
            ->addRawHeader(
214
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
215
            )
216
            ->setResponseEntity(new CollectPaymentsResponse())
217
            ->build();
218
219
        return $this->getHttpClient()->execute($request);
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     *
225
     * @throws \Exception
226
     *
227
     * @deprecated This request is only available for Santander DE Invoice and not used anywhere
228
     */
229
    public function latePaymentsRequest($paymentId)
230
    {
231
        $this->configuration->assertLoaded();
232
233
        $request = RequestBuilder::post($this->getLatePaymentsURL($paymentId))
234
            ->addRawHeader(
235
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
236
            )
237
            ->setResponseEntity(new LatePaymentsResponse())
238
            ->build();
239
240
        return $this->getHttpClient()->execute($request);
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     *
246
     * @throws \Exception
247
     */
248
    public function shippingGoodsPaymentRequest(
249
        $paymentId,
250
        ShippingGoodsPaymentRequest $shippingGoodsPaymentRequest = null
251
    )
252
    {
253
        $this->configuration->assertLoaded();
254
255
        $request = RequestBuilder::post($this->getShippingGoodsPaymentURL($paymentId))
256
            ->addRawHeader(
257
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
258
            )
259
            ->setRequestEntity($shippingGoodsPaymentRequest ?: new ShippingGoodsPaymentRequest())
260
            ->setResponseEntity(new ShippingGoodsPaymentResponse())
261
            ->build();
262
263
        return $this->getHttpClient()->execute($request);
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     *
269
     * @throws \Exception
270
     */
271
    public function cancelPaymentRequest($paymentId)
272
    {
273
        $this->configuration->assertLoaded();
274
275
        $request = RequestBuilder::post($this->getCancelPaymentURL($paymentId))
276
            ->addRawHeader(
277
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
278
            )
279
            ->setResponseEntity(new CancelPaymentResponse())
280
            ->build();
281
282
        return $this->getHttpClient()->execute($request);
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     *
288
     * @throws \Exception
289
     */
290
    public function retrieveApiCallRequest($callId)
291
    {
292
        $this->configuration->assertLoaded();
293
294
        $request = RequestBuilder::get($this->getRetrieveApiCallURL($callId))
295
            ->addRawHeader(
296
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
297
            )
298
            ->setResponseEntity(new RetrieveApiCallResponse())
299
            ->build();
300
301
        return $this->getHttpClient()->execute($request);
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     *
307
     * @throws \Exception
308
     */
309
    public function listPaymentOptionsRequest($params = array(), $businessUuid = '', $channel = '')
310
    {
311
        $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid();
312
        $channel = $channel ?: $this->getConfiguration()->getChannelSet();
313
314
        $request = RequestBuilder::get($this->getListPaymentOptionsURL($businessUuid, $channel, $params))
315
            ->setResponseEntity(new ListPaymentOptionsResponse())
316
            ->build();
317
318
        return $this->getHttpClient()->execute($request);
319
    }
320
321
    /**
322
     * {@inheritdoc}
323
     *
324
     * @throws \Exception
325
     */
326
    public function listPaymentOptionsWithVariantsRequest($params = array(), $businessUuid = '', $channel = '')
327
    {
328
        $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid();
329
        $channel = $channel ?: $this->getConfiguration()->getChannelSet();
330
331
        $request = RequestBuilder::get($this->getListPaymentOptionsVariantsURL($businessUuid, $channel, $params))
332
            ->setResponseEntity(new ListPaymentOptionsWithVariantsResponse())
333
            ->build();
334
335
        return $this->getHttpClient()->execute($request);
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     *
341
     * @throws \Exception
342
     */
343
    public function getTransactionRequest($paymentId)
344
    {
345
        $this->configuration->assertLoaded();
346
347
        $request = RequestBuilder::get($this->getTransactionURL($paymentId))
348
            ->addRawHeader(
349
                $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString()
350
            )
351
            ->setResponseEntity(new GetTransactionResponse())
352
            ->build();
353
354
        return $this->getHttpClient()->execute($request);
355
    }
356
357
    /**
358
     * Returns URL for Create Payment path
359
     *
360
     * @return string
361
     */
362
    protected function getCreatePaymentURL()
363
    {
364
        return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT;
365
    }
366
367
    /**
368
     * Returns URL for Retrieve Payment path
369
     *
370
     * @param string $paymentId
371
     *
372
     * @return string
373
     */
374
    protected function getRetrievePaymentURL($paymentId)
375
    {
376
        return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_PAYMENT, $paymentId);
377
    }
378
379
    /**
380
     * Returns URL for List Payments path
381
     *
382
     * @return string
383
     */
384
    protected function getListPaymentsURL()
385
    {
386
        return $this->getBaseUrl() . self::SUB_URL_LIST_PAYMENTS;
387
    }
388
389
    /**
390
     * Returns URL for Refund Payment path
391
     *
392
     * @param string $paymentId
393
     *
394
     * @return string
395
     */
396
    protected function getRefundPaymentURL($paymentId)
397
    {
398
        return $this->getBaseUrl() . sprintf(self::SUB_URL_REFUND_PAYMENT, $paymentId);
399
    }
400
401
    /**
402
     * Returns URL for Authorize Payment path
403
     *
404
     * @param string $paymentId
405
     *
406
     * @return string
407
     */
408
    protected function getAuthorizePaymentURL($paymentId)
409
    {
410
        return $this->getBaseUrl() . sprintf(self::SUB_URL_AUTHORIZE_PAYMENT, $paymentId);
411
    }
412
413
    /**
414
     * Returns URL for Remind Payment path
415
     *
416
     * @param string $paymentId
417
     *
418
     * @return string
419
     */
420
    protected function getRemindPaymentURL($paymentId)
421
    {
422
        return $this->getBaseUrl() . sprintf(self::SUB_URL_REMIND_PAYMENT, $paymentId);
423
    }
424
425
    /**
426
     * Returns URL for Collect Payment path
427
     *
428
     * @param string $paymentId
429
     *
430
     * @return string
431
     */
432
    protected function getCollectPaymentsURL($paymentId)
433
    {
434
        return $this->getBaseUrl() . sprintf(self::SUB_URL_COLLECT_PAYMENTS, $paymentId);
435
    }
436
437
    /**
438
     * Returns URL for Late Payments path
439
     *
440
     * @param string $paymentId
441
     *
442
     * @return string
443
     */
444
    protected function getLatePaymentsURL($paymentId)
445
    {
446
        return $this->getBaseUrl() . sprintf(self::SUB_URL_LATE_PAYMENTS, $paymentId);
447
    }
448
449
    /**
450
     * Returns URL for Shipping Goods Payment path
451
     *
452
     * @param string $paymentId
453
     *
454
     * @return string
455
     */
456
    protected function getShippingGoodsPaymentURL($paymentId)
457
    {
458
        return $this->getBaseUrl() . sprintf(self::SUB_URL_SHIPPING_GOODS_PAYMENT, $paymentId);
459
    }
460
461
    /**
462
     * Returns URL for Cancel Payment path
463
     *
464
     * @param string $paymentId
465
     *
466
     * @return string
467
     */
468
    protected function getCancelPaymentURL($paymentId)
469
    {
470
        return $this->getBaseUrl() . sprintf(self::SUB_URL_CANCEL_PAYMENT, $paymentId);
471
    }
472
473
    /**
474
     * Returns URL for Retrieve API Call path
475
     *
476
     * @param string $callId
477
     *
478
     * @return string
479
     */
480
    protected function getRetrieveApiCallURL($callId)
481
    {
482
        return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_API_CALL, $callId);
483
    }
484
485
    /**
486
     * Returns URL for Available Payment Options path
487
     *
488
     * @param string $businessUuid
489
     * @param string $channel
490
     * @param array $params
491
     *
492
     * @return string
493
     */
494
    protected function getListPaymentOptionsURL($businessUuid, $channel, $params = array())
495
    {
496
        return $this->getBaseUrl()
497
            . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS, $businessUuid, $channel)
498
            . (empty($params) ? '' : '?' . http_build_query($params));
499
    }
500
501
    /**
502
     * Returns URL for Available Payment Options request
503
     *
504
     * @param string $businessUuid
505
     * @param string $channel
506
     * @param array $params
507
     *
508
     * @return string
509
     */
510
    protected function getListPaymentOptionsVariantsURL($businessUuid, $channel, $params = array())
511
    {
512
        return $this->getBaseUrl()
513
            . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS, $businessUuid, $channel)
514
            . (empty($params) ? '' : '?' . http_build_query($params));
515
    }
516
517
    /**
518
     * Returns URL to Transaction path
519
     *
520
     * @param int $paymentId
521
     *
522
     * @return string
523
     */
524
    protected function getTransactionURL($paymentId)
525
    {
526
        return $this->getBaseUrl() . sprintf(self::SUB_URL_TRANSACTION, $paymentId);
527
    }
528
}
529