AuthenticationRoute.php
2 years ago
DonateRoute.php
2 years ago
DonateRouteSignature.php
2 years ago
ValidationRoute.php
2 years ago
DonateRoute.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Routes; |
| 4 | |
| 5 | |
| 6 | use Give\DonationForms\Controllers\DonateController; |
| 7 | use Give\DonationForms\DataTransferObjects\DonateFormRouteData; |
| 8 | use Give\DonationForms\DataTransferObjects\DonateRouteData; |
| 9 | use Give\DonationForms\Exceptions\DonationFormFieldErrorsException; |
| 10 | use Give\DonationForms\ValueObjects\DonationFormErrorTypes; |
| 11 | use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 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 DonateRoute |
| 20 | { |
| 21 | use HandleHttpResponses; |
| 22 | |
| 23 | /** |
| 24 | * @var DonateController |
| 25 | */ |
| 26 | private $donateController; |
| 27 | |
| 28 | /** |
| 29 | * @since 3.0.0 |
| 30 | * |
| 31 | * @param DonateController $donateController |
| 32 | */ |
| 33 | public function __construct(DonateController $donateController) |
| 34 | { |
| 35 | $this->donateController = $donateController; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @since 3.0.0 |
| 40 | * |
| 41 | * @return void |
| 42 | */ |
| 43 | public function __invoke(array $request) |
| 44 | { |
| 45 | // create DTO from GET request |
| 46 | $routeData = DonateRouteData::fromRequest(give_clean($_GET)); |
| 47 | |
| 48 | // validate signature |
| 49 | $routeData->validateSignature(); |
| 50 | |
| 51 | // create DTO from POST request |
| 52 | $formData = DonateFormRouteData::fromRequest($request); |
| 53 | |
| 54 | try { |
| 55 | $data = $formData->validated(); |
| 56 | $this->donateController->donate($data, $data->getGateway()); |
| 57 | } catch (DonationFormFieldErrorsException $exception) { |
| 58 | $type = DonationFormErrorTypes::VALIDATION; |
| 59 | $this->logError($type, $exception->getMessage(), $formData); |
| 60 | $this->sendJsonError($type, $exception->getError()); |
| 61 | } catch (PaymentGatewayException $exception) { |
| 62 | $type = DonationFormErrorTypes::GATEWAY; |
| 63 | $this->logError($type, $exception->getMessage(), $formData); |
| 64 | $this->sendJsonError($type, new WP_Error($type, $exception->getMessage())); |
| 65 | } catch (\Exception $exception) { |
| 66 | $type = DonationFormErrorTypes::UNKNOWN; |
| 67 | $this->logError($type, $exception->getMessage(), $formData); |
| 68 | $this->sendJsonError($type, new WP_Error($type, $exception->getMessage())); |
| 69 | } |
| 70 | |
| 71 | exit; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 3.0.0 |
| 76 | */ |
| 77 | private function logError( |
| 78 | string $type, |
| 79 | string $exceptionMessage, |
| 80 | DonateFormRouteData $formData |
| 81 | ) { |
| 82 | Log::error( |
| 83 | "Donation Route Error: $type", |
| 84 | [ |
| 85 | 'error_type' => $type, |
| 86 | 'exceptionMessage' => $exceptionMessage, |
| 87 | 'formData' => $formData->toArray(), |
| 88 | ] |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param string $type |
| 94 | * @param array|string|WP_Error $errors |
| 95 | * @return void |
| 96 | */ |
| 97 | protected function sendJsonError(string $type, WP_Error $errors) |
| 98 | { |
| 99 | wp_send_json_error([ |
| 100 | 'type' => $type, |
| 101 | 'errors' => $errors, |
| 102 | ]); |
| 103 | } |
| 104 | } |
| 105 |