1 | <?php |
||||||
2 | declare(strict_types=1); |
||||||
3 | |||||||
4 | namespace B2Binpay; |
||||||
5 | |||||||
6 | use B2Binpay\v1\Api; |
||||||
7 | use B2Binpay\Exception\B2BinpayException; |
||||||
8 | use B2Binpay\Exception\IncorrectRatesException; |
||||||
9 | use GuzzleHttp\Client; |
||||||
10 | |||||||
11 | /** |
||||||
12 | * B2BinPay payment provider |
||||||
13 | * |
||||||
14 | * @package B2Binpay |
||||||
15 | */ |
||||||
16 | class Provider |
||||||
17 | { |
||||||
18 | /** |
||||||
19 | * @var ApiInterface |
||||||
20 | */ |
||||||
21 | private $api; |
||||||
22 | |||||||
23 | /** |
||||||
24 | * @var Currency |
||||||
25 | */ |
||||||
26 | private $currency; |
||||||
27 | |||||||
28 | /** |
||||||
29 | * @var AmountFactory |
||||||
30 | */ |
||||||
31 | private $amountFactory; |
||||||
32 | |||||||
33 | /** |
||||||
34 | * @param string $authKey |
||||||
35 | * @param string $authSecret |
||||||
36 | * @param bool|false $testing |
||||||
37 | * @param Client|null $client |
||||||
38 | * @param Currency|null $currency |
||||||
39 | * @param AmountFactory|null $amountFactory |
||||||
40 | 31 | * @param ApiInterface|null $api |
|||||
41 | */ |
||||||
42 | public function __construct( |
||||||
43 | string $authKey, |
||||||
44 | string $authSecret, |
||||||
45 | bool $testing = false, |
||||||
46 | Client $client = null, |
||||||
47 | Currency $currency = null, |
||||||
48 | AmountFactory $amountFactory = null, |
||||||
49 | 31 | ApiInterface $api = null |
|||||
50 | 31 | ) |
|||||
51 | { |
||||||
52 | 31 | $this->currency = $currency ?? new Currency(); |
|||||
53 | $this->amountFactory = $amountFactory ?? new AmountFactory($this->currency); |
||||||
54 | 31 | ||||||
55 | 19 | $request = ($client) ? new Request($client) : null; |
|||||
56 | 19 | ||||||
57 | 19 | $this->api = $api ?? new Api( |
|||||
58 | 19 | $authKey, |
|||||
59 | $authSecret, |
||||||
60 | 31 | $request, |
|||||
61 | $testing |
||||||
62 | ); |
||||||
63 | } |
||||||
64 | |||||||
65 | 2 | /** |
|||||
66 | * @return string |
||||||
67 | 2 | */ |
|||||
68 | public function getAuthorization(): string |
||||||
69 | { |
||||||
70 | return 'Basic ' . $this->api->genAuthBasic(); |
||||||
71 | } |
||||||
72 | |||||||
73 | 2 | /** |
|||||
74 | * @return string |
||||||
75 | 2 | */ |
|||||
76 | public function getAuthToken(): string |
||||||
77 | { |
||||||
78 | return $this->api->getAccessToken(); |
||||||
79 | } |
||||||
80 | |||||||
81 | /** |
||||||
82 | * @param string $currency |
||||||
83 | 2 | * @param string $rate_type = 'deposit' or 'withdraw' |
|||||
84 | * @return mixed Rates |
||||||
85 | 2 | * @throws B2BinpayException |
|||||
86 | 2 | */ |
|||||
87 | 2 | public function getRates(string $currency = 'USD', string $rate_type = 'deposit') |
|||||
88 | { |
||||||
89 | 2 | $url = $this->api->getRatesUrl($rate_type) . strtolower($currency); |
|||||
90 | |||||||
91 | 2 | $response = $this->api->sendRequest('get', $url); |
|||||
92 | |||||||
93 | return $response->data; |
||||||
94 | } |
||||||
95 | |||||||
96 | /** |
||||||
97 | * @param string $sum |
||||||
98 | 2 | * @param string $currencyFrom |
|||||
99 | * @param string $currencyTo |
||||||
100 | 2 | * @param array|null $rates |
|||||
101 | * @return string |
||||||
102 | 2 | * @throws IncorrectRatesException |
|||||
103 | */ |
||||||
104 | 2 | public function convertCurrency( |
|||||
105 | string $sum, |
||||||
106 | string $currencyFrom, |
||||||
107 | string $currencyTo, |
||||||
108 | array $rates = null |
||||||
109 | ): string |
||||||
110 | { |
||||||
111 | $isoFrom = $this->currency->getIso($currencyFrom); |
||||||
112 | 2 | $isoTo = $this->currency->getIso($currencyTo); |
|||||
113 | |||||||
114 | 2 | $input = $this->amountFactory->create($sum, $isoFrom); |
|||||
115 | |||||||
116 | 2 | if ($isoFrom === $isoTo) { |
|||||
117 | return $input->getValue(); |
||||||
118 | 2 | } |
|||||
119 | |||||||
120 | $rates = $rates ?? $this->getRates($currencyFrom); |
||||||
121 | |||||||
122 | $rate = array_reduce( |
||||||
123 | $rates, |
||||||
124 | function ($carry, $item) use ($isoTo) { |
||||||
125 | if ($item->to->iso === $isoTo) { |
||||||
126 | $carry = $this->amountFactory->create($item->rate, null, $item->pow); |
||||||
127 | } |
||||||
128 | return $carry; |
||||||
129 | 9 | } |
|||||
130 | ); |
||||||
131 | |||||||
132 | if (empty($rate)) { |
||||||
133 | throw new IncorrectRatesException("Can't get rates to convert from $isoFrom to $isoTo"); |
||||||
134 | } |
||||||
135 | 9 | ||||||
136 | 9 | $precision = $this->currency->getPrecision($isoTo); |
|||||
137 | |||||||
138 | 9 | return $input->convert($rate, $precision)->getValue(); |
|||||
139 | } |
||||||
140 | 9 | ||||||
141 | 2 | /** |
|||||
142 | * @param string $sum |
||||||
143 | * @param string $currency |
||||||
144 | 7 | * @param int $percent |
|||||
145 | * @return string |
||||||
146 | 7 | */ |
|||||
147 | 7 | public function addMarkup(string $sum, string $currency, int $percent): string |
|||||
148 | 7 | { |
|||||
149 | 6 | $iso = $this->currency->getIso($currency); |
|||||
150 | 6 | ||||||
151 | $amount = $this->amountFactory->create($sum, $iso); |
||||||
152 | 6 | ||||||
153 | 7 | return $amount->percentage($percent)->getValue(); |
|||||
154 | 7 | } |
|||||
155 | |||||||
156 | /** |
||||||
157 | 7 | * @param int $walletId |
|||||
158 | 1 | * @param string $amount |
|||||
159 | * @param string $currency |
||||||
160 | * @param int $lifetime |
||||||
161 | 6 | * @param string|null $trackingId |
|||||
162 | * @param string|null $callbackUrl |
||||||
163 | 6 | * @param string|null $successUrl |
|||||
164 | * @param string|null $errorUrl |
||||||
165 | * @param string|null $address |
||||||
166 | * @return mixed Bill |
||||||
167 | */ |
||||||
168 | public function createBill( |
||||||
169 | int $walletId, |
||||||
170 | string $amount, |
||||||
171 | string $currency, |
||||||
172 | 6 | int $lifetime, |
|||||
173 | string $trackingId = null, |
||||||
174 | 6 | string $callbackUrl = null, |
|||||
175 | string $successUrl = null, |
||||||
176 | 6 | string $errorUrl = null, |
|||||
177 | string $address = null |
||||||
178 | 6 | ) |
|||||
179 | { |
||||||
180 | $iso = $this->currency->getIso($currency); |
||||||
181 | $url = $this->api->getNewBillUrl($currency); |
||||||
182 | |||||||
183 | $amountFactory = $this->amountFactory->create($amount, $iso); |
||||||
184 | |||||||
185 | $params = [ |
||||||
186 | 'form_params' => [ |
||||||
187 | 'amount' => $amountFactory->getPowered(), |
||||||
188 | 'wallet' => $walletId, |
||||||
189 | 'pow' => $amountFactory->getPrecision(), |
||||||
190 | 2 | 'lifetime' => $lifetime, |
|||||
191 | 'tracking_id' => $trackingId, |
||||||
192 | 'callback_url' => $callbackUrl, |
||||||
193 | 'success_url' => $successUrl, |
||||||
194 | 'error_url' => $errorUrl, |
||||||
195 | 'address' => $address |
||||||
196 | ] |
||||||
197 | ]; |
||||||
198 | 2 | ||||||
199 | 2 | $response = $this->api->sendRequest('post', $url, $params); |
|||||
200 | |||||||
201 | 2 | return $response->data; |
|||||
202 | } |
||||||
203 | |||||||
204 | 2 | /** |
|||||
205 | 2 | * @param array $params |
|||||
206 | 2 | * @return mixed Bills list |
|||||
207 | 2 | * @throws B2BinpayException |
|||||
208 | 2 | */ |
|||||
209 | 2 | public function getBills(array $params = []) |
|||||
210 | { |
||||||
211 | $url = $this->api->getBillsUrl(null, $params); |
||||||
0 ignored issues
–
show
|
|||||||
212 | 2 | ||||||
213 | return $this->api->sendRequest('get', $url, $params); |
||||||
214 | 2 | } |
|||||
215 | |||||||
216 | /** |
||||||
217 | * @param int $billId |
||||||
218 | * @return mixed Bill |
||||||
219 | * @throws B2BinpayException |
||||||
220 | */ |
||||||
221 | public function getBill(int $billId) |
||||||
222 | 2 | { |
|||||
223 | $url = $this->api->getBillsUrl($billId); |
||||||
224 | 2 | ||||||
225 | $response = $this->api->sendRequest('get', $url); |
||||||
226 | 2 | ||||||
227 | return $response->data; |
||||||
228 | 2 | } |
|||||
229 | |||||||
230 | /** |
||||||
231 | * @param array $params |
||||||
232 | * @return mixed Transactions list |
||||||
233 | * @throws B2BinpayException |
||||||
234 | */ |
||||||
235 | public function getTransactions(array $params = []) |
||||||
236 | 2 | { |
|||||
237 | $url = $this->api->getTransactionsUrl(null, $params); |
||||||
0 ignored issues
–
show
The call to
B2Binpay\ApiInterface::getTransactionsUrl() has too many arguments starting with $params .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
238 | 2 | ||||||
239 | return $this->api->sendRequest('get', $url, $params); |
||||||
240 | 2 | } |
|||||
241 | |||||||
242 | 2 | /** |
|||||
243 | * @param int $transaction_id |
||||||
244 | * @return mixed Transaction |
||||||
245 | * @throws B2BinpayException |
||||||
246 | */ |
||||||
247 | public function getTransaction(int $transaction_id) |
||||||
248 | { |
||||||
249 | $url = $this->api->getTransactionsUrl($transaction_id); |
||||||
250 | |||||||
251 | $response = $this->api->sendRequest('get', $url); |
||||||
252 | |||||||
253 | return $response->data; |
||||||
254 | } |
||||||
255 | |||||||
256 | /** |
||||||
257 | * @param array $params |
||||||
258 | * @return mixed VirtualWallets list |
||||||
259 | * @throws B2BinpayException |
||||||
260 | */ |
||||||
261 | public function getVirtualWallets(array $params = []) |
||||||
262 | { |
||||||
263 | $url = $this->api->getVirtualWalletsUrl(null, $params); |
||||||
0 ignored issues
–
show
The call to
B2Binpay\ApiInterface::getVirtualWalletsUrl() has too many arguments starting with $params .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
264 | |||||||
265 | return $this->api->sendRequest('get', $url, $params); |
||||||
266 | } |
||||||
267 | |||||||
268 | /** |
||||||
269 | * @param int $virtual_wallet_id |
||||||
270 | * @return mixed VirtualWallet |
||||||
271 | * @throws B2BinpayException |
||||||
272 | */ |
||||||
273 | public function getVirtualWallet(int $virtual_wallet_id) |
||||||
274 | { |
||||||
275 | $url = $this->api->getVirtualWalletsUrl($virtual_wallet_id); |
||||||
276 | |||||||
277 | $response = $this->api->sendRequest('get', $url); |
||||||
278 | |||||||
279 | return $response->data; |
||||||
280 | } |
||||||
281 | |||||||
282 | /** |
||||||
283 | * @param int $virtual_wallet_id |
||||||
284 | * @param string $amount |
||||||
285 | * @param string $currency |
||||||
286 | * @param string $address |
||||||
287 | * @param int $uniqueId |
||||||
288 | * @param string|null $trackingId |
||||||
289 | * @param string|null $callbackUrl |
||||||
290 | * @param string|null $message |
||||||
291 | * @param boolean $with_fee |
||||||
292 | * @return mixed Withdrawal |
||||||
293 | */ |
||||||
294 | public function createWithdrawal( |
||||||
295 | int $virtual_wallet_id, |
||||||
296 | string $amount, |
||||||
297 | string $currency, |
||||||
298 | string $address, |
||||||
299 | int $uniqueId, |
||||||
300 | string $trackingId = null, |
||||||
301 | string $callbackUrl = null, |
||||||
302 | string $message = null, |
||||||
303 | bool $with_fee = false |
||||||
304 | ) |
||||||
305 | { |
||||||
306 | $iso = $this->currency->getIso($currency); |
||||||
307 | $url = $this->api->getNewWithdrawalUrl(); |
||||||
308 | |||||||
309 | $amountFactory = $this->amountFactory->create($amount, $iso); |
||||||
310 | |||||||
311 | $params = [ |
||||||
312 | 'form_params' => [ |
||||||
313 | 'amount' => $amountFactory->getPowered(), |
||||||
314 | 'virtual_wallet_id' => $virtual_wallet_id, |
||||||
315 | 'address' => $address, |
||||||
316 | 'currency' => $iso, |
||||||
317 | 'unique_id' => $uniqueId, |
||||||
318 | 'tracking_id' => $trackingId, |
||||||
319 | 'pow' => $amountFactory->getPrecision(), |
||||||
320 | 'callback_url' => $callbackUrl, |
||||||
321 | 'message' => $message, |
||||||
322 | 'with_fee' => $with_fee |
||||||
323 | ] |
||||||
324 | ]; |
||||||
325 | |||||||
326 | $response = $this->api->sendRequest('post', $url, $params); |
||||||
327 | |||||||
328 | return $response->data; |
||||||
329 | } |
||||||
330 | |||||||
331 | /** |
||||||
332 | * @param array $params |
||||||
333 | * @return mixed Withdrawals list |
||||||
334 | * @throws B2BinpayException |
||||||
335 | */ |
||||||
336 | public function getWithdrawals(array $params = []) |
||||||
337 | { |
||||||
338 | $url = $this->api->getWithdrawalsUrl(null, $params); |
||||||
0 ignored issues
–
show
The call to
B2Binpay\ApiInterface::getWithdrawalsUrl() has too many arguments starting with $params .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
339 | |||||||
340 | return $this->api->sendRequest('get', $url, $params); |
||||||
341 | } |
||||||
342 | |||||||
343 | /** |
||||||
344 | * @param int $withdrawal_id |
||||||
345 | * @return mixed Withdrawal |
||||||
346 | * @throws B2BinpayException |
||||||
347 | */ |
||||||
348 | public function getWithdrawal(int $withdrawal_id) |
||||||
349 | { |
||||||
350 | $url = $this->api->getWithdrawalsUrl($withdrawal_id); |
||||||
351 | |||||||
352 | $response = $this->api->sendRequest('get', $url); |
||||||
353 | |||||||
354 | return $response->data; |
||||||
355 | } |
||||||
356 | |||||||
357 | /** |
||||||
358 | * @param array $params |
||||||
359 | * @return mixed Transfers list |
||||||
360 | * @throws B2BinpayException |
||||||
361 | */ |
||||||
362 | public function getTransfers(array $params = []) |
||||||
363 | { |
||||||
364 | $url = $this->api->getTransfersUrl(null, $params); |
||||||
0 ignored issues
–
show
The call to
B2Binpay\ApiInterface::getTransfersUrl() has too many arguments starting with $params .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
365 | |||||||
366 | return $this->api->sendRequest('get', $url, $params); |
||||||
367 | } |
||||||
368 | |||||||
369 | /** |
||||||
370 | * @param int $transfer_id |
||||||
371 | * @return mixed Transfer |
||||||
372 | * @throws B2BinpayException |
||||||
373 | */ |
||||||
374 | public function getTransfer(int $transfer_id) |
||||||
375 | { |
||||||
376 | $url = $this->api->getTransfersUrl($transfer_id); |
||||||
377 | |||||||
378 | $response = $this->api->sendRequest('get', $url); |
||||||
379 | |||||||
380 | return $response->data; |
||||||
381 | } |
||||||
382 | |||||||
383 | /** |
||||||
384 | * @param string $time |
||||||
385 | * @param string $sign |
||||||
386 | * @return boolean |
||||||
387 | */ |
||||||
388 | public function verifySign(string $time, string $sign): bool |
||||||
389 | { |
||||||
390 | $verify = $this->api->genSignString($time); |
||||||
391 | |||||||
392 | return password_verify($verify, $sign); |
||||||
393 | } |
||||||
394 | |||||||
395 | } |
||||||
396 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.