Last active
August 25, 2023 12:34
-
-
Save wesllycode/40c23545820e8802a6c71d04c7ec506a to your computer and use it in GitHub Desktop.
Meu service no laravel com Mercadopago para gerar QRCODE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\GatewayPayments; | |
use App\Models\Company; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Exception\GuzzleException; | |
use GuzzleHttp\Psr7\Request; | |
use Illuminate\Http\JsonResponse; | |
use Illuminate\Http\Response; | |
class Mercadopago | |
{ | |
/** | |
* This is function __construct instantiate connection with API of MercadoPago | |
* | |
* @param $client | |
* @param $base_uri | |
* @param $authorization | |
* @param $tenantid | |
* | |
* @see $client create guzzle/http | |
* @see $base_uri add url to connect to Mercadopago api | |
* @see $authorization add an access 'token' to connect the Mercadopago API | |
* @see $tenantid variable with tenant_id value from tenancy/laravel package | |
*/ | |
private string $base_uri = 'https://api.mercadopago.com/'; | |
private mixed $client; | |
private mixed $authorization; | |
private string $tenantid; | |
public function __construct() | |
{ | |
$this->tenantid = $tenantid = tenant()->id; | |
$this->authorization = Company::query()->withWhereHas('tenant', function ($query) use ($tenantid) { | |
$query->where('id', $tenantid); | |
})->withWhereHas('paymentgateway')->first(); | |
$this->client = new \GuzzleHttp\Client([ | |
'base_uri' => $this->base_uri, | |
'timeout' => 0, | |
'headers' => [ | |
"Authorization" => $this->authorization->paymentgateway->access_token, | |
'Content-Type' => 'application/json', | |
] | |
]); | |
$this->client; | |
} | |
/** | |
* This is function create QRCODE pix | |
* | |
* @param array $body | |
* @return string | |
* | |
* @see $body receives the information to generate payment in Mercadopago. | |
*/ | |
public function createQrCodePix(array $body): string | |
{ | |
$result = $this->client->postAsync('/v1/payments', ['json' => $body])->then( | |
function ($response) { | |
return $response->getBody()->getContents(); | |
} | |
); | |
$result = $result->wait(); | |
return $result; | |
} | |
/** | |
* @param int|null $id | |
* @return JsonResponse|string | |
* | |
* @catch \GuzzleHttp\Exception\ClientException for errors 400 | |
* @catch \GuzzleHttp\Exception\ConnectException for errors 500 | |
*/ | |
public function notificationPayment(int|null $id) | |
{ | |
try { | |
$result = $this->client->getAsync('collections/notifications/' . $id)->then( | |
function ($response) { | |
return $response->getBody()->getContents(); | |
} | |
); | |
$result = json_decode($result->wait()); | |
return $result->collection; | |
} catch (\GuzzleHttp\Exception\ClientException $e) { | |
$response = $e->getResponse(); | |
logs()->info($response); | |
return $responseBodyAsString = $response->getStatusCode(); | |
} catch (\GuzzleHttp\Exception\ConnectException $e) { | |
$response = $e->getCode(); | |
logs()->info($response); | |
return $response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment