From 0a5a71c4886018ebba7d9b25734aedc7c4216f6b Mon Sep 17 00:00:00 2001 From: SirDomin Date: Fri, 9 Oct 2020 11:14:20 +0200 Subject: [PATCH 1/2] webhook already registered exception --- psalm.xml | 2 ++ .../PayPalWebhookAlreadyExistsException.php | 13 +++++++++++++ .../Processor/BasicOnboardingProcessor.php | 3 +++ src/Registrar/SellerWebhookRegistrar.php | 16 +++++++++++++++- 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/Exception/PayPalWebhookAlreadyExistsException.php diff --git a/psalm.xml b/psalm.xml index 769393df..a0b10f10 100644 --- a/psalm.xml +++ b/psalm.xml @@ -43,12 +43,14 @@ + + diff --git a/src/Exception/PayPalWebhookAlreadyExistsException.php b/src/Exception/PayPalWebhookAlreadyExistsException.php new file mode 100644 index 00000000..f780a7b7 --- /dev/null +++ b/src/Exception/PayPalWebhookAlreadyExistsException.php @@ -0,0 +1,13 @@ +sellerWebhookRegistrar->register($paymentMethod); } catch (PayPalWebhookUrlNotValidException $exception) { $paymentMethod->setEnabled(false); + } catch (PayPalWebhookAlreadyExistsException $exception) { + $paymentMethod->setEnabled(true); } return $paymentMethod; diff --git a/src/Registrar/SellerWebhookRegistrar.php b/src/Registrar/SellerWebhookRegistrar.php index 95c996ac..6e7e5718 100644 --- a/src/Registrar/SellerWebhookRegistrar.php +++ b/src/Registrar/SellerWebhookRegistrar.php @@ -13,10 +13,13 @@ namespace Sylius\PayPalPlugin\Registrar; +use GuzzleHttp\Exception\ClientException; use Payum\Core\Model\GatewayConfigInterface; +use Psr\Http\Message\ResponseInterface; use Sylius\Component\Core\Model\PaymentMethodInterface; use Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface; use Sylius\PayPalPlugin\Api\WebhookApiInterface; +use Sylius\PayPalPlugin\Exception\PayPalWebhookAlreadyExistsException; use Sylius\PayPalPlugin\Exception\PayPalWebhookUrlNotValidException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -49,7 +52,18 @@ public function register(PaymentMethodInterface $paymentMethod): void $token = $this->authorizeClientApi->authorize((string) $config['client_id'], (string) $config['client_secret']); $webhookUrl = $this->urlGenerator->generate('sylius_paypal_plugin_webhook_refund_order', [], UrlGeneratorInterface::ABSOLUTE_URL); - $response = $this->webhookApi->register($token, $webhookUrl); + + try { + $response = $this->webhookApi->register($token, $webhookUrl); + } catch (ClientException $exception) { + /** @var ResponseInterface $exceptionResponse */ + $exceptionResponse = $exception->getResponse(); + $exceptionMessage = json_decode($exceptionResponse->getBody()->getContents(), true); + + if ($exceptionMessage['name'] === 'WEBHOOK_URL_ALREADY_EXISTS') { + throw new PayPalWebhookAlreadyExistsException(); + } + } if (isset($response['name']) && $response['name'] === 'VALIDATION_ERROR') { throw new PayPalWebhookUrlNotValidException(); From 6418a206501455b99654dc363ad3a4892586855a Mon Sep 17 00:00:00 2001 From: SirDomin Date: Fri, 9 Oct 2020 17:19:25 +0200 Subject: [PATCH 2/2] pr fix --- psalm.xml | 2 -- ...ption.php => PayPalWebhookAlreadyRegisteredException.php} | 2 +- src/Onboarding/Processor/BasicOnboardingProcessor.php | 4 ++-- src/Registrar/SellerWebhookRegistrar.php | 5 +++-- 4 files changed, 6 insertions(+), 7 deletions(-) rename src/Exception/{PayPalWebhookAlreadyExistsException.php => PayPalWebhookAlreadyRegisteredException.php} (72%) diff --git a/psalm.xml b/psalm.xml index a0b10f10..769393df 100644 --- a/psalm.xml +++ b/psalm.xml @@ -43,14 +43,12 @@ - - diff --git a/src/Exception/PayPalWebhookAlreadyExistsException.php b/src/Exception/PayPalWebhookAlreadyRegisteredException.php similarity index 72% rename from src/Exception/PayPalWebhookAlreadyExistsException.php rename to src/Exception/PayPalWebhookAlreadyRegisteredException.php index f780a7b7..84c5fce0 100644 --- a/src/Exception/PayPalWebhookAlreadyExistsException.php +++ b/src/Exception/PayPalWebhookAlreadyRegisteredException.php @@ -4,7 +4,7 @@ namespace Sylius\PayPalPlugin\Exception; -final class PayPalWebhookAlreadyExistsException extends \Exception +final class PayPalWebhookAlreadyRegisteredException extends \Exception { public function __construct() { diff --git a/src/Onboarding/Processor/BasicOnboardingProcessor.php b/src/Onboarding/Processor/BasicOnboardingProcessor.php index 0116f5d3..e23f50d6 100644 --- a/src/Onboarding/Processor/BasicOnboardingProcessor.php +++ b/src/Onboarding/Processor/BasicOnboardingProcessor.php @@ -8,7 +8,7 @@ use Sylius\Bundle\PayumBundle\Model\GatewayConfig; use Sylius\Component\Core\Model\PaymentMethodInterface; use Sylius\PayPalPlugin\Exception\PayPalPluginException; -use Sylius\PayPalPlugin\Exception\PayPalWebhookAlreadyExistsException; +use Sylius\PayPalPlugin\Exception\PayPalWebhookAlreadyRegisteredException; use Sylius\PayPalPlugin\Exception\PayPalWebhookUrlNotValidException; use Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface; use Symfony\Component\HttpFoundation\Request; @@ -85,7 +85,7 @@ public function process( $this->sellerWebhookRegistrar->register($paymentMethod); } catch (PayPalWebhookUrlNotValidException $exception) { $paymentMethod->setEnabled(false); - } catch (PayPalWebhookAlreadyExistsException $exception) { + } catch (PayPalWebhookAlreadyRegisteredException $exception) { $paymentMethod->setEnabled(true); } diff --git a/src/Registrar/SellerWebhookRegistrar.php b/src/Registrar/SellerWebhookRegistrar.php index 6e7e5718..0a1c8d23 100644 --- a/src/Registrar/SellerWebhookRegistrar.php +++ b/src/Registrar/SellerWebhookRegistrar.php @@ -19,7 +19,7 @@ use Sylius\Component\Core\Model\PaymentMethodInterface; use Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface; use Sylius\PayPalPlugin\Api\WebhookApiInterface; -use Sylius\PayPalPlugin\Exception\PayPalWebhookAlreadyExistsException; +use Sylius\PayPalPlugin\Exception\PayPalWebhookAlreadyRegisteredException; use Sylius\PayPalPlugin\Exception\PayPalWebhookUrlNotValidException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -58,10 +58,11 @@ public function register(PaymentMethodInterface $paymentMethod): void } catch (ClientException $exception) { /** @var ResponseInterface $exceptionResponse */ $exceptionResponse = $exception->getResponse(); + /** @var array $exceptionMessage */ $exceptionMessage = json_decode($exceptionResponse->getBody()->getContents(), true); if ($exceptionMessage['name'] === 'WEBHOOK_URL_ALREADY_EXISTS') { - throw new PayPalWebhookAlreadyExistsException(); + throw new PayPalWebhookAlreadyRegisteredException(); } }