Skip to content

Commit

Permalink
Add Salla Provider (#1261)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Michot <[email protected]>
  • Loading branch information
ghmoha and lucasmichot authored Sep 11, 2024
1 parent 14f2a4f commit 469d808
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions monorepo-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ parameters:
src/Sage: '[email protected]:SocialiteProviders/Sage.git'
src/SalesForce: '[email protected]:SocialiteProviders/SalesForce.git'
src/Salesloft: '[email protected]:SocialiteProviders/Salesloft.git'
src/Salla: '[email protected]:SocialiteProviders/Salla.git'
src/Saml2: '[email protected]:SocialiteProviders/Saml2.git'
src/SciStarter: '[email protected]:SocialiteProviders/SciStarter.git'
src/SharePoint: '[email protected]:SocialiteProviders/SharePoint.git'
Expand Down
72 changes: 72 additions & 0 deletions src/Salla/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace SocialiteProviders\Salla;

use GuzzleHttp\RequestOptions;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
public const IDENTIFIER = 'SALLA';

/**
* {@inheritdoc}
*/
protected $scopeSeparator = ' ';

/**
* {@inheritdoc}
*/
protected $scopes = [
'offline_access',
];

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->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);
}

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
$data = $user['data'];

return (new User)->setRaw($user)->map([
'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,
]);
}
}
83 changes: 83 additions & 0 deletions src/Salla/README.md
Original file line number Diff line number Diff line change
@@ -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);
});
```
<details>
<summary>
Laravel 10 or below
</summary>
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',
],
];
```
</details>

### 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``
13 changes: 13 additions & 0 deletions src/Salla/SallaExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SocialiteProviders\Salla;

use SocialiteProviders\Manager\SocialiteWasCalled;

class SallaExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled): void
{
$socialiteWasCalled->extendSocialite('salla', Provider::class);
}
}
34 changes: 34 additions & 0 deletions src/Salla/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"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": "[email protected]"
}
],
"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\\": ""
}
}
}

0 comments on commit 469d808

Please sign in to comment.