From 5b556508f7ec70fe31986f6a0ae9ad4fab609c14 Mon Sep 17 00:00:00 2001 From: Mateusz Deszert-Klosowski Date: Thu, 14 Mar 2024 16:37:25 +0100 Subject: [PATCH] Fix 1.13 --- .github/workflows/build.yml | 5 +- composer.json | 11 +++- src/Api/WebhookApi.php | 51 +++++++++++------ src/Client/PayPalClient.php | 56 +++++++++++++++++-- .../Processor/BasicOnboardingProcessor.php | 24 ++++---- src/Resources/config/services/api.xml | 4 ++ src/Resources/config/services/onboarding.xml | 1 + tests/Application/composer.json | 5 +- tests/Application/config/bundles.php | 12 +++- .../config/packages/test/framework.yaml | 7 +++ 10 files changed, 138 insertions(+), 38 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 53fe8f2a..1afb251a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,11 +19,11 @@ jobs: strategy: fail-fast: false matrix: - php: ["8.0", "8.1"] + php: ["8.1", "8.2"] node: ["16.x"] mysql: ["5.7", "8.0"] symfony: ["^5.4", "^6.0"] - sylius: ["~1.12.0"] + sylius: ["~1.12.0", "1.13.x-dev"] env: APP_ENV: test DATABASE_URL: "mysql://root:root@127.0.0.1/sylius?serverVersion=${{ matrix.mysql }}" @@ -149,6 +149,7 @@ jobs: - name: Validate composer.json + if: ${{ matrix.sylius != '1.13.x-dev' }} run: composer validate --ansi --strict - diff --git a/composer.json b/composer.json index 2bd8f17f..7519b5ea 100644 --- a/composer.json +++ b/composer.json @@ -8,9 +8,13 @@ "php": "^8.0", "doctrine/doctrine-migrations-bundle": "^3.0", "phpseclib/phpseclib": "^2.0", + "psr/http-client": "^1.0", + "php-http/discovery": "^1.17", + "psr/http-client-implementation": "*", + "psr/http-factory-implementation": "*", "polishsymfonycommunity/symfony-mocker-container": "^1.0", "sylius-labs/doctrine-migrations-extra-bundle": "^0.1.4 || ^0.2", - "sylius/sylius": "~1.12.0", + "sylius/sylius": "~1.12.0 || 1.13.x-dev", "symfony/mailer": "^5.4 || ^6.0" }, "require-dev": { @@ -77,5 +81,8 @@ ] }, "prefer-stable": true, - "minimum-stability": "dev" + "minimum-stability": "dev", + "suggest": { + "php-http/guzzle6-adapter ":"Required to use this package on 32bit PHP" + } } diff --git a/src/Api/WebhookApi.php b/src/Api/WebhookApi.php index 89c0be6f..e836bb40 100644 --- a/src/Api/WebhookApi.php +++ b/src/Api/WebhookApi.php @@ -4,35 +4,54 @@ namespace Sylius\PayPalPlugin\Api; -use GuzzleHttp\ClientInterface; + +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\StreamFactoryInterface; final class WebhookApi implements WebhookApiInterface { private ClientInterface $client; + private RequestFactoryInterface $requestFactory; + + private StreamFactoryInterface $streamFactory; + private string $baseUrl; - public function __construct(ClientInterface $client, string $baseUrl) - { + public function __construct( + ClientInterface $client, + RequestFactoryInterface $requestFactory, + StreamFactoryInterface $streamFactory, + string $baseUrl + ) { $this->client = $client; + $this->requestFactory = $requestFactory; + $this->streamFactory = $streamFactory; $this->baseUrl = $baseUrl; } public function register(string $token, string $webhookUrl): array { - $response = $this->client->request('POST', $this->baseUrl . 'v1/notifications/webhooks', [ - 'headers' => [ - 'Authorization' => 'Bearer ' . $token, - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - ], - 'json' => [ - 'url' => preg_replace('/^http:/i', 'https:', $webhookUrl), - 'event_types' => [ - ['name' => 'PAYMENT.CAPTURE.REFUNDED'], - ], - ], - ]); + $request = $this->requestFactory->createRequest('POST', $this->baseUrl . 'v1/notifications/webhooks') + ->withHeader('Authorization', 'Bearer ' . $token) + ->withHeader('Content-Type', 'application/json') + ->withHeader('Accept', 'application/json'); + + $request = $request->withBody( + $this->streamFactory->createStream( + json_encode( + [ + 'url' => preg_replace('/^http:/i', 'https:', $webhookUrl), + 'event_types' => [ + ['name' => 'PAYMENT.CAPTURE.REFUNDED'], + ] + ] + ) + ) + ); + + $response = $this->client->sendRequest($request); return (array) json_decode($response->getBody()->getContents(), true); } diff --git a/src/Client/PayPalClient.php b/src/Client/PayPalClient.php index a4935bc3..61a06fe7 100644 --- a/src/Client/PayPalClient.php +++ b/src/Client/PayPalClient.php @@ -13,11 +13,14 @@ namespace Sylius\PayPalPlugin\Client; -use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamFactoryInterface; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpClient\Psr18Client; use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\PayPalPlugin\Exception\PayPalApiTimeoutException; @@ -27,7 +30,11 @@ final class PayPalClient implements PayPalClientInterface { - private ClientInterface $client; + private ClientInterface $client; + + private RequestFactoryInterface $requestFactory; + + private StreamFactoryInterface $streamFactory; private LoggerInterface $logger; @@ -44,7 +51,9 @@ final class PayPalClient implements PayPalClientInterface private bool $loggingLevelIncreased; public function __construct( - ClientInterface $client, + ClientInterface $client, + RequestFactoryInterface $requestFactory, + StreamFactoryInterface $streamFactory, LoggerInterface $logger, UuidProviderInterface $uuidProvider, PayPalConfigurationProviderInterface $payPalConfigurationProvider, @@ -54,6 +63,8 @@ public function __construct( bool $loggingLevelIncreased = false ) { $this->client = $client; + $this->requestFactory = $requestFactory; + $this->streamFactory = $streamFactory; $this->logger = $logger; $this->uuidProvider = $uuidProvider; $this->payPalConfigurationProvider = $payPalConfigurationProvider; @@ -145,7 +156,44 @@ private function request(string $method, string $url, string $token, array $data private function doRequest(string $method, string $fullUrl, array $options): ResponseInterface { try { - $response = $this->client->request($method, $fullUrl, $options); + $request = $this->requestFactory->createRequest($method, $fullUrl); + + if (isset($options['auth'])) { + $request = $request->withHeader( + 'Authorization', + sprintf( + "Basic %s", + base64_encode(sprintf("%s:%s", $options['auth'][0], $options['auth'][1])) + ) + ); + } + + if (isset($options['form_params'])) { + $request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded'); + $request = $request->withBody( + $this->streamFactory->createStream(http_build_query( + $options['form_params'], + '', + '&', + PHP_QUERY_RFC1738 + )) + ); + + } + + if (isset($options['json'])) { + $request = $request->withBody( + $this->streamFactory->createStream(json_encode($options['json'])) + ); + } + + if (isset($options['headers'])) { + foreach ($options['headers'] as $header => $headerValue) { + $request = $request->withHeader($header, $headerValue); + } + } + + $response = $this->client->sendRequest($request); } catch (ConnectException $exception) { --$this->requestTrialsLimit; if ($this->requestTrialsLimit === 0) { diff --git a/src/Onboarding/Processor/BasicOnboardingProcessor.php b/src/Onboarding/Processor/BasicOnboardingProcessor.php index 89e69335..8a1a583c 100644 --- a/src/Onboarding/Processor/BasicOnboardingProcessor.php +++ b/src/Onboarding/Processor/BasicOnboardingProcessor.php @@ -4,7 +4,8 @@ namespace Sylius\PayPalPlugin\Onboarding\Processor; -use GuzzleHttp\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Client\ClientInterface; use Sylius\Component\Core\Model\PaymentMethodInterface; use Sylius\PayPalPlugin\Exception\PayPalPluginException; use Sylius\PayPalPlugin\Exception\PayPalWebhookAlreadyRegisteredException; @@ -17,16 +18,20 @@ final class BasicOnboardingProcessor implements OnboardingProcessorInterface { private ClientInterface $httpClient; + private RequestFactoryInterface $requestFactory; + private SellerWebhookRegistrarInterface $sellerWebhookRegistrar; private string $url; public function __construct( ClientInterface $httpClient, + RequestFactoryInterface $requestFactory, SellerWebhookRegistrarInterface $sellerWebhookRegistrar, string $url ) { $this->httpClient = $httpClient; + $this->requestFactory = $requestFactory; $this->sellerWebhookRegistrar = $sellerWebhookRegistrar; $this->url = $url; } @@ -43,16 +48,15 @@ public function process( Assert::notNull($gatewayConfig); $onboardingId = (string) $request->query->get('onboarding_id'); - $checkPartnerReferralsResponse = $this->httpClient->request( + $checkPartnerReferralsRequest = $this->requestFactory->createRequest( 'GET', - sprintf('%s/partner-referrals/check/%s', $this->url, $onboardingId), - [ - 'headers' => [ - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - ], - ] - ); + sprintf('%s/partner-referrals/check/%s', $this->url, $onboardingId) + ) + ->withHeader('Content-Type', 'application/json') + ->withHeader('Accept', 'application/json') + ; + + $checkPartnerReferralsResponse = $this->httpClient->sendRequest($checkPartnerReferralsRequest); $response = (array) json_decode($checkPartnerReferralsResponse->getBody()->getContents(), true); diff --git a/src/Resources/config/services/api.xml b/src/Resources/config/services/api.xml index 88b94775..70a242ac 100644 --- a/src/Resources/config/services/api.xml +++ b/src/Resources/config/services/api.xml @@ -11,6 +11,8 @@ class="Sylius\PayPalPlugin\Client\PayPalClient" > + + @@ -87,6 +89,8 @@ class="Sylius\PayPalPlugin\Api\WebhookApi" > + + %sylius.pay_pal.api_base_url% diff --git a/src/Resources/config/services/onboarding.xml b/src/Resources/config/services/onboarding.xml index 917f0971..8324519c 100644 --- a/src/Resources/config/services/onboarding.xml +++ b/src/Resources/config/services/onboarding.xml @@ -16,6 +16,7 @@ class="Sylius\PayPalPlugin\Onboarding\Processor\BasicOnboardingProcessor" > + %sylius.pay_pal.facilitator_url% diff --git a/tests/Application/composer.json b/tests/Application/composer.json index af8a51b2..c5ee6856 100644 --- a/tests/Application/composer.json +++ b/tests/Application/composer.json @@ -1,5 +1,8 @@ { "name": "sylius/paypal-plugin", "description": "PayPal plugin for Sylius", - "license": "MIT" + "license": "MIT", + "require": { + "nyholm/psr7": "^1.8" + } } diff --git a/tests/Application/config/bundles.php b/tests/Application/config/bundles.php index d260b314..4f4da692 100644 --- a/tests/Application/config/bundles.php +++ b/tests/Application/config/bundles.php @@ -1,6 +1,7 @@ ['all' => true], @@ -62,10 +63,15 @@ Sylius\Calendar\SyliusCalendarBundle::class => ['all' => true], ]; -if (Kernel::MINOR_VERSION < 12) { - $bundles[Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class] = ['all' => true]; -} else { +if (defined(SyliusCoreBundle::class.'::VERSION_ID') && SyliusCoreBundle::VERSION_ID >= '11200') { $bundles[League\FlysystemBundle\FlysystemBundle::class] = ['all' => true]; +} else { + $bundles[Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class] = ['all' => true]; +} + + +if ( defined(SyliusCoreBundle::class.'::VERSION_ID') && SyliusCoreBundle::VERSION_ID >= '11300') { + $bundles[Sylius\Abstraction\StateMachine\SyliusStateMachineAbstractionBundle::class] = ['all' => true]; } return $bundles; diff --git a/tests/Application/config/packages/test/framework.yaml b/tests/Application/config/packages/test/framework.yaml index fc1d3c13..1890c8d0 100644 --- a/tests/Application/config/packages/test/framework.yaml +++ b/tests/Application/config/packages/test/framework.yaml @@ -2,3 +2,10 @@ framework: test: ~ session: storage_factory_id: session.storage.factory.mock_file + + mailer: + dsn: 'null://null' + cache: + pools: + test.mailer_pool: + adapter: cache.adapter.filesystem \ No newline at end of file