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/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 45b1400..12d8eec 100644 --- a/src/Models/WebhookCall.php +++ b/src/Models/WebhookCall.php @@ -3,7 +3,9 @@ namespace Spatie\WebhookClient\Models; use Exception; +use Illuminate\Http\Request; use Illuminate\Database\Eloquent\Model; +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/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/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/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 @@ $config->name, + 'payload' => [], + ]); + } +} diff --git a/tests/WebhookControllerTest.php b/tests/WebhookControllerTest.php index ca6ae98..12f3980 100644 --- a/tests/WebhookControllerTest.php +++ b/tests/WebhookControllerTest.php @@ -8,10 +8,11 @@ use Illuminate\Support\Facades\Route; use Spatie\WebhookClient\Models\WebhookCall; use Spatie\WebhookClient\Events\InvalidSignatureEvent; -use Spatie\WebhookClient\Tests\TestClasses\EverythingIsValidSignatureValidator; -use Spatie\WebhookClient\Tests\TestClasses\NothingIsValidSignatureValidator; use Spatie\WebhookClient\Tests\TestClasses\ProcessWebhookJobTestClass; use Spatie\WebhookClient\Tests\TestClasses\ProcessNothingWebhookProfile; +use Spatie\WebhookClient\Tests\TestClasses\WebhookModelWithoutPayloadSaved; +use Spatie\WebhookClient\Tests\TestClasses\NothingIsValidSignatureValidator; +use Spatie\WebhookClient\Tests\TestClasses\EverythingIsValidSignatureValidator; 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');