Skip to content

Commit 478fffa

Browse files
committed
apply new webhook option sendRawBody(string $body) in order to allow people to send non-array formats as raw body strings (xml, plain text, etc.)
Cannot be combined with signing since the Signer function expects an array, so use `doNotSign()` with this functionality.
1 parent 58ad35c commit 478fffa

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/CallWebhookJob.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ class CallWebhookJob implements ShouldQueue
4646
/** @var string|null */
4747
public $queue = null;
4848

49-
public array $payload = [];
49+
public array|string $payload = [];
5050

5151
public array $meta = [];
5252

5353
public array $tags = [];
5454

5555
public string $uuid = '';
5656

57+
public string $outputType = "JSON";
58+
5759
protected ?Response $response = null;
5860

5961
protected ?string $errorType = null;
@@ -69,7 +71,7 @@ public function handle()
6971
try {
7072
$body = strtoupper($this->httpVerb) === 'GET'
7173
? ['query' => $this->payload]
72-
: ['body' => json_encode($this->payload)];
74+
: ['body' => $this->generateBody()];
7375

7476
$this->response = $this->createRequest($body);
7577

@@ -162,4 +164,12 @@ private function dispatchEvent(string $eventClass)
162164
$this->transferStats
163165
));
164166
}
167+
168+
private function generateBody(): string
169+
{
170+
return match ($this->outputType) {
171+
"RAW" => $this->payload,
172+
default => json_encode($this->payload),
173+
};
174+
}
165175
}

src/WebhookCall.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class WebhookCall
2525

2626
private array $payload = [];
2727

28+
private string $rawBody;
2829
private $signWebhook = true;
2930

3031
public static function create(): self
@@ -255,6 +256,14 @@ public function dispatchSyncUnless($condition): void
255256
$this->dispatchSyncIf(! $condition);
256257
}
257258

259+
public function sendRawBody(string $body): self
260+
{
261+
$this->callWebhookJob->payload = $body;
262+
$this->callWebhookJob->outputType = "RAW";
263+
264+
return $this;
265+
}
266+
258267
protected function prepareForDispatch(): void
259268
{
260269
if (! $this->callWebhookJob->webhookUrl) {

tests/CallWebhookJobTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,24 @@ function baseGetRequest(array $overrides = []): array
300300
return true;
301301
});
302302
});
303+
304+
it('send raw body data if rawBody is set', function () {
305+
$testBody = "<xml>anotherOption</xml>";
306+
WebhookCall::create()
307+
->url('https://example.com/webhooks')
308+
->useSecret('abc')
309+
->sendRawBody($testBody)
310+
->doNotSign()
311+
->dispatch();
312+
313+
$baseRequest = baseRequest();
314+
315+
$baseRequest['options']['body'] = $testBody;
316+
unset($baseRequest['options']['headers']['Signature']);
317+
318+
artisan('queue:work --once');
319+
320+
$this
321+
->testClient
322+
->assertRequestsMade([$baseRequest]);
323+
});

0 commit comments

Comments
 (0)