wandesnet / mercado-livre
PHP SDK for integration with Mercado Livre
1.2.5
2023-10-25 18:41 UTC
Requires
- php: ^8.1.0
- saloonphp/saloon: ^3.0
Requires (Dev)
- laravel/pint: ^1.7
- pestphp/pest: ^2.4.0
- phpstan/phpstan: ^1.10.10
- symfony/var-dumper: ^6.2.8
README
This integration package with Mercado Livre (not the official one)
Required
- PHP 8.1+
Installation
composer require wandesnet/mercado-livre
Usage
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());
Tests
./vendor/bin/pest