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