Completed
Push — master ( 3f4b39...daa8e9 )
by Kirill
10:57
created

Client::getFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Personnage\Tinkoff\SDK\E2Card;
4
5
use GuzzleHttp\Psr7\Request;
6
use Personnage\Tinkoff\SDK\Exception\HttpException;
7
use Personnage\Tinkoff\SDK\Exception\ResponseException;
8
use Personnage\Tinkoff\SDK\HasSender;
9
use Personnage\Tinkoff\SDK\Response\Init;
10
use Personnage\Tinkoff\SDK\Response\Payment;
11
use Personnage\Tinkoff\SDK\Sender;
12
use Psr\Http\Message\RequestInterface;
13
14
final class Client
15
{
16
    use HasSignature, HasSender;
17
18
    private $baseUri;
19
    private $terminalKey;
20
    private $pemFile;
21
22
    /**
23
     * @var array
24
     */
25
    private static $requestHeaders = [
26
        'Accept' => 'application/json',
27
        'Content-Type' => 'application/x-www-form-urlencoded',
28
    ];
29
30
    /**
31
     * Init a new instance.
32
     *
33
     * @param string $uri
34
     * @param string $terminalKey
35
     * @param string $pemFile
36
     * @param Sender $sender
37
     */
38
    public function __construct($uri, $terminalKey, $pemFile, Sender $sender)
39
    {
40
        $this->baseUri = rtrim($uri, '/');
41
        $this->terminalKey = $terminalKey;
42
        $this->pemFile = $pemFile;
43
        $this->setSender($sender);
44
    }
45
46
    /**
47
     * Init a new payment session.
48
     *
49
     * @param string $orderId Номер заказа в системе Продавца.
50
     * @param string $cardId  Идентификатор карты пополнения.
51
     * @param int    $amount  Сумма в копейках.
52
     * @param array  $extra   Массив дополнительных полей, которые могут быть переданы в этом запросе.
53
     *
54
     * @return Init
55
     * @throws HttpException
56
     */
57 View Code Duplication
    public function init(string $orderId, string $cardId, int $amount, array $extra = []): Init
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...
58
    {
59
        $extra['Amount'] = $amount;
60
        $extra['CardId'] = $cardId;
61
        $extra['OrderId'] = $orderId;
62
63
        return new Init($this->send($this->makeRequest('Init', $extra)));
64
    }
65
66
    /**
67
     * Make payout to card.
68
     *
69
     * @param mixed $paymentId
70
     *
71
     * @return Payment
72
     * @throws HttpException
73
     */
74
    public function payment($paymentId): Payment
75
    {
76
        return new Payment($this->send($this->makeRequest('Payment', ['PaymentId' => $paymentId])));
77
    }
78
79
    /**
80
     * Init a new payment session and make payout to card.
81
     *
82
     * @param string $orderId Номер заказа в системе Продавца.
83
     * @param string $cardId  Идентификатор карты пополнения.
84
     * @param int    $amount  Сумма в копейках.
85
     * @param array  $extra   Массив дополнительных полей, которые могут быть переданы в этом запросе.
86
     *
87
     * @return Payment
88
     * @throws HttpException
89
     * @throws ResponseException
90
     */
91 View Code Duplication
    public function payout(string $orderId, string $cardId, int $amount, array $extra = []): Payment
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...
92
    {
93
        $response = $this->init($orderId, $cardId, $amount, $extra);
94
        if ($response->hasError() || 'CHECKED' !== $response->getStatus()) {
95
            throw new ResponseException($response);
96
        }
97
98
        $response = $this->payment($response->getPaymentId());
99
        if ($response->hasError()) {
100
            throw new ResponseException($response);
101
        }
102
103
        return $response;
104
    }
105
106
    /**
107
     * Make a new http request.
108
     *
109
     * @param string $uri
110
     * @param array  $body
111
     *
112
     * @return RequestInterface
113
     */
114
    private function makeRequest(string $uri, array $body = []): RequestInterface
115
    {
116
        $body['TerminalKey'] = $this->terminalKey;
117
118
        $body['DigestValue'] = base64_encode($this->digest($body));
119
        $body['SignatureValue'] = base64_encode($this->sign($body));
120
        $body['X509SerialNumber'] = $this->getSerialNumber($this->pemFile);
121
122
        return new Request('post', "$this->baseUri/$uri", self::$requestHeaders, http_build_query($body, '', '&'));
123
    }
124
}
125