Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Mar 20, 2024
1 parent 550c6df commit 3953434
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 142 deletions.
2 changes: 0 additions & 2 deletions .phpstan.ignoreErrors.neon

This file was deleted.

23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

# Mollie Connect in PHP #

This package provides Mollie OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client). Visit our [API documentation](https://www.mollie.com/en/docs/oauth/overview) for more information about the Mollie implementation of OAuth2.
This package provides Mollie OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client). Visit our [API documentation](https://docs.mollie.com/connect/getting-started) for more information about the Mollie implementation of OAuth2.

Use Mollie Connect (OAuth) to easily connect Mollie Merchant accounts to your application. [Mollie Connect](https://www.mollie.com/en/connect) also makes it possible to charge additional fees to your costumers with [Application Fee](https://www.mollie.com/en/docs/reference/payments/create#pfp-params).
Use Mollie Connect (OAuth) to easily connect Mollie Merchant accounts to your application. [Mollie Connect](https://docs.mollie.com/connect/overview) also makes it possible to charge additional fees to your costumers with [Application Fee](https://docs.mollie.com/connect/application-fees).

## Installation ##

Expand All @@ -31,7 +31,7 @@ Usage is the same as The League's OAuth client, using `\Mollie\OAuth2\Client\Pro
```php
$provider = new \Mollie\OAuth2\Client\Provider\Mollie([
'clientId' => 'YOUR_CLIENT_ID',
'clientSecret' => 'YOUR_CLIENT_SECRET',
'clientSecret' => 'YOUR_CLIENT_SECRET',****
'redirectUri' => 'https://your-redirect-uri',
]);

Expand All @@ -43,13 +43,13 @@ if (!isset($_GET['code']))
// (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl([
// Optional, only use this if you want to ask for scopes the user previously denied.
'approval_prompt' => 'force',
'approval_prompt' => 'force',

// Optional, a list of scopes. Defaults to only 'organizations.read'.
'scope' => [
\Mollie\OAuth2\Client\Provider\Mollie::SCOPE_ORGANIZATIONS_READ,
\Mollie\OAuth2\Client\Provider\Mollie::SCOPE_ORGANIZATIONS_READ,
\Mollie\OAuth2\Client\Provider\Mollie::SCOPE_PAYMENTS_READ,
],
],
]);

// Get the state generated for you and store it to the session.
Expand All @@ -75,10 +75,10 @@ else
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);

// Using the access token, we may look up details about the resource owner.
$resourceOwner = $provider->getResourceOwner($accessToken);

print_r($resourceOwner->toArray());
}
catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e)
Expand Down Expand Up @@ -115,6 +115,9 @@ $mollie->setAccessToken($token->getToken());
$payments = $mollie->payments->page();
```

> [!NOTE]
> In order to access the mollie api via `\Molie\Api\MollieApiClient`, the [mollie/mollie-api-php](github.com/mollie/mollie-api-php) library is required!
### Revoking a token

Both AccessTokens and RefreshTokens are revokable. Here's how to revoke an AccessToken:
Expand All @@ -137,7 +140,7 @@ Similarly, here's how to revoke a RefreshToken:
$provider = new \Mollie\OAuth2\Client\Provider\Mollie([
'clientId' => 'YOUR_CLIENT_ID',
'clientSecret' => 'YOUR_CLIENT_SECRET',
'redirectUri' => 'https://your-redirect-uri',
'redirectUri' => 'https://your-redirect-uri',****
]);

$provider->revokeRefreshToken($refreshToken->getToken());
Expand Down
88 changes: 54 additions & 34 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
{
"name": "mollie/oauth2-mollie-php",
"description": "Mollie Provider for OAuth 2.0 Client",
"homepage": "https://github.com/mollie/oauth2-mollie-php",
"license": "BSD-2-Clause",
"authors": [
{
"name": "Mollie B.V.",
"email": "[email protected]"
}
],
"keywords": [
"oauth", "oauth2", "client", "authorization", "authorisation",
"mollie", "payment", "service", "ideal", "creditcard", "mistercash", "bancontact", "sofort", "sofortbanking",
"sepa", "paypal", "paysafecard", "banktransfer", "direct debit", "belfius", "belfius direct net",
"refunds", "api", "payments", "gateway"
],
"require": {
"php": ">=7.4.0",
"league/oauth2-client": "^1.0 || ^2.0",
"mollie/mollie-api-php": "^1.19 || ^2.39"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
"mockery/mockery": "^0.9|^1.0"
},
"autoload": {
"psr-4": {
"Mollie\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mollie\\OAuth2\\Client\\Test\\": "tests/src/"
}
"name": "mollie/oauth2-mollie-php",
"description": "Mollie Provider for OAuth 2.0 Client",
"homepage": "https://github.com/mollie/oauth2-mollie-php",
"license": "BSD-2-Clause",
"authors": [
{
"name": "Mollie B.V.",
"email": "[email protected]"
}
],
"keywords": [
"oauth",
"oauth2",
"client",
"authorization",
"authorisation",
"mollie",
"payment",
"service",
"ideal",
"creditcard",
"mistercash",
"bancontact",
"sofort",
"sofortbanking",
"sepa",
"paypal",
"paysafecard",
"banktransfer",
"direct debit",
"belfius",
"belfius direct net",
"refunds",
"api",
"payments",
"gateway"
],
"require": {
"php": "^7.4|^8.0",
"league/oauth2-client": "^2.7"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"mockery/mockery": "^1.5"
},
"autoload": {
"psr-4": {
"Mollie\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mollie\\OAuth2\\Client\\Test\\": "tests/src/"
}
}
}
12 changes: 6 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
87 changes: 44 additions & 43 deletions src/Provider/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;

class Mollie extends AbstractProvider
{
use BearerAuthorizationTrait;

/**
* Version of this client.
*/
const CLIENT_VERSION = "2.7.0";
const CLIENT_VERSION = "2.8.0";

/**
* The base url to the Mollie API.
Expand Down Expand Up @@ -113,7 +114,7 @@ public function __construct(array $options = [], array $collaborators = [])
* @param string $url
* @return Mollie
*/
public function setMollieApiUrl($url)
public function setMollieApiUrl($url): self
{
$this->mollieApiUrl = $url;

Expand All @@ -126,7 +127,7 @@ public function setMollieApiUrl($url)
* @param string $url
* @return Mollie
*/
public function setMollieWebUrl($url)
public function setMollieWebUrl($url): self
{
$this->mollieWebUrl = $url;

Expand All @@ -140,7 +141,7 @@ public function setMollieWebUrl($url)
*
* @return string
*/
public function getBaseAuthorizationUrl()
public function getBaseAuthorizationUrl(): string
{
return $this->mollieWebUrl . '/oauth2/authorize';
}
Expand All @@ -153,7 +154,7 @@ public function getBaseAuthorizationUrl()
* @param array $params
* @return string
*/
public function getBaseAccessTokenUrl(array $params)
public function getBaseAccessTokenUrl(array $params): string
{
return $this->mollieApiUrl . '/oauth2/tokens';
}
Expand All @@ -164,7 +165,7 @@ public function getBaseAccessTokenUrl(array $params)
* @param AccessToken $token
* @return string
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
public function getResourceOwnerDetailsUrl(AccessToken $token): string
{
return static::MOLLIE_API_URL . '/v2/organizations/me';
}
Expand All @@ -177,7 +178,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token)
* @return \Psr\Http\Message\ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function revokeAccessToken($accessToken)
public function revokeAccessToken($accessToken): ResponseInterface
{
return $this->revokeToken(self::TOKEN_TYPE_ACCESS, $accessToken);
}
Expand All @@ -190,7 +191,7 @@ public function revokeAccessToken($accessToken)
* @return \Psr\Http\Message\ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function revokeRefreshToken($refreshToken)
public function revokeRefreshToken($refreshToken): ResponseInterface
{
return $this->revokeToken(self::TOKEN_TYPE_REFRESH, $refreshToken);
}
Expand All @@ -204,7 +205,7 @@ public function revokeRefreshToken($refreshToken)
* @return \Psr\Http\Message\ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function revokeToken($type, $token)
public function revokeToken($type, $token): ResponseInterface
{
return $this->getRevokeTokenResponse([
'token_type_hint' => $type,
Expand All @@ -220,7 +221,7 @@ public function revokeToken($type, $token)
* @return \Psr\Http\Message\ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getRevokeTokenResponse(array $params)
protected function getRevokeTokenResponse(array $params): ResponseInterface
{
$params['client_id'] = $this->clientId;
$params['client_secret'] = $this->clientSecret;
Expand All @@ -244,7 +245,7 @@ protected function getRevokeTokenResponse(array $params)
*
* @return string[]
*/
protected function getDefaultScopes()
protected function getDefaultScopes(): array
{
return [
self::SCOPE_ORGANIZATIONS_READ,
Expand All @@ -257,7 +258,7 @@ protected function getDefaultScopes()
*
* @return string Scope separator, defaults to ','
*/
protected function getScopeSeparator()
protected function getScopeSeparator(): string
{
return ' ';
}
Expand All @@ -270,25 +271,27 @@ protected function getScopeSeparator()
* @param array|string $data Parsed response data
* @return void
*/
protected function checkResponse(ResponseInterface $response, $data)
protected function checkResponse(ResponseInterface $response, $data): void
{
if ($response->getStatusCode() >= 400) {
if (isset($data['error'])) {
if (isset($data['error']['type']) && isset($data['error']['message'])) {
$message = sprintf('[%s] %s', $data['error']['type'], $data['error']['message']);
} else {
$message = $data['error'];
}

if (isset($data['error']['field'])) {
$message .= sprintf(' (field: %s)', $data['error']['field']);
}
} else {
$message = $response->getReasonPhrase();
}

throw new IdentityProviderException($message, $response->getStatusCode(), $response);
if ($response->getStatusCode() < 400) {
return;
}

if (!isset($data['error'])) {
throw new IdentityProviderException($response->getReasonPhrase(), $response->getStatusCode(), $response);
}

if (isset($data['error']['type']) && isset($data['error']['message'])) {
$message = sprintf('[%s] %s', $data['error']['type'], $data['error']['message']);
} else {
$message = $data['error'];
}

if (isset($data['error']['field'])) {
$message .= sprintf(' (field: %s)', $data['error']['field']);
}

throw new IdentityProviderException($message, $response->getStatusCode(), $response);
}

/**
Expand All @@ -297,29 +300,27 @@ protected function checkResponse(ResponseInterface $response, $data)
*
* @param array $response
* @param AccessToken $token
* @return ResourceOwnerInterface
* @return MollieResourceOwner
*/
protected function createResourceOwner(array $response, AccessToken $token)
protected function createResourceOwner(array $response, AccessToken $token): MollieResourceOwner
{
return new MollieResourceOwner($response);
}

/**
* Returns required authorization headers plus Mollie user agent strings.
* Returns the default headers used by this provider.
*
* Typically this is used to set 'Accept' or 'Content-Type' headers.
*
* @param AccessTokenInterface|string|null $token Either a string or an access token instance
* @return array
*/
protected function getAuthorizationHeaders($token = null)
protected function getDefaultHeaders()
{
$userAgent = implode(' ', [
"MollieOAuth2PHP/" . self::CLIENT_VERSION,
"PHP/" . phpversion(),
]);

return [
'Authorization' => 'Bearer ' . $token,
'User-Agent' => $userAgent,
'User-Agent' => implode(' ', [
"MollieOAuth2PHP/" . self::CLIENT_VERSION,
"PHP/" . phpversion(),
])
];
}
}
Loading

0 comments on commit 3953434

Please sign in to comment.