Skip to content

Commit

Permalink
Merge pull request #32 from mollie/support-laravel-11
Browse files Browse the repository at this point in the history
Upgrade
  • Loading branch information
Naoray authored Mar 27, 2024
2 parents 550c6df + 1a6b653 commit 8ce2ca8
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 141 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'
- cron: "0 0 * * *"
jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [7.4, 8.0, 8.1]
php: [7.4, 8.0, 8.1, 8.2, 8.3]
name: PHP - ${{ matrix.php }}
steps:
- name: Checkout code
Expand All @@ -30,4 +30,4 @@ jobs:
composer require "phpstan/phpstan:0.12.99"
vendor/bin/phpstan analyse --no-progress
- name: Execute tests
run: vendor/bin/phpunit --verbose
run: vendor/bin/phpunit
2 changes: 0 additions & 2 deletions .phpstan.ignoreErrors.neon

This file was deleted.

21 changes: 12 additions & 9 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 Down Expand Up @@ -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
91 changes: 57 additions & 34 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
{
"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": "^9.6|^10.0",
"mockery/mockery": "^1.5"
},
"suggest": {
"mollie/mollie-api-php": "To use the Mollie API client to interact with the Mollie API."
},
"autoload": {
"psr-4": {
"Mollie\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mollie\\OAuth2\\Client\\Test\\": "tests/src/"
}
}
}
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ parameters:
- %currentWorkingDirectory%/tests
excludes_analyse:
- %currentWorkingDirectory%/vendor
includes:
- .phpstan.ignoreErrors.neon
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 8ce2ca8

Please sign in to comment.