Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Mar 7, 2024
1 parent 4a3a46b commit 24c14e4
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 1,051 deletions.
21 changes: 21 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
![Mollie](https://www.mollie.nl/files/Mollie-Logo-Style-Small.png)

# Migrating from Laravel-Mollie v2.x to v3

### Step 1: Update composer dependencies

Update `composer.json` to match this:

```json
"require": {
"mollie/laravel-mollie": "^3.0"
}
```

Then run `composer update mollie/laravel-mollie`.

### Step 2:


## Stuck?
Feel free to open an [issue](https://github.com/mollie/laravel-mollie/issues).
57 changes: 53 additions & 4 deletions src/Facades/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,62 @@
namespace Mollie\Laravel\Facades;

use Illuminate\Support\Facades\Facade;
use Mollie\Laravel\MollieManager;
use Mollie\Laravel\Wrappers\MollieApiWrapper;
use Mollie\Api\MollieApiClient;

/**
* (Facade) Class Mollie.
*
* @method static MollieApiWrapper api()
* @method \Mollie\Api\Endpoints\BalanceEndpoint balanceReports()
* @method \Mollie\Api\Endpoints\BalanceEndpoint balances()
* @method \Mollie\Api\Endpoints\BalanceTransactionEndpoint balanceTransactions()
* @method \Mollie\Api\Endpoints\ChargebackEndpoint chargebacks()
* @method \Mollie\Api\Endpoints\ClientLinkEndpoint clientLinks()
* @method \Mollie\Api\Endpoints\ClientEndpoint clients()
* @method \Mollie\Api\Endpoints\CustomerPaymentEndpoint customerPayments()
* @method \Mollie\Api\Endpoints\CustomerEndpoint customers()
* @method \Mollie\Api\Endpoints\InvoiceEndpoint invoices()
* @method \Mollie\Api\Endpoints\OnboardingEndpoint onboarding()
* @method \Mollie\Api\Endpoints\OrderLineEndpoint orderLines()
* @method \Mollie\Api\Endpoints\OrderPaymentEndpoint orderPayments()
* @method \Mollie\Api\Endpoints\OrderRefundEndpoint orderRefunds()
* @method \Mollie\Api\Endpoints\OrderEndpoint orders()
* @method \Mollie\Api\Endpoints\OrganizationPartnerEndpoint organizationPartners()
* @method \Mollie\Api\Endpoints\OrganizationEndpoint organizations()
* @method \Mollie\Api\Endpoints\PaymentCaptureEndpoint paymentCaptures()
* @method \Mollie\Api\Endpoints\PaymentChargebackEndpoint paymentChargebacks()
* @method \Mollie\Api\Endpoints\PaymentLinkEndpoint paymentLinks()
* @method \Mollie\Api\Endpoints\PaymentRefundEndpoint paymentRefunds()
* @method \Mollie\Api\Endpoints\PaymentRouteEndpoint paymentRoutes()
* @method \Mollie\Api\Endpoints\PaymentEndpoint payments()
* @method \Mollie\Api\Endpoints\PermissionEndpoint permissions()
* @method \Mollie\Api\Endpoints\ProfileMethodEndpoint profileMethods()
* @method \Mollie\Api\Endpoints\ProfileEndpoint profiles()
* @method \Mollie\Api\Endpoints\MandateEndpoint mandates()
* @method \Mollie\Api\Endpoints\MethodEndpoint methods()
* @method \Mollie\Api\Endpoints\RefundEndpoint refunds()
* @method \Mollie\Api\Endpoints\SettlementCaptureEndpoint settlementCaptures()
* @method \Mollie\Api\Endpoints\SettlementChargebackEndpoint settlementChargebacks()
* @method \Mollie\Api\Endpoints\SettlementPaymentEndpoint settlementPayments()
* @method \Mollie\Api\Endpoints\SettlementRefundEndpoint settlementRefunds()
* @method \Mollie\Api\Endpoints\SettlementEndpoint settlements()
* @method \Mollie\Api\Endpoints\ShipmentEndpoint shipments()
* @method \Mollie\Api\Endpoints\SubscriptionEndpoint subscriptions()
* @method \Mollie\Api\Endpoints\TerminalEndpoint terminals()
* @method \Mollie\Api\Endpoints\WalletEndpoint wallets()
* @method MollieApiClient setApiEndpoint(string $url)
* @method string getApiEndpoint()
* @method string getVersionStrings()
* @method MollieApiClient setApiKey(string $apiKey)
* @method MollieApiClient setAccessToken(string $accessToken)
* @method ?bool usesOAuth()
* @method MollieApiClient addVersionString(string $versionString)
* @method void enableDebugging()
* @method void disableDebugging()
* @method MollieApiClient setIdempotencyKey(string $key)
* @method string getIdempotencyKey()
* @method MollieApiClient resetIdempotencyKey()
* @method MollieApiClient setIdempotencyKeyGenerator(\Mollie\Api\Idempotency\IdempotencyKeyGeneratorContract $generator)
* @method MollieApiClient clearIdempotencyKeyGenerator()
*/
class Mollie extends Facade
{
Expand All @@ -52,6 +101,6 @@ class Mollie extends Facade
*/
protected static function getFacadeAccessor()
{
return MollieManager::class;
return MollieApiClient::class;
}
}
69 changes: 5 additions & 64 deletions src/MollieConnectProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2016, Mollie B.V.
* All rights reserved.
Expand Down Expand Up @@ -33,7 +34,6 @@

namespace Mollie\Laravel;

use Illuminate\Support\Arr;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
Expand Down Expand Up @@ -79,7 +79,7 @@ class MollieConnectProvider extends AbstractProvider implements ProviderInterfac
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(static::MOLLIE_WEB_URL.'/oauth2/authorize', $state);
return $this->buildAuthUrlFromBase(static::MOLLIE_WEB_URL . '/oauth2/authorize', $state);
}

/**
Expand All @@ -89,66 +89,7 @@ protected function getAuthUrl($state)
*/
protected function getTokenUrl()
{
return static::MOLLIE_API_URL.'/oauth2/tokens';
}

/**
* Get the access token for the given code.
*
* @param string $code
* @return string
*/
public function getAccessToken($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Authorization' => 'Basic '.base64_encode($this->clientId.':'.$this->clientSecret)],
'form_params' => $this->getTokenFields($code),
]);

