From cbd4d03d83ba2988cfce00a0438beb81eac95a7e Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Tue, 9 Jul 2019 14:34:46 +0200 Subject: [PATCH 1/2] Add possibility the change how a WebookCall model is stored --- README.md | 2 ++ src/Models/WebhookCall.php | 10 ++++++++++ src/WebhookProcessor.php | 5 +---- .../WebhookModelWithoutPayloadSaved.php | 18 ++++++++++++++++++ tests/WebhookControllerTest.php | 14 ++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/TestClasses/WebhookModelWithoutPayloadSaved.php diff --git a/README.md b/README.md index 1c6e2ab..b64618b 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,8 @@ After the signature is validated and the webhook profile has determined that the The request will first be stored in the `webhook_calls` table. This is done using the `WebhookCall` model. Should you want to customize the table name or anything on the storage behavior, you can let the package use an alternative model. A webhook storing model can be specified in the `webhook_model`. Make sure you model extends `Spatie\WebhookClient\Models\WebhookCall`. +You can change how the `WebhookCall` model is stored, by overriding the `storeWebhook` method of `WebhookCall`. In the `storeWebhook` method you should return a in the database stored model. + Next, the newly created `WebhookCall` model will be passed to a queued job that will process the request. Any class that extends `\Spatie\WebhookClient\ProcessWebhookJob` is a valid job. Here's an example: ```php diff --git a/src/Models/WebhookCall.php b/src/Models/WebhookCall.php index 45b1400..c247789 100644 --- a/src/Models/WebhookCall.php +++ b/src/Models/WebhookCall.php @@ -4,6 +4,8 @@ use Exception; use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\Request; +use Spatie\WebhookClient\WebhookConfig; class WebhookCall extends Model { @@ -14,6 +16,14 @@ class WebhookCall extends Model 'exception' => 'array', ]; + public static function storeWebhook(WebhookConfig $config, Request $request): WebhookCall + { + return self::create([ + 'name' => $config->name, + 'payload' => $request->input(), + ]); + } + public function saveException(Exception $exception) { $this->exception = [ diff --git a/src/WebhookProcessor.php b/src/WebhookProcessor.php index 8cfbcf6..988187b 100644 --- a/src/WebhookProcessor.php +++ b/src/WebhookProcessor.php @@ -49,10 +49,7 @@ protected function ensureValidSignature() protected function storeWebhook(): WebhookCall { - return $this->config->webhookModel::create([ - 'name' => $this->config->name, - 'payload' => $this->request->input(), - ]); + return $this->config->webhookModel::storeWebhook($this->config, $this->request); } protected function processWebhook(WebhookCall $webhookCall): void diff --git a/tests/TestClasses/WebhookModelWithoutPayloadSaved.php b/tests/TestClasses/WebhookModelWithoutPayloadSaved.php new file mode 100644 index 0000000..a931e79 --- /dev/null +++ b/tests/TestClasses/WebhookModelWithoutPayloadSaved.php @@ -0,0 +1,18 @@ + $config->name, + 'payload' => [], + ]); + } +} diff --git a/tests/WebhookControllerTest.php b/tests/WebhookControllerTest.php index ca6ae98..a45c682 100644 --- a/tests/WebhookControllerTest.php +++ b/tests/WebhookControllerTest.php @@ -12,6 +12,7 @@ use Spatie\WebhookClient\Tests\TestClasses\NothingIsValidSignatureValidator; use Spatie\WebhookClient\Tests\TestClasses\ProcessWebhookJobTestClass; use Spatie\WebhookClient\Tests\TestClasses\ProcessNothingWebhookProfile; +use Spatie\WebhookClient\Tests\TestClasses\WebhookModelWithoutPayloadSaved; class WebhookControllerTest extends TestCase { @@ -124,6 +125,19 @@ public function it_can_work_with_an_alternative_config() ->assertSuccessful(); } + /** @test */ + public function it_can_work_with_an_alternative_model() + { + config()->set('webhook-client.configs.0.webhook_model', WebhookModelWithoutPayloadSaved::class); + + $this + ->postJson('incoming-webhooks', $this->payload, $this->headers) + ->assertSuccessful(); + + $this->assertCount(1, WebhookCall::get()); + $this->assertEquals([], WebhookCall::first()->payload); + } + private function determineSignature(array $payload): string { $secret = config('webhook-client.configs.0.signing_secret'); From 4955a07d031d51d247b595e17d11d272d10b9a43 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Tue, 9 Jul 2019 12:37:47 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Exceptions/WebhookFailed.php | 2 +- src/Models/WebhookCall.php | 2 +- src/SignatureValidator/DefaultSignatureValidator.php | 3 +-- tests/TestClasses/EverythingIsValidSignatureValidator.php | 6 ++---- tests/TestClasses/NothingIsValidSignatureValidator.php | 6 ++---- tests/TestClasses/WebhookModelWithoutPayloadSaved.php | 2 +- tests/WebhookControllerTest.php | 4 ++-- 7 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Exceptions/WebhookFailed.php b/src/Exceptions/WebhookFailed.php index 8e48ceb..96cc508 100644 --- a/src/Exceptions/WebhookFailed.php +++ b/src/Exceptions/WebhookFailed.php @@ -8,7 +8,7 @@ class WebhookFailed extends Exception { public static function invalidSignature(): WebhookFailed { - return new static("The signature is invalid."); + return new static('The signature is invalid.'); } public static function signingSecretNotSet(): WebhookFailed diff --git a/src/Models/WebhookCall.php b/src/Models/WebhookCall.php index c247789..12d8eec 100644 --- a/src/Models/WebhookCall.php +++ b/src/Models/WebhookCall.php @@ -3,8 +3,8 @@ namespace Spatie\WebhookClient\Models; use Exception; -use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; +use Illuminate\Database\Eloquent\Model; use Spatie\WebhookClient\WebhookConfig; class WebhookCall extends Model diff --git a/src/SignatureValidator/DefaultSignatureValidator.php b/src/SignatureValidator/DefaultSignatureValidator.php index 73ecd3c..13d10a6 100644 --- a/src/SignatureValidator/DefaultSignatureValidator.php +++ b/src/SignatureValidator/DefaultSignatureValidator.php @@ -3,7 +3,6 @@ namespace Spatie\WebhookClient\SignatureValidator; use Illuminate\Http\Request; -use Spatie\WebhookClient\Events\InvalidSignatureEvent; use Spatie\WebhookClient\WebhookConfig; use Spatie\WebhookClient\Exceptions\WebhookFailed; @@ -14,7 +13,7 @@ public function isValid(Request $request, WebhookConfig $config): bool $signature = $request->header($config->signatureHeaderName); if (! $signature) { - return false; + return false; } $signingSecret = $config->signingSecret; diff --git a/tests/TestClasses/EverythingIsValidSignatureValidator.php b/tests/TestClasses/EverythingIsValidSignatureValidator.php index 67530b3..ec2d277 100644 --- a/tests/TestClasses/EverythingIsValidSignatureValidator.php +++ b/tests/TestClasses/EverythingIsValidSignatureValidator.php @@ -1,12 +1,10 @@