Skip to content

Commit

Permalink
Add ability to accept webhook request.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Apr 4, 2019
1 parent 9b1aaaf commit 65c0f81
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 7 deletions.
96 changes: 96 additions & 0 deletions src/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Katsana\Exceptions;

use Exception;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ValidationException extends HttpException
{
/**
* The validator instance.
*
* @var \Illuminate\Contracts\Validation\Validator
*/
public $validator;

/**
* The recommended response to send to the client.
*
* @var \Symfony\Component\HttpFoundation\Response|null
*/
public $response;

/**
* The name of the error bag.
*
* @var string
*/
public $errorBag;

/**
* Create a new exception instance.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @param \Symfony\Component\HttpFoundation\Response $response
* @param string $errorBag
*
* @return void
*/
public function __construct($validator, $response = null, $errorBag = 'default')
{
parent::__construct(422, 'The given data was invalid.');

$this->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;
}
}
24 changes: 24 additions & 0 deletions src/Http/Requests/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Katsana\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Katsana\Exceptions\ValidationException;
use Illuminate\Contracts\Validation\Validator;

abstract class Request extends FormRequest
{
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
*
* @throws \Billplz\Laravel\Exceptions\ValidationException
*
* @return void
*/
protected function failedValidation(Validator $validator): void
{
throw new ValidationException($validator, null, $this->errorBag);
}
}
38 changes: 38 additions & 0 deletions src/Http/Requests/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Katsana\Http\Requests;

class Webhook extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'device_id' => ['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();
}
}
10 changes: 4 additions & 6 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Katsana;

use Katsana\Sdk\Client;

class Manager extends \Illuminate\Support\Manager
{
/**
Expand Down Expand Up @@ -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'])) {
Expand All @@ -50,7 +48,7 @@ protected function createLaravelDriver(): Client
*
* @return \Katsana\Sdk\Client
*/
protected function createSdkDriver(): Client
protected function createSdkDriver(): Sdk\Client
{
return $this->createHttpClient();
}
Expand All @@ -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');
Expand Down
1 change: 0 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 65c0f81

Please sign in to comment.