From 9d2e444e9fc9cd93eb16fd7fb254f261ad9db332 Mon Sep 17 00:00:00 2001 From: ghazi mohammed Date: Mon, 9 Sep 2024 20:57:10 +0300 Subject: [PATCH 1/2] Add Salla Provider --- monorepo-builder.yml | 1 + src/Salla/Provider.php | 80 ++++++++++++++++++++++++++++ src/Salla/README.md | 83 ++++++++++++++++++++++++++++++ src/Salla/SallaExtendSocialite.php | 13 +++++ src/Salla/composer.json | 33 ++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 src/Salla/Provider.php create mode 100644 src/Salla/README.md create mode 100644 src/Salla/SallaExtendSocialite.php create mode 100644 src/Salla/composer.json diff --git a/monorepo-builder.yml b/monorepo-builder.yml index acfcf885f..d7f9c5fc3 100644 --- a/monorepo-builder.yml +++ b/monorepo-builder.yml @@ -166,6 +166,7 @@ parameters: src/Sage: 'git@github.com:SocialiteProviders/Sage.git' src/SalesForce: 'git@github.com:SocialiteProviders/SalesForce.git' src/Salesloft: 'git@github.com:SocialiteProviders/Salesloft.git' + src/Salla: 'git@github.com:SocialiteProviders/Salla.git' src/Saml2: 'git@github.com:SocialiteProviders/Saml2.git' src/SciStarter: 'git@github.com:SocialiteProviders/SciStarter.git' src/SharePoint: 'git@github.com:SocialiteProviders/SharePoint.git' diff --git a/src/Salla/Provider.php b/src/Salla/Provider.php new file mode 100644 index 000000000..3adbc4600 --- /dev/null +++ b/src/Salla/Provider.php @@ -0,0 +1,80 @@ +buildAuthUrlFromBase( + 'https://accounts.salla.sa/oauth2/auth', + $state + ); + } + + /** + * {@inheritdoc} + */ + protected function getTokenUrl() + { + return 'https://accounts.salla.sa/oauth2/token'; + } + + /** + * {@inheritdoc} + */ + protected function getUserByToken($token) + { + $response = $this->getHttpClient()->get( + 'https://accounts.salla.sa/oauth2/user/info', + [ + RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer '.$token, + ], + ] + ); + + return json_decode((string) $response->getBody(), true)['data']; + } + + /** + * {@inheritdoc} + */ + protected function mapUserToObject(array $user) + { + return (new User)->setRaw($user)->map([ + 'id' => $user['id'], + 'name' => $user['name'] ?? null, + 'email' => $user['email'] ?? null, + 'mobile' => $user['mobile'] ?? null, + 'avatar' => $user['merchant']['avatar'] ?? null, + 'domain' => $user['merchant']['domain'] ?? null, + ]); + } +} diff --git a/src/Salla/README.md b/src/Salla/README.md new file mode 100644 index 000000000..1b544c6e4 --- /dev/null +++ b/src/Salla/README.md @@ -0,0 +1,83 @@ +# Salla + +```bash +composer require socialiteproviders/salla +``` +## Create Account & App on Salla Partner +First you need to register account on [Salla Partners Portal](https://salla.partners/) and create your app to get credentials `client_id` & `client_secret`. +See the docs for more info [Salla API Docs](https://docs.salla.dev) + +## Installation & Basic Usage + +Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below. + +### Add configuration to `config/services.php` + +```php +'salla' => [ + 'client_id' => env('SALLA_CLIENT_ID'), + 'client_secret' => env('SALLA_CLIENT_SECRET'), + 'redirect' => env('SALLA_REDIRECT_URI') +], +``` + +### Add provider event listener + +#### Laravel 11+ + +In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method. + +* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers. + +```php +Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) { + $event->extendSocialite('salla', \SocialiteProviders\Salla\Provider::class); +}); +``` +
+ +Laravel 10 or below + +Configure the package's listener to listen for `SocialiteWasCalled` events. + +Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions. + +```php +protected $listen = [ + \SocialiteProviders\Manager\SocialiteWasCalled::class => [ + // ... other providers + \SocialiteProviders\Salla\SallaExtendSocialite::class.'@handle', + ], +]; +``` +
+ +### Usage + +You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed): + +```php +return Socialite::driver('salla')->redirect(); +``` +After redirect, You may return User instance: +```php +return Socialite::driver('salla')->user(); +``` + +### Returned User fields + +- ``id`` +- ``name`` +- ``email`` +- ``mobile`` +- ``role`` +- ``merchant`` +- - ``id`` +- - ``username`` +- - ``name`` +- - ``avatar`` +- - ``plan`` +- - ``status`` +- - ``domain`` +- - ``tax_number`` +- - ``commercial_number`` diff --git a/src/Salla/SallaExtendSocialite.php b/src/Salla/SallaExtendSocialite.php new file mode 100644 index 000000000..dbc604c42 --- /dev/null +++ b/src/Salla/SallaExtendSocialite.php @@ -0,0 +1,13 @@ +extendSocialite('salla', Provider::class); + } +} \ No newline at end of file diff --git a/src/Salla/composer.json b/src/Salla/composer.json new file mode 100644 index 000000000..3291ffbcf --- /dev/null +++ b/src/Salla/composer.json @@ -0,0 +1,33 @@ +{ + "name": "socialiteproviders/salla", + "description": "Salla Ecommerce OAuth2 Provider for Laravel Socialite", + "license": "MIT", + "keywords": [ + "salla", + "ecommerce", + "laravel", + "provider", + "socialite" + ], + "authors": [ + { + "name": "Ghazi Mohammed", + "email": "ghazi@ghazi.im" + } + ], + "support": { + "issues": "https://github.com/socialiteproviders/providers/issues", + "source": "https://github.com/socialiteproviders/providers", + "docs": "https://socialiteproviders.com/salla" + }, + "require": { + "php": "^8.0", + "ext-json": "*", + "socialiteproviders/manager": "^4.4" + }, + "autoload": { + "psr-4": { + "SocialiteProviders\\Salla\\": "" + } + } +} \ No newline at end of file From 749eae2528140b8c830166c7cb8767a0c15ae1d6 Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Wed, 11 Sep 2024 13:18:20 +0200 Subject: [PATCH 2/2] Some code fix --- src/Salla/Provider.php | 44 +++++++++------------ src/Salla/README.md | 6 +-- src/Salla/SallaExtendSocialite.php | 2 +- src/Salla/composer.json | 63 +++++++++++++++--------------- 4 files changed, 54 insertions(+), 61 deletions(-) diff --git a/src/Salla/Provider.php b/src/Salla/Provider.php index 3adbc4600..c5c9c32a0 100644 --- a/src/Salla/Provider.php +++ b/src/Salla/Provider.php @@ -3,7 +3,6 @@ namespace SocialiteProviders\Salla; use GuzzleHttp\RequestOptions; -use Illuminate\Support\Arr; use SocialiteProviders\Manager\OAuth2\AbstractProvider; use SocialiteProviders\Manager\OAuth2\User; @@ -12,9 +11,7 @@ class Provider extends AbstractProvider public const IDENTIFIER = 'SALLA'; /** - * The separating character for the requested scopes. - * - * @var string + * {@inheritdoc} */ protected $scopeSeparator = ' '; @@ -22,19 +19,15 @@ class Provider extends AbstractProvider * {@inheritdoc} */ protected $scopes = [ - 'offline_access' + 'offline_access', ]; - /** * {@inheritdoc} */ protected function getAuthUrl($state) { - return $this->buildAuthUrlFromBase( - 'https://accounts.salla.sa/oauth2/auth', - $state - ); + return $this->buildAuthUrlFromBase('https://accounts.salla.sa/oauth2/auth', $state); } /** @@ -50,17 +43,14 @@ protected function getTokenUrl() */ protected function getUserByToken($token) { - $response = $this->getHttpClient()->get( - 'https://accounts.salla.sa/oauth2/user/info', - [ - RequestOptions::HEADERS => [ - 'Accept' => 'application/json', - 'Authorization' => 'Bearer '.$token, - ], - ] - ); + $response = $this->getHttpClient()->get('https://accounts.salla.sa/oauth2/user/info', [ + RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer '.$token, + ], + ]); - return json_decode((string) $response->getBody(), true)['data']; + return json_decode((string) $response->getBody(), true); } /** @@ -68,13 +58,15 @@ protected function getUserByToken($token) */ protected function mapUserToObject(array $user) { + $data = $user['data']; + return (new User)->setRaw($user)->map([ - 'id' => $user['id'], - 'name' => $user['name'] ?? null, - 'email' => $user['email'] ?? null, - 'mobile' => $user['mobile'] ?? null, - 'avatar' => $user['merchant']['avatar'] ?? null, - 'domain' => $user['merchant']['domain'] ?? null, + 'id' => $data['id'], + 'name' => $data['name'] ?? null, + 'email' => $data['email'] ?? null, + 'mobile' => $data['mobile'] ?? null, + 'avatar' => $data['merchant']['avatar'] ?? null, + 'domain' => $data['merchant']['domain'] ?? null, ]); } } diff --git a/src/Salla/README.md b/src/Salla/README.md index 1b544c6e4..9c4a76ebf 100644 --- a/src/Salla/README.md +++ b/src/Salla/README.md @@ -15,9 +15,9 @@ Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), ```php 'salla' => [ - 'client_id' => env('SALLA_CLIENT_ID'), - 'client_secret' => env('SALLA_CLIENT_SECRET'), - 'redirect' => env('SALLA_REDIRECT_URI') + 'client_id' => env('SALLA_CLIENT_ID'), + 'client_secret' => env('SALLA_CLIENT_SECRET'), + 'redirect' => env('SALLA_REDIRECT_URI'), ], ``` diff --git a/src/Salla/SallaExtendSocialite.php b/src/Salla/SallaExtendSocialite.php index dbc604c42..bf4e73548 100644 --- a/src/Salla/SallaExtendSocialite.php +++ b/src/Salla/SallaExtendSocialite.php @@ -10,4 +10,4 @@ public function handle(SocialiteWasCalled $socialiteWasCalled): void { $socialiteWasCalled->extendSocialite('salla', Provider::class); } -} \ No newline at end of file +} diff --git a/src/Salla/composer.json b/src/Salla/composer.json index 3291ffbcf..cc575159e 100644 --- a/src/Salla/composer.json +++ b/src/Salla/composer.json @@ -1,33 +1,34 @@ { - "name": "socialiteproviders/salla", - "description": "Salla Ecommerce OAuth2 Provider for Laravel Socialite", - "license": "MIT", - "keywords": [ - "salla", - "ecommerce", - "laravel", - "provider", - "socialite" - ], - "authors": [ - { - "name": "Ghazi Mohammed", - "email": "ghazi@ghazi.im" + "name": "socialiteproviders/salla", + "description": "Salla Ecommerce OAuth2 Provider for Laravel Socialite", + "license": "MIT", + "keywords": [ + "ecommerce", + "laravel", + "oauth", + "provider", + "salla", + "socialite" + ], + "authors": [ + { + "name": "Ghazi Mohammed", + "email": "ghazi@ghazi.im" + } + ], + "support": { + "issues": "https://github.com/socialiteproviders/providers/issues", + "source": "https://github.com/socialiteproviders/providers", + "docs": "https://socialiteproviders.com/salla" + }, + "require": { + "php": "^8.0", + "ext-json": "*", + "socialiteproviders/manager": "^4.4" + }, + "autoload": { + "psr-4": { + "SocialiteProviders\\Salla\\": "" + } } - ], - "support": { - "issues": "https://github.com/socialiteproviders/providers/issues", - "source": "https://github.com/socialiteproviders/providers", - "docs": "https://socialiteproviders.com/salla" - }, - "require": { - "php": "^8.0", - "ext-json": "*", - "socialiteproviders/manager": "^4.4" - }, - "autoload": { - "psr-4": { - "SocialiteProviders\\Salla\\": "" - } - } -} \ No newline at end of file +}