start verification of webhooks

This commit is contained in:
David Remer 2020-07-09 16:31:06 +03:00
parent a63e51818b
commit 2b208817bd
3 changed files with 36 additions and 5 deletions

View file

@ -30,7 +30,9 @@
"container-interop/service-provider": "^0.4.0",
"dhii/containers": "dev-develop",
"dhii/wp-containers": "^0.1.0@alpha",
"psr/log": "^1.1"
"psr/log": "^1.1",
"ext-json": "*",
"ext-apache": "*"
},
"require-dev": {
"inpsyde/php-coding-standards": "^1",

View file

@ -21,10 +21,12 @@ return [
);
},
'webhook.endpoint.controller' => function(ContainerInterface $container) : IncomingWebhookEndpoint {
$webhookEndpoint = $container->get('api.endpoint.webhook');
$webhookFactory = $container->get('api.factory.webhook');
$handler = $container->get('webhook.endpoint.handler');
$logger = $container->get('woocommerce.logger.woocommerce');
return new IncomingWebhookEndpoint($logger, ... $handler);
return new IncomingWebhookEndpoint($webhookEndpoint, $webhookFactory, $logger, ... $handler);
},
'webhook.endpoint.handler' => function(ContainerInterface $container) : array {
$logger = $container->get('woocommerce.logger.woocommerce');

View file

@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Webhooks;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\Factory\WebhookFactory;
use Inpsyde\PayPalCommerce\Webhooks\Handler\RequestHandler;
use Psr\Log\LoggerInterface;
@ -12,10 +15,19 @@ class IncomingWebhookEndpoint
public const NAMESPACE = 'paypal/v1';
public const ROUTE = 'incoming';
private $webhookEndpoint;
private $webhookFactory;
private $handlers;
private $logger;
public function __construct(LoggerInterface $logger, RequestHandler ...$handlers)
{
public function __construct(
WebhookEndpoint $webhookEndpoint,
WebhookFactory $webhookFactory,
LoggerInterface $logger,
RequestHandler ...$handlers
) {
$this->webhookEndpoint = $webhookEndpoint;
$this->webhookFactory = $webhookFactory;
$this->handlers = $handlers;
$this->logger = $logger;
}
@ -33,10 +45,25 @@ class IncomingWebhookEndpoint
$this,
'handleRequest',
],
'permission_callback' => [
$this,
'verifyRequest',
],
]
);
}
public function verifyRequest() : bool {
try {
$data = (array) get_option(WebhookRegistrar::KEY, []);
$webhook = $this->webhookFactory->fromArray($data);
return $this->webhookEndpoint->verifyCurrentRequestForWebhook($webhook);
} catch (RuntimeException $exception) {
return false;
}
}
public function handleRequest(\WP_REST_Request $request) : \WP_REST_Response {
/**
@ -65,7 +92,7 @@ class IncomingWebhookEndpoint
}
public function url() : string {
return rest_url(self::NAMESPACE . '/' . self::ROUTE);
return str_replace('http', 'https', rest_url(self::NAMESPACE . '/' . self::ROUTE));
}
public function handledEventTypes() : array {