PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 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 All 257 releases
give / src / DonationForms / Routes / ValidationRoute.php

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

91 lines 2.7 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 use Exception;
6 use Give\DonationForms\Actions\AuthenticateFormRequestWithToken;
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 4.17.0 Sign the donor in from the auth token when the login cookie is unavailable.
25 * @since 3.22.0 added additional catch statements for forbidden and unknown errors
26 * @since 3.0.0
27 */
28 public function __invoke(array $request): bool
29 {
30 // create DTO from GET request
31 $routeData = DonateRouteData::fromRequest(give_clean($_GET));
32
33 // validate signature
34 $routeData->validateSignature();
35
36 (new AuthenticateFormRequestWithToken())($request);
37
38 // create DTO from POST request
39 $formData = ValidationRouteData::fromRequest($request);
40
41 try {
42 $response = $formData->validate();
43
44 $this->handleResponse($response);
45 } catch (DonationFormFieldErrorsException $exception) {
46 $type = DonationFormErrorTypes::VALIDATION;
47 $this->logError($type, $exception->getMessage(), $formData);
48 $this->sendJsonError($type, $exception->getError());
49 } catch (DonationFormForbidden $exception) {
50 wp_die($exception->getMessage(), 403);
51 } catch (Exception $exception) {
52 $type = DonationFormErrorTypes::UNKNOWN;
53 $this->logError($type, $exception->getMessage(), $formData);
54 $this->sendJsonError($type, new WP_Error($type, $exception->getMessage()));
55 }
56
57 exit;
58 }
59
60 /**
61 * @since 3.0.0
62 */
63 private function logError(
64 string $type,
65 string $exceptionMessage,
66 ValidationRouteData $formData
67 ) {
68 Log::error(
69 "Donation Route Error: $type",
70 [
71 'error_type' => $type,
72 'exceptionMessage' => $exceptionMessage,
73 'formData' => $formData->toArray(),
74 ]
75 );
76 }
77
78 /**
79 * @param string $type
80 * @param array|string|WP_Error $errors
81 * @return void
82 */
83 protected function sendJsonError(string $type, WP_Error $errors)
84 {
85 wp_send_json_error([
86 'type' => $type,
87 'errors' => $errors,
88 ]);
89 }
90 }
91