### Install Mollie OAuth 2.0 Client via Composer Source: https://github.com/mollie/oauth2-mollie-php/blob/master/README.md Use Composer to add the package to your project dependencies. ```bash $ composer require mollie/oauth2-mollie-php ^2.0 ``` ```json { "require": { "mollie/oauth2-mollie-php": "^2.0" } } ``` -------------------------------- ### Install Mollie OAuth2 PHP Package Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Install the package using Composer to add Mollie OAuth2 support to your PHP application. ```bash composer require mollie/oauth2-mollie-php ^2.0 ``` -------------------------------- ### Define Available OAuth Scopes in PHP Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Request only the necessary scopes for your application. This example shows how to define arrays of scopes for different Mollie API functionalities. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); $authUrl = $provider->getAuthorizationUrl([ 'scope' => array_merge($paymentScopes, $customerScopes), ]); ``` -------------------------------- ### Initialize Mollie OAuth2 Provider Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Create a Mollie OAuth provider instance with your application credentials. The client ID must be prefixed with 'app_'. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); // Provider is ready for OAuth operations ``` -------------------------------- ### Configure Custom API URLs Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Override default API and web URLs for testing or regional endpoint requirements. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); // Override the default API URL (default: https://api.mollie.com) $provider->setMollieApiUrl('https://api.mollie.nl'); // Override the default web URL for authorization (default: https://my.mollie.com) $provider->setMollieWebUrl('https://www.mollie.nl'); // These URLs will now be used for all OAuth operations $authUrl = $provider->getAuthorizationUrl(); // Returns: https://www.mollie.nl/oauth2/authorize?... $tokenUrl = $provider->getBaseAccessTokenUrl([]); // Returns: https://api.mollie.nl/oauth2/tokens ``` -------------------------------- ### Integrate OAuth Token with Mollie API Client Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Initialize the Mollie API client using an OAuth access token to perform operations on behalf of a merchant. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); // ... complete OAuth flow to get $accessToken ... // Initialize Mollie API client with the access token $mollie = new MollieApiClient(); $mollie->setAccessToken($accessToken->getToken()); // Now you can access Mollie API endpoints on behalf of the merchant // List payments (requires payments.read scope) $payments = $mollie->payments->page(); foreach ($payments as $payment) { echo "Payment ID: " . $payment->id . "\n"; echo "Amount: " . $payment->amount->value . " " . $payment->amount->currency . "\n"; echo "Status: " . $payment->status . "\n"; } // Create a payment (requires payments.write scope) $payment = $mollie->payments->create([ 'amount' => [ 'currency' => 'EUR', 'value' => '10.00', ], 'description' => 'Order #12345', 'redirectUrl' => 'https://your-app.com/order/12345/thank-you', 'webhookUrl' => 'https://your-app.com/webhooks/mollie', ]); echo "Checkout URL: " . $payment->getCheckoutUrl() . "\n"; ``` -------------------------------- ### Retrieve Resource Owner Details in PHP Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Fetch details about the authenticated Mollie organization using an access token. This requires the `MollieResourceOwner` class and handles potential `IdentityProviderException`. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); try { // Get resource owner using the access token $resourceOwner = $provider->getResourceOwner($accessToken); // Access organization details via MollieResourceOwner methods echo "Organization ID: " . $resourceOwner->getId() . "\n"; echo "Email: " . $resourceOwner->getEmail() . "\n"; echo "Registration Number: " . $resourceOwner->getRegistrationNumber() . "\n"; echo "VAT Number: " . $resourceOwner->getVatNumber() . "\n"; // Get complete organization data as array $data = $resourceOwner->toArray(); // Example response data structure: // [ // 'resource' => 'organization', // 'id' => 'org_12345678', // 'name' => 'Your Company Name', // 'email' => 'info@yourcompany.com', // 'address' => [ // 'streetAndNumber' => 'Example Street 123', // 'postalCode' => '1234 AB', // 'city' => 'Amsterdam', // 'country' => 'NL' // ], // 'registrationNumber' => '12345678', // 'vatNumber' => 'NL123456789B01', // '_links' => [...] // ] print_r($data); } catch (IdentityProviderException $e) { exit('Failed to get resource owner: ' . $e->getMessage()); } ``` -------------------------------- ### Authenticate Mollie API Client with Access Token Source: https://github.com/mollie/oauth2-mollie-php/blob/master/README.md Configures the mollie-api-php client to use an obtained access token for API requests. ```php $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(); ``` -------------------------------- ### Implement Authorization Code Flow Source: https://github.com/mollie/oauth2-mollie-php/blob/master/README.md Handles the OAuth 2.0 authorization flow, including redirecting the user, validating state to prevent CSRF, and exchanging the authorization code for an access token. ```php $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()); } } ``` -------------------------------- ### Refresh Access Token in PHP Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Use a refresh token to obtain a new access token when the current one expires. Ensure the refresh token is retrieved from secure storage. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); // Retrieve stored refresh token from your database/session $existingRefreshToken = $_SESSION['refresh_token']; try { $grant = new RefreshToken(); $newAccessToken = $provider->getAccessToken($grant, [ 'refresh_token' => $existingRefreshToken ]); // Store new tokens $_SESSION['access_token'] = $newAccessToken->getToken(); $_SESSION['refresh_token'] = $newAccessToken->getRefreshToken(); echo "New Access Token: " . $newAccessToken->getToken() . "\n"; echo "Token expires at: " . date('Y-m-d H:i:s', $newAccessToken->getExpires()) . "\n"; } catch (IdentityProviderException $e) { // Refresh token may be expired or revoked exit('Failed to refresh token: ' . $e->getMessage()); } ``` -------------------------------- ### Refresh an Access Token Source: https://github.com/mollie/oauth2-mollie-php/blob/master/README.md Uses a refresh token to obtain a new access token via the RefreshToken grant. ```php $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]); ``` -------------------------------- ### Mollie OAuth2 Authorization Code Flow Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Implement the authorization code flow to redirect users to Mollie for authorization and handle the callback to exchange the code for an access token. Includes CSRF protection and token storage. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); // Step 1: Redirect to authorization URL if no code present if (!isset($_GET['code'])) { $authorizationUrl = $provider->getAuthorizationUrl([ // Force approval prompt even if user previously authorized 'approval_prompt' => 'force', // Request specific scopes for API access 'scope' => [ Mollie::SCOPE_ORGANIZATIONS_READ, Mollie::SCOPE_PAYMENTS_READ, Mollie::SCOPE_PAYMENTS_WRITE, Mollie::SCOPE_REFUNDS_READ, ], ]); // Store state for CSRF protection $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authorizationUrl); exit; } // Step 2: Validate state to prevent CSRF attacks if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state - possible CSRF attack'); } // Step 3: Exchange authorization code for access token try { $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); echo "Access Token: " . $accessToken->getToken() . "\n"; echo "Refresh Token: " . $accessToken->getRefreshToken() . "\n"; echo "Expires: " . $accessToken->getExpires() . "\n"; // Store tokens securely for later use $_SESSION['access_token'] = $accessToken->getToken(); $_SESSION['refresh_token'] = $accessToken->getRefreshToken(); } catch (IdentityProviderException $e) { exit('Failed to get access token: ' . $e->getMessage()); } ``` -------------------------------- ### Revoke Access and Refresh Tokens in PHP Source: https://github.com/mollie/oauth2-mollie-php/blob/master/README.md Use the Mollie provider to revoke tokens. Note that revoking a refresh token invalidates all access tokens derived from the same authorization grant. ```php $provider = new \Mollie\OAuth2\Client\Provider\Mollie([ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'redirectUri' => 'https://your-redirect-uri', ]); $provider->revokeAccessToken($accessToken->getToken()); ``` ```php $provider = new \Mollie\OAuth2\Client\Provider\Mollie([ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'redirectUri' => 'https://your-redirect-uri',**** ]); $provider->revokeRefreshToken($refreshToken->getToken()); ``` -------------------------------- ### Revoke Access Token in PHP Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Invalidate an access token immediately to prevent further API access. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); // Revoke the access token $response = $provider->revokeAccessToken($accessToken->getToken()); if ($response->getStatusCode() === 204) { echo "Access token successfully revoked\n"; // Clear stored access token unset($_SESSION['access_token']); } ``` -------------------------------- ### Revoke Refresh Token in PHP Source: https://context7.com/mollie/oauth2-mollie-php/llms.txt Invalidate a refresh token and all associated access tokens. This action requires the user to re-authorize the application. ```php 'app_your_client_id', 'clientSecret' => 'your_client_secret', 'redirectUri' => 'https://your-app.com/callback', ]); // WARNING: Revoking a refresh token also revokes all access tokens // based on the same authorization grant $response = $provider->revokeRefreshToken($refreshToken); if ($response->getStatusCode() === 204) { echo "Refresh token and all related access tokens revoked\n"; // Clear all stored tokens - user will need to re-authorize unset($_SESSION['access_token']); unset($_SESSION['refresh_token']); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.