Completed
Push — master ( f863cd...5b5105 )
by Kirill
12:27
created

Client::validateNotification()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Personnage\Tinkoff\SDK\Merchant;
4
5
use GuzzleHttp\Psr7\Request;
6
use Personnage\Tinkoff\SDK\Event\FailureEvent;
7
use Personnage\Tinkoff\SDK\Event\StartedEvent;
8
use Personnage\Tinkoff\SDK\Event\SuccessEvent;
9
use Personnage\Tinkoff\SDK\Exception\HttpException;
10
use Personnage\Tinkoff\SDK\Exception\InvalidToken;
11
use function Personnage\Tinkoff\SDK\pack;
12
use Personnage\Tinkoff\SDK\Exception\ResponseException;
13
use Personnage\Tinkoff\SDK\HasEvents;
14
use Personnage\Tinkoff\SDK\Response\Cancel;
15
use Personnage\Tinkoff\SDK\Response\Charge;
16
use Personnage\Tinkoff\SDK\Response\Confirm;
17
use Personnage\Tinkoff\SDK\Response\Init;
18
use Personnage\Tinkoff\SDK\Sender;
19
use Psr\Http\Message\RequestInterface;
20
use Psr\Http\Message\ResponseInterface;
21
22
final class Client
23
{
24
    use HasSignature, HasEvents;
25
26
    private $baseUri;
27
    private $terminalKey;
28
    private $secretKey;
29
    private $sender;
30
31
    /**
32
     * @var array
33
     */
34
    private static $requestHeaders = [
35
        'Accept' => 'application/json',
36
        'Content-Type' => 'application/x-www-form-urlencoded',
37
    ];
38
39
    /**
40
     * Init a new instance.
41
     *
42
     * @param string  $uri
43
     * @param string  $terminalKey
44
     * @param string  $secretKey
45
     * @param Sender  $sender
46
     */
47
    public function __construct(string $uri, string $terminalKey, string $secretKey, Sender $sender)
48
    {
49
        $this->baseUri = rtrim($uri, '/');
50
        $this->terminalKey = $terminalKey;
51
        $this->secretKey = $secretKey;
52
        $this->sender = $sender;
53
    }
54
55
    /**
56
     * Init a new payment session.
57
     *
58
     * @param string  $orderId Номер заказа в системе Продавца.
59
     * @param int     $amount  Сумма в копейках.
60
     * @param array   $extra   Массив дополнительных полей, которые могут быть переданы в этом запросе.
61
     *
62
     * @return Init
63
     * @throws HttpException
64
     */
65 View Code Duplication
    public function init(string $orderId, 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...
66
    {
67
        $extra['Amount'] = $amount;
68
        $extra['OrderId'] = $orderId;
69
70
        return new Init($this->send($this->makeRequest('Init', $extra)));
71
    }
72
73
    /**
74
     * Init a new payment session.
75
     *
76
     * @param string  $customerKey
77
     * @param string  $orderId
78
     * @param int     $amount
79
     * @param array   $extra
80
     *
81
     * @return Init
82
     * @throws HttpException
83
     */
84
    public function rInit(string $customerKey, string $orderId, int $amount, array $extra = []): Init
85
    {
86
        $extra['Recurrent'] = 'Y';
87
        $extra['CustomerKey'] = $customerKey;
88
89
        return $this->init($orderId, $amount, $extra);
90
    }
91
92
    /**
93
     * Call charge.
94
     *
95
     * @param int  $paymentId
96
     * @param int  $rebillId
97
     *
98
     * @return Charge
99
     *
100
     * @throws HttpException
101
     */
102
    public function charge(int $paymentId, int $rebillId): Charge
103
    {
104
        return new Charge($this->send($this->makeRequest('Charge', [
105
            'PaymentId' => $paymentId,
106
            'RebillId' => $rebillId,
107
        ])));
108
    }
109
110
    /**
111
     * Init a new payment session and call charge.
112
     *
113
     * @param int     $rebillId
114
     * @param string  $orderId
115
     * @param int     $amount
116
     * @param array   $extra
117
     *
118
     * @return Charge
119
     * @throws HttpException
120
     * @throws ResponseException
121
     */
122 View Code Duplication
    public function recurrent(int $rebillId, string $orderId, int $amount, array $extra = []): Charge
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...
123
    {
124
        $response = $this->init($orderId, $amount, $extra);
125
126
        if ($response->hasError() || 'NEW' !== $response->getStatus()) {
127
            throw new ResponseException($response);
128
        }
129
130
        return $this->charge($response->getPaymentId(), $rebillId);
131
    }
132
133
    /**
134
     * Send confirm request.
135
     *
136
     * @param int       $paymentId
137
     * @param int|null  $amount
138
     * @param array     $extra
139
     *
140
     * @return Confirm
141
     * @throws HttpException
142
     */
143 View Code Duplication
    public function confirm(int $paymentId, int $amount = null, array $extra = []): Confirm
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...
144
    {
145
        $extra['PaymentId'] = $paymentId;
146
147
        if (null !== $amount) {
148
            $extra['Amount'] = $amount;
149
        }
150
151
        return new Confirm($this->send($this->makeRequest('Confirm', $extra)));
152
    }
153
154
    /**
155
     * Send cancel request.
156
     *
157
     * @param int       $paymentId
158
     * @param int|null  $amount
159
     * @param array     $extra
160
     *
161
     * @return Cancel
162
     * @throws HttpException
163
     */
164 View Code Duplication
    public function cancel(int $paymentId, int $amount = null, array $extra = []): Cancel
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...
165
    {
166
        $extra['PaymentId'] = $paymentId;
167
168
        if (null !== $amount) {
169
            $extra['Amount'] = $amount;
170
        }
171
172
        return new Cancel($this->send($this->makeRequest('Cancel', $extra)));
173
    }
174
175
    /**
176
     * Make a new http request.
177
     *
178
     * @param string  $uri
179
     * @param array   $body
180
     *
181
     * @return RequestInterface
182
     */
183
    private function makeRequest(string $uri, array $body = []): RequestInterface
184
    {
185
        if (isset($body['DATA'])) {
186
            $body['DATA'] = pack($body['DATA']);
187
        }
188
189
        $body['TerminalKey'] = $this->terminalKey;
190
        $body['Token'] = $this->sign($body, $this->secretKey);
191
192
        return new Request('post', "$this->baseUri/$uri", self::$requestHeaders, http_build_query($body, '', '&'));
193
    }
194
195
    /**
196
     * Call http request and throw events.
197
     *
198
     * @param  RequestInterface $request
199
     *
200
     * @return ResponseInterface
201
     * @throws HttpException
202
     */
203 View Code Duplication
    private function send(RequestInterface $request): ResponseInterface
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...
204
    {
205
        $started = microtime(true);
206
207
        try {
208
            $this->fire(StartedEvent::class, [$request, $started]);
209
            $response = $this->sender->send($request);
210
            $this->fire(SuccessEvent::class, [$request, $response, microtime(true), $started]);
211
212
            return $response;
213
        } catch (HttpException $e) {
214
            $this->fire(FailureEvent::class, [$request, microtime(true), $started]);
215
216
            throw $e;
217
        }
218
    }
219
}
220