|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2013-2016 Mailgun |
|
5
|
|
|
* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Mailgun\Api; |
|
11
|
|
|
|
|
12
|
|
|
use Http\Client\HttpClient; |
|
13
|
|
|
use Mailgun\Assert; |
|
14
|
|
|
use Mailgun\Hydrator\Hydrator; |
|
15
|
|
|
use Mailgun\Model\Webhook\CreateResponse; |
|
16
|
|
|
use Mailgun\Model\Webhook\DeleteResponse; |
|
17
|
|
|
use Mailgun\Model\Webhook\IndexResponse; |
|
18
|
|
|
use Mailgun\Model\Webhook\ShowResponse; |
|
19
|
|
|
use Mailgun\Model\Webhook\UpdateResponse; |
|
20
|
|
|
use Mailgun\RequestBuilder; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Webhook extends HttpApi |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $apiKey; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param HttpClient $httpClient |
|
34
|
|
|
* @param RequestBuilder $requestBuilder |
|
35
|
|
|
* @param Hydrator $hydrator |
|
36
|
|
|
* @param string $apiKey |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, $apiKey) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct($httpClient, $requestBuilder, $hydrator); |
|
41
|
|
|
$this->apiKey = $apiKey; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* This function verifies the webhook signature with your API key to to see if it is authentic. |
|
46
|
|
|
* |
|
47
|
|
|
* If this function returns FALSE, you must not process the request. |
|
48
|
|
|
* You should reject the request with status code 403 Forbidden. |
|
49
|
|
|
* |
|
50
|
|
|
* @param int $timestamp |
|
51
|
|
|
* @param string $token |
|
52
|
|
|
* @param string $signature |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function verifyWebhookSignature($timestamp, $token, $signature) |
|
57
|
|
|
{ |
|
58
|
|
|
if (empty($timestamp) || empty($token) || empty($signature)) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$hmac = hash_hmac('sha256', $timestamp.$token, $this->apiKey); |
|
63
|
|
|
|
|
64
|
|
|
if (function_exists('hash_equals')) { |
|
65
|
|
|
// hash_equals is constant time, but will not be introduced until PHP 5.6 |
|
66
|
|
|
return hash_equals($hmac, $signature); |
|
67
|
|
|
} else { |
|
68
|
|
|
return $hmac === $signature; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $domain |
|
74
|
|
|
* |
|
75
|
|
|
* @return IndexResponse |
|
76
|
|
|
*/ |
|
77
|
|
|
public function index($domain) |
|
78
|
|
|
{ |
|
79
|
|
|
Assert::notEmpty($domain); |
|
80
|
|
|
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks', $domain)); |
|
81
|
|
|
|
|
82
|
|
|
return $this->hydrateResponse($response, IndexResponse::class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $domain |
|
87
|
|
|
* @param string $webhook |
|
88
|
|
|
* |
|
89
|
|
|
* @return ShowResponse |
|
90
|
|
|
*/ |
|
91
|
|
|
public function show($domain, $webhook) |
|
92
|
|
|
{ |
|
93
|
|
|
Assert::notEmpty($domain); |
|
94
|
|
|
Assert::notEmpty($webhook); |
|
95
|
|
|
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks/%s', $domain, $webhook)); |
|
96
|
|
|
|
|
97
|
|
|
return $this->hydrateResponse($response, ShowResponse::class); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $domain |
|
102
|
|
|
* @param string $id |
|
103
|
|
|
* @param string $url |
|
104
|
|
|
* |
|
105
|
|
|
* @return CreateResponse |
|
106
|
|
|
*/ |
|
107
|
|
|
public function create($domain, $id, $url) |
|
108
|
|
|
{ |
|
109
|
|
|
Assert::notEmpty($domain); |
|
110
|
|
|
Assert::notEmpty($id); |
|
111
|
|
|
Assert::notEmpty($url); |
|
112
|
|
|
|
|
113
|
|
|
$params = [ |
|
114
|
|
|
'id' => $id, |
|
115
|
|
|
'url' => $url, |
|
116
|
|
|
]; |
|
117
|
|
|
|
|
118
|
|
|
$response = $this->httpPost(sprintf('/v3/domains/%s/webhooks', $domain), $params); |
|
119
|
|
|
|
|
120
|
|
|
return $this->hydrateResponse($response, CreateResponse::class); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $domain |
|
125
|
|
|
* @param string $id |
|
126
|
|
|
* @param string $url |
|
127
|
|
|
* |
|
128
|
|
|
* @return UpdateResponse |
|
129
|
|
|
*/ |
|
130
|
|
|
public function update($domain, $id, $url) |
|
131
|
|
|
{ |
|
132
|
|
|
Assert::notEmpty($domain); |
|
133
|
|
|
Assert::notEmpty($id); |
|
134
|
|
|
Assert::notEmpty($url); |
|
135
|
|
|
|
|
136
|
|
|
$params = [ |
|
137
|
|
|
'url' => $url, |
|
138
|
|
|
]; |
|
139
|
|
|
|
|
140
|
|
|
$response = $this->httpPut(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id), $params); |
|
141
|
|
|
|
|
142
|
|
|
return $this->hydrateResponse($response, UpdateResponse::class); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $domain |
|
147
|
|
|
* @param string $id |
|
148
|
|
|
* |
|
149
|
|
|
* @return DeleteResponse |
|
150
|
|
|
*/ |
|
151
|
|
|
public function delete($domain, $id) |
|
152
|
|
|
{ |
|
153
|
|
|
Assert::notEmpty($domain); |
|
154
|
|
|
Assert::notEmpty($id); |
|
155
|
|
|
|
|
156
|
|
|
$response = $this->httpDelete(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id)); |
|
157
|
|
|
|
|
158
|
|
|
return $this->hydrateResponse($response, DeleteResponse::class); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|