Mujhtech /
laravel-lazerpay
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | /* |
||||||
| 4 | * |
||||||
| 5 | * (c) Muhideen Mujeeb Adeoye <[email protected]> |
||||||
| 6 | * |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | namespace Mujhtech\Lazerpay; |
||||||
| 10 | |||||||
| 11 | use GuzzleHttp\Client; |
||||||
| 12 | use Illuminate\Support\Facades\Config; |
||||||
| 13 | use Mujhtech\Lazerpay\Exceptions\LazerpayException; |
||||||
| 14 | |||||||
| 15 | class Lazerpay |
||||||
| 16 | { |
||||||
| 17 | |||||||
| 18 | /** |
||||||
| 19 | * @var string |
||||||
| 20 | */ |
||||||
| 21 | |||||||
| 22 | protected $publicKey; |
||||||
| 23 | |||||||
| 24 | /** |
||||||
| 25 | * @var string |
||||||
| 26 | */ |
||||||
| 27 | |||||||
| 28 | protected $secretKey; |
||||||
| 29 | |||||||
| 30 | /** |
||||||
| 31 | * @var string |
||||||
| 32 | */ |
||||||
| 33 | |||||||
| 34 | protected $client; |
||||||
| 35 | |||||||
| 36 | /** |
||||||
| 37 | * Response from lazerpay api |
||||||
| 38 | * @var mixed |
||||||
| 39 | */ |
||||||
| 40 | |||||||
| 41 | protected $response; |
||||||
| 42 | |||||||
| 43 | /** |
||||||
| 44 | * @var string |
||||||
| 45 | */ |
||||||
| 46 | |||||||
| 47 | protected $baseUrl; |
||||||
| 48 | |||||||
| 49 | /** |
||||||
| 50 | * @var array |
||||||
| 51 | */ |
||||||
| 52 | |||||||
| 53 | private $coins = ['USDT', 'DAI', 'BUSD', 'USDC']; |
||||||
| 54 | |||||||
| 55 | /** |
||||||
| 56 | * @var array |
||||||
| 57 | */ |
||||||
| 58 | |||||||
| 59 | private $currencies = ['USD', 'AED', 'NGN', 'GBP', 'EUR']; |
||||||
| 60 | |||||||
| 61 | public function __construct() |
||||||
| 62 | { |
||||||
| 63 | $this->getKey(); |
||||||
| 64 | $this->getBaseUrl(); |
||||||
| 65 | $this->setRequestOptions(); |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | /** |
||||||
| 69 | * Get base url from lazerpay config |
||||||
| 70 | */ |
||||||
| 71 | |||||||
| 72 | public function getBaseUrl() |
||||||
| 73 | { |
||||||
| 74 | $this->baseUrl = Config::get('lazerpay.baseUrl'); |
||||||
| 75 | } |
||||||
| 76 | |||||||
| 77 | /** |
||||||
| 78 | * Get secret key from lazerpay cofig |
||||||
| 79 | */ |
||||||
| 80 | |||||||
| 81 | public function getKey() |
||||||
| 82 | { |
||||||
| 83 | $this->publicKey = Config::get('lazerpay.publicKey'); |
||||||
| 84 | $this->secretKey = Config::get('lazerpay.secretKey'); |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | /** |
||||||
| 88 | * Set request options |
||||||
| 89 | * @return client |
||||||
| 90 | */ |
||||||
| 91 | |||||||
| 92 | private function setRequestOptions() |
||||||
| 93 | { |
||||||
| 94 | $authBearer = 'Bearer ' . $this->secretKey; |
||||||
| 95 | |||||||
| 96 | $this->client = new Client( |
||||||
| 97 | [ |
||||||
| 98 | 'base_uri' => $this->baseUrl, |
||||||
| 99 | 'headers' => [ |
||||||
| 100 | 'Authorization' => $authBearer, |
||||||
| 101 | 'X-api-key' => $this->publicKey, |
||||||
| 102 | 'Content-Type' => 'application/json', |
||||||
| 103 | 'Accept' => 'application/json', |
||||||
| 104 | ], |
||||||
| 105 | ] |
||||||
| 106 | ); |
||||||
| 107 | |||||||
| 108 | return $this; |
||||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
| 109 | } |
||||||
| 110 | |||||||
| 111 | /** |
||||||
| 112 | * Set http response |
||||||
| 113 | * @param string $url |
||||||
| 114 | * @param string $method |
||||||
| 115 | * @param array $data |
||||||
| 116 | * @return Lazerpay |
||||||
| 117 | */ |
||||||
| 118 | |||||||
| 119 | private function setHttpResponse($url, $method = null, $body = []) |
||||||
| 120 | { |
||||||
| 121 | if (is_null($method)) { |
||||||
| 122 | throw new LazerpayException("Empty method not allowed"); |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | $this->response = $this->client->{strtolower($method)}( |
||||||
| 126 | $this->baseUrl . $url, |
||||||
| 127 | ["body" => json_encode($body)] |
||||||
| 128 | ); |
||||||
| 129 | |||||||
| 130 | return $this; |
||||||
| 131 | } |
||||||
| 132 | |||||||
| 133 | /** |
||||||
| 134 | * Decode json response into an array |
||||||
| 135 | * @return array |
||||||
| 136 | */ |
||||||
| 137 | |||||||
| 138 | private function getResponse() |
||||||
| 139 | { |
||||||
| 140 | return json_decode($this->response->getBody(), true); |
||||||
| 141 | } |
||||||
| 142 | |||||||
| 143 | /** |
||||||
| 144 | * Get the data response from a get operation |
||||||
| 145 | * @return array |
||||||
| 146 | */ |
||||||
| 147 | private function getData() |
||||||
| 148 | { |
||||||
| 149 | return $this->getResponse()['data']; |
||||||
| 150 | } |
||||||
| 151 | |||||||
| 152 | /** |
||||||
| 153 | * Verify transaction |
||||||
| 154 | * Verify transactions after payments |
||||||
| 155 | * @return array |
||||||
| 156 | */ |
||||||
| 157 | |||||||
| 158 | public function verifyTransaction(string $reference) |
||||||
| 159 | { |
||||||
| 160 | |||||||
| 161 | return $this->setRequestOptions()->setHttpResponse('/transaction/verify/' . $reference, 'GET', [])->getData(); |
||||||
|
0 ignored issues
–
show
The method
getData() does not exist on Psr\Http\Message\ResponseInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
The method
getData() does not exist on GuzzleHttp\Promise\PromiseInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 162 | |||||||
| 163 | } |
||||||
| 164 | |||||||
| 165 | /** |
||||||
| 166 | * Get all coins |
||||||
| 167 | * @return array |
||||||
| 168 | */ |
||||||
| 169 | |||||||
| 170 | public function getAllCoins() |
||||||
| 171 | { |
||||||
| 172 | |||||||
| 173 | return $this->setRequestOptions()->setHttpResponse('/coins', 'GET', [])->getData(); |
||||||
| 174 | |||||||
| 175 | } |
||||||
| 176 | |||||||
| 177 | /** |
||||||
| 178 | * Get coin rate |
||||||
| 179 | * @query string $coin, string $currency |
||||||
| 180 | * @return array |
||||||
| 181 | */ |
||||||
| 182 | |||||||
| 183 | public function getCoinRate(string $coin, string $currency) |
||||||
| 184 | { |
||||||
| 185 | |||||||
| 186 | if (!in_array($coin, $this->coins)) { |
||||||
| 187 | |||||||
| 188 | throw new LazerpayException("Invalid coin"); |
||||||
| 189 | |||||||
| 190 | } |
||||||
| 191 | |||||||
| 192 | if (!in_array($currency, $this->currencies)) { |
||||||
| 193 | |||||||
| 194 | throw new LazerpayException("Invalid coin"); |
||||||
| 195 | |||||||
| 196 | } |
||||||
| 197 | |||||||
| 198 | return $this->setRequestOptions()->setHttpResponse('/rate?coin=' . $coin . '¤cy=' . $currency, 'GET', [])->getData(); |
||||||
| 199 | |||||||
| 200 | } |
||||||
| 201 | |||||||
| 202 | /** |
||||||
| 203 | * Get wallet balance |
||||||
| 204 | * @query string $coin |
||||||
| 205 | * @return array |
||||||
| 206 | */ |
||||||
| 207 | |||||||
| 208 | public function getWalletBalance(string $coin) |
||||||
| 209 | { |
||||||
| 210 | |||||||
| 211 | if (!in_array($coin, $this->coins)) { |
||||||
| 212 | |||||||
| 213 | throw new LazerpayException("Invalid coin"); |
||||||
| 214 | |||||||
| 215 | } |
||||||
| 216 | |||||||
| 217 | return $this->setRequestOptions()->setHttpResponse('/wallet/balance?coin=' . $coin, 'GET', [])->getData(); |
||||||
| 218 | |||||||
| 219 | } |
||||||
| 220 | |||||||
| 221 | /** |
||||||
| 222 | * Crypto Transfer |
||||||
| 223 | * @param string $reference |
||||||
| 224 | * Unique case sensitive transaction reference. If you do not pass this parameter, Lazerpay will generate a unique reference for you. |
||||||
| 225 | * @param string $amount |
||||||
| 226 | * The amount you want to send out |
||||||
| 227 | * @param string $fromCoin |
||||||
| 228 | * Crypto you want to swap from |
||||||
| 229 | * @param string $toCoin |
||||||
| 230 | * Crypto you want to swap to |
||||||
| 231 | * @param string $blockchain |
||||||
| 232 | * The blockchain network you are sending to |
||||||
| 233 | * @param array $metadata e.g ['type' => "Crypto swap"] |
||||||
| 234 | * @return array |
||||||
| 235 | */ |
||||||
| 236 | |||||||
| 237 | public function cryptoTransfer(string $reference, string $coin, string $recipient, string $amount, array $metadata, string $blockchain) |
||||||
| 238 | { |
||||||
| 239 | |||||||
| 240 | if (!in_array($coin, $this->coins)) { |
||||||
| 241 | |||||||
| 242 | throw new LazerpayException("Invalid coin"); |
||||||
| 243 | |||||||
| 244 | } |
||||||
| 245 | |||||||
| 246 | if ($blockchain != 'Binance Smart Chain') { |
||||||
| 247 | |||||||
| 248 | throw new LazerpayException("We only support the Binance smart chain for swaps"); |
||||||
| 249 | |||||||
| 250 | } |
||||||
| 251 | |||||||
| 252 | $data = [ |
||||||
| 253 | 'reference' => $reference, |
||||||
| 254 | 'amount' => $amount, |
||||||
| 255 | 'recipient' => $recipient, |
||||||
| 256 | 'coin' => $coin, |
||||||
| 257 | 'metadata' => $metadata, |
||||||
| 258 | 'blockchain' => $blockchain, |
||||||
| 259 | ]; |
||||||
| 260 | |||||||
| 261 | return $this->setRequestOptions()->setHttpResponse('/transfer', 'POST', $data)->getData(); |
||||||
| 262 | |||||||
| 263 | } |
||||||
| 264 | |||||||
| 265 | /** |
||||||
| 266 | * Crypto Swap |
||||||
| 267 | * @param string $reference |
||||||
| 268 | * Unique case sensitive transaction reference. If you do not pass this parameter, Lazerpay will generate a unique reference for you. |
||||||
| 269 | * @param string $amount |
||||||
| 270 | * The amount you want to send out |
||||||
| 271 | * @param string $fromCoin |
||||||
| 272 | * Crypto you want to swap from |
||||||
| 273 | * @param string $toCoin |
||||||
| 274 | * Crypto you want to swap to |
||||||
| 275 | * @param string $blockchain |
||||||
| 276 | * The blockchain network you are sending to |
||||||
| 277 | * @param array $metadata e.g ['type' => "Crypto swap"] |
||||||
| 278 | * @return array |
||||||
| 279 | */ |
||||||
| 280 | |||||||
| 281 | public function cryptoSwap(string $reference, string $fromCoin, string $toCoin, integer $amount, array $metadata, string $blockchain) |
||||||
| 282 | { |
||||||
| 283 | |||||||
| 284 | if (!in_array($fromCoin, $this->coins)) { |
||||||
| 285 | |||||||
| 286 | throw new LazerpayException("Invalid from coin"); |
||||||
| 287 | |||||||
| 288 | } |
||||||
| 289 | |||||||
| 290 | if (!in_array($toCoin, $this->coins)) { |
||||||
| 291 | |||||||
| 292 | throw new LazerpayException("Invalid to coin"); |
||||||
| 293 | |||||||
| 294 | } |
||||||
| 295 | |||||||
| 296 | if ($toCoin == $fromCoin) { |
||||||
| 297 | |||||||
| 298 | throw new LazerpayException("toCoin and fromCoin cannot be the same"); |
||||||
| 299 | |||||||
| 300 | } |
||||||
| 301 | |||||||
| 302 | if ($blockchain != 'Binance Smart Chain') { |
||||||
| 303 | |||||||
| 304 | throw new LazerpayException("We only support the Binance smart chain for swaps"); |
||||||
| 305 | |||||||
| 306 | } |
||||||
| 307 | |||||||
| 308 | $data = [ |
||||||
| 309 | 'reference' => $reference, |
||||||
| 310 | 'amount' => $amount, |
||||||
| 311 | 'fromCoin' => $fromCoin, |
||||||
| 312 | 'toCoin' => $toCoin, |
||||||
| 313 | 'metadata' => $metadata, |
||||||
| 314 | 'blockchain' => $blockchain, |
||||||
| 315 | ]; |
||||||
| 316 | |||||||
| 317 | return $this->setRequestOptions()->setHttpResponse('/swap/crypto', 'POST', $data)->getData(); |
||||||
| 318 | |||||||
| 319 | } |
||||||
| 320 | |||||||
| 321 | /** |
||||||
| 322 | * Crypto Swap Amount |
||||||
| 323 | * This endpoint helps you get the amount you will receive on swap even before initiating the swap |
||||||
| 324 | * @param string $amount |
||||||
| 325 | * The amount you want to send out |
||||||
| 326 | * @param string $fromCoin |
||||||
| 327 | * Crypto you want to swap from |
||||||
| 328 | * @param string $toCoin |
||||||
| 329 | * Crypto you want to swap to |
||||||
| 330 | * @param string $blockchain |
||||||
| 331 | * The blockchain network you are sending to |
||||||
| 332 | * @return array |
||||||
| 333 | */ |
||||||
| 334 | |||||||
| 335 | public function cryptoSwapAmount(string $fromCoin, string $toCoin, string $amount, string $blockchain) |
||||||
| 336 | { |
||||||
| 337 | |||||||
| 338 | if (!in_array($fromCoin, $this->coins)) { |
||||||
| 339 | |||||||
| 340 | throw new LazerpayException("Invalid from coin"); |
||||||
| 341 | |||||||
| 342 | } |
||||||
| 343 | |||||||
| 344 | if (!in_array($toCoin, $this->coins)) { |
||||||
| 345 | |||||||
| 346 | throw new LazerpayException("Invalid to coin"); |
||||||
| 347 | |||||||
| 348 | } |
||||||
| 349 | |||||||
| 350 | if ($toCoin == $fromCoin) { |
||||||
| 351 | |||||||
| 352 | throw new LazerpayException("toCoin and fromCoin cannot be the same"); |
||||||
| 353 | |||||||
| 354 | } |
||||||
| 355 | |||||||
| 356 | if ($blockchain != 'Binance Smart Chain') { |
||||||
| 357 | |||||||
| 358 | throw new LazerpayException("We only support the Binance smart chain for swaps"); |
||||||
| 359 | |||||||
| 360 | } |
||||||
| 361 | |||||||
| 362 | $data = [ |
||||||
| 363 | 'amount' => $amount, |
||||||
| 364 | 'fromCoin' => $fromCoin, |
||||||
| 365 | 'toCoin' => $toCoin, |
||||||
| 366 | 'metadata' => $metadata, |
||||||
| 367 | ]; |
||||||
| 368 | |||||||
| 369 | return $this->setRequestOptions()->setHttpResponse('/swap/crypto/amount-out', 'POST', $data)->getData(); |
||||||
| 370 | |||||||
| 371 | } |
||||||
| 372 | |||||||
| 373 | /** |
||||||
| 374 | * Get all payment links |
||||||
| 375 | * @return array |
||||||
| 376 | */ |
||||||
| 377 | |||||||
| 378 | public function getPaymentLinks() |
||||||
| 379 | { |
||||||
| 380 | |||||||
| 381 | return $this->setRequestOptions()->setHttpResponse('/payment-links', 'GET', [])->getData(); |
||||||
| 382 | |||||||
| 383 | } |
||||||
| 384 | |||||||
| 385 | /** |
||||||
| 386 | * Fetch payment link |
||||||
| 387 | * @return array |
||||||
| 388 | */ |
||||||
| 389 | |||||||
| 390 | public function fetchPaymentLink(string $reference) |
||||||
| 391 | { |
||||||
| 392 | |||||||
| 393 | return $this->setRequestOptions()->setHttpResponse('/payment-links/' . $reference, 'GET', [])->getData(); |
||||||
| 394 | |||||||
| 395 | } |
||||||
| 396 | |||||||
| 397 | /** |
||||||
| 398 | * Create Payment link |
||||||
| 399 | * With payment links, you can share your unique payment link to anyone in the world. |
||||||
| 400 | * @param string $amount |
||||||
| 401 | * Amount the user will pay |
||||||
| 402 | * @param string $currency |
||||||
| 403 | * Payment page currency |
||||||
| 404 | * @param string $title |
||||||
| 405 | * The title of the link |
||||||
| 406 | * @param string $description |
||||||
| 407 | * Description of the payment page |
||||||
| 408 | * @param string $logo |
||||||
| 409 | * Your logo url |
||||||
| 410 | * @param string $type |
||||||
| 411 | * Payment links type default is "standard" |
||||||
| 412 | * @return array |
||||||
| 413 | */ |
||||||
| 414 | |||||||
| 415 | public function createPaymentLink(string $title, string $description, string $type = 'standard', string $logo, string $amount, string $currency, string $redirect_url) |
||||||
| 416 | { |
||||||
| 417 | |||||||
| 418 | $data = [ |
||||||
| 419 | 'amount' => $amount, |
||||||
| 420 | 'title' => $title, |
||||||
| 421 | 'description' => $description, |
||||||
| 422 | 'type' => $type, |
||||||
| 423 | 'logo' => $logo, |
||||||
| 424 | 'currency' => $currency, |
||||||
| 425 | 'redirect_url' => $redirect_url, |
||||||
| 426 | ]; |
||||||
| 427 | |||||||
| 428 | return $this->setRequestOptions()->setHttpResponse('/payment-links', 'POST', $data)->getData(); |
||||||
| 429 | |||||||
| 430 | } |
||||||
| 431 | |||||||
| 432 | /** |
||||||
| 433 | * Update Payment link |
||||||
| 434 | * Update a particular payment link with the following endpoint. |
||||||
| 435 | * @param string $reference |
||||||
| 436 | * Id or reference |
||||||
| 437 | * @param string $amount |
||||||
| 438 | * Amount the user will pay |
||||||
| 439 | * @param string $currency |
||||||
| 440 | * Payment page currency |
||||||
| 441 | * @param string $title |
||||||
| 442 | * The title of the link |
||||||
| 443 | * @param string $description |
||||||
| 444 | * Description of the payment page |
||||||
| 445 | * @param string $logo |
||||||
| 446 | * Your logo url |
||||||
| 447 | * @param string $type |
||||||
| 448 | * Payment links type default is "standard" |
||||||
| 449 | * @return array |
||||||
| 450 | */ |
||||||
| 451 | |||||||
| 452 | public function updatePaymentLink(string $reference, string $title, string $description, string $type = 'standard', string $logo, string $amount, string $currency, string $redirect_url) |
||||||
| 453 | { |
||||||
| 454 | |||||||
| 455 | $data = [ |
||||||
| 456 | 'amount' => $amount, |
||||||
| 457 | 'title' => $title, |
||||||
| 458 | 'description' => $description, |
||||||
| 459 | 'type' => $type, |
||||||
| 460 | 'logo' => $logo, |
||||||
| 461 | 'currency' => $currency, |
||||||
| 462 | 'redirect_url' => $redirect_url, |
||||||
| 463 | ]; |
||||||
| 464 | |||||||
| 465 | return $this->setRequestOptions()->setHttpResponse('/payment-links/' . $reference, 'PUT', $data)->getData(); |
||||||
| 466 | |||||||
| 467 | } |
||||||
| 468 | |||||||
| 469 | } |
||||||
| 470 |