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