Skip to content

Commit

Permalink
Merge branch 'main' of github.com:juststeveking/laravel-webhooks into…
Browse files Browse the repository at this point in the history
… feature/webhook-notification
  • Loading branch information
JustSteveKing committed Apr 22, 2023
2 parents 25f8222 + aac78af commit d1dad4a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ The Pending Webhook has the following properties in the constructor:
- `url` - The URL you want to send the webhook to.
- `signer` - An instance of the webhook signer you want to use. This must implement the `SigningContract`.
- `payload` - You can pre-pass in the payload that you want to send in your webhook. This should be an `array`.
- `signature` - You can pre-pass in the signature that you want to use to sign your Webhooks with.
- `signed` - You can pre-pass in whether you want this webhook to be signed or not, the default is `true`
.- `signature` - You can pre-pass in the signature that you want to use to sign your Webhooks with.
- `request` - You can pre-pass in a `PendingRequest` that you want to use to send your webhooks, this is useful when you need to attach an API token to your Webhooks.

## A simple example
Expand All @@ -85,8 +86,7 @@ In the below example, we are sending a webhook to `https://your-url.com/` and se
use JustSteveKing\Webhooks\Facades\Webhook;

Webhook::to('https://your-url.com/')
->payload(Post::query()->first()->toArray())
->sign()
->with(Post::query()->first()->toArray())
->send();
```

Expand All @@ -101,27 +101,24 @@ use Illuminate\Http\Client\PendingRequest;
use JustSteveKing\Webhooks\Facades\Webhook;

Webhook::to('https://your-url.com/')
->payload(Post::query()->first()->toArray())
->sign()
->with(Post::query()->first()->toArray())
->intercept(fn (PendingRequest $request) => $request
->withToken('YOUR-BEARER-TOKEN'),
)->queue('my-queue-name');
```

## Not signing the webhook

If you don't need to sign the webhook, then you can skip calling the `sign` method.
If you don't need to sign the webhook.

```php
use JustSteveKing\Webhooks\Facades\Webhook;

Webhook::to('https://your-url.com/')->payload(
Webhook::to('https://your-url.com/')->with(
Post::query()->first()->toArray()
)->send();
)->notSigned()->send();
```

**Please note, it is important that the payload is set before you call `sign` otherwise your signature will not match your payload**

## Testing

To run the test:
Expand Down
39 changes: 27 additions & 12 deletions src/Builder/PendingWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,45 @@ public function __construct(
public readonly string $url,
public readonly SigningContract $signer,
public array $payload = [],
public bool $signed = true,
public null|string $signature = null,
public null|PendingRequest $request = null,
) {
}

/**
* Create the webhook signature.
* Enforce the webhook to be signed.
*
* @return PendingWebhook
* @throws JsonException
*/
public function sign(): PendingWebhook
public function signed(): PendingWebhook
{
$this->signature = $this->signer->sign(
payload: $this->payload,
);
$this->signed = true;

return $this;
}

/**
* Do not sign this webhook.
*
* @return PendingWebhook
*/
public function notSigned(): PendingWebhook
{
$this->signed = false;

return $this;
}


/**
* Set the payload for the Webhook.
*
* @param array $payload
* @return PendingWebhook
*/
public function payload(array $payload): PendingWebhook
public function with(array $payload): PendingWebhook
{
$this->payload = $payload;

Expand Down Expand Up @@ -97,6 +109,7 @@ public function intercept(Closure $callback): PendingWebhook
*
* @param string|null $queue
* @return PendingDispatch|PendingClosureDispatch
* @throws JsonException
*/
public function queue(null|string $queue = null): PendingDispatch|PendingClosureDispatch
{
Expand Down Expand Up @@ -124,12 +137,14 @@ public function send(Method $method = Method::POST): Response
);
}

/** @phpstan-ignore-next-line */
$this->request->withHeaders(
headers: [
strval(config('webhooks.signing.header')) => $this->signature ?: $this->sign()->signature,
],
);
if ($this->signed) {
/** @phpstan-ignore-next-line */
$this->request->withHeaders(
headers: [
strval(config('webhooks.signing.header')) => $this->signature ?: $this->signed()->signature,
],
);
}

/** @phpstan-ignore-next-line */
return $this->request->send(
Expand Down
11 changes: 9 additions & 2 deletions src/Contracts/Builder/PendingWebhookContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ interface PendingWebhookContract
* Create the webhook signature.
* @return PendingWebhookContract
*/
public function sign(): PendingWebhookContract;
public function signed(): PendingWebhookContract;

/**
* Do not sign this webhook.
*
* @return PendingWebhookContract
*/
public function notSigned(): PendingWebhookContract;

/**
* Set the payload for the Webhook.
*
* @param array $payload
* @return PendingWebhookContract
*/
public function payload(array $payload): PendingWebhookContract;
public function with(array $payload): PendingWebhookContract;

/**
* Intercept the Http Request to override options.
Expand Down
14 changes: 11 additions & 3 deletions tests/Builder/PendingWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@
signer: $this->signer,
))->toBeInstanceOf(PendingWebhook::class);

test('it can create a signature')
test('it can set the webhook to be signed')
->with('urls')
->expect(fn (string $url) => (new PendingWebhook(
url: $url,
signer: $this->signer,
payload: ['foo' => 'bar'],
))->sign()->signature)->toBeString();
))->signed()->signed)->toBeTrue();

test('it can set the webhook to not be signed')
->with('urls')
->expect(fn (string $url) => (new PendingWebhook(
url: $url,
signer: $this->signer,
payload: ['foo' => 'bar'],
))->notSigned()->signed)->toBeFalse();

it('can set the payload', function (string $url): void {
$pendingWebhook = new PendingWebhook(
Expand All @@ -35,7 +43,7 @@
expect(
$pendingWebhook->payload
)->toBeArray()->toBeEmpty()->and(
$pendingWebhook->payload(['foo' => 'bar'])->payload,
$pendingWebhook->with(['foo' => 'bar'])->payload,
)->toBeArray()->toEqual(['foo' => 'bar']);
})->with('urls');

Expand Down

0 comments on commit d1dad4a

Please sign in to comment.