mollie/oauth2-mollie-php

Mollie OAuth 2.0 客户端提供者

v2.8.1 2024-08-15 14:31 UTC

README

Mollie-Logo-Style-Small

PHP中的Mollie Connect

此软件包为PHP League的OAuth 2.0 客户端提供Mollie OAuth 2.0 支持。访问我们的API 文档,了解有关Mollie OAuth2实现的更多信息。

使用Mollie Connect(OAuth)轻松连接Mollie 商户账户到您的应用程序。通过Mollie Connect,您还可以使用应用程序费用向客户收取额外费用。

安装

安装Mollie API客户端最简单的方法是使用Composer进行需求。

$ composer require mollie/oauth2-mollie-php ^2.0

    {
        "require": {
            "mollie/oauth2-mollie-php": "^2.0"
        }
    }

您也可以使用git checkout或下载所有文件,然后手动包含OAuth 2.0提供者。

用法

用法与The League的OAuth客户端相同,使用\Mollie\OAuth2\Client\Provider\Mollie作为提供者。

授权码流

$provider = new \Mollie\OAuth2\Client\Provider\Mollie([
    'clientId'     => 'YOUR_CLIENT_ID',
    'clientSecret' => 'YOUR_CLIENT_SECRET',
    'redirectUri'  => 'https://your-redirect-uri',
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code']))
{
    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl([
        // Optional, only use this if you want to ask for scopes the user previously denied.
        '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_PAYMENTS_READ,
	],
    ]);

    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect the user to the authorization URL.
    header('Location: ' . $authorizationUrl);
    exit;
}

// Check given state against previously stored one to mitigate CSRF attack
elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state']))
{
    unset($_SESSION['oauth2state']);
    exit('Invalid state');
}

else
{
    try
    {
        // Try to get an access token using the authorization code grant.
        $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)
    {
        // Failed to get the access token or user details.
        exit($e->getMessage());
    }
}

刷新令牌

$provider = new \Mollie\OAuth2\Client\Provider\Mollie([
    'clientId'     => 'YOUR_CLIENT_ID',
    'clientSecret' => 'YOUR_CLIENT_SECRET',
    'redirectUri'  => 'https://your-redirect-uri',
]);

$grant = new \League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);

使用AccessToken进行身份验证(mollie-api-php 示例)

刷新AccessToken后,以下是使用mollie-api-php软件包使用它的方法。请注意,使用getToken()方法获取访问令牌字符串。

$mollie = new \Mollie\Api\MollieApiClient;
$mollie->setAccessToken($token->getToken());

// With the correct scopes, you can now interact with Mollie's API on behalf of the Merchant
$payments = $mollie->payments->page();

注意

要使用\Molie\Api\MollieApiClient通过mollie api进行访问,需要mollie/mollie-api-php库!

撤销令牌

AccessTokens和RefreshTokens都可以撤销。以下是撤销AccessToken的示例

$provider = new \Mollie\OAuth2\Client\Provider\Mollie([
    'clientId'     => 'YOUR_CLIENT_ID',
    'clientSecret' => 'YOUR_CLIENT_SECRET',
    'redirectUri'  => 'https://your-redirect-uri',
]);

$provider->revokeAccessToken($accessToken->getToken());

同样,以下是撤销RefreshToken的示例

注意:当您撤销刷新令牌时,基于相同授权授予的所有访问令牌也将被撤销。

$provider = new \Mollie\OAuth2\Client\Provider\Mollie([
    'clientId'     => 'YOUR_CLIENT_ID',
    'clientSecret' => 'YOUR_CLIENT_SECRET',
    'redirectUri'  => 'https://your-redirect-uri',****
]);

$provider->revokeRefreshToken($refreshToken->getToken());

API 文档

如果您想了解更多关于我们API的信息,请访问Mollie 开发者门户

想要帮助我们使我们的API客户端变得更好吗?

想要帮助我们使我们的API客户端变得更好?当然,我们接受pull requests。但您愿意如何为一个技术导向的组织做出贡献?Mollie正在招聘开发人员和系统工程师。请查看我们的空缺职位联系我们

许可证

BSD (伯克利软件发行版) 许可证。版权所有 (c) 2015-2018, Mollie B.V.

支持

联系方式:www.mollie.com[email protected] — +31 20-612 88 55