/
mistercode
/
calculate-price
Обзор
Документация
Войти
/
mistercode
/
calculate-price
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/Controller/PurchaseController.php
48 строк
2 KB
codefinecode
Fixtures, DTO, Services, Validators, Strategies, Controllers and Routes
07 фев 2025, 16:46
07 фев 2025, 16:46
4b208d2
Код
Авторство
О чём код?
<?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Validator\Validator\ValidatorInterface; use App\Dto\PurchaseInput; use App\Service\PaymentService; class PurchaseController extends AbstractController { #[Route('/purchase', methods: ['POST'])] public function purchase( Request $request, ValidatorInterface $validator, PaymentService $paymentService ): JsonResponse { $data = json_decode($request->getContent(), true); $input = new PurchaseInput( $data['product'] ?? null, $data['taxNumber'] ?? null, $data['couponCode'] ?? null, $data['paymentProcessor'] ?? null ); $violations = $validator->validate($input); if (count($violations) > 0) { return new JsonResponse(['errors' => (string)$violations], JsonResponse::HTTP_UNPROCESSABLE_ENTITY); } try { $result = $paymentService->processPayment( $input->getProduct(), $input->getTaxNumber(), $input->getCouponCode(), $input->getPaymentProcessor() ); return new JsonResponse($result); } catch (\Exception $e) { return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST); } } }