|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace B2Binpay\v1; |
|
5
|
|
|
|
|
6
|
|
|
use B2Binpay\ApiInterface; |
|
7
|
|
|
use B2Binpay\Request; |
|
8
|
|
|
use B2Binpay\Exception\UpdateTokenException; |
|
9
|
|
|
use B2Binpay\Exception\UnknownValueException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* B2BinPay API Client v1 Implementation |
|
13
|
|
|
* |
|
14
|
|
|
* @package B2Binpay |
|
15
|
|
|
*/ |
|
16
|
|
|
class Api implements ApiInterface |
|
17
|
|
|
{ |
|
18
|
|
|
const GW_PRODUCTION = 'https://gw.b2binpay.com'; |
|
19
|
|
|
const GW_TEST = 'https://gw-test.b2binpay.com'; |
|
20
|
|
|
|
|
21
|
|
|
const URI_LOGIN = '/api/login'; |
|
22
|
|
|
const URI_BILLS = '/api/v1/pay/bills'; |
|
23
|
|
|
const URI_WALLETS = '/api/v1/pay/wallets'; |
|
24
|
|
|
const URI_TRANSACTIONS = '/api/v1/pay/transactions'; |
|
25
|
|
|
const URI_RATES_DEPOSIT = '/api/v1/rates/deposit/'; |
|
26
|
|
|
const URI_RATES_WITHDRAW = '/api/v1/rates/withdraw/'; |
|
27
|
|
|
const URI_VIRTUAL_WALLETS = '/api/v1/virtualwallets/wallets'; |
|
28
|
|
|
const URI_WITHDRAWS = '/api/v1/virtualwallets/withdraws'; |
|
29
|
|
|
const URI_TRANSFERS = '/api/v1/virtualwallets/transfers'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array List of nodes |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $node = [ |
|
35
|
|
|
'ADA' => 'https://ada.b2binpay.com', |
|
36
|
|
|
'BCH' => 'https://bch.b2binpay.com', |
|
37
|
|
|
'BNB' => 'https://bnb.b2binpay.com', |
|
38
|
|
|
'BTC' => 'https://btc.b2binpay.com', |
|
39
|
|
|
'BUSD-ETH' => 'https://eth.b2binpay.com', |
|
40
|
|
|
'DAI-ETH' => 'https://eth.b2binpay.com', |
|
41
|
|
|
'DASH' => 'https://dash.b2binpay.com', |
|
42
|
|
|
'DOGE' => 'https://doge.b2binpay.com', |
|
43
|
|
|
'EOS' => 'https://eos.b2binpay.com', |
|
44
|
|
|
'ETH' => 'https://eth.b2binpay.com', |
|
45
|
|
|
'GUSD-ETH' => 'https://eth.b2binpay.com', |
|
46
|
|
|
'LTC' => 'https://ltc.b2binpay.com', |
|
47
|
|
|
'NEO' => 'https://neo.b2binpay.com', |
|
48
|
|
|
'PAX-ETH' => 'https://eth.b2binpay.com', |
|
49
|
|
|
'TRX' => 'https://tron.b2binpay.com', |
|
50
|
|
|
'TUSD-ETH' => 'https://eth.b2binpay.com', |
|
51
|
|
|
'USDC-ETH' => 'https://eth.b2binpay.com', |
|
52
|
|
|
'USDT-ETH' => 'https://eth.b2binpay.com', |
|
53
|
|
|
'USDT-OMNI' => 'https://omni.b2binpay.com', |
|
54
|
|
|
'XEM' => 'https://nem.b2binpay.com', |
|
55
|
|
|
'XLM' => 'https://xlm.b2binpay.com', |
|
56
|
|
|
'XMR' => 'https://xmr.b2binpay.com', |
|
57
|
|
|
'XRP' => 'https://xrp.b2binpay.com', |
|
58
|
|
|
'ZEC' => 'https://zec.b2binpay.com', |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private $authKey; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
private $authSecret; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
27 |
|
private $accessToken; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var Request |
|
78
|
|
|
*/ |
|
79
|
|
|
private $request; |
|
80
|
27 |
|
|
|
81
|
27 |
|
/** |
|
82
|
27 |
|
* @var bool |
|
83
|
27 |
|
*/ |
|
84
|
27 |
|
private $testing; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $authKey |
|
88
|
|
|
* @param string $authSecret |
|
89
|
|
|
* @param Request|null $request |
|
90
|
|
|
* @param bool|false $testing |
|
91
|
|
|
*/ |
|
92
|
7 |
|
public function __construct( |
|
93
|
|
|
string $authKey, |
|
94
|
7 |
|
string $authSecret, |
|
95
|
|
|
Request $request = null, |
|
96
|
|
|
bool $testing = false |
|
97
|
7 |
|
) { |
|
98
|
1 |
|
$this->authKey = $authKey; |
|
99
|
1 |
|
$this->authSecret = $authSecret; |
|
100
|
1 |
|
$this->request = $request ?? new Request(); |
|
101
|
1 |
|
$this->testing = $testing; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
7 |
|
/** |
|
105
|
|
|
* @param string $method |
|
106
|
|
|
* @param string $url |
|
107
|
|
|
* @param array $params |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
10 |
|
public function sendRequest(string $method, string $url, array $params = []) |
|
111
|
|
|
{ |
|
112
|
10 |
|
$token = $this->getAccessToken(); |
|
113
|
|
|
|
|
114
|
|
|
try { |
|
115
|
|
|
$result = $this->request->send($token, $method, $url, $params); |
|
116
|
|
|
} catch (UpdateTokenException $e) { |
|
117
|
|
|
$this->accessToken = null; |
|
118
|
|
|
$token = $this->getAccessToken(); |
|
119
|
2 |
|
$result = $this->request->send($token, $method, $url, $params); |
|
120
|
|
|
} |
|
121
|
2 |
|
|
|
122
|
1 |
|
return $result; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
/** |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
private function getGateway(): string |
|
129
|
|
|
{ |
|
130
|
|
|
return ($this->testing) ? self::GW_TEST : self::GW_PRODUCTION; |
|
131
|
|
|
} |
|
132
|
2 |
|
|
|
133
|
|
|
/** |
|
134
|
2 |
|
* @param string $currency |
|
135
|
2 |
|
* @return string |
|
136
|
|
|
* @throws UnknownValueException |
|
137
|
2 |
|
*/ |
|
138
|
|
|
public static function getNode(string $currency): string |
|
139
|
|
|
{ |
|
140
|
|
|
if (!array_key_exists($currency, self::$node)) { |
|
141
|
|
|
throw new UnknownValueException($currency); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
3 |
|
return self::$node[$currency]; |
|
145
|
|
|
} |
|
146
|
3 |
|
|
|
147
|
3 |
|
/** |
|
148
|
|
|
* @param string $rateType = 'deposit' or 'withdraw' |
|
149
|
3 |
|
* @return string |
|
150
|
3 |
|
*/ |
|
151
|
|
|
public function getRatesUrl(string $rateType = 'deposit'): string |
|
152
|
|
|
{ |
|
153
|
3 |
|
$url = $this->getGateway(); |
|
154
|
|
|
$url .= ('deposit' === $rateType) ? self::URI_RATES_DEPOSIT : self::URI_RATES_WITHDRAW; |
|
155
|
|
|
|
|
156
|
|
|
return $url; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
2 |
|
* @param int|null $wallet |
|
161
|
|
|
* @return string |
|
162
|
2 |
|
*/ |
|
163
|
2 |
|
public function getWalletsUrl(int $wallet = null): string |
|
164
|
|
|
{ |
|
165
|
2 |
|
$uri = self::URI_WALLETS; |
|
166
|
|
|
if (!empty($wallet)) { |
|
167
|
|
|
$uri .= '/' . $wallet; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $this->getGateway() . $uri; |
|
171
|
|
|
} |
|
172
|
3 |
|
|
|
173
|
|
|
/** |
|
174
|
3 |
|
* @param string $currency |
|
175
|
3 |
|
* @return string |
|
176
|
2 |
|
*/ |
|
177
|
|
|
public function getNewBillUrl(string $currency): string |
|
178
|
|
|
{ |
|
179
|
3 |
|
$gateway = ($this->testing) ? self::GW_TEST : $this->getNode($currency); |
|
180
|
|
|
$uri = self::URI_BILLS; |
|
181
|
|
|
|
|
182
|
|
|
return $gateway . $uri; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
10 |
|
/** |
|
186
|
|
|
* @param int|null $bill |
|
187
|
10 |
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getBillsUrl(int $bill = null): string |
|
190
|
|
|
{ |
|
191
|
|
|
$gateway = ($this->testing) ? self::GW_TEST : self::GW_PRODUCTION; |
|
192
|
|
|
$uri = self::URI_BILLS; |
|
193
|
9 |
|
|
|
194
|
|
|
if (!empty($bill)) { |
|
195
|
9 |
|
$uri .= '/' . $bill; |
|
196
|
8 |
|
} |
|
197
|
8 |
|
|
|
198
|
|
|
return $gateway . $uri; |
|
199
|
9 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param int|null $transactionId |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
2 |
|
public function getTransactionsUrl(int $transactionId = null): string |
|
206
|
|
|
{ |
|
207
|
2 |
|
$uri = self::URI_TRANSACTIONS; |
|
208
|
2 |
|
if (!empty($transactionId)) { |
|
209
|
|
|
$uri .= '/' . $transactionId; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $this->getGateway() . $uri; |
|
213
|
4 |
|
} |
|
214
|
|
|
|
|
215
|
4 |
|
/** |
|
216
|
4 |
|
* @param int|null $virtualWalletId |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
|
public function getVirtualWalletsUrl(int $virtualWalletId = null): string |
|
220
|
|
|
{ |
|
221
|
|
|
$uri = self::URI_VIRTUAL_WALLETS; |
|
222
|
|
|
if (!empty($virtualWalletId)) { |
|
223
|
|
|
$uri .= '/' . $virtualWalletId; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $this->getGateway() . $uri; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getNewWithdrawalUrl(): string |
|
233
|
|
|
{ |
|
234
|
|
|
$uri = self::URI_WITHDRAWS; |
|
235
|
|
|
|
|
236
|
|
|
return $this->getGateway() . $uri; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param int|null $withdrawalId |
|
241
|
|
|
* @return string |
|
242
|
|
|
*/ |
|
243
|
|
|
public function getWithdrawalsUrl(int $withdrawalId = null): string |
|
244
|
|
|
{ |
|
245
|
|
|
$uri = self::URI_WITHDRAWS; |
|
246
|
|
|
if (!empty($withdrawalId)) { |
|
247
|
|
|
$uri .= '/' . $withdrawalId; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return $this->getGateway() . $uri; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param int|null $transferId |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
public function getTransfersUrl(int $transferId = null): string |
|
258
|
|
|
{ |
|
259
|
|
|
$uri = self::URI_TRANSFERS; |
|
260
|
|
|
if (!empty($transferId)) { |
|
261
|
|
|
$uri .= '/' . $transferId; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
return $this->getGateway() . $uri; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @return string |
|
269
|
|
|
*/ |
|
270
|
|
|
public function genAuthBasic(): string |
|
271
|
|
|
{ |
|
272
|
|
|
return (string)base64_encode("$this->authKey:$this->authSecret"); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @return string |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getAccessToken(): string |
|
279
|
|
|
{ |
|
280
|
|
|
if (empty($this->accessToken)) { |
|
281
|
|
|
$url = $this->getGateway() . self::URI_LOGIN; |
|
282
|
|
|
$this->accessToken = $this->request->token($this->genAuthBasic(), $url); |
|
283
|
|
|
} |
|
284
|
|
|
return $this->accessToken; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* @param string $time |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
|
|
public function genSignString(string $time): string |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->authKey . ':' . $this->authSecret . ':' . $time; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @param string $accessToken |
|
298
|
|
|
*/ |
|
299
|
|
|
public function setAccessToken(string $accessToken) |
|
300
|
|
|
{ |
|
301
|
|
|
$this->accessToken = $accessToken; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param bool $testing |
|
306
|
|
|
*/ |
|
307
|
|
|
public function setTesting(bool $testing) |
|
308
|
|
|
{ |
|
309
|
|
|
$this->testing = $testing; |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|