Appearance
Laravel Envato
Installation
You can install the package via composer:
sh
composer require infyomlabs/laravel-envato
You can optionally publish a configuration file by running the following command:
sh
php artisan vendor:publish --provider="InfyOmLabs\LaravelEnvato\LaravelEnvatoServiceProvider"
Usage
Configuration
Register your app with Envato and get the app credentials. After that Set the following values into your .env
file:
sh
ENVATO_CLIENT_ID =
ENVATO_CLIENT_SECRET =
ENVATO_REDIRECT_URI =
Authentication
OAuth Authentication
Redirect user to the Envato authentication by the following code,
sh
use LaravelEnvato;
...
...
$redirectUri = LaravelEnvato::auth()->authRedirect();
return response()->redirectTo($redirectUri);
It will redirect the user to the Envato Authentication page. After successful authentication, it will redirect back to the ENVATO_REDIRECT_URI
specified in the .env
file. Envato will return the authentication code by which we can retrieve the auth credentials of the user.
Put the following code to retrieve auth credentials:
sh
$code = $request->get('code');
$authCredentials = LaravelEnvato::auth()->handleRedirect($code);
It will return the instance of InfyOmLabs\LaravelEnvato\Auth\EnvatoCredentials
which will contain the following attributes:
- accessToken (string)
- refreshToken (string)
- expiresIn (Carbon DateTime Object)
You can save these values wherever you want as per your convenience. Remember, you will need these credentials in future to make API calls.
Persistent Authentication (Personal Token)
If your application do not want to access the personal data of the user, you can use the personalToken
to make api calls. In that case, you need to set ENVATO_PERSONAL_TOKEN
in your .env
file and then make
InfyOmLabs\LaravelEnvato\Auth\EnvatoCredentials` manually by the following code:
sh
$authCredentials = new EnvatoCredentials();
$authCredentials->accessToken = config('laravel-envato.personal_token');
$authCredentials->refreshToken = "";
$authCredentials->expiresIn = now()->addHours(24);
Calling APIs
Load Auth Session
Before using any APIs, you need to load the auth session by the credentials that you got from the previous step. You can do that by the following code:
sh
$envatoCredentials = new \InfyOmLabs\LaravelEnvato\Auth\EnvatoCredentials([
'access_token' => '',
'refresh_token' => '',
'expires_in' => '',
]);
LaravelEnvato::auth()->loadAuthSession($envatoCredentials);
Make API Call
sh
$response = LaravelEnvato::items()->getItem("26344507");
$result = $response->body;
It will return the instance of \InfyOmLabs\LaravelEnvato\Client\EnvatoResponse
which will contain the following attributes:
- statusCode (int)
- headers (array)
- body (mixed)
- retryAfter (int) - Only if rate limit exception was thrown
You can retrieve a response by getting the body
attribute.
Rate Limit Exception
If you are hitting a rate limiting as per the policy of Envato, package will throw InfyOmLabs\LaravelEnvato\Exceptions\EnvatoRateLimitException
exception. You can use the retryAfter
attribute to wait for the specified time and then try again to make an API call.
Refresh Token
The package will automatically try to refresh the token if it is expired and set the new credentials in Auth Session. You can listen for the event InfyOmLabs\LaravelEnvato\Events\EnvatoCredentialsRefreshed
and refresh the credentials in your storage.
List of Implementing APIs
Authentication
Envato Market Catalog
sh
LaravelEnvato::items()->searchItems([
'term' => 'InfyHMS',
'site' => 'codecanyon.net',
]);
sh
LaravelEnvato::items()->getItem("26344507");
User Details
sh
LaravelEnvato::items()->userItemsBySite("infyomlabs");
sh
LaravelEnvato::sales()->accountDetails("infyomlabs");
Private User Details
sh
LaravelEnvato::sales()->authorSales(1);
sh
LaravelEnvato::sales()->statement();
LaravelEnvato::sales()->statement(['page' => $this->page]);
LaravelEnvato::sales()->statement(['type' => 'Sale Refund', 'page' => $this->page]);
sh
LaravelEnvato::sales()->saleByCode("00000000-0000-0000-0000-000000000000");
sh
LaravelEnvato::user()->getUsername();
Testing
sh
composer test