1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\WebhookServer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Spatie\WebhookServer\BackoffStrategy\BackoffStrategy; |
7
|
|
|
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook; |
8
|
|
|
use Spatie\WebhookServer\Exceptions\InvalidBackoffStrategy; |
9
|
|
|
use Spatie\WebhookServer\Exceptions\InvalidSigner; |
10
|
|
|
use Spatie\WebhookServer\Signer\Signer; |
11
|
|
|
|
12
|
|
|
class WebhookCall |
13
|
|
|
{ |
14
|
|
|
protected CallWebhookJob $callWebhookJob; |
15
|
|
|
|
16
|
|
|
protected string $uuid = ''; |
17
|
|
|
|
18
|
|
|
protected string $secret; |
19
|
|
|
|
20
|
|
|
protected Signer $signer; |
21
|
|
|
|
22
|
|
|
protected array $headers = []; |
23
|
|
|
|
24
|
|
|
private array $payload = []; |
25
|
|
|
|
26
|
|
|
private $signWebhook = true; |
27
|
|
|
|
28
|
|
|
public static function create(): self |
29
|
|
|
{ |
30
|
|
|
$config = config('webhook-server'); |
31
|
|
|
|
32
|
|
|
return (new static()) |
33
|
|
|
->uuid(Str::uuid()) |
34
|
|
|
->onQueue($config['queue']) |
35
|
|
|
->useHttpVerb($config['http_verb']) |
36
|
|
|
->maximumTries($config['tries']) |
37
|
|
|
->useBackoffStrategy($config['backoff_strategy']) |
38
|
|
|
->timeoutInSeconds($config['timeout_in_seconds']) |
39
|
|
|
->signUsing($config['signer']) |
40
|
|
|
->withHeaders($config['headers']) |
41
|
|
|
->withTags($config['tags']) |
42
|
|
|
->verifySsl($config['verify_ssl']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$this->callWebhookJob = app(CallWebhookJob::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function url(string $url): self |
51
|
|
|
{ |
52
|
|
|
$this->callWebhookJob->webhookUrl = $url; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function payload(array $payload): self |
58
|
|
|
{ |
59
|
|
|
$this->payload = $payload; |
60
|
|
|
|
61
|
|
|
$this->callWebhookJob->payload = $payload; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function uuid(string $uuid): self |
67
|
|
|
{ |
68
|
|
|
$this->uuid = $uuid; |
69
|
|
|
|
70
|
|
|
$this->callWebhookJob->uuid = $uuid; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function onQueue(string $queue): self |
76
|
|
|
{ |
77
|
|
|
$this->callWebhookJob->queue = $queue; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function useSecret(string $secret): self |
83
|
|
|
{ |
84
|
|
|
$this->secret = $secret; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function useHttpVerb(string $verb): self |
90
|
|
|
{ |
91
|
|
|
$this->callWebhookJob->httpVerb = $verb; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function maximumTries(int $tries): self |
97
|
|
|
{ |
98
|
|
|
$this->callWebhookJob->tries = $tries; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function useBackoffStrategy(string $backoffStrategyClass): self |
104
|
|
|
{ |
105
|
|
|
if (! is_subclass_of($backoffStrategyClass, BackoffStrategy::class)) { |
106
|
|
|
throw InvalidBackoffStrategy::doesNotExtendBackoffStrategy($backoffStrategyClass); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->callWebhookJob->backoffStrategyClass = $backoffStrategyClass; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function timeoutInSeconds(int $timeoutInSeconds): self |
115
|
|
|
{ |
116
|
|
|
$this->callWebhookJob->requestTimeout = $timeoutInSeconds; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function signUsing(string $signerClass): self |
122
|
|
|
{ |
123
|
|
|
if (! is_subclass_of($signerClass, Signer::class)) { |
124
|
|
|
throw InvalidSigner::doesImplementSigner($signerClass); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->signer = app($signerClass); |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function doNotSign(): self |
133
|
|
|
{ |
134
|
|
|
$this->signWebhook = false; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function withHeaders(array $headers): self |
140
|
|
|
{ |
141
|
|
|
$this->headers = $headers; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function verifySsl(bool $verifySsl = true): self |
147
|
|
|
{ |
148
|
|
|
$this->callWebhookJob->verifySsl = $verifySsl; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function doNotVerifySsl(): self |
154
|
|
|
{ |
155
|
|
|
$this->verifySsl(false); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function meta(array $meta): self |
161
|
|
|
{ |
162
|
|
|
$this->callWebhookJob->meta = $meta; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function withTags(array $tags): self |
168
|
|
|
{ |
169
|
|
|
$this->callWebhookJob->tags = $tags; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function dispatch(): void |
175
|
|
|
{ |
176
|
|
|
$this->prepareForDispatch(); |
177
|
|
|
|
178
|
|
|
dispatch($this->callWebhookJob); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function dispatchNow(): void |
182
|
|
|
{ |
183
|
|
|
$this->prepareForDispatch(); |
184
|
|
|
|
185
|
|
|
dispatch_now($this->callWebhookJob); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function prepareForDispatch(): void |
189
|
|
|
{ |
190
|
|
|
if (! $this->callWebhookJob->webhookUrl) { |
191
|
|
|
throw CouldNotCallWebhook::urlNotSet(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($this->signWebhook && empty($this->secret)) { |
195
|
|
|
throw CouldNotCallWebhook::secretNotSet(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$this->callWebhookJob->headers = $this->getAllHeaders(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function getAllHeaders(): array |
202
|
|
|
{ |
203
|
|
|
$headers = $this->headers; |
204
|
|
|
|
205
|
|
|
if (! $this->signWebhook) { |
206
|
|
|
return $headers; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$signature = $this->signer->calculateSignature($this->payload, $this->secret); |
210
|
|
|
|
211
|
|
|
$headers[$this->signer->signatureHeaderName()] = $signature; |
212
|
|
|
|
213
|
|
|
return $headers; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|