|
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
|
|
|
* Creates a request to the Authorize.Net Webhooks endpoints |
|
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/features/webhooks.html |
|
23
|
|
|
*/ |
|
24
|
|
|
class AuthnetWebhooksRequest |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string Base URL for processing a webhook |
|
28
|
|
|
*/ |
|
29
|
|
|
private $url; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string Endpoint for processing a webhook |
|
33
|
|
|
*/ |
|
34
|
|
|
private $endpoint; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string JSON formatted API request |
|
38
|
|
|
*/ |
|
39
|
|
|
private $requestJson; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var object Wrapper object representing an endpoint |
|
43
|
|
|
*/ |
|
44
|
|
|
private $processor; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Creates the request object by setting the Authorize.Net credentials and URL of the endpoint to be used |
|
48
|
|
|
* for the API call |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $api_url URL endpoint for processing a transaction |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($api_url) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->url = $api_url; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Outputs the account credentials, endpoint URL, and request JSON in a human readable format |
|
59
|
|
|
* |
|
60
|
|
|
* @return string HTML table containing debugging information |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __toString() |
|
63
|
|
|
{ |
|
64
|
|
|
$output = '<table summary="Authorize.Net Request" id="authnet-request">'."\n"; |
|
65
|
|
|
$output .= '<tr>'."\n\t\t".'<th colspan="2"><b>Class Parameters</b></th>'."\n".'</tr>'."\n"; |
|
66
|
|
|
$output .= '<tr>'."\n\t\t".'<td><b>Authnet Server URL</b></td><td>'.$this->url.'</td>'."\n".'</tr>'."\n"; |
|
67
|
|
|
$output .= '<tr>'."\n\t\t".'<th colspan="2"><b>Request JSON</b></th>'."\n".'</tr>'."\n"; |
|
68
|
|
|
if (!empty($this->requestJson)) { |
|
69
|
|
|
$output .= '<tr><td colspan="2"><pre>'."\n"; |
|
70
|
|
|
$output .= $this->requestJson."\n"; |
|
71
|
|
|
$output .= '</pre></td></tr>'."\n"; |
|
72
|
|
|
} else { |
|
73
|
|
|
$output .= '<tr><td colspan="2" style="text-align: center;"><pre>N/A</pre></td></tr>'."\n"; |
|
74
|
|
|
} |
|
75
|
|
|
$output .= '</table>'; |
|
76
|
|
|
|
|
77
|
|
|
return $output; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Creates a new webhook |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $webhooks Array of webhooks to be created or modified |
|
84
|
|
|
* @param string $webhookUrl URL of where webhook notifications will be sent |
|
85
|
|
|
* @param string $status Status of webhooks to be created or modified [active/inactive] |
|
86
|
|
|
* @return \JohnConde\Authnet\AuthnetWebhooksResponse |
|
87
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
|
88
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
89
|
|
|
*/ |
|
90
|
|
View Code Duplication |
public function createWebhooks(array $webhooks, string $webhookUrl, string $status = 'active') : object |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$this->endpoint = 'webhooks'; |
|
93
|
|
|
$this->url = sprintf('%s%s', $this->url, $this->endpoint); |
|
94
|
|
|
$request = [ |
|
95
|
|
|
'url' => $webhookUrl, |
|
96
|
|
|
'eventTypes' => $webhooks, |
|
97
|
|
|
'status' => $status |
|
98
|
|
|
]; |
|
99
|
|
|
$this->requestJson = json_encode($request); |
|
100
|
|
|
$response = $this->post($this->url, $this->requestJson); |
|
101
|
|
|
return new AuthnetWebhooksResponse($response); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Sends a test ping to a URL for (a) designated webhook(s) |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $webhookId Webhook ID to be tested |
|
108
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
109
|
|
|
*/ |
|
110
|
|
|
public function testWebhook(string $webhookId) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->endpoint = 'webhooks'; |
|
113
|
|
|
$this->url = sprintf('%s%s/%s/pings', $this->url, $this->endpoint, $webhookId); |
|
114
|
|
|
$this->requestJson = json_encode([]); |
|
115
|
|
|
$this->post($this->url, $this->requestJson); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Gets all of the available event types |
|
120
|
|
|
* |
|
121
|
|
|
* @return \JohnConde\Authnet\AuthnetWebhooksResponse |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getEventTypes() : object |
|
124
|
|
|
{ |
|
125
|
|
|
$this->endpoint = 'eventtypes'; |
|
126
|
|
|
$this->url = sprintf('%s%s', $this->url, $this->endpoint); |
|
127
|
|
|
return $this->getByUrl('eventtypes', '%s%s'); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* List all of your webhooks |
|
132
|
|
|
* |
|
133
|
|
|
* @return \JohnConde\Authnet\AuthnetWebhooksResponse |
|
134
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
135
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getWebhooks() : object |
|
138
|
|
|
{ |
|
139
|
|
|
$this->endpoint = 'webhooks'; |
|
140
|
|
|
$this->url = sprintf('%s%s', $this->url, $this->endpoint); |
|
141
|
|
|
return $this->getByUrl('webhooks', '%s%s'); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get a webhook |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $webhookId Webhook ID to be retrieved |
|
148
|
|
|
* @return \JohnConde\Authnet\AuthnetWebhooksResponse |
|
149
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
150
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getWebhook(string $webhookId) : object |
|
153
|
|
|
{ |
|
154
|
|
|
$this->endpoint = 'webhooks'; |
|
155
|
|
|
$this->url = sprintf('%s%s/%s', $this->url, $this->endpoint, $webhookId); |
|
156
|
|
|
return $this->getByUrl('webhooks', '%s%s/%s'); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* GET API request |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $url API endpoint to hit |
|
163
|
|
|
* @return object |
|
164
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
165
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
|
166
|
|
|
*/ |
|
167
|
|
|
private function getByUrl(string $url) : object |
|
168
|
|
|
{ |
|
169
|
|
|
$response = $this->get($url); |
|
170
|
|
|
return new AuthnetWebhooksResponse($response); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Updates webhook event types |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $webhookId Webhook ID to be modified |
|
177
|
|
|
* @param string $webhookUrl URL of where webhook notifications will be sent |
|
178
|
|
|
* @param array $eventTypes Array of event types to be added/removed |
|
179
|
|
|
* @param string $status Status of webhooks to be modified [active/inactive] |
|
180
|
|
|
* @return \JohnConde\Authnet\AuthnetWebhooksResponse |
|
181
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
|
182
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
183
|
|
|
*/ |
|
184
|
|
View Code Duplication |
public function updateWebhook(string $webhookId, string $webhookUrl, array $eventTypes, string $status = 'active') : object |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
|
|
$this->endpoint = 'webhooks'; |
|
187
|
|
|
$this->url = sprintf('%s%s/%s', $this->url, $this->endpoint, $webhookId); |
|
188
|
|
|
$request = [ |
|
189
|
|
|
'url' => $webhookUrl, |
|
190
|
|
|
'eventTypes' => $eventTypes, |
|
191
|
|
|
'status' => $status |
|
192
|
|
|
]; |
|
193
|
|
|
$this->requestJson = json_encode($request); |
|
194
|
|
|
$response = $this->put($this->url, $this->requestJson); |
|
195
|
|
|
return new AuthnetWebhooksResponse($response); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Delete a webhook |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $webhookId Webhook ID to be deleted |
|
202
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
203
|
|
|
*/ |
|
204
|
|
|
public function deleteWebhook(string $webhookId) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->endpoint = 'webhooks'; |
|
207
|
|
|
$this->url = sprintf('%s%s/%s', $this->url, $this->endpoint, $webhookId); |
|
208
|
|
|
$this->delete($this->url); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Retrieve Notification History |
|
213
|
|
|
* |
|
214
|
|
|
* @param integer $limit Default: 1000 |
|
215
|
|
|
* @param integer $offset Default: 0 |
|
216
|
|
|
* @return \JohnConde\Authnet\AuthnetWebhooksResponse |
|
217
|
|
|
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
|
218
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getNotificationHistory(int $limit = 1000, int $offset = 0) : object |
|
221
|
|
|
{ |
|
222
|
|
|
$this->endpoint = 'notifications'; |
|
223
|
|
|
$this->url = sprintf('%s%s', $this->url, $this->endpoint); |
|
224
|
|
|
$response = $this->get($this->url, [ |
|
225
|
|
|
'offset' => $offset, |
|
226
|
|
|
'limit' => $limit |
|
227
|
|
|
]); |
|
228
|
|
|
return new AuthnetWebhooksResponse($response); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Tells the handler to make the API call to Authorize.Net |
|
233
|
|
|
* |
|
234
|
|
|
* @return string |
|
235
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
236
|
|
|
*/ |
|
237
|
|
|
private function handleResponse() : string |
|
238
|
|
|
{ |
|
239
|
|
|
if (!$this->processor->error) { |
|
240
|
|
|
return $this->processor->response; |
|
241
|
|
|
} |
|
242
|
|
|
$error_message = null; |
|
243
|
|
|
$error_code = null; |
|
244
|
|
|
if ($this->processor->error_code) { |
|
245
|
|
|
$error_message = $this->processor->error_message; |
|
246
|
|
|
$error_code = $this->processor->error_code; |
|
247
|
|
|
if (empty($error_message)) { |
|
248
|
|
|
$response = json_decode($this->processor->response); |
|
249
|
|
|
$error_message = sprintf('(%u) %s: %s', $response->status, $response->reason, $response->message); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
throw new AuthnetCurlException(sprintf('Connection error: %s (%s)', $error_message, $error_code)); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Make GET request via Curl |
|
257
|
|
|
* |
|
258
|
|
|
* @param string $url |
|
259
|
|
|
* @param array $params |
|
260
|
|
|
* @return string |
|
261
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
262
|
|
|
* |
|
263
|
|
|
* @codeCoverageIgnore |
|
264
|
|
|
*/ |
|
265
|
|
|
private function get(string $url, array $params = []) : string |
|
266
|
|
|
{ |
|
267
|
|
|
$this->processor->get($url, $params); |
|
268
|
|
|
return $this->handleResponse(); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Make POST request via Curl |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $url API endpoint |
|
275
|
|
|
* @param string $request JSON request payload |
|
276
|
|
|
* @return string |
|
277
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
278
|
|
|
* |
|
279
|
|
|
* @codeCoverageIgnore |
|
280
|
|
|
*/ |
|
281
|
|
|
private function post(string $url, string $request) : string |
|
282
|
|
|
{ |
|
283
|
|
|
$this->processor->post($url, $request); |
|
284
|
|
|
return $this->handleResponse(); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Make PUT request via Curl |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $url API endpoint |
|
291
|
|
|
* @param string $request JSON request payload |
|
292
|
|
|
* @return string |
|
293
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
294
|
|
|
* |
|
295
|
|
|
* @codeCoverageIgnore |
|
296
|
|
|
*/ |
|
297
|
|
|
private function put(string $url, string $request) : string |
|
298
|
|
|
{ |
|
299
|
|
|
$this->processor->put($url, $request, true); |
|
300
|
|
|
return $this->handleResponse(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Make DELETE request via Curl |
|
305
|
|
|
* |
|
306
|
|
|
* @param string $url API endpoint |
|
307
|
|
|
* @return string |
|
308
|
|
|
* @throws \JohnConde\Authnet\AuthnetCurlException |
|
309
|
|
|
* |
|
310
|
|
|
* @codeCoverageIgnore |
|
311
|
|
|
*/ |
|
312
|
|
|
private function delete(string $url) : string |
|
313
|
|
|
{ |
|
314
|
|
|
$this->processor->delete($url, [], true); |
|
315
|
|
|
return $this->handleResponse(); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Sets the handler to be used to handle our API call. Mainly used for unit testing as Curl is used by default. |
|
320
|
|
|
* |
|
321
|
|
|
* @param \Curl\Curl $processor |
|
322
|
|
|
*/ |
|
323
|
|
|
public function setProcessHandler(\Curl\Curl $processor) |
|
324
|
|
|
{ |
|
325
|
|
|
$this->processor = $processor; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Gets the request sent to Authorize.Net in JSON format for logging purposes |
|
330
|
|
|
* |
|
331
|
|
|
* @return string transaction request sent to Authorize.Net in JSON format |
|
332
|
|
|
*/ |
|
333
|
|
|
public function getRawRequest() : string |
|
334
|
|
|
{ |
|
335
|
|
|
return $this->requestJson; |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
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.