Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function routeNotificationForWebhook()
### Available methods

- `data('')`: Accepts a JSON-encodable value for the Webhook body.
- `form('')`: Accepts a associative array for the Webhook send as form encoded data.
- `query('')`: Accepts an associative array of query string values to add to the request.
- `userAgent('')`: Accepts a string value for the Webhook user agent.
- `header($name, $value)`: Sets additional headers to send with the POST Webhook.
Expand Down
18 changes: 13 additions & 5 deletions src/WebhookChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function __construct(Client $client)
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return \GuzzleHttp\Psr7\Response
*
Expand All @@ -38,12 +38,20 @@ public function send($notifiable, Notification $notification)

$webhookData = $notification->toWebhook($notifiable)->toArray();

$response = $this->client->post($url, [
$options = [
'query' => Arr::get($webhookData, 'query'),
'body' => json_encode(Arr::get($webhookData, 'data')),
'verify' => Arr::get($webhookData, 'verify'),
'headers' => Arr::get($webhookData, 'headers'),
]);
];

if (Arr::get($webhookData, 'form')) {
$options['form_params'] = Arr::get($webhookData, 'form');
}
if (Arr::get($webhookData, 'data')) {
$options['body'] = json_encode(Arr::get($webhookData, 'data'));
}

$response = $this->client->post($url, $options);

if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
Expand Down
39 changes: 28 additions & 11 deletions src/WebhookMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class WebhookMessage
*/
protected $data;

/**
* The POST data of the Webhook request which is send as x-www-form-urlencoded.
*
* @var null|array
*/
protected $form;

/**
* The headers to send with the request.
*
Expand All @@ -40,7 +47,7 @@ class WebhookMessage
protected $verify = false;

/**
* @param mixed $data
* @param mixed $data
*
* @return static
*/
Expand All @@ -50,7 +57,7 @@ public static function create($data = '')
}

/**
* @param mixed $data
* @param mixed $data
*/
public function __construct($data = '')
{
Expand All @@ -60,8 +67,7 @@ public function __construct($data = '')
/**
* Set the Webhook parameters to be URL encoded.
*
* @param mixed $query
*
* @param mixed $query
* @return $this
*/
public function query($query)
Expand All @@ -74,8 +80,7 @@ public function query($query)
/**
* Set the Webhook data to be JSON encoded.
*
* @param mixed $data
*
* @param mixed $data
* @return $this
*/
public function data($data)
Expand All @@ -86,11 +91,23 @@ public function data($data)
}

/**
* Add a Webhook request custom header.
* Set the Webhook data to be send as x-www-form-urlencoded.
*
* @param string $name
* @param string $value
* @param null|array $form
* @return $this
*/
public function form($form)
{
$this->form = $form;

return $this;
}

/**
* Add a Webhook request custom header.
*
* @param string $name
* @param string $value
* @return $this
*/
public function header($name, $value)
Expand All @@ -103,8 +120,7 @@ public function header($name, $value)
/**
* Set the Webhook request UserAgent.
*
* @param string $userAgent
*
* @param string $userAgent
* @return $this
*/
public function userAgent($userAgent)
Expand Down Expand Up @@ -134,6 +150,7 @@ public function toArray()
return [
'query' => $this->query,
'data' => $this->data,
'form' => $this->form,
'headers' => $this->headers,
'verify' => $this->verify,
];
Expand Down
45 changes: 41 additions & 4 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function it_can_send_a_notification_with_query_string()
'query' => [
'webhook' => 'data',
],
'body' => '""',
'verify' => false,
'headers' => [
'User-Agent' => 'WebhookAgent',
Expand All @@ -88,6 +87,33 @@ public function it_can_send_a_notification_with_query_string()
$channel->send(new TestNotifiable(), new QueryTestNotification());
}

/** @test */
public function it_can_send_a_notification_as_form_urlencoded()
{
$response = new Response();
$client = \Mockery::mock(Client::class);
$client->shouldReceive('post')
->once()
->with(
'https://notifiable-webhook-url.com',
[
'query' => null,
'form_params' => [
'webhook' => 'data',
],
'verify' => false,
'headers' => [
'User-Agent' => 'WebhookAgent',
'X-Custom' => 'CustomHeader',
],
]
)
->andReturn($response);

$channel = new WebhookChannel($client);
$channel->send(new TestNotifiable(), new FormTestNotification());
}

/**
* @test
*/
Expand Down Expand Up @@ -143,8 +169,19 @@ public function toWebhook($notifiable)
{
return
(new WebhookMessage())
->query(['webhook' => 'data'])
->userAgent('WebhookAgent')
->header('X-Custom', 'CustomHeader');
->query(['webhook' => 'data'])
->userAgent('WebhookAgent')
->header('X-Custom', 'CustomHeader');
}
}

class FormTestNotification extends Notification
{
public function toWebhook($notifiable)
{
return (new WebhookMessage())
->form(['webhook' => 'data'])
->userAgent('WebhookAgent')
->header('X-Custom', 'CustomHeader');
}
}
7 changes: 7 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function it_can_set_the_webhook_query()
$this->assertEquals(['foo' => 'bar'], Arr::get($this->message->toArray(), 'query'));
}

/** @test */
public function it_can_set_the_webhook_form_params()
{
$this->message->form(['foo' => 'bar']);
$this->assertEquals(['foo' => 'bar'], Arr::get($this->message->toArray(), 'form'));
}

/** @test */
public function it_can_set_the_webhook_data()
{
Expand Down