Skip to content

Commit

Permalink
Add Salla Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ghmoha committed Sep 9, 2024
1 parent 14f2a4f commit 9d2e444
Show file tree
Hide file tree
Showing 5 changed files with 210 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
80 changes: 80 additions & 0 deletions src/Salla/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace SocialiteProviders\Salla;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

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

/**
* The separating character for the requested scopes.
*
* @var string
*/
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)['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,
]);
}
}
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);
}
}
33 changes: 33 additions & 0 deletions src/Salla/composer.json
Original file line number Diff line number Diff line change
@@ -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": "[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 9d2e444

Please sign in to comment.