|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\WebhookServer; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use Illuminate\Bus\Queueable; |
|
8
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
9
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
10
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
11
|
|
|
use Illuminate\Queue\SerializesModels; |
|
12
|
|
|
use Illuminate\Support\Str; |
|
13
|
|
|
use Spatie\WebhookServer\Events\FinalWebhookCallFailedEvent; |
|
14
|
|
|
use Spatie\WebhookServer\Events\WebhookCallFailedEvent; |
|
15
|
|
|
use Spatie\WebhookServer\Events\WebhookCallSucceededEvent; |
|
16
|
|
|
|
|
17
|
|
|
class CallWebhookJob implements ShouldQueue |
|
18
|
|
|
{ |
|
19
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
public $webhookUrl; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
public $httpVerb; |
|
26
|
|
|
|
|
27
|
|
|
/** @var int */ |
|
28
|
|
|
public $tries; |
|
29
|
|
|
|
|
30
|
|
|
/** @var int */ |
|
31
|
|
|
public $requestTimeout; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
public $backoffStrategyClass; |
|
35
|
|
|
|
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
public $signerClass; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array */ |
|
40
|
|
|
public $headers = []; |
|
41
|
|
|
|
|
42
|
|
|
/** @var bool */ |
|
43
|
|
|
public $verifySsl; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string */ |
|
46
|
|
|
public $queue; |
|
47
|
|
|
|
|
48
|
|
|
/** @var array */ |
|
49
|
|
|
public $payload = []; |
|
50
|
|
|
|
|
51
|
|
|
/** @var array */ |
|
52
|
|
|
public $meta = []; |
|
53
|
|
|
|
|
54
|
|
|
/** @var array */ |
|
55
|
|
|
public $tags = []; |
|
56
|
|
|
|
|
57
|
|
|
/** @var \GuzzleHttp\Psr7\Response|null */ |
|
58
|
|
|
private $response; |
|
59
|
|
|
|
|
60
|
|
|
/** @var int|null */ |
|
61
|
|
|
private $responseStatusCode; |
|
62
|
|
|
|
|
63
|
|
|
/** @var string */ |
|
64
|
|
|
private $responseBody; |
|
65
|
|
|
|
|
66
|
|
|
public function handle() |
|
67
|
|
|
{ |
|
68
|
|
|
/** @var \GuzzleHttp\Client $client */ |
|
69
|
|
|
$client = app(Client::class); |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
$this->response = $client->request($this->httpVerb, $this->webhookUrl, [ |
|
|
|
|
|
|
73
|
|
|
'timeout' => $this->requestTimeout, |
|
74
|
|
|
'body' => json_encode($this->payload), |
|
75
|
|
|
'verify' => $this->verifySsl, |
|
76
|
|
|
'headers' => $this->headers, |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
|
|
$this->responseStatusCode = $this->response->getStatusCode(); |
|
80
|
|
|
$this->responseBody = $this->response->getBody()->getContents(); |
|
81
|
|
|
|
|
82
|
|
|
if (!Str::startsWith($this->responseStatusCode, 2)) { |
|
83
|
|
|
throw new Exception('Webhook call failed'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->dispatchEvent(WebhookCallSucceededEvent::class); |
|
87
|
|
|
|
|
88
|
|
|
return; |
|
89
|
|
|
} catch (Exception $exception) { |
|
90
|
|
|
/** @var \Spatie\WebhookServer\BackoffStrategy\BackoffStrategy $backoffStrategy */ |
|
91
|
|
|
$backoffStrategy = app($this->backoffStrategyClass); |
|
92
|
|
|
|
|
93
|
|
|
$waitInSeconds = $backoffStrategy->waitInSecondsAfterAttempt($this->attempts()); |
|
94
|
|
|
|
|
95
|
|
|
$this->dispatchEvent(WebhookCallFailedEvent::class); |
|
96
|
|
|
|
|
97
|
|
|
$this->release($waitInSeconds); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($this->attempts() >= $this->tries) { |
|
101
|
|
|
$this->dispatchEvent(FinalWebhookCallFailedEvent::class); |
|
102
|
|
|
|
|
103
|
|
|
$this->delete(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function dispatchEvent(string $eventClass) |
|
108
|
|
|
{ |
|
109
|
|
|
event(new $eventClass( |
|
110
|
|
|
$this->httpVerb, |
|
111
|
|
|
$this->webhookUrl, |
|
112
|
|
|
$this->payload, |
|
113
|
|
|
$this->headers, |
|
114
|
|
|
$this->meta, |
|
115
|
|
|
$this->tags, |
|
116
|
|
|
$this->attempts(), |
|
117
|
|
|
$this->response, |
|
118
|
|
|
$this->responseStatusCode, |
|
119
|
|
|
$this->responseBody |
|
120
|
|
|
)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function tags() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->tags; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..