Skip to content

Commit

Permalink
Fix 1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
leszczuu committed Mar 14, 2024
1 parent 9ab11fd commit 055285a
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 38 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]/sylius?serverVersion=${{ matrix.mysql }}"
Expand Down Expand Up @@ -149,6 +149,7 @@ jobs:

-
name: Validate composer.json
if: ${{ matrix.sylius != '1.13.x-dev' }}
run: composer validate --ansi --strict

-
Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
}
}
51 changes: 35 additions & 16 deletions src/Api/WebhookApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
56 changes: 52 additions & 4 deletions src/Client/PayPalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +30,11 @@

final class PayPalClient implements PayPalClientInterface
{
private ClientInterface $client;
private ClientInterface $client;

private RequestFactoryInterface $requestFactory;

private StreamFactoryInterface $streamFactory;

private LoggerInterface $logger;

Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 14 additions & 10 deletions src/Onboarding/Processor/BasicOnboardingProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class="Sylius\PayPalPlugin\Client\PayPalClient"
>
<argument type="service" id="sylius.http_client" />
<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" />
Expand Down Expand Up @@ -87,6 +89,8 @@
class="Sylius\PayPalPlugin\Api\WebhookApi"
>
<argument type="service" id="sylius.http_client" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" />
<argument>%sylius.pay_pal.api_base_url%</argument>
</service>

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/onboarding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class="Sylius\PayPalPlugin\Onboarding\Processor\BasicOnboardingProcessor"
>
<argument type="service" id="sylius.http_client" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface" />
<argument>%sylius.pay_pal.facilitator_url%</argument>
</service>
Expand Down
5 changes: 4 additions & 1 deletion tests/Application/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "sylius/paypal-plugin",
"description": "PayPal plugin for Sylius",
"license": "MIT"
"license": "MIT",
"require": {
"nyholm/psr7": "^1.8"
}
}
12 changes: 9 additions & 3 deletions tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Sylius\Bundle\CoreBundle\Application\Kernel;
use Sylius\Bundle\CoreBundle\SyliusCoreBundle;

$bundles = [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Expand Down Expand Up @@ -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;

0 comments on commit 055285a

Please sign in to comment.