1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
|
|
|
|
6
|
|
|
* This file is part of the AuthnetJSON package. |
7
|
|
|
* |
8
|
|
|
* (c) John Conde <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Authnetjson; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Handles a Webhook notification from the Authorize.Net Webhooks API |
18
|
|
|
* |
19
|
|
|
* @package AuthnetJSON |
20
|
|
|
* @author John Conde <[email protected]> |
21
|
|
|
* @copyright John Conde <[email protected]> |
22
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
23
|
|
|
* @link https://github.com/stymiee/authnetjson |
24
|
|
|
* @see https://developer.authorize.net/api/reference/ |
25
|
|
|
*/ |
26
|
|
|
class AuthnetWebhook |
27
|
|
|
{ |
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @var object SimpleXML object representing the Webhook notification |
30
|
|
|
*/ |
31
|
|
|
private $webhook; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* @var string JSON string that is the Webhook notification sent by Authorize.Net |
35
|
|
|
*/ |
36
|
|
|
private $webhookJson; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @var array HTTP headers sent with the notification |
40
|
|
|
*/ |
41
|
|
|
private $headers; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* @var string Authorize.Net Signature Key |
45
|
|
|
*/ |
46
|
|
|
private $signature; |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates the response object with the response json returned from the API call |
50
|
|
|
* |
51
|
|
|
* @param string $signature Authorize.Net Signature Key |
|
|
|
|
52
|
|
|
* @param string $payload Webhook Notification sent by Authorize.Net |
|
|
|
|
53
|
|
|
* @param array $headers HTTP headers sent with Webhook. Optional if PHP is run as an Apache module |
|
|
|
|
54
|
|
|
* @throws AuthnetInvalidCredentialsException |
|
|
|
|
55
|
|
|
* @throws AuthnetInvalidJsonException |
|
|
|
|
56
|
|
|
*/ |
57
|
3 |
|
public function __construct(string $signature, string $payload, array $headers = []) |
|
|
|
|
58
|
|
|
{ |
|
|
|
|
59
|
3 |
|
$this->signature = $signature; |
60
|
3 |
|
$this->webhookJson = $payload; |
61
|
3 |
|
$this->headers = $headers; |
62
|
3 |
|
if (empty($this->headers)) { |
63
|
2 |
|
$this->headers = $this->getAllHeaders(); |
64
|
|
|
} |
|
|
|
|
65
|
3 |
|
if (empty($this->signature)) { |
66
|
1 |
|
throw new AuthnetInvalidCredentialsException('You have not configured your signature properly.'); |
|
|
|
|
67
|
|
|
} |
|
|
|
|
68
|
2 |
|
if (($this->webhook = json_decode($this->webhookJson, false)) === null) { |
|
|
|
|
69
|
1 |
|
throw new AuthnetInvalidJsonException('Invalid JSON sent in the Webhook notification'); |
70
|
|
|
} |
|
|
|
|
71
|
1 |
|
$this->headers = array_change_key_case($this->headers, CASE_UPPER); |
72
|
1 |
|
} |
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Outputs the response JSON in a human readable format |
76
|
|
|
* |
77
|
|
|
* @return string HTML table containing debugging information |
78
|
|
|
*/ |
79
|
1 |
|
public function __toString() |
80
|
|
|
{ |
|
|
|
|
81
|
1 |
|
$output = '<table id="authnet-webhook">'."\n"; |
|
|
|
|
82
|
1 |
|
$output .= '<caption>Authorize.Net Webhook</caption>'."\n"; |
|
|
|
|
83
|
1 |
|
$output .= '<tr><th colspan="2"><b>Response HTTP Headers</b></th></tr>'."\n"; |
|
|
|
|
84
|
1 |
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
|
|
|
|
85
|
1 |
|
$output .= var_export($this->headers)."\n"; |
|
|
|
|
86
|
1 |
|
$output .= '</pre></td></tr>'."\n"; |
|
|
|
|
87
|
1 |
|
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>'."\n"; |
|
|
|
|
88
|
1 |
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
|
|
|
|
89
|
1 |
|
$output .= $this->webhookJson."\n"; |
|
|
|
|
90
|
1 |
|
$output .= '</pre></td></tr>'."\n"; |
|
|
|
|
91
|
1 |
|
$output .= '</table>'; |
92
|
|
|
|
93
|
1 |
|
return $output; |
94
|
|
|
} |
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets a response variable from the Webhook notification |
98
|
|
|
* |
99
|
|
|
* @param string $var |
|
|
|
|
100
|
|
|
* @return string requested variable from the API call response |
|
|
|
|
101
|
|
|
*/ |
102
|
1 |
|
public function __get($var) |
|
|
|
|
103
|
|
|
{ |
|
|
|
|
104
|
1 |
|
return $this->webhook->{$var}; |
105
|
|
|
} |
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Validates a webhook signature to determine if the webhook is valid |
109
|
|
|
* |
110
|
|
|
* @return bool |
|
|
|
|
111
|
|
|
*/ |
112
|
3 |
|
public function isValid(): bool |
113
|
|
|
{ |
|
|
|
|
114
|
3 |
|
$hashedBody = strtoupper(hash_hmac('sha512', $this->webhookJson, $this->signature)); |
115
|
3 |
|
return (isset($this->headers['X-ANET-SIGNATURE']) && |
|
|
|
|
116
|
3 |
|
strtoupper(explode('=', $this->headers['X-ANET-SIGNATURE'])[1]) === $hashedBody); |
117
|
|
|
} |
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Validates a webhook signature to determine if the webhook is valid |
121
|
|
|
* |
122
|
|
|
* @return string|null |
123
|
|
|
*/ |
124
|
1 |
|
public function getRequestId(): ?string |
125
|
|
|
{ |
|
|
|
|
126
|
1 |
|
return $this->headers['X-REQUEST-ID'] ?? null; |
|
|
|
|
127
|
|
|
} |
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieves all HTTP headers of a given request |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
1 |
|
protected function getAllHeaders(): array |
135
|
|
|
{ |
|
|
|
|
136
|
1 |
|
if (function_exists('apache_request_headers')) { |
137
|
|
|
$headers = apache_request_headers(); |
138
|
|
|
} else { |
139
|
1 |
|
$headers = []; |
140
|
1 |
|
foreach ($_SERVER as $key => $value) { |
141
|
1 |
|
if (strpos($key, 'HTTP_') === 0) { |
142
|
1 |
|
$headers[str_replace('_', '-', substr($key, 5))] = $value; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
|
|
|
|
146
|
1 |
|
return $headers; |
|
|
|
|
147
|
|
|
} |
|
|
|
|
148
|
|
|
} |
|
|
|
|
149
|
|
|
|