1 | <?php |
||
2 | |||
3 | namespace GoCardlessPayment\Http\Controllers; |
||
4 | |||
5 | use GoCardlessPayment\Events\GoCardlessWebhookEventReceived; |
||
6 | use GoCardlessPayment\GoCardlessPayment; |
||
7 | use GoCardlessPro\Resources\Event; |
||
8 | use Illuminate\Http\Request; |
||
9 | use Illuminate\Support\Facades\Log; |
||
10 | use Symfony\Component\HttpFoundation\Response; |
||
11 | |||
12 | class WebhookController |
||
13 | { |
||
14 | /** |
||
15 | * @see https://developer.gocardless.com/getting-started/staying-up-to-date-with-webhooks/#processing_events |
||
16 | */ |
||
17 | 6 | public function handleWebhook(Request $request) |
|
18 | { |
||
19 | 6 | $requestSignature = $request->header('Webhook-Signature'); |
|
20 | |||
21 | try { |
||
22 | /** @var Event[] $events */ |
||
23 | /** @psalm-suppress UndefinedDocblockClass */ |
||
24 | 6 | $events = \GoCardlessPro\Webhook::parse( |
|
25 | 6 | $request->getContent(), |
|
26 | 6 | $requestSignature, |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
27 | 6 | config('gocardless-payment.web.webhook_endpoint_secret') |
|
28 | 6 | ); |
|
29 | 1 | } catch (\Exception $e) { |
|
30 | Log::error($e->getMessage()); |
||
31 | |||
32 | return new Response($e->getMessage(), 500); |
||
33 | } |
||
34 | |||
35 | 5 | foreach ($events as $event) { |
|
36 | 5 | $this->handleEvent($event); |
|
37 | } |
||
38 | |||
39 | 4 | return new Response; |
|
40 | } |
||
41 | |||
42 | 5 | protected function handleEvent(Event $event): void |
|
43 | { |
||
44 | 5 | GoCardlessWebhookEventReceived::dispatch($event); |
|
45 | |||
46 | 5 | $jobClass = GoCardlessPayment::getWebhookJob("{$event->resource_type}.{$event->action}"); |
|
47 | 5 | if ($jobClass) { |
|
48 | 5 | $jobClass::dispatch($event)->onQueue(config('gocardless-payment.queue')); |
|
49 | } |
||
50 | } |
||
51 | } |
||
52 |