This integration package with Mercado Livre (not the official one)
- PHP 8.1+
composer require wandesnet/mercado-livre
Create a class Meli
extends from Meli Connector
and implement the methods requires:
The refreshTokenExpireIn
method is free for you to implement as per your logic for refreshing the token; See the example below:
Set setClientId
, setClientSecret
, setRedirectUri
change the settings:
final class Meli extends MeliConnector
{
public ?int $tries = 2; // default 1, try to request api 2 times if fail
public function resolveBaseUrl(): string
{
return 'https://api.mercadolibre.com';
}
protected function defaultOauthConfig(): OAuthConfig
{
return OAuthConfig::make()
->setClientId('3232321')
->setClientSecret('fs4343fs423')
->setAuthorizeEndpoint('https://auth.mercadolivre.com.br/authorization')
->setRedirectUri('http://my-site.com/callback.php')
->setTokenEndpoint('oauth/token');
}
protected function refreshTokenExpireIn(): void
{
if ($this->hasExpired()) {
$refresh = $this->refreshAccessToken($this->refresh_token);
$this->access_token = $refresh->getAccessToken();
$this->refresh_token = $refresh->getRefreshToken();
$this->expires_in = $refresh->getExpiresAt()->getTimestamp();
}
}
}
Example of use Meli::make()
or new Meli()
:
$response = Meli::make('access_token', 'refresh_token', 'expire_in')->auth()->request()->order(123456789);
Example to generate authentication url in Mercado Livre:
Meli::make()->getAuthorizationUrl();
Example to get access_token
after authentication in Mercado Livre:
$authenticator = Meli::make()->getAccessToken($_GET['code']);
var_dump($authenticator->getAccessToken(), $authenticator->getRefreshToken(), $authenticator->getExpiresAt()->getTimestamp());
./vendor/bin/pest