src/FMT/Application/Controller/Common/PaymentController.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * Author: Anton Orlov
  4.  * Date: 06.04.2018
  5.  * Time: 17:49
  6.  */
  7. namespace FMT\Application\Controller\Common;
  8. use FMT\Data\Entity\User;
  9. use FMT\Domain\Exception\InvalidDonationException;
  10. use FMT\Domain\Exception\PaymentException;
  11. use FMT\Domain\Service\PaymentManagerInterface;
  12. use FMT\Domain\Type\Payment\Donation;
  13. use FMT\Domain\Type\Payment\Settings;
  14. use FMT\Application\Controller\AbstractBaseController;
  15. use FMT\Application\FormType\PaymentType;
  16. use FMT\Application\Voter\TransactionVoter;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  23. /**
  24.  * @var Route
  25.  * @var Template
  26.  * @var ParamConverter
  27.  *
  28.  * Class CheckoutController
  29.  * @package FMT\Application\Controller\Common
  30.  * @Route("/payment")
  31.  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  32.  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  33.  * @Template()
  34.  */
  35. class PaymentController extends AbstractBaseController
  36. {
  37.     const TRANSACTION_SESSION_KEY "thank_you_txn_id";
  38.     const ROUTE_DONATE "fmt-donate";
  39.     const ROUTE_DONATE_THANK_YOU "fmt-donate-thank-you";
  40.     const ROUTE_NOT_COMPLETED_DONOR "fmt-donor-profile-index";
  41.     const ROUTE_NOT_COMPLETED_STUDENT "fmt-student-profile-index";
  42.     /** @var string */
  43.     private $token;
  44.     /** @var PaymentManagerInterface */
  45.     private $manager;
  46.     public function __construct(Settings $settings)
  47.     {
  48.         $this->token $settings->publicKey;
  49.     }
  50.     /**
  51.      * @param PaymentManagerInterface $manager
  52.      * @required
  53.      */
  54.     public function setPaymentManager(PaymentManagerInterface $manager)
  55.     {
  56.         $this->manager $manager;
  57.     }
  58.     /**
  59.      * @param Request $request
  60.      * @param User $student
  61.      * @return array|RedirectResponse
  62.      * @Route("/donate/{id}", name=PaymentController::ROUTE_DONATE)
  63.      * @ParamConverter("student", class="FMT\Data\Entity\User", options={"active_campaign" = true})
  64.      * @throws \Exception
  65.      */
  66.     public function donateAction(Request $requestUser $student)
  67.     {
  68.         if ($user $this->getUser()) {
  69.             if ($user->isIncompleteDonor()) {
  70.                 return $this->redirectToRoute(self::ROUTE_NOT_COMPLETED_DONOR);
  71.             } elseif ($user->isIncompleteStudent()) {
  72.                 return $this->redirectToRoute(self::ROUTE_NOT_COMPLETED_STUDENT);
  73.             }
  74.         }
  75.         $donation = new Donation($student);
  76.         $donation->setDonor($user);
  77.         $attributes = [
  78.             "action" => $this->generateUrl(self::ROUTE_DONATE, ["id" => $student->getId()]),
  79.             "attr" => [
  80.                 "data-checkout" => false,
  81.                 "data-token" => $this->token
  82.             ]
  83.         ];
  84.         $form $this->createForm(PaymentType::class, $donation$attributes);
  85.         $form->handleRequest($request);
  86.         $response = [
  87.             'form' => $form->createView(),
  88.             'student' => $student,
  89.         ];
  90.         if ($form->isSubmitted() && $form->isValid()) {
  91.             try {
  92.                 $transaction $this->manager->sendDonation($donation);
  93.                 $this->setSessionVariable(self::TRANSACTION_SESSION_KEY$transaction->getId());
  94.             } catch (PaymentException InvalidDonationException $exception) {
  95.                 $this->addFlashBagError($exception->getMessage());
  96.                 return $response;
  97.             }
  98.             return $this->redirectToRoute(self::ROUTE_DONATE_THANK_YOU);
  99.         }
  100.         return $response;
  101.     }
  102.     /**
  103.      * @param Request $request
  104.      * @return array
  105.      * @Route("/thank-you-for-donation", name=PaymentController::ROUTE_DONATE_THANK_YOU)
  106.      * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  107.      */
  108.     public function donateThankYouAction(Request $request)
  109.     {
  110.         if (!$this->hasSessionVariable(self::TRANSACTION_SESSION_KEY)) {
  111.             throw new NotFoundHttpException("Transaction is not defined for this page");
  112.         }
  113.         $transaction $this->manager->getTransaction($this->getSessionVariable(self::TRANSACTION_SESSION_KEY));
  114.         $this->denyAccessUnlessGranted(TransactionVoter::CAN_VIEW_TRANSACTION$transaction);
  115.         return [
  116.             "student" => $transaction->getRecipient()->getProfile()->getFirstName(),
  117.             "isRegistered" => is_null($this->getUser()),
  118.         ];
  119.     }
  120. }