diff --git a/spec/Enabler/PayPalPaymentMethodEnablerSpec.php b/spec/Enabler/PayPalPaymentMethodEnablerSpec.php index 0ffd1e78..258d2b26 100644 --- a/spec/Enabler/PayPalPaymentMethodEnablerSpec.php +++ b/spec/Enabler/PayPalPaymentMethodEnablerSpec.php @@ -35,7 +35,7 @@ function let( SellerWebhookRegistrarInterface $sellerWebhookRegistrar ): void { $this->beConstructedWith( - $client, $requestFactory, 'http://base-url.com', $paymentMethodManager, $sellerWebhookRegistrar + $client, 'http://base-url.com', $paymentMethodManager, $sellerWebhookRegistrar, $requestFactory ); } diff --git a/src/Api/GenericApi.php b/src/Api/GenericApi.php index 23b4e642..ac0dcce8 100644 --- a/src/Api/GenericApi.php +++ b/src/Api/GenericApi.php @@ -4,21 +4,40 @@ namespace Sylius\PayPalPlugin\Api; - +use GuzzleHttp\ClientInterface as GuzzleClientInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; final class GenericApi implements GenericApiInterface { - public function __construct( - private readonly ClientInterface $client, - private readonly RequestFactoryInterface $requestFactory - ){ + private readonly GuzzleClientInterface|ClientInterface $client, + private readonly ?RequestFactoryInterface $requestFactory = null, + ) { + if ($this->client instanceof GuzzleClientInterface) { + trigger_deprecation( + 'sylius/paypal-plugin', + '1.6', + 'Passing GuzzleHttp\ClientInterface as a first argument in the constructor is deprecated and will be removed. Use Psr\Http\Client\ClientInterface instead.', + ); + } + + if (null === $this->requestFactory) { + trigger_deprecation( + 'sylius/paypal-plugin', + '1.6', + 'Not passing $requestFactory to %s constructor is deprecated and will be removed', + self::class, + ); + } } public function get(string $token, string $url): array { + if ($this->client instanceof GuzzleClientInterface && null === $this->requestFactory) { + return $this->legacyGet($token, $url); + } + $request = $this->requestFactory->createRequest('GET', $url) ->withHeader('Authorization', 'Bearer ' . $token) ->withHeader('Content-Type', 'application/json') @@ -26,4 +45,17 @@ public function get(string $token, string $url): array return (array) json_decode($this->client->sendRequest($request)->getBody()->getContents(), true); } + + private function legacyGet(string $token, string $url): array + { + $response = $this->client->request('GET', $url, [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + ], + ]); + + return (array) json_decode($response->getBody()->getContents(), true); + } } diff --git a/src/Enabler/PayPalPaymentMethodEnabler.php b/src/Enabler/PayPalPaymentMethodEnabler.php index 976131c0..0910d05f 100644 --- a/src/Enabler/PayPalPaymentMethodEnabler.php +++ b/src/Enabler/PayPalPaymentMethodEnabler.php @@ -14,6 +14,7 @@ namespace Sylius\PayPalPlugin\Enabler; use Doctrine\Persistence\ObjectManager; +use GuzzleHttp\Client; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; @@ -24,12 +25,28 @@ final class PayPalPaymentMethodEnabler implements PaymentMethodEnablerInterface { public function __construct( - private readonly ClientInterface $client, - private readonly RequestFactoryInterface $requestFactory, + private readonly Client|ClientInterface $client, private readonly string $baseUrl, private readonly ObjectManager $paymentMethodManager, - private readonly SellerWebhookRegistrarInterface $sellerWebhookRegistrar + private readonly SellerWebhookRegistrarInterface $sellerWebhookRegistrar, + private readonly ?RequestFactoryInterface $requestFactory = null, ) { + if ($this->client instanceof Client) { + trigger_deprecation( + 'sylius/paypal-plugin', + '1.6', + 'Passing GuzzleHttp\Client as a first argument in the constructor is deprecated and will be removed. Use Psr\Http\Client\ClientInterface instead.', + ); + } + + if (null === $this->requestFactory) { + trigger_deprecation( + 'sylius/paypal-plugin', + '1.6', + 'Not passing $requestFactory to %s constructor is deprecated and will be removed', + self::class, + ); + } } public function enable(PaymentMethodInterface $paymentMethod): void @@ -38,12 +55,19 @@ public function enable(PaymentMethodInterface $paymentMethod): void $gatewayConfig = $paymentMethod->getGatewayConfig(); $config = $gatewayConfig->getConfig(); - $response = $this->client->sendRequest( - $this->requestFactory->createRequest( - 'GET', - sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id']) - ) - ); + if ($this->client instanceof Client && null === $this->requestFactory) { + $response = $this->client->request( + 'GET', + sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id']), + ); + } else { + $response = $this->client->sendRequest( + $this->requestFactory->createRequest( + 'GET', + sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id']), + ), + ); + } $content = (array) json_decode($response->getBody()->getContents(), true); if (!((bool) $content['permissionsGranted'])) { diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 4a5c5f5d..710e258a 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -232,10 +232,10 @@ class="Sylius\PayPalPlugin\Enabler\PayPalPaymentMethodEnabler" > - %sylius.pay_pal.facilitator_url% +