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
|
|
|
* Adapter for 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 AuthnetWebhooksResponse |
27
|
|
|
{ |
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @var object SimpleXML object representing the API response |
30
|
|
|
*/ |
31
|
|
|
private $response; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* @var string JSON string that is the response sent by Authorize.Net |
35
|
|
|
*/ |
36
|
|
|
private $responseJson; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates the response object with the response json returned from the API call |
40
|
|
|
* |
41
|
|
|
* @param string $responseJson Response from Authorize.Net |
|
|
|
|
42
|
|
|
* @throws AuthnetInvalidJsonException |
|
|
|
|
43
|
|
|
*/ |
44
|
2 |
|
public function __construct(string $responseJson) |
|
|
|
|
45
|
|
|
{ |
|
|
|
|
46
|
2 |
|
$this->responseJson = $responseJson; |
47
|
2 |
|
if (($this->response = json_decode($this->responseJson, false)) === null) { |
|
|
|
|
48
|
1 |
|
throw new AuthnetInvalidJsonException('Invalid JSON returned by the API'); |
49
|
|
|
} |
50
|
1 |
|
} |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Outputs the response JSON in a human readable format |
54
|
|
|
* |
55
|
|
|
* @return string HTML table containing debugging information |
56
|
|
|
*/ |
57
|
1 |
|
public function __toString() |
58
|
|
|
{ |
|
|
|
|
59
|
1 |
|
$output = '<table id="authnet-response">'."\n"; |
|
|
|
|
60
|
1 |
|
$output .= '<caption>Authorize.Net Webhook Response</caption>'."\n"; |
|
|
|
|
61
|
1 |
|
$output .= '<tr><th colspan="2"><b>Webhook Response JSON</b></th></tr>'."\n"; |
|
|
|
|
62
|
1 |
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
|
|
|
|
63
|
1 |
|
$output .= $this->responseJson."\n"; |
|
|
|
|
64
|
1 |
|
$output .= '</pre></td></tr>'."\n"; |
|
|
|
|
65
|
1 |
|
$output .= '</table>'; |
66
|
|
|
|
67
|
1 |
|
return $output; |
68
|
|
|
} |
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Gets a response variable from the API response |
72
|
|
|
* |
73
|
|
|
* net.authorize.customer.created |
|
|
|
|
74
|
|
|
* net.authorize.customer.deleted |
75
|
|
|
* net.authorize.customer.updated |
76
|
|
|
* net.authorize.customer.paymentProfile.created |
77
|
|
|
* net.authorize.customer.paymentProfile.deleted |
78
|
|
|
* net.authorize.customer.paymentProfile.updated |
79
|
|
|
* net.authorize.customer.subscription.cancelled |
80
|
|
|
* net.authorize.customer.subscription.created |
81
|
|
|
* net.authorize.customer.subscription.expiring |
82
|
|
|
* net.authorize.customer.subscription.suspended |
83
|
|
|
* net.authorize.customer.subscription.terminated |
84
|
|
|
* net.authorize.customer.subscription.updated |
85
|
|
|
* net.authorize.payment.authcapture.created |
86
|
|
|
* net.authorize.payment.authorization.created |
87
|
|
|
* net.authorize.payment.capture.created |
88
|
|
|
* net.authorize.payment.fraud.approved |
89
|
|
|
* net.authorize.payment.fraud.declined |
90
|
|
|
* net.authorize.payment.fraud.held |
91
|
|
|
* net.authorize.payment.priorAuthCapture.created |
92
|
|
|
* net.authorize.payment.refund.created |
93
|
|
|
* net.authorize.payment.void.created |
94
|
|
|
* |
95
|
|
|
* @return array Array of event types supported by Webhooks API |
96
|
|
|
*/ |
97
|
2 |
|
public function getEventTypes(): array |
98
|
|
|
{ |
|
|
|
|
99
|
2 |
|
$events = []; |
100
|
2 |
|
if (isset($this->response->eventTypes)) { |
101
|
1 |
|
foreach ($this->response->eventTypes as $event) { |
102
|
1 |
|
$events[] = $event; |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
1 |
|
$events = array_column($this->response, 'name'); |
|
|
|
|
106
|
|
|
} |
|
|
|
|
107
|
2 |
|
return $events; |
108
|
|
|
} |
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets the webhooks ID |
112
|
|
|
* |
113
|
|
|
* @return string Webhooks ID |
114
|
|
|
*/ |
115
|
1 |
|
public function getWebhooksId(): string |
116
|
|
|
{ |
|
|
|
|
117
|
1 |
|
return $this->response->webhookId; |
118
|
|
|
} |
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets the status of the Webhooks |
122
|
|
|
* |
123
|
|
|
* @return string Status of the webhooks [active|inactive] |
124
|
|
|
*/ |
125
|
1 |
|
public function getStatus(): string |
126
|
|
|
{ |
|
|
|
|
127
|
1 |
|
return $this->response->status; |
128
|
|
|
} |
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Gets the URL the Webhooks API will use for these Webhooks |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
1 |
|
public function getUrl(): string |
136
|
|
|
{ |
|
|
|
|
137
|
1 |
|
return $this->response->url; |
138
|
|
|
} |
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Gets a list of webhooks |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
* @throws AuthnetInvalidJsonException |
|
|
|
|
145
|
|
|
*/ |
146
|
1 |
|
public function getWebhooks(): array |
147
|
|
|
{ |
|
|
|
|
148
|
1 |
|
$webhooks = []; |
149
|
1 |
|
foreach ($this->response as $webhook) { |
150
|
1 |
|
$webhooks[] = new AuthnetWebhooksResponse(json_encode($webhook)); |
151
|
|
|
} |
|
|
|
|
152
|
1 |
|
return $webhooks; |
153
|
|
|
} |
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Gets a list of webhooks |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
* @throws AuthnetInvalidJsonException |
|
|
|
|
160
|
|
|
*/ |
161
|
1 |
|
public function getNotificationHistory(): array |
162
|
|
|
{ |
|
|
|
|
163
|
1 |
|
$notifications = []; |
164
|
1 |
|
if (count($this->response->notifications)) { |
165
|
1 |
|
foreach ($this->response->notifications as $notification) { |
166
|
1 |
|
$notifications[] = new AuthnetWebhooksResponse(json_encode($notification)); |
167
|
|
|
} |
168
|
|
|
} |
|
|
|
|
169
|
1 |
|
return $notifications; |
170
|
|
|
} |
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Gets the notification ID of a notification |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
1 |
|
public function getNotificationId(): string |
178
|
|
|
{ |
|
|
|
|
179
|
1 |
|
return $this->response->notificationId; |
180
|
|
|
} |
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Gets the delivery status of a notification |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
1 |
|
public function getDeliveryStatus(): string |
188
|
|
|
{ |
|
|
|
|
189
|
1 |
|
return $this->response->deliveryStatus; |
190
|
|
|
} |
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Gets the event type of a notification |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
1 |
|
public function getEventType(): string |
198
|
|
|
{ |
|
|
|
|
199
|
1 |
|
return $this->response->eventType; |
200
|
|
|
} |
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Gets the event date of a notification |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
1 |
|
public function getEventDate(): string |
208
|
|
|
{ |
|
|
|
|
209
|
1 |
|
return $this->response->eventDate; |
210
|
|
|
} |
|
|
|
|
211
|
|
|
} |
|
|
|
|
212
|
|
|
|