From 65c0f81ae139a67dde00ee46e04a863165857596 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 4 Apr 2019 11:54:00 +0800 Subject: [PATCH] Add ability to accept webhook request. Signed-off-by: Mior Muhammad Zaki --- src/Exceptions/ValidationException.php | 96 ++++++++++++++++++++++++++ src/Http/Requests/Request.php | 24 +++++++ src/Http/Requests/Webhook.php | 38 ++++++++++ src/Manager.php | 10 ++- src/ServiceProvider.php | 1 - 5 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 src/Exceptions/ValidationException.php create mode 100644 src/Http/Requests/Request.php create mode 100644 src/Http/Requests/Webhook.php diff --git a/src/Exceptions/ValidationException.php b/src/Exceptions/ValidationException.php new file mode 100644 index 0000000..5da0b4f --- /dev/null +++ b/src/Exceptions/ValidationException.php @@ -0,0 +1,96 @@ +response = $response; + $this->errorBag = $errorBag; + $this->validator = $validator; + } + + /** + * Get all of the validation error messages. + * + * @return array + */ + public function errors() + { + return $this->validator->errors()->messages(); + } + + /** + * Set the HTTP status code to be used for the response. + * + * @param int $status + * + * @return $this + */ + public function status($status) + { + $this->status = $status; + + return $this; + } + + /** + * Set the error bag on the exception. + * + * @param string $errorBag + * + * @return $this + */ + public function errorBag($errorBag) + { + $this->errorBag = $errorBag; + + return $this; + } + + /** + * Get the underlying response instance. + * + * @return \Symfony\Component\HttpFoundation\Response|null + */ + public function getResponse() + { + return $this->response; + } +} diff --git a/src/Http/Requests/Request.php b/src/Http/Requests/Request.php new file mode 100644 index 0000000..eb17948 --- /dev/null +++ b/src/Http/Requests/Request.php @@ -0,0 +1,24 @@ +errorBag); + } +} diff --git a/src/Http/Requests/Webhook.php b/src/Http/Requests/Webhook.php new file mode 100644 index 0000000..2698936 --- /dev/null +++ b/src/Http/Requests/Webhook.php @@ -0,0 +1,38 @@ + ['required'], + 'event' => ['required'], + ]; + } + + /** + * Get the validated data from the request. + * + * @return array + */ + public function validated(): array + { + $config = \config('services.katsana.webhook'); + + $signature = new Signature($webhook['signature']); + $header = $this->header('HTTP_X_SIGNATURE'); + + if ($signature->verify($header, $this->getContent(), ($config['threshold'] ?? 3600))) { + throw new HttpException(419, 'Unable to verify X-Signature.'); + } + + return $this->post(); + } +} diff --git a/src/Manager.php b/src/Manager.php index 2e4fa90..d208e2a 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -2,8 +2,6 @@ namespace Katsana; -use Katsana\Sdk\Client; - class Manager extends \Illuminate\Support\Manager { /** @@ -31,7 +29,7 @@ public function __construct($app, array $config) * * @return \Katsana\Sdk\Client */ - protected function createLaravelDriver(): Client + protected function createLaravelDriver(): Sdk\Client { return \tap($this->createHttpClient(), function ($client) { if (isset($this->config['client_id']) || isset($this->config['client_secret'])) { @@ -50,7 +48,7 @@ protected function createLaravelDriver(): Client * * @return \Katsana\Sdk\Client */ - protected function createSdkDriver(): Client + protected function createSdkDriver(): Sdk\Client { return $this->createHttpClient(); } @@ -62,9 +60,9 @@ protected function createSdkDriver(): Client * * @return \Katsana\Sdk\Client */ - protected function createHttpClient(): Client + protected function createHttpClient(): Sdk\Client { - $client = new Client($this->app->make('katsana.http')); + $client = new Sdk\Client($this->app->make('katsana.http')); if (($this->config['environment'] ?? 'production') === 'carbon') { $client->useCustomApiEndpoint('https://carbon.api.katsana.com'); diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index c91279b..63f2bc1 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider as BaseServiceProvider; -use Katsana\Sdk\Client; use Laravie\Codex\Discovery; class ServiceProvider extends BaseServiceProvider implements DeferrableProvider