Skip to content

Commit

Permalink
Fixes after testing 'Enable' method
Browse files Browse the repository at this point in the history
  • Loading branch information
leszczuu committed Mar 27, 2024
1 parent a8265ab commit 0065fc5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 54 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "PayPal plugin for Sylius.",
"license": "MIT",
"require": {
"php": "^8.0",
"php": "^8.1",
"doctrine/doctrine-migrations-bundle": "^3.0",
"php-http/discovery": "^1.17",
"phpseclib/phpseclib": "^2.0",
Expand Down
24 changes: 14 additions & 10 deletions spec/Api/GenericApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\ClientInterface;
use PhpSpec\ObjectBehavior;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\PayPalPlugin\Api\GenericApiInterface;

final class GenericApiSpec extends ObjectBehavior
{
function let(ClientInterface $client): void
function let(ClientInterface $client, RequestFactoryInterface $requestFactory): void
{
$this->beConstructedWith($client);
$this->beConstructedWith($client, $requestFactory);
}

function it_implements_generic_api_interface(): void
Expand All @@ -33,17 +35,19 @@ function it_implements_generic_api_interface(): void

function it_calls_api_by_url(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
RequestInterface $request,
ResponseInterface $response,
StreamInterface $body
): void {
$client->request('GET', 'http://url.com/', [
'headers' => [
'Authorization' => 'Bearer TOKEN',
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
])->willReturn($response);

$requestFactory->createRequest('GET', 'http://url.com/')->willReturn($request);

$request->withHeader('Authorization', 'Bearer TOKEN')->willReturn($request);
$request->withHeader('Content-Type', 'application/json')->willReturn($request);
$request->withHeader('Accept', 'application/json')->willReturn($request);

$client->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{ "parameter": "VALUE" }');

Expand Down
25 changes: 18 additions & 7 deletions spec/Enabler/PayPalPaymentMethodEnablerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
namespace spec\Sylius\PayPalPlugin\Enabler;

use Doctrine\Persistence\ObjectManager;
use GuzzleHttp\Client;
use Payum\Core\Model\GatewayConfigInterface;
use PhpSpec\ObjectBehavior;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
Expand All @@ -27,12 +29,13 @@
final class PayPalPaymentMethodEnablerSpec extends ObjectBehavior
{
function let(
Client $client,
ClientInterface $client,
RequestFactoryInterface $requestFactory,
ObjectManager $paymentMethodManager,
SellerWebhookRegistrarInterface $sellerWebhookRegistrar
): void {
$this->beConstructedWith(
$client, 'http://base-url.com', $paymentMethodManager, $sellerWebhookRegistrar
$client, $requestFactory, 'http://base-url.com', $paymentMethodManager, $sellerWebhookRegistrar
);
}

Expand All @@ -42,7 +45,9 @@ function it_implements_payment_method_enabler_interface(): void
}

function it_enables_payment_method_if_it_has_proper_credentials_and_webhook_are_set(
Client $client,
ClientInterface $client,
RequestFactoryInterface $requestFactory,
RequestInterface $request,
ObjectManager $paymentMethodManager,
SellerWebhookRegistrarInterface $sellerWebhookRegistrar,
PaymentMethodInterface $paymentMethod,
Expand All @@ -53,7 +58,9 @@ function it_enables_payment_method_if_it_has_proper_credentials_and_webhook_are_
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
$gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123', 'client_id' => 'CLIENT-ID', 'client_secret' => 'SECRET']);

$client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response);
$requestFactory->createRequest('GET', 'http://base-url.com/seller-permissions/check/123123')
->willReturn($request);
$client->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{ "permissionsGranted": true }');

Expand All @@ -66,7 +73,9 @@ function it_enables_payment_method_if_it_has_proper_credentials_and_webhook_are_
}

function it_throws_exception_if_payment_method_credentials_are_not_granted(
Client $client,
ClientInterface $client,
RequestFactoryInterface $requestFactory,
RequestInterface $request,
ObjectManager $paymentMethodManager,
SellerWebhookRegistrarInterface $sellerWebhookRegistrar,
PaymentMethodInterface $paymentMethod,
Expand All @@ -77,7 +86,9 @@ function it_throws_exception_if_payment_method_credentials_are_not_granted(
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
$gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123', 'client_id' => 'CLIENT-ID', 'client_secret' => 'SECRET']);

$client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response);
$requestFactory->createRequest('GET', 'http://base-url.com/seller-permissions/check/123123')
->willReturn($request);
$client->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{ "permissionsGranted": false }');

Expand Down
27 changes: 13 additions & 14 deletions src/Api/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@

namespace Sylius\PayPalPlugin\Api;

use GuzzleHttp\ClientInterface;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

final class GenericApi implements GenericApiInterface
{
private ClientInterface $client;

public function __construct(ClientInterface $client)
{
$this->client = $client;
public function __construct(
private readonly ClientInterface $client,
private readonly RequestFactoryInterface $requestFactory
){
}

public function get(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);
$request = $this->requestFactory->createRequest('GET', $url)
->withHeader('Authorization', 'Bearer ' . $token)
->withHeader('Content-Type', 'application/json')
->withHeader('Accept', 'application/json');

return (array) json_decode($this->client->sendRequest($request)->getBody()->getContents(), true);
}
}
1 change: 0 additions & 1 deletion src/Client/PayPalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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 Down
30 changes: 11 additions & 19 deletions src/Enabler/PayPalPaymentMethodEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,22 @@
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;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException;
use Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface;

final class PayPalPaymentMethodEnabler implements PaymentMethodEnablerInterface
{
private Client $client;

private string $baseUrl;

private ObjectManager $paymentMethodManager;

private SellerWebhookRegistrarInterface $sellerWebhookRegistrar;

public function __construct(
Client $client,
string $baseUrl,
ObjectManager $paymentMethodManager,
SellerWebhookRegistrarInterface $sellerWebhookRegistrar
private readonly ClientInterface $client,
private readonly RequestFactoryInterface $requestFactory,
private readonly string $baseUrl,
private readonly ObjectManager $paymentMethodManager,
private readonly SellerWebhookRegistrarInterface $sellerWebhookRegistrar
) {
$this->client = $client;
$this->baseUrl = $baseUrl;
$this->paymentMethodManager = $paymentMethodManager;
$this->sellerWebhookRegistrar = $sellerWebhookRegistrar;
}

public function enable(PaymentMethodInterface $paymentMethod): void
Expand All @@ -48,9 +38,11 @@ public function enable(PaymentMethodInterface $paymentMethod): void
$gatewayConfig = $paymentMethod->getGatewayConfig();
$config = $gatewayConfig->getConfig();

$response = $this->client->request(
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
'GET',
sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id'])
sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id'])
)
);

$content = (array) json_decode($response->getBody()->getContents(), true);
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@
id="Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface"
class="Sylius\PayPalPlugin\Enabler\PayPalPaymentMethodEnabler"
>
<argument type="service" id="sylius.http_client" />
<argument type="service" id="Http\Discovery\Psr18Client" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
<argument>%sylius.pay_pal.facilitator_url%</argument>
<argument type="service" id="sylius.manager.payment_method" />
<argument type="service" id="Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface" />
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
id="Sylius\PayPalPlugin\Api\GenericApiInterface"
class="Sylius\PayPalPlugin\Api\GenericApi"
>
<argument type="service" id="sylius.http_client" />
<argument type="service" id="Http\Discovery\Psr18Client" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
</service>

<service
Expand Down

0 comments on commit 0065fc5

Please sign in to comment.