Skip to content

Commit

Permalink
minor #132 webhook already registered exception (SirDomin)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------



Commits
-------

0a5a71c webhook already registered exception
6418a20 pr fix
  • Loading branch information
Zales0123 authored Oct 9, 2020
2 parents 6f9b8f8 + 6418a20 commit ca7fecb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Exception/PayPalWebhookAlreadyRegisteredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Exception;

final class PayPalWebhookAlreadyRegisteredException extends \Exception
{
public function __construct()
{
parent::__construct('Webhook already registered!');
}
}
3 changes: 3 additions & 0 deletions src/Onboarding/Processor/BasicOnboardingProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sylius\Bundle\PayumBundle\Model\GatewayConfig;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\PayPalPlugin\Exception\PayPalPluginException;
use Sylius\PayPalPlugin\Exception\PayPalWebhookAlreadyRegisteredException;
use Sylius\PayPalPlugin\Exception\PayPalWebhookUrlNotValidException;
use Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -84,6 +85,8 @@ public function process(
$this->sellerWebhookRegistrar->register($paymentMethod);
} catch (PayPalWebhookUrlNotValidException $exception) {
$paymentMethod->setEnabled(false);
} catch (PayPalWebhookAlreadyRegisteredException $exception) {
$paymentMethod->setEnabled(true);
}

return $paymentMethod;
Expand Down
17 changes: 16 additions & 1 deletion src/Registrar/SellerWebhookRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\PayPalWebhookAlreadyRegisteredException;
use Sylius\PayPalPlugin\Exception\PayPalWebhookUrlNotValidException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

Expand Down Expand Up @@ -49,7 +52,19 @@ 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();
/** @var array $exceptionMessage */
$exceptionMessage = json_decode($exceptionResponse->getBody()->getContents(), true);

if ($exceptionMessage['name'] === 'WEBHOOK_URL_ALREADY_EXISTS') {
throw new PayPalWebhookAlreadyRegisteredException();
}
}

if (isset($response['name']) && $response['name'] === 'VALIDATION_ERROR') {
throw new PayPalWebhookUrlNotValidException();
Expand Down

0 comments on commit ca7fecb

Please sign in to comment.