Skip to content

Commit 92610f9

Browse files
authored
feat: implement OAuth2 client for Paymenter (#1400)
1 parent 5d3d1f9 commit 92610f9

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

monorepo-builder.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ parameters:
153153
src/Orcid: 'git@github.com:SocialiteProviders/Orcid.git'
154154
src/Ovh: 'git@github.com:SocialiteProviders/Ovh.git'
155155
src/Patreon: 'git@github.com:SocialiteProviders/Patreon.git'
156+
src/Paymenter: 'git@github.com:SocialiteProviders/Paymenter.git'
156157
src/PayPal: 'git@github.com:SocialiteProviders/PayPal.git'
157158
src/PayPalSandbox: 'git@github.com:SocialiteProviders/PayPal-Sandbox.git'
158159
src/Paymill: 'git@github.com:SocialiteProviders/Paymill.git'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SocialiteProviders\Paymenter;
4+
5+
use SocialiteProviders\Manager\SocialiteWasCalled;
6+
7+
class PaymenterExtendSocialite
8+
{
9+
public function handle(SocialiteWasCalled $socialiteWasCalled): void
10+
{
11+
$socialiteWasCalled->extendSocialite('paymenter', Provider::class);
12+
}
13+
}

src/Paymenter/Provider.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace SocialiteProviders\Paymenter;
4+
5+
use GuzzleHttp\RequestOptions;
6+
use InvalidArgumentException;
7+
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
8+
use SocialiteProviders\Manager\OAuth2\User;
9+
10+
class Provider extends AbstractProvider
11+
{
12+
public const IDENTIFIER = 'PAYMENTER';
13+
14+
protected $scopes = ['profile'];
15+
16+
public static function additionalConfigKeys(): array
17+
{
18+
return ['base_url'];
19+
}
20+
21+
protected function getBaseUrl()
22+
{
23+
$baseurl = $this->getConfig('base_url');
24+
if ($baseurl === null) {
25+
throw new InvalidArgumentException('Missing base_url');
26+
}
27+
28+
return rtrim($baseurl, '/');
29+
}
30+
31+
protected function getAuthUrl($state): string
32+
{
33+
return $this->buildAuthUrlFromBase($this->getBaseUrl().'/oauth/authorize', $state);
34+
}
35+
36+
protected function getTokenUrl(): string
37+
{
38+
return $this->getBaseUrl().'/api/oauth/token';
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function getUserByToken($token)
45+
{
46+
$response = $this->getHttpClient()->get($this->getBaseUrl().'/api/oauth/me', [
47+
RequestOptions::HEADERS => [
48+
'Authorization' => 'Bearer '.$token,
49+
],
50+
]);
51+
52+
return json_decode((string) $response->getBody(), true);
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function refreshToken($refreshToken)
59+
{
60+
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
61+
RequestOptions::FORM_PARAMS => [
62+
'client_id' => $this->clientId,
63+
'client_secret' => $this->clientSecret,
64+
'grant_type' => 'refresh_token',
65+
'refresh_token' => $refreshToken,
66+
],
67+
]);
68+
69+
return json_decode((string) $response->getBody(), true);
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
protected function mapUserToObject(array $user)
76+
{
77+
return (new User)->setRaw($user)->map([
78+
'email' => $user['email'],
79+
'email_verified' => !empty($user['email_verified_at']),
80+
'name' => $user['first_name'],
81+
'family_name' => $user['last_name'],
82+
'groups' => $user['role_id'],
83+
'id' => $user['id'],
84+
]);
85+
}
86+
}

src/Paymenter/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Paymenter
2+
3+
```bash
4+
composer require socialiteproviders/paymenter
5+
```
6+
7+
## Installation & Basic Usage
8+
9+
Please see the [Base Installation Guide](https://socialiteproviders.com/usage/),
10+
then follow the provider specific instructions below.
11+
12+
### Prepare OAuth provider & application in Paymenter
13+
14+
Create a new OAuth Client within Paymenter, according to the Paymenter
15+
Documentation (https://paymenter.org/development/OAuth)
16+
17+
### Add configuration to `config/services.php`
18+
19+
```php
20+
'paymenter' => [
21+
'base_url' => env('PAYMENTER_BASE_URL'),
22+
'client_id' => env('PAYMENTER_CLIENT_ID'),
23+
'client_secret' => env('PAYMENTER_CLIENT_SECRET'),
24+
'redirect' => env('PAYMENTER_REDIRECT_URI')
25+
],
26+
```
27+
28+
### Add provider event listener
29+
30+
#### Laravel 11+
31+
32+
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.
33+
34+
* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
35+
36+
```php
37+
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
38+
$event->extendSocialite('paymenter', \SocialiteProviders\Paymenter\Provider::class);
39+
});
40+
```
41+
<details>
42+
<summary>
43+
Laravel 10 or below
44+
</summary>
45+
Configure the package's listener to listen for `SocialiteWasCalled` events.
46+
47+
Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions.
48+
49+
```php
50+
protected $listen = [
51+
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
52+
// ... other providers
53+
\SocialiteProviders\Paymenter\PaymenterExtendSocialite::class.'@handle',
54+
],
55+
];
56+
```
57+
</details>
58+
59+
### Usage
60+
61+
You should now be able to use the provider like you would regularly use
62+
Socialite (assuming you have the facade installed):
63+
64+
```php
65+
return Socialite::driver('paymenter')->redirect();
66+
```
67+
68+
To redirect to the authentication, and then:
69+
70+
```php
71+
$user = Socialite::driver('paymenter')->user()
72+
```
73+
74+
In the return function. The user will contain a `name` and `email` field
75+
populated from the OAuth source.

src/Paymenter/composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "socialiteproviders/paymenter",
3+
"description": "Paymenter OAuth2 Provider for Laravel Socialite",
4+
"license": "MIT",
5+
"keywords": [
6+
"paymenter",
7+
"laravel",
8+
"oauth",
9+
"provider",
10+
"socialite"
11+
],
12+
"authors": [
13+
{
14+
"name": "damoun",
15+
"email": "damien@plenard.me"
16+
}
17+
],
18+
"support": {
19+
"issues": "https://github.com/socialiteproviders/providers/issues",
20+
"source": "https://github.com/socialiteproviders/providers",
21+
"docs": "https://socialiteproviders.com/paymenter"
22+
},
23+
"require": {
24+
"php": "^8.0",
25+
"ext-json": "*",
26+
"socialiteproviders/manager": "^4.4"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"SocialiteProviders\\Paymenter\\": ""
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)