diff --git a/README.md b/README.md index 3fd40a3..d637510 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/WebhookChannel.php b/src/WebhookChannel.php index 3785ccd..69047eb 100644 --- a/src/WebhookChannel.php +++ b/src/WebhookChannel.php @@ -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 * @@ -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); diff --git a/src/WebhookMessage.php b/src/WebhookMessage.php index 63c3367..2491cfd 100644 --- a/src/WebhookMessage.php +++ b/src/WebhookMessage.php @@ -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. * @@ -40,7 +47,7 @@ class WebhookMessage protected $verify = false; /** - * @param mixed $data + * @param mixed $data * * @return static */ @@ -50,7 +57,7 @@ public static function create($data = '') } /** - * @param mixed $data + * @param mixed $data */ public function __construct($data = '') { @@ -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) @@ -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) @@ -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) @@ -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) @@ -134,6 +150,7 @@ public function toArray() return [ 'query' => $this->query, 'data' => $this->data, + 'form' => $this->form, 'headers' => $this->headers, 'verify' => $this->verify, ]; diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 6e57427..99ace21 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -74,7 +74,6 @@ public function it_can_send_a_notification_with_query_string() 'query' => [ 'webhook' => 'data', ], - 'body' => '""', 'verify' => false, 'headers' => [ 'User-Agent' => 'WebhookAgent', @@ -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 */ @@ -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'); } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 3e5a800..ba6944c 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -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() {