src/Controller/OrderController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CouponCode;
  4. use App\Entity\Invoice;
  5. use App\Entity\Order;
  6. use App\Entity\Template;
  7. use App\Form\OrderFormType;
  8. use App\Repository\CouponCodeRepository;
  9. use App\Security\MailerController;
  10. use BarionClient;
  11. use Currency;
  12. use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
  13. use Dompdf\Dompdf;
  14. use Dompdf\Options;
  15. use FundingSourceType;
  16. use ItemModel;
  17. use PaymentTransactionModel;
  18. use PaymentType;
  19. use PreparePaymentRequestModel;
  20. use PreparePaymentResponseModel;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\Security\Core\Security;
  26. use UILocale;
  27. use Symfony\Component\Serializer\Serializer;
  28. use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  29. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  30. class OrderController extends AbstractController
  31. {
  32.     private MailerController $mailerController;
  33.     private CouponCodeRepository $couponCodeRepository;
  34.     public function __construct(
  35.         MailerController $emailVerifier,
  36.         CouponCodeRepository $couponCodeRepository,
  37.     ) {
  38.         $this->mailerController $emailVerifier;
  39.         $this->couponCodeRepository $couponCodeRepository;
  40.     }
  41.     #[Route('/order'name'app_order')]
  42.     public function index(Security $securityPersistenceManagerRegistry $doctrineRequest $request): Response
  43.     {
  44.         $order = new Order();
  45.         $form $this->createForm(OrderFormType::class, $order, [
  46.             'action' => $this->generateUrl('app_order_process'),
  47.             'method' => 'POST',
  48.         ]);
  49.         $entityManager $doctrine->getManager();
  50.         $templates $entityManager->getRepository(Template::class)->findAll();
  51.         $array = [
  52.             'form' => $form->createView(),
  53.             'templates' => $templates,
  54.         ];
  55.         // Check if the user has accepted or declined cookies
  56.         $cookiesAccepted $request->cookies->get('cookies_accepted');
  57.         if ($cookiesAccepted === 'true') {
  58.             // User accepted cookies
  59.             $array['loggedUser'] = $security->getUser();
  60.         } else {
  61.             // User declined cookies
  62.             $array['loggedUser'] = null;
  63.         }
  64.         return $this->render('order/index.html.twig'$array);
  65.     }
  66.     #[Route('/order/process'name'app_order_process')]
  67.     public function processOrder(Request $requestPersistenceManagerRegistry $doctrine): Response
  68.     {
  69.         $entityManager $doctrine->getManager();
  70.         $user $this->getUser();
  71.         $data $request->request->all('order_form');
  72.         $lastVariableSymbol $doctrine->getRepository(Order::class)->findLastVariableSymbol();
  73.         if ($lastVariableSymbol) {
  74.             $lastVariableSymbol $lastVariableSymbol[0]->getVariableSymbol();
  75.             $lastVariableSymbol++;
  76.         } else {
  77.             $lastVariableSymbol date("Y") . 100;
  78.         }
  79.         $template $doctrine->getRepository(Template::class)->findOneBy([
  80.             'path' => $data['template']
  81.         ]);
  82.         $order $this->prepareOrder($data$user);
  83.         $order->setVariableSymbol($lastVariableSymbol);
  84.         $order->setTemplate($template);
  85.         $entityManager->persist($order);
  86.         $entityManager->flush();
  87.         $path $_SERVER['DOCUMENT_ROOT'] . $this->getParameter('order') . $lastVariableSymbol;
  88.         if (!file_exists($path)) {
  89.             mkdir($path0777true);
  90.         }
  91.         $pdfOptions = new Options();
  92.         $pdfOptions->set('defaultFont''Arial');
  93.         $pdfOptions->set('isRemoteEnabled'true);
  94.         $code $doctrine->getRepository(CouponCode::class)->findOneByCode($order->getCode());
  95.         $html $this->renderView('invoice/index.html.twig', [
  96.             'order' => $order,
  97.             'code' => $code,
  98.         ]);
  99.         // Instantiate Dompdf with our options
  100.         $dompdf = new Dompdf($pdfOptions);
  101.         $dompdf->loadHtml($html);
  102.         $dompdf->setPaper('A4');
  103.         $dompdf->render();
  104.         file_put_contents($path '/' $order->getVariableSymbol() . '.pdf'$dompdf->output());
  105.         $paymentRequest $this->makePayment($order->getFinalPrice(), $order->getBillingEmail(), $order->getVariableSymbol());
  106.         $order->setPaymentId($paymentRequest->PaymentId);
  107.         $invoice = new Invoice();
  108.         $invoice->setName($order->getVariableSymbol() . '.pdf');
  109.         $invoice->setPath($path);
  110.         $invoice->setAssociatedOrder($order);
  111.         $order->setInvoice($invoice);
  112.         $entityManager->persist($order);
  113.         $entityManager->persist($invoice);
  114.         $entityManager->flush();
  115.         $this->mailerController->sendNewOrderEmail($request$order$path '/' $order->getVariableSymbol() . '.pdf'$paymentRequest->PaymentRedirectUrl);
  116.         return $this->redirect($paymentRequest->PaymentRedirectUrl);
  117.     }
  118.     public function makePayment($price$email$variableSymbol): PreparePaymentResponseModel
  119.     {
  120.         $barionClient = new BarionClient($this->getParameter('app.barionKey'), 2$this->getParameter('app.barionEnv'));
  121.         $item = new ItemModel();
  122.         $item->Name "MojeZpráva.cz - dopis";
  123.         $item->Description "Objednávka zprávy s vlastním textem na přibližné datum.";
  124.         $item->Quantity 1;
  125.         $item->Unit "zpráva";
  126.         $item->UnitPrice $price;
  127.         $item->ItemTotal $price;
  128.         $item->SKU "Zpráva s vlastním textem";
  129.         $trans = new PaymentTransactionModel();
  130.         $trans->POSTransactionId "Platba za zprávu";
  131.         $trans->Payee $this->getParameter('app.barionUser');
  132.         $trans->Total $price;
  133.         $trans->AddItem($item);
  134.         $ppr = new PreparePaymentRequestModel();
  135.         $ppr->GuestCheckout true;
  136.         $ppr->PaymentType PaymentType::Immediate;
  137.         $ppr->FundingSources = array(FundingSourceType::All);
  138.         $ppr->PaymentRequestId "PAYMENT-01 - dopsat co to vlastne je";
  139.         $ppr->PayerHint $email;
  140.         $ppr->Locale UILocale::CZ;
  141.         $ppr->OrderNumber $variableSymbol;
  142.         $ppr->Currency Currency::CZK;
  143.         $ppr->RedirectUrl $this->getParameter('app.url') . "order/accepted";
  144.         $ppr->CallbackUrl $this->getParameter('app.url') . "order";
  145.         $ppr->AddTransaction($trans);
  146.         return $barionClient->PreparePayment($ppr);
  147.     }
  148.     public function prepareOrder($data$user): Order
  149.     {
  150.         $order = new Order();
  151.         if (strlen($data['deliveryCity']) > 0) {
  152.             $order->setDeliveryCity($data['deliveryCity']);
  153.             $order->setDeliveryFirstname($data['deliveryFirstname']);
  154.             $order->setDeliveryLastname($data['deliveryLastname']);
  155.             $order->setDeliveryState($data['deliveryCountry']);
  156.             $order->setDeliveryStreet1($data['deliveryStreet']);
  157.             $order->setDeliveryStreet2($data['deliveryStreet2']);
  158.             $order->setDeliveryZipCode($data['deliveryZipcode']);
  159.             $order->setDeliveryEmail($data['deliveryEmail']);
  160.             $order->setDeliveryPhone($data['deliveryPhone']);
  161.         }
  162.         $order->setBillingCity($data['billingCity']);
  163.         $order->setBillingFirstname($data['billingFirstname']);
  164.         $order->setBillingLastname($data['billingLastname']);
  165.         $order->setBillingState($data['billingCountry']);
  166.         $order->setBillingStreet($data['billingStreet']);
  167.         $order->setBillingStreet2($data['billingStreet2']);
  168.         $order->setBillingZipCode($data['billingZipcode']);
  169.         $order->setBillingEmail($data['billingEmail']);
  170.         $order->setBillingPhone($data['billingPhone']);
  171.         $order->setPrice($this->getParameter('messagePrice'));
  172.         $order->setFinalPrice($data['price']);
  173.         $order->setCode($data['code'] ?? '0');
  174.         $order->setText($data['text']);
  175.         $order->setDeliveryDate(new \DateTime($data['deliveryDate']));
  176.         $order->setUser($user);
  177.         $order->setIssuedAt(new \DateTime());
  178.         return $order;
  179.     }
  180.     #[Route('/order/accepted'name'app_order_accepted')]
  181.     public function orderAccepted(Request $requestPersistenceManagerRegistry $doctrine): Response
  182.     {
  183.         $order $doctrine->getRepository(Order::class)->findOneByPaymentId($request->get('paymentId'));
  184.         $order->setPaid(true);
  185.         $entityManager $doctrine->getManager();
  186.         $entityManager->persist($order);
  187.         $entityManager->flush();
  188.         return $this->render('order/accepted.html.twig');
  189.     }
  190.     #[Route('/couponCode'name'app_get_couponCode')]
  191.     public function getCouponCode(Request $requestPersistenceManagerRegistry $doctrine): Response
  192.     {
  193.         $code $doctrine->getRepository(CouponCode::class)->findOneByCode($request->get('code'));
  194.         $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
  195.         $json $serializer->serialize($code'json');
  196.         if ($code) {
  197.             return new Response($json200);
  198.         } else {
  199.             return new Response(null404);
  200.         }
  201.     }
  202. }