diff --git a/resources/lang/en/Debug.properties b/resources/lang/en/Debug.properties index 9087c8bb7..0e1e73cca 100644 --- a/resources/lang/en/Debug.properties +++ b/resources/lang/en/Debug.properties @@ -47,6 +47,9 @@ ContactMailService_noMailContent = "Cannot send mail: Mail content is empty." ; CountryService CountryService_noNameFound = "No names configured for the country with the given ID." +; CancellationResource +CancellationResource_missingFields = "Required fields are missing and couldn't be mapped to the email template." + ; CustomerAuthenticationResource CustomerAuthenticationResource_loginFailed = "Cannot perform login." diff --git a/src/Api/Resources/CancellationResource.php b/src/Api/Resources/CancellationResource.php new file mode 100644 index 000000000..b404eda94 --- /dev/null +++ b/src/Api/Resources/CancellationResource.php @@ -0,0 +1,103 @@ +cancellationRepository = $cancellationRepository; + } + + /** + * Submit a cancellation request. + * @return Response + */ + public function store(): Response + { + if (!ReCaptcha::verify($this->request->get('recaptchaToken', null), true)) { + /** @var NotificationService $notificationService */ + $notificationService = pluginApp(NotificationService::class); + $notificationService->addNotificationCode(LogLevel::ERROR, 13); + + return $this->response->create('', ResponseCode::BAD_REQUEST); + } + + try { + $requestData = $this->request->all(); + $formData = $requestData['data'] ?? []; + $errors = []; + + if(empty($formData)){ + return $this->response->create(false, ResponseCode::BAD_REQUEST); + } + + foreach (['email', 'name', 'order'] as $field) { + if(empty($formData[$field]['value'])) { + $errors[] = $field; + } + } + + if(!empty($errors)) { + $message = 'Keys "' . implode('", "', $errors) . '" of the contract withdrawal form couldn\'t be mapped to the email template.'; + + $this->getLogger(self::class)->error( + "IO::Debug.CancellationResource_missingFields", + [ + "code" => ResponseCode::BAD_REQUEST, + "message" => $message + ] + ); + + return $this->response->create(false, ResponseCode::BAD_REQUEST); + } + + $this->cancellationRepository->submitCancellationRequest([ + 'email' => $formData['email']['value'], + 'name' => $formData['name']['value'], + 'lang' => Utils::getLang(), + 'orderId' => (int) ($formData['order']['value']), + 'reason' => $formData['reason']['value'] ?? '', + 'recipient' => $requestData['recipient'] ?? '', + ]); + + return $this->response->create(true, ResponseCode::OK); + } catch (\Exception $exception) { + $code = $exception->getCode() ?: ResponseCode::INTERNAL_SERVER_ERROR; + return $this->response->create(false, $code); + } + } +} diff --git a/src/Providers/IORouteServiceProvider.php b/src/Providers/IORouteServiceProvider.php index 2ad99b963..ac4a4cc38 100644 --- a/src/Providers/IORouteServiceProvider.php +++ b/src/Providers/IORouteServiceProvider.php @@ -60,6 +60,14 @@ function (ApiRouter $api) { } ); + $api->version( + ['v1'], + ['namespace' => 'IO\Api\Resources', 'middleware' => ['throttleFrontend:cancellation-form']], + function (ApiRouter $api) { + $api->post('io/cancellation', 'CancellationResource@store'); + } + ); + $api->version(['v1'], ['namespace' => 'IO\Api\Resources'], function (ApiRouter $api) { $api->get('io/basket', 'BasketResource@index'); $api->resource('io/basket/items', 'BasketItemResource');