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 = realpath($pemFile); |
43
|
|
|
$this->setSender($sender); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getPemFile() |
50
|
|
|
{ |
51
|
|
|
return $this->pemFile; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Init a new payment session. |
56
|
|
|
* |
57
|
|
|
* @param string $orderId Номер заказа в системе Продавца. |
58
|
|
|
* @param string $cardId Идентификатор карты пополнения. |
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, string $cardId, int $amount, array $extra = []): Init |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$extra['Amount'] = $amount; |
68
|
|
|
$extra['CardId'] = $cardId; |
69
|
|
|
$extra['OrderId'] = $orderId; |
70
|
|
|
|
71
|
|
|
return new Init($this->send($this->makeRequest('Init', $extra))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Make payout to card. |
76
|
|
|
* |
77
|
|
|
* @param mixed $paymentId |
78
|
|
|
* |
79
|
|
|
* @return Payment |
80
|
|
|
* @throws HttpException |
81
|
|
|
*/ |
82
|
|
|
public function payment($paymentId): Payment |
83
|
|
|
{ |
84
|
|
|
return new Payment($this->send($this->makeRequest('Payment', ['PaymentId' => $paymentId]))); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Init a new payment session and make payout to card. |
89
|
|
|
* |
90
|
|
|
* @param string $orderId Номер заказа в системе Продавца. |
91
|
|
|
* @param string $cardId Идентификатор карты пополнения. |
92
|
|
|
* @param int $amount Сумма в копейках. |
93
|
|
|
* @param array $extra Массив дополнительных полей, которые могут быть переданы в этом запросе. |
94
|
|
|
* |
95
|
|
|
* @return Payment |
96
|
|
|
* @throws HttpException |
97
|
|
|
* @throws ResponseException |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function payout(string $orderId, string $cardId, int $amount, array $extra = []): Payment |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$response = $this->init($orderId, $cardId, $amount, $extra); |
102
|
|
|
if ($response->hasError() || 'CHECKED' !== $response->getStatus()) { |
103
|
|
|
throw new ResponseException($response); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$response = $this->payment($response->getPaymentId()); |
107
|
|
|
if ($response->hasError()) { |
108
|
|
|
throw new ResponseException($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $response; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Make a new http request. |
116
|
|
|
* |
117
|
|
|
* @param string $uri |
118
|
|
|
* @param array $body |
119
|
|
|
* |
120
|
|
|
* @return RequestInterface |
121
|
|
|
*/ |
122
|
|
|
private function makeRequest(string $uri, array $body = []): RequestInterface |
123
|
|
|
{ |
124
|
|
|
$body['TerminalKey'] = $this->terminalKey; |
125
|
|
|
|
126
|
|
|
$body['DigestValue'] = base64_encode($this->digest($body)); |
127
|
|
|
$body['SignatureValue'] = base64_encode($this->sign($body)); |
128
|
|
|
$body['X509SerialNumber'] = $this->getSerialNumber($this->pemFile); |
|
|
|
|
129
|
|
|
|
130
|
|
|
return new Request('post', "$this->baseUri/$uri", self::$requestHeaders, http_build_query($body, '', '&')); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.