PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / DonationForms / Routes / ValidationRoute.php

ValidationRoute.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/DonationForms/Routes/ValidationRoute.php

88 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonationForms\Routes;
4
5
6 use Exception;
7 use Give\DonationForms\DataTransferObjects\DonateRouteData;
8 use Give\DonationForms\DataTransferObjects\ValidationRouteData;
9 use Give\DonationForms\Exceptions\DonationFormFieldErrorsException;
10 use Give\DonationForms\Exceptions\DonationFormForbidden;
11 use Give\DonationForms\ValueObjects\DonationFormErrorTypes;
12 use Give\Framework\PaymentGateways\Traits\HandleHttpResponses;
13 use Give\Log\Log;
14 use WP_Error;
15
16 /**
17 * @since 3.0.0
18 */
19 class ValidationRoute
20 {
21 use HandleHttpResponses;
22
23 /**
24 * @since 3.22.0 added additional catch statements for forbidden and unknown errors
25 * @since 3.0.0
26 */
27 public function __invoke(array $request): bool
28 {
29 // create DTO from GET request
30 $routeData = DonateRouteData::fromRequest(give_clean($_GET));
31
32 // validate signature
33 $routeData->validateSignature();
34
35 // create DTO from POST request
36 $formData = ValidationRouteData::fromRequest($request);
37
38 try {
39 $response = $formData->validate();
40
41 $this->handleResponse($response);
42 } catch (DonationFormFieldErrorsException $exception) {
43 $type = DonationFormErrorTypes::VALIDATION;
44 $this->logError($type, $exception->getMessage(), $formData);
45 $this->sendJsonError($type, $exception->getError());
46 } catch (DonationFormForbidden $exception) {
47 wp_die($exception->getMessage(), 403);
48 } catch (Exception $exception) {
49 $type = DonationFormErrorTypes::UNKNOWN;
50 $this->logError($type, $exception->getMessage(), $formData);
51 $this->sendJsonError($type, new WP_Error($type, $exception->getMessage()));
52 }
53
54 exit;
55 }
56
57 /**
58 * @since 3.0.0
59 */
60 private function logError(
61 string $type,
62 string $exceptionMessage,
63 ValidationRouteData $formData
64 ) {
65 Log::error(
66 "Donation Route Error: $type",
67 [
68 'error_type' => $type,
69 'exceptionMessage' => $exceptionMessage,
70 'formData' => $formData->toArray(),
71 ]
72 );
73 }
74
75 /**
76 * @param string $type
77 * @param array|string|WP_Error $errors
78 * @return void
79 */
80 protected function sendJsonError(string $type, WP_Error $errors)
81 {
82 wp_send_json_error([
83 'type' => $type,
84 'errors' => $errors,
85 ]);
86 }
87 }
88