src/Controller/ServicesController.php line 130

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\EnrollFormType;
  4. use App\Services\AttendeesService;
  5. use App\Services\CheckwebsitesettingService;
  6. use App\Services\GetPricesForMembershipService;
  7. use App\Services\MailService;
  8. use App\Services\MollieService;
  9. use Carbon\Carbon;
  10. use DateInterval;
  11. use DateTime;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Mollie\Api\Exceptions\ApiException;
  14. use Mollie\Api\MollieApiClient;
  15. use MultilingualBundle\Service\DocumentLookupService;
  16. use Pimcore\Model\DataObject\Attendees;
  17. use Pimcore\Model\DataObject\Campscourse;
  18. use Pimcore\Model\DataObject\Order;
  19. use Pimcore\Model\DataObject\Realisation;
  20. use Pimcore\Model\DataObject\TypeCampscourse;
  21. use Pimcore\Model\Document;
  22. use Pimcore\Model\Notification\Service\NotificationService;
  23. use Pimcore\Model\WebsiteSetting;
  24. use Pimcore\Translation\Translator;
  25. use Pimcore\Twig\Extension\Templating\PimcoreUrl;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\HttpFoundation\RedirectResponse;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  31. use Symfony\Component\Routing\Annotation\Route;
  32. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  33. class ServicesController extends BaseController
  34. {
  35.     protected $checkwebsitesettingService;
  36.     protected $inotherlang;
  37.     protected $pimcoreUrl;
  38.     public function __construct(CheckwebsitesettingService $checkwebsitesettingServiceDocumentLookupService $inotherlangPimcoreUrl $pimcoreUrl)
  39.     {
  40.         $this->checkwebsitesettingService $checkwebsitesettingService;
  41.         $this->inotherlang $inotherlang;
  42.         $this->pimcoreUrl $pimcoreUrl;
  43.     }
  44.     /**
  45.      *
  46.      * @param Request $request
  47.      * @return Response
  48.      *
  49.      */
  50.     public function defaultAction(Request $request)
  51.     {
  52.         $documentID $request->get('contentDocument')->getId();
  53.         $Infolinks = [];
  54.         if ($documentID !== null) {
  55.             $document Document::getById($documentID);
  56.             if ($document->hasChildren()) {
  57.                 $foldersLink = [];
  58.                 $folders $document->getChildren();
  59.                 foreach ($folders as $folder) {
  60.                     $pagesLink = [];
  61.                     if ($folder->hasChildren()) {
  62.                         $pages $folder->getChildren();
  63.                         foreach ($pages as $page) {
  64.                             $pagesLink[] = [
  65.                                 'name_page' => $page->getProperties()['navigation_name']->getData(),
  66.                                 'link_page' => $page->getPath() . $page->getKey(),
  67.                             ];
  68.                         }
  69.                     }
  70.                     $foldersLink[] = [
  71.                         'name_folder' => $folder->getProperties()['navigation_name']->getData(),
  72.                         'links' => $pagesLink,
  73.                     ];
  74.                 }
  75.                 $Infolinks $foldersLink;
  76.             }
  77.         }
  78.         return $this->render('services/default.html.twig', [
  79.             'Infolinks' => $Infolinks,
  80.         ]);
  81.     }
  82.     public function overviewAction(Request $request): Response
  83.     {
  84.         if ($this->document) {
  85.             if ($this->document->getEditable('typeCampscourse') && $this->document->getEditable('typeCampscourse')->getElementIds() !== []) {
  86.                 foreach ($this->document->getEditable('typeCampscourse')->getElementIds() as $items) {
  87.                     $ArrayTypeCampscourse TypeCampscourse::getById($items['id'])?->getCampscourse();
  88.                     $Campscourse = [];
  89.                     foreach ($ArrayTypeCampscourse as $itemOject) {
  90.                         $Campscourse[] = $itemOject;
  91.                     }
  92.                 }
  93.                 usort($Campscourse, static function ($a$b) {
  94.                     return strtotime($a->getStart_date()) - strtotime($b->getStart_date());
  95.                 });
  96.                 return $this->render('services/overview.html.twig', [
  97.                     'Campscourse' => $Campscourse
  98.                 ]);
  99.             }
  100.         }
  101.         return $this->render('services/overview.html.twig');
  102.     }
  103.     public function overviewALLAction(Request $request): Response
  104.     {
  105.         $Campscourse Campscourse::getList();
  106.         $Campscourse->setOrderKey("start_date");
  107.         $Campscourse->setOrder("asc");
  108.         return $this->render('services/overview-all.html.twig', [
  109.             'Campscourse' => $Campscourse
  110.         ]);
  111.     }
  112.     /**
  113.      * @Route("{_locale}/{path}/{key}~c{page}", name="campscourse-detail", defaults={"path"=""}, requirements={"path"=".*?", "key"="[\w-]+", "page"="\d+"})
  114.      *
  115.      * @param Request $request
  116.      *
  117.      * @return Response
  118.      * @throws \Exception
  119.      */
  120.     public function detailAction(Request $request): Response
  121.     {
  122.         $campscourse Campscourse::getById($request->get('page'));
  123.         if (!($campscourse instanceof Campscourse && ($campscourse->isPublished() || $this->verifyPreviewRequest($request$campscourse)))) {
  124.             throw new NotFoundHttpException('Campscourse not found.');
  125.         }
  126.         $form $this->createForm(EnrollFormType::class);
  127.         if (!$campscourse->getZeilofSurfKamp()) {
  128.             $form->remove('gezondheidmedische');
  129.             $form->remove('phoneNumber2');
  130.             $form->remove('zeilofsurfervaring');
  131.         }
  132.         $form->handleRequest($request);
  133.         $session $request->getSession();
  134.         // Set a session variable
  135.         $lidkaartNumber $session->get("lidkaart");
  136.         if ($form->isSubmitted() && $form->isValid()) {
  137.             if ($form->getData()['email'] === $form->getData()['confirmEmail']){
  138.                 $attendees AttendeesService::Create($form->getData(), $campscourse$lidkaartNumber);
  139.                 $session->remove("lidkaart");
  140.                 $menberCard = new GetPricesForMembershipService();
  141.                 $prices $menberCard->getPrices();
  142.                 //mollie
  143.                 $mollie = new MollieService();
  144.                 if (!$attendees->getLidformZSG()) {
  145.                     $attendees->setAmount(number_format($campscourse->getPricePerPerson(), 2'.'''));
  146.                 } else {
  147.                     $attendees->setAmount(number_format($campscourse->getPricePerPerson() - $prices['wwsv_licentienummer_korting'], 2'.'''));
  148.                 }
  149.                 if ((float)$attendees->getAmount() > 0) {
  150.                     $payment $mollie->sendPayment($campscourse$attendees$this->pimcoreUrl'campscourse');
  151.                     $attendees->setPaymentId($payment->id);
  152.                     $attendees->save();
  153.                     $response = new RedirectResponse($payment->getCheckoutUrl());
  154.                     $response->send();
  155.                     return $response;
  156.                 } else {
  157.                     // No Mollie payment needed, mark as paid
  158.                     $attendees->setPayed(true);
  159.                     $attendees->setDatePayment(\Carbon\Carbon::now());
  160.                     $attendees->save();
  161.                     return $this->redirectToRoute('thanks-page', [
  162.                         '_locale' => $request->getLocale(),
  163.                         'o' => $attendees->getId(),
  164.                     ]);
  165.                 }
  166.             }
  167.             $this->addFlash('message''Email not the same');
  168.         }
  169.         return $this->render('services/detail.html.twig', [
  170.             'campscourse' => $campscourse,
  171.             'form' => $form->createView(),
  172.         ]);
  173.     }
  174.     /**
  175.      * Checkpayment cart page
  176.      *
  177.      * @Route("{_locale}/payconnect/checkpayment", name="checkpayment-page")
  178.      *
  179.      * @param Request $request
  180.      * @return Response
  181.      * @throws \Exception
  182.      */
  183.     public function checkpaymentAction(Request $request)
  184.     {
  185.         if ($request->isMethod('POST')) {
  186.             $mollie = new MollieService();
  187.             $attendees Attendees::getById($request->get('id-pay'null));
  188.             $paymentId $attendees->getPaymentId();
  189.             $response $mollie->checkPayment($paymentId);
  190.             // Process response
  191.             if ($response) {
  192.                 $payment $response;
  193.                 $this->sendNotificationFromOrder($payment$attendees);
  194.             }
  195.         } else {
  196.             return $this->redirect("/" $this->language);
  197.         }
  198.         return $this->redirect("/" $this->language);
  199.     }
  200.     public function sendNotificationFromOrder($payment$attendees)
  201.     {
  202.         try {
  203.             $order_id $payment->metadata->order_id;
  204.             $invoicenumber $payment->metadata->invoicenumber;
  205.             $campcourse_id $payment->metadata->campcourse_id;
  206.             $paymentObject Attendees::getById($order_id);
  207.             $orderIdPimcore $paymentObject->getKey();
  208.             $keyattendees $attendees->getKey();
  209.             if ($payment->isPaid() == TRUE && $paymentObject) {
  210.                 if (!$paymentObject->getPayed()) {
  211.                     $dateToday \Carbon\Carbon::now();
  212.                     $campcourse Campscourse::getById($campcourse_id);
  213.                     $paymentObject->setPayed(true);
  214.                     $paymentObject->setDatePayment($dateToday);
  215.                     $paymentObject->setPublished(true);
  216.                     $paymentObject->save();
  217.                     $mailData = [];
  218.                     $mailData['order_id'] = $orderIdPimcore;
  219.                     $mailData['email'] = $paymentObject->getEmailAddress();
  220.                     $mailData['name'] = $paymentObject->getFirstname() . ' ' $paymentObject->getLastname();
  221.                     $mailData['lastname'] = $paymentObject->getLastname();
  222.                     $mailData['firstname'] = $paymentObject->getFirstname();
  223.                     $mailData['street'] = $paymentObject->getStreet();
  224.                     $mailData['street_nr'] = $paymentObject->getStreetNumber();
  225.                     $mailData['postcode'] = $paymentObject->getPostcode();
  226.                     $mailData['place'] = $paymentObject->getPlace();
  227.                     $mailData['country'] = $paymentObject->getCountry();
  228.                     $mailData['phone'] = $paymentObject->getPhoneNumber();
  229.                     $mailData['phone_2'] = $paymentObject->getPhonenumber1();
  230.                     $mailData['gender'] = $paymentObject->getGender();
  231.                     $mailData['birdt_date'] = $paymentObject->getBirthdate();
  232.                     $mailData['note'] = $paymentObject->getNote();
  233.                     $mailData['datum_order'] = $paymentObject->getDatePayment()?->format('d-m-Y');
  234.                     $mailData['price_order'] = $paymentObject->getAmount();
  235.                     $mailData['start_campcourse'] = $campcourse->getStart_date()?->format('d-m-Y');
  236.                     $mailData['end_campcourse'] = $campcourse->getEnd_date()?->format('d-m-Y');
  237.                     $mailData['invoicenumber'] = $campcourse->getTitle();
  238.                     $emailTemplate 'kamp-opleiding-mail-user';
  239.                     $emailTemplateAdmin 'kamp-opleiding-mail-club';
  240.                     //send mail + Order voor bevestingen
  241.                     if ($this->checkwebsitesettingService->check((string)$emailTemplate"document")) {
  242.                         $getEmailTemplate \Pimcore\Model\WebsiteSetting::getByName((string)$emailTemplate);
  243.                         $getEmailTemplateConfig $getEmailTemplate->getData();
  244.                         $emailDocument $this->inotherlang->getLocalizedDocument($getEmailTemplateConfig'nl');
  245.                     }
  246.                     if ($this->checkwebsitesettingService->check((string)$emailTemplateAdmin"document")) {
  247.                         $getEmailTemplateAdmin \Pimcore\Model\WebsiteSetting::getByName((string)$emailTemplateAdmin);
  248.                         $getEmailTemplateConfigAdmin $getEmailTemplateAdmin->getData();
  249.                         $emailDocumentAdmin $this->inotherlang->getLocalizedDocument($getEmailTemplateConfigAdmin'nl');
  250.                     }
  251.                     error_log(date('l jS \of F Y h:i:s A') . " Payment for $keyattendees succesfull paid \n"3PIMCORE_LOG_DIRECTORY "/payments.log");
  252.                     //send email to seller
  253.                     if (!$paymentObject->getMailsend()) {
  254.                         //send email to seller
  255.                         (new \Pimcore\Mail())
  256.                             ->from('noreply@zsg.be')
  257.                             ->subject('Order : ' $invoicenumber)
  258.                             ->setDocument($emailDocumentAdmin)
  259.                             ->setParams($mailData)
  260.                             ->send();
  261.                         // send customer e-mail confirmation
  262.                         (new \Pimcore\Mail())
  263.                             ->to($paymentObject->getEmailAddress())
  264.                             ->from('noreply@zsg.be')
  265.                             ->subject('Order : ' $invoicenumber)
  266.                             ->setDocument($emailDocument)
  267.                             ->setParams($mailData)
  268.                             ->send();
  269.                         $paymentObject->setMailsend(true);
  270.                         $paymentObject->save();
  271.                     }
  272.                     $paymentObject->save();
  273.                 }
  274.             } elseif ($payment->isOpen() == FALSE) {
  275.                 //send email to seller
  276. //                $mailSeller = new MailService();
  277. //                $documentSalesNotpaid = $inotherlang->getLocalizedDocument($config->payment_notice);
  278. //                $mailSeller->setSubject($translator->trans("Order ").' '.$invoicenumber.' '.$translator->trans("is not paid"));
  279. //                $mailSeller->setDocument($documentSalesNotpaid);
  280. //                $mailSeller->setParams($mailData);
  281. //                $mailSeller->send();
  282.                 /*
  283.                  * The payment isn't paid and isn't open anymore. We can assume it was aborted.
  284.                  */
  285.                 error_log(date('l jS \of F Y h:i:s A') . " Problem with $keyattendees, not paid \n"3PIMCORE_LOG_DIRECTORY "/payments.log");
  286.             }
  287.             $paymentObject->save();
  288.             header('HTTP/1.1 200 OK');
  289.             die();
  290.         } catch (Mollie_API_Exception $e) {
  291.             echo "API call failed: " htmlspecialchars($e->getMessage());
  292.         }
  293.     }
  294.     /**
  295.      * Redirectpayment cart page
  296.      *
  297.      * @Route("{_locale}/redirectpayment", name="redirectpayment-page")
  298.      *
  299.      * @param Request $request
  300.      * @return Response
  301.      * @throws \Exception
  302.      */
  303.     public function redirectpaymentAction(Request $request\Pimcore\Config\Config $websiteConfig)
  304.     {
  305.         if ($this->checkwebsitesettingService->check("payment_successfull""document")) {
  306.             $paymentSuccesfullDocument $websiteConfig->get('payment_successfull');
  307.         }
  308.         if ($paymentSuccesfullDocument) {
  309.             $language $request->getLocale();
  310.             $paymentObject $request->get('id-pay'null);
  311.             $returnAddress $this->inotherlang->getLocalizedDocument($paymentSuccesfullDocument"$language") . '?o=' $paymentObject;
  312.             return $this->redirect($returnAddress);
  313.         }
  314.     }
  315.     /**
  316.      * Redirectpayment cart page
  317.      *
  318.      * @Route("{_locale}/bedankt", name="thanks-page")
  319.      *
  320.      * @param Request $request
  321.      * @return Response
  322.      * @throws \Exception
  323.      */
  324.     public function redirectpaymentviewAction(Request $request)
  325.     {
  326.         if (!$this->editmode) {
  327.             $language $request->getLocale();
  328.             $paymentItem Attendees::getById($request->get('o'null));
  329.             if ($paymentItem) {
  330.                 return $this->render('services/cart-redirect-payment.html.twig', [
  331.                     'paymentItem' => $paymentItem
  332.                 ]);
  333.             } else {
  334.                 return $this->redirect("/" $language);
  335.             }
  336.         } else {
  337.             $paymentItem null;
  338.             return $this->render('services/cart-redirect-payment.html.twig', [
  339.                 'paymentItem' => $paymentItem
  340.             ]);
  341.         }
  342.     }
  343. }