Passed
Push — master ( 6c42be...776013 )
by Igor
02:03
created

PaymentsApiClient::listPaymentOptionsRequest()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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