-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathPicqerWebhook.php
91 lines (70 loc) · 2.4 KB
/
PicqerWebhook.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Picqer\Api;
/**
* Picqer PHP Webhook helper
*
* @author Casper Bakker <[email protected]>
* @license http://creativecommons.org/licenses/MIT/ MIT
*/
class PicqerWebhook
{
protected int $idhook;
protected string $name;
protected string $event;
protected array $data;
protected string $event_triggered_at;
protected array $rawPayload;
public function __construct(array $webhookPayload)
{
$this->rawPayload = $webhookPayload;
$fieldsToParse = ['idhook', 'name', 'event', 'data', 'event_triggered_at'];
foreach ($fieldsToParse as $field) {
if (array_key_exists($field, $webhookPayload)) {
$this->$field = $webhookPayload[$field];
}
}
}
public static function retrieve(): PicqerWebhook
{
$webhookPayloadRaw = file_get_contents('php://input');
$webhookPayloadDecoded = json_decode($webhookPayloadRaw, true);
if ($webhookPayloadDecoded === false) {
throw new WebhookException('Could not decode webhook payload');
}
return new self($webhookPayloadDecoded);
}
public static function retrieveWithSecret($secret): PicqerWebhook
{
if (! isset($_SERVER) || ! array_key_exists('HTTP_X_PICQER_SIGNATURE', $_SERVER)) {
throw new WebhookSignatureMismatchException('Could not find signature header in webhook');
}
$webhookPayloadRaw = file_get_contents('php://input');
$signatureHeader = $_SERVER['HTTP_X_PICQER_SIGNATURE'];
$calculatedSignature = base64_encode(hash_hmac('sha256', $webhookPayloadRaw, $secret, true));
if (! hash_equals($calculatedSignature, $signatureHeader)) {
throw new WebhookSignatureMismatchException('Signatures do not match');
}
$webhookPayloadDecoded = json_decode($webhookPayloadRaw, true);
return new self($webhookPayloadDecoded);
}
public function getIdhook(): int
{
return $this->idhook;
}
public function getName(): string
{
return $this->name;
}
public function getEvent(): string
{
return $this->event;
}
public function getData(): array
{
return $this->data;
}
public function getEventTriggeredAt(): string
{
return $this->event_triggered_at;
}
}