return $this->parseAccessToken($response->getBody());
}

/**
* Get the access token with a refresh token.
*
* @param string $refresh_token
* @return array
*/
public function getRefreshTokenResponse($refresh_token)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Accept' => 'application/json'],
'form_params' => $this->getRefreshTokenFields($refresh_token),
]);

return json_decode($response->getBody(), true);
}

/**
* Get the refresh tokenfields with a refresh token.
*
* @param string $refresh_token
* @return array
*/
protected function getRefreshTokenFields($refresh_token)
{
return [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
];
}

/**
* Get the POST fields for the token request.
*
* @param string $code
* @return array
*/
public function getTokenFields($code)
{
return Arr::add(parent::getTokenFields($code), 'grant_type', 'authorization_code');
return static::MOLLIE_API_URL . '/oauth2/tokens';
}

/**
Expand All @@ -159,8 +100,8 @@ public function getTokenFields($code)
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(static::MOLLIE_API_URL.'/v2/organizations/me', [
'headers' => ['Authorization' => 'Bearer '.$token],
$response = $this->getHttpClient()->get(static::MOLLIE_API_URL . '/v2/organizations/me', [
'headers' => ['Authorization' => 'Bearer ' . $token],
]);

return json_decode($response->getBody(), true);
Expand Down
20 changes: 6 additions & 14 deletions src/MollieLaravelHttpClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

class MollieLaravelHttpClientAdapter implements MollieHttpAdapterInterface
{
private const HTTP_NO_CONTENT = 204;

public function send($httpMethod, $url, $headers, $httpBody): ?object
{
$contentType = $headers['Content-Type'] ?? 'application/json';
Expand All @@ -20,30 +18,24 @@ public function send($httpMethod, $url, $headers, $httpBody): ?object
->withHeaders($headers)
->send($httpMethod, $url);

return $this->parseResponseBody($response);
return match (true) {
$response->noContent() => null,
$response->failed() => throw ApiException::createFromResponse($response->toPsrResponse(), null),
empty($response->body()) => throw new ApiException("Mollie response body is empty."),
default => $this->parseResponseBody($response),
};
}

private function parseResponseBody(Response $response): ?object
{
$body = $response->body();
if (empty($body)) {
if ($response->status() === self::HTTP_NO_CONTENT) {
return null;
}

throw new ApiException('No response body found.');
}

$object = @json_decode($body);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new ApiException("Unable to decode Mollie response: '{$body}'.");
}

if ($response->status() >= 400) {
throw ApiException::createFromResponse($response->toPsrResponse(), null);
}

return $object;
}

Expand Down
67 changes: 0 additions & 67 deletions src/MollieManager.php

This file was deleted.

13 changes: 8 additions & 5 deletions src/MollieServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;
use Mollie\Api\MollieApiClient;
use Mollie\Laravel\Wrappers\MollieApiWrapper;

/**
* Class MollieServiceProvider.
Expand Down Expand Up @@ -98,13 +97,17 @@ protected function extendSocialite()
*/
public function register()
{
$this->app->singleton(MollieManager::class);
$this->app->singleton(MollieApiWrapper::class);
$this->app->singleton(
MollieApiClient::class,
function () {
return (new MollieApiClient(new MollieLaravelHttpClientAdapter))
function (Container $app) {
$client = (new MollieApiClient(new MollieLaravelHttpClientAdapter))
->addVersionString('MollieLaravel/' . self::PACKAGE_VERSION);

if (!empty($apiKey = $app['config']['mollie.key'])) {
$client->setApiKey($apiKey);
}

return $client;
}
);
}
Expand Down
Loading

0 comments on commit 24c14e4

Please sign in to comment.