|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the AuthnetJSON package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) John Conde <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace JohnConde\Authnet; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Adapter for the Authorize.Net Webhooks API |
|
16
|
|
|
* |
|
17
|
|
|
* @package AuthnetJSON |
|
18
|
|
|
* @author John Conde <[email protected]> |
|
19
|
|
|
* @copyright John Conde <[email protected]> |
|
20
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0 |
|
21
|
|
|
* @link https://github.com/stymiee/authnetjson |
|
22
|
|
|
* @see https://developer.authorize.net/api/reference/ |
|
23
|
|
|
* |
|
24
|
|
|
* @property object $messages |
|
25
|
|
|
* @property string $directResponse |
|
26
|
|
|
* @property string $validationDirectResponse |
|
27
|
|
|
* @property object $transactionResponse |
|
28
|
|
|
*/ |
|
29
|
|
|
class AuthnetWebhooksResponse |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var object SimpleXML object representing the API response |
|
33
|
|
|
*/ |
|
34
|
|
|
private $response; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string JSON string that is the response sent by Authorize.Net |
|
38
|
|
|
*/ |
|
39
|
|
|
private $responseJson; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates the response object with the response json returned from the API call |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $responseJson Response from Authorize.Net |
|
45
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($responseJson) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->responseJson = $responseJson; |
|
50
|
|
|
if (($this->response = json_decode($this->responseJson)) === null) { |
|
51
|
|
|
throw new AuthnetInvalidJsonException('Invalid JSON returned by the API'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Outputs the response JSON in a human readable format |
|
57
|
|
|
* |
|
58
|
|
|
* @return string HTML table containing debugging information |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function __toString() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$output = ''; |
|
63
|
|
|
$output .= '<table summary="Authorize.Net Webhooks Response" id="authnet-response">' . "\n"; |
|
64
|
|
|
$output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Response JSON</b></th>' . "\n" . '</tr>' . "\n"; |
|
65
|
|
|
$output .= '<tr><td colspan="2"><pre>' . "\n"; |
|
66
|
|
|
$output .= $this->responseJson . "\n"; |
|
67
|
|
|
$output .= '</pre></td></tr>' . "\n"; |
|
68
|
|
|
$output .= '</table>'; |
|
69
|
|
|
|
|
70
|
|
|
return $output; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Gets a response variable from the API response |
|
75
|
|
|
* |
|
76
|
|
|
* net.authorize.customer.created |
|
77
|
|
|
* net.authorize.customer.deleted |
|
78
|
|
|
* net.authorize.customer.updated |
|
79
|
|
|
* net.authorize.customer.paymentProfile.created |
|
80
|
|
|
* net.authorize.customer.paymentProfile.deleted |
|
81
|
|
|
* net.authorize.customer.paymentProfile.updated |
|
82
|
|
|
* net.authorize.customer.subscription.cancelled |
|
83
|
|
|
* net.authorize.customer.subscription.created |
|
84
|
|
|
* net.authorize.customer.subscription.expiring |
|
85
|
|
|
* net.authorize.customer.subscription.suspended |
|
86
|
|
|
* net.authorize.customer.subscription.terminated |
|
87
|
|
|
* net.authorize.customer.subscription.updated |
|
88
|
|
|
* net.authorize.payment.authcapture.created |
|
89
|
|
|
* net.authorize.payment.authorization.created |
|
90
|
|
|
* net.authorize.payment.capture.created |
|
91
|
|
|
* net.authorize.payment.fraud.approved |
|
92
|
|
|
* net.authorize.payment.fraud.declined |
|
93
|
|
|
* net.authorize.payment.fraud.held |
|
94
|
|
|
* net.authorize.payment.priorAuthCapture.created |
|
95
|
|
|
* net.authorize.payment.refund.created |
|
96
|
|
|
* net.authorize.payment.void.created |
|
97
|
|
|
* |
|
98
|
|
|
* @return array Array of event types supported by Webhooks API |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getEventTypes() |
|
101
|
|
|
{ |
|
102
|
|
|
$events = []; |
|
103
|
|
|
if (isset($this->response->eventTypes)) { |
|
104
|
|
|
foreach ($this->response->eventTypes as $event) { |
|
105
|
|
|
$events[] = $event; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
else { |
|
109
|
|
|
foreach ($this->response as $event) { |
|
110
|
|
|
$events[] = $event->name; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
return $events; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Gets the webhooks ID |
|
118
|
|
|
* |
|
119
|
|
|
* @return string Webhooks ID |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getWebhooksId() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->response->webhookId; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Gets the status of the Webhooks |
|
128
|
|
|
* |
|
129
|
|
|
* @return string Staus of the webhooks [active|inactive] |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getStatus() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->response->status; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Gets the URL the Webhooks API will use for these Webhooks |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getUrl() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->response->url; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Gets a list of webhooks |
|
148
|
|
|
* |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getWebhooks() |
|
152
|
|
|
{ |
|
153
|
|
|
$webhooks = []; |
|
154
|
|
|
foreach ($this->response as $webhook) { |
|
155
|
|
|
$webhooks[] = new AuthnetWebhooksResponse(json_encode($webhook)); |
|
156
|
|
|
} |
|
157
|
|
|
return $webhooks; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Gets a list of webhooks |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getNotificationHistory() |
|
166
|
|
|
{ |
|
167
|
|
|
$notifications = []; |
|
168
|
|
|
if (count($this->response)) { |
|
169
|
|
|
foreach ($this->response->notifications as $notification) { |
|
170
|
|
|
$notifications[] = new AuthnetWebhooksResponse(json_encode($notification)); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
return $notifications; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Gets the notification ID of a notification |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getNotificationId() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->response->notificationId; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Gets the delivery status of a notification |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getDeliveryStatus() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->response->deliveryStatus; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Gets the event type of a notification |
|
198
|
|
|
* |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getEventType() |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->response->eventType; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Gets the event date of a notification |
|
208
|
|
|
* |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getEventDate() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->response->eventDate; |
|
214
|
|
|
} |
|
215
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.