forked from arxpw/WebhookForwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
65 lines (49 loc) · 1.48 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use GoCardlessPro\Webhook as GoCardlessWebhook;
require __DIR__ . '/vendor/autoload.php';
require_once 'WebhookSender.php';
$request = Request::createFromGlobals();
$payload = $request->getContent();
$signature = $request->headers->get('Webhook-Signature');
if (empty($payload)) {
return new JsonResponse('No body provided', Response::HTTP_BAD_REQUEST);
}
if (!$signature) {
$response = new JsonResponse(['error' => 'invalid webhook supplied'], 401);
$response->send();
return;
}
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$webhook_secret = $_ENV['WEBHOOK_SECRET'];
if (!$webhook_secret) {
$response = new JsonResponse(['error' => 'webhook not configured'], 500);
$response->send();
return;
}
$valid_signature = GoCardlessWebhook::isSignatureValid(
$payload,
$signature,
$webhook_secret
);
if (!$valid_signature) {
$response = new JsonResponse(['error' => 'Invalid Token'], 498);
$response->send();
return;
}
$sender = new WebhookSender();
$options = [
'body' => $request->getContent(),
'headers' => [
'Webhook-Signature' => $signature
]
];
$receiver_urls = explode("\n", file_get_contents('.receivers'));
foreach ($receiver_urls as $receiver) {
$sender->sendTo($receiver, $options);
}
$response = new JsonResponse('', 204);
$response->send();