-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to accept webhook request.
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
- Loading branch information
Showing
5 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters