Skip to content

Commit

Permalink
Fix BC changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpysiak committed Mar 27, 2024
1 parent 793091f commit eebcada
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 46 deletions.
13 changes: 7 additions & 6 deletions spec/Client/PayPalClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ function let(

$this->beConstructedWith(
$client,
$requestFactory,
$streamFactory,
$logger,
$uuidProvider,
$payPalConfigurationProvider,
$channelContext,
'https://test-api.paypal.com/',
5
5,
false,
$requestFactory,
$streamFactory,
);
}

Expand Down Expand Up @@ -140,15 +141,15 @@ function it_logs_all_requests_if_logging_level_is_increased(
): void {
$this->beConstructedWith(
$client,
$requestFactory,
$streamFactory,
$logger,
$uuidProvider,
$payPalConfigurationProvider,
$channelContext,
'https://test-api.paypal.com/',
5,
true
true,
$requestFactory,
$streamFactory,
);

$channelContext->getChannel()->willReturn($channel);
Expand Down
47 changes: 44 additions & 3 deletions src/Api/WebhookApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,44 @@

namespace Sylius\PayPalPlugin\Api;

use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

final class WebhookApi implements WebhookApiInterface
{
public function __construct(
private readonly ClientInterface $client,
private readonly GuzzleClientInterface|ClientInterface $client,
private readonly string $baseUrl,
private readonly RequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly ?RequestFactoryInterface $requestFactory = null,
private readonly ?StreamFactoryInterface $streamFactory = 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.',
self::class,
);
}

if (null === $this->requestFactory && null === $this->streamFactory) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Not passing $requestFactory and $streamFactory to %s constructor is deprecated and will be removed',
self::class,
);
}
}

public function register(string $token, string $webhookUrl): array
{
if ($this->client instanceof GuzzleClientInterface && null === $this->requestFactory && null === $this->streamFactory) {
return $this->legacyRegister($token, $webhookUrl);
}

$request = $this->requestFactory->createRequest('POST', $this->baseUrl . 'v1/notifications/webhooks')
->withHeader('Authorization', 'Bearer ' . $token)
->withHeader('Content-Type', 'application/json')
Expand All @@ -42,4 +64,23 @@ public function register(string $token, string $webhookUrl): array

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

private function legacyRegister(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'],
],
],
]);

return (array) json_decode($response->getBody()->getContents(), true);
}
}
94 changes: 59 additions & 35 deletions src/Client/PayPalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\PayPalPlugin\Client;

use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Client\ClientInterface;
Expand All @@ -30,17 +31,34 @@
final class PayPalClient implements PayPalClientInterface
{
public function __construct(
private readonly ClientInterface $client,
private readonly RequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly GuzzleClientInterface|ClientInterface $client,
private readonly LoggerInterface $logger,
private readonly UuidProviderInterface $uuidProvider,
private readonly PayPalConfigurationProviderInterface $payPalConfigurationProvider,
private readonly ChannelContextInterface $channelContext,
private readonly string $baseUrl,
private int $requestTrialsLimit,
private readonly bool $loggingLevelIncreased = false,
private readonly ?RequestFactoryInterface $requestFactory = null,
private readonly ?StreamFactoryInterface $streamFactory = 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.',
self::class,
);
}

if (null === $this->requestFactory && null === $this->streamFactory) {
trigger_deprecation(
'sylius/paypal-plugin',
'1.6',
'Not passing $requestFactory and $streamFactory to %s constructor is deprecated and will be removed',
self::class,
);
}
}

public function authorize(string $clientId, string $clientSecret): array
Expand Down Expand Up @@ -125,43 +143,49 @@ private function request(string $method, string $url, string $token, array $data
private function doRequest(string $method, string $fullUrl, array $options): ResponseInterface
{
try {
$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 ($this->client instanceof GuzzleClientInterface && null === $this->requestFactory && null === $this->streamFactory) {
$response = $this->client->request($method, $fullUrl, $options);
} else {
$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['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['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);
if (isset($options['headers'])) {
foreach ($options['headers'] as $header => $headerValue) {
$request = $request->withHeader($header, $headerValue);
}
}
}

$response = $this->client->sendRequest($request);
$response = $this->client->sendRequest($request);
}
} catch (ConnectException $exception) {
--$this->requestTrialsLimit;
if ($this->requestTrialsLimit === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
class="Sylius\PayPalPlugin\Client\PayPalClient"
>
<argument type="service" id="Http\Discovery\Psr18Client" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" />
<argument type="service" id="monolog.logger.paypal" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\UuidProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\PayPalConfigurationProviderInterface" />
<argument type="service" id="sylius.context.channel" />
<argument>%sylius.pay_pal.api_base_url%</argument>
<argument>%sylius.pay_pal.request_trials_limit%</argument>
<argument>%sylius.paypal.logging.increased%</argument>
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" />
</service>

<service
Expand Down

0 comments on commit eebcada

Please sign in to comment.