AuthenticationRoute.php
2 years ago
DonateRoute.php
1 year ago
DonateRouteSignature.php
2 years ago
ValidationRoute.php
2 years ago
DonateRoute.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Routes; |
| 4 | |
| 5 | |
| 6 | use Exception; |
| 7 | use Give\DonationForms\Controllers\DonateController; |
| 8 | use Give\DonationForms\DataTransferObjects\DonateControllerData; |
| 9 | use Give\DonationForms\DataTransferObjects\DonateFormRouteData; |
| 10 | use Give\DonationForms\DataTransferObjects\DonateRouteData; |
| 11 | use Give\DonationForms\Exceptions\DonationFormFieldErrorsException; |
| 12 | use Give\DonationForms\Exceptions\DonationFormForbidden; |
| 13 | use Give\DonationForms\ValueObjects\DonationFormErrorTypes; |
| 14 | use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 15 | use Give\Framework\PaymentGateways\Traits\HandleHttpResponses; |
| 16 | use Give\Log\Log; |
| 17 | use WP_Error; |
| 18 | |
| 19 | /** |
| 20 | * @since 3.0.0 |
| 21 | */ |
| 22 | class DonateRoute |
| 23 | { |
| 24 | use HandleHttpResponses; |
| 25 | |
| 26 | /** |
| 27 | * @var DonateController |
| 28 | */ |
| 29 | private $donateController; |
| 30 | |
| 31 | /** |
| 32 | * @since 3.0.0 |
| 33 | * |
| 34 | * @param DonateController $donateController |
| 35 | */ |
| 36 | public function __construct(DonateController $donateController) |
| 37 | { |
| 38 | $this->donateController = $donateController; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @since 3.0.0 |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function __invoke(array $request) |
| 47 | { |
| 48 | // create DTO from GET request |
| 49 | $routeData = DonateRouteData::fromRequest(give_clean($_GET)); |
| 50 | |
| 51 | // validate signature |
| 52 | $routeData->validateSignature(); |
| 53 | |
| 54 | // create DTO from POST request |
| 55 | $formData = DonateFormRouteData::fromRequest($request); |
| 56 | |
| 57 | try { |
| 58 | $data = $formData->validated(); |
| 59 | |
| 60 | /** |
| 61 | * Allow for additional validation of the donation form data. |
| 62 | * The donation flow can be interrupted by throwing an Exception. |
| 63 | * |
| 64 | * @since 3.15.0 |
| 65 | * |
| 66 | * @param DonateControllerData $data |
| 67 | */ |
| 68 | do_action('givewp_donate_form_data_validated', $data); |
| 69 | |
| 70 | $this->donateController->donate($data, $data->getGateway()); |
| 71 | } catch (DonationFormFieldErrorsException $exception) { |
| 72 | $type = DonationFormErrorTypes::VALIDATION; |
| 73 | $this->logError($type, $exception->getMessage(), $formData); |
| 74 | $this->sendJsonError($type, $exception->getError()); |
| 75 | } catch (PaymentGatewayException $exception) { |
| 76 | $type = DonationFormErrorTypes::GATEWAY; |
| 77 | $this->logError($type, $exception->getMessage(), $formData); |
| 78 | $this->sendJsonError($type, new WP_Error($type, $exception->getMessage())); |
| 79 | } catch (DonationFormForbidden $exception) { |
| 80 | wp_die($exception->getMessage(), 403); |
| 81 | } catch (Exception $exception) { |
| 82 | $type = DonationFormErrorTypes::UNKNOWN; |
| 83 | $this->logError($type, $exception->getMessage(), $formData); |
| 84 | $this->sendJsonError($type, new WP_Error($type, $exception->getMessage())); |
| 85 | } |
| 86 | |
| 87 | exit; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @since 3.0.0 |
| 92 | */ |
| 93 | private function logError( |
| 94 | string $type, |
| 95 | string $exceptionMessage, |
| 96 | DonateFormRouteData $formData |
| 97 | ) { |
| 98 | Log::error( |
| 99 | "Donation Route Error: $type", |
| 100 | [ |
| 101 | 'error_type' => $type, |
| 102 | 'exceptionMessage' => $exceptionMessage, |
| 103 | 'formData' => $formData->toArray(), |
| 104 | ] |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param string $type |
| 110 | * @param array|string|WP_Error $errors |
| 111 | * @return void |
| 112 | */ |
| 113 | protected function sendJsonError(string $type, WP_Error $errors) |
| 114 | { |
| 115 | wp_send_json_error([ |
| 116 | 'type' => $type, |
| 117 | 'errors' => $errors, |
| 118 | ]); |
| 119 | } |
| 120 | } |
| 121 |