src/Controller/DonationsController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Mollie\Api\MollieApiClient;
  9. use App\Entity\Donations;
  10. use App\Form\DonationsFormType;
  11. use App\Form\DonationsFormType2;
  12. class DonationsController extends AbstractController 
  13. {
  14.     private function generateRandomString($length 11) {
  15.         $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  16.         $charactersLength strlen($characters);
  17.         $randomString '';
  18.         for ($i 0$i $length$i++) {
  19.             $randomString .= $characters[rand(0$charactersLength 1)];
  20.         }
  21.         $repository $this->getDoctrine()->getRepository(Donations::class);
  22.         $db_result $repository->findOneBy(array('id' => $randomString ));
  23.         if (!empty($db_result)){
  24.             $randomString $this->generateRandomString();
  25.         }
  26.         return $randomString;
  27.     }
  28.     public function sidebar(Request $request) {
  29.         $form $this->createForm(DonationsFormType::class);
  30.         $form->handleRequest($request);
  31.         $monthgoal=50.00;
  32.         $repository $this->getDoctrine()->getRepository(Donations::class);
  33.         $donations $repository->findAllThisMonth();
  34.         $donated 0;
  35.         foreach ($donations as $i => $donation) {
  36.             $donated $donated $donation->getAmount();
  37.         }
  38.         $percentage = ( $donated 100 ) / $monthgoal;
  39.         if ($percentage >= 100) {
  40.             $percentage 100;
  41.         }
  42.         return $this->render('donations_sidebar.html.twig', [
  43.             'form' => $form->createView(),
  44.             'donated' => $donated,
  45.             'percentage' => $percentage,
  46.             'monthgoal' => $monthgoal
  47.         ]);
  48.     }
  49.     /**
  50.      * @Route("/donations/make", name="donation_make")
  51.      */
  52.     public function make(Request $requestEntityManagerInterface $em) {
  53.         if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
  54.         
  55.         $redirect = array();
  56.         $form $this->createForm(DonationsFormType::class);
  57.         $form->handleRequest($request);
  58.         if($request->query->get('referrer') != null && $request->query->get('amount') != null ) {
  59.              $redirect['referrer'] = $request->query->get('referrer');
  60.              $redirect['amount'] = $request->query->get('amount');
  61.         }
  62.         $donation $form->getData();
  63.         if ($donation == null) {
  64.             return $this->render('donations_logon.html.twig', [
  65.                 'form' => $form->createView(),
  66.                 'amount' => 5
  67.             ]);
  68.         }
  69.         if($donation->getAmount() < 1) {
  70.             $this->addFlash('error'$translator->trans("Donaties onder de 1 euro kunnen wij helaas niet in ontvangst nemen."));
  71.             return $this->redirectToRoute('frontpage');
  72.         }
  73.         if($redirect) {
  74.             $donation = new Donations;
  75.             $donation->setAmount($redirect['amount']);
  76.         }
  77.         if (!$this->isGranted('ROLE_USER') && $donation->getUserId() === null) {
  78.             $donation->setUserId("0");
  79.             $form $this->createForm(DonationsFormType::class, $donation);
  80.             return $this->render('donations_logon.html.twig', [
  81.                 'form' => $form->createView(),
  82.                 'amount' => $donation->getAmount()
  83.             ]);
  84.         } else {
  85.             $mollie = new \Mollie\Api\MollieApiClient();
  86.             $mollie->setApiKey($_ENV['MOLLIE_API_KEY']);
  87.             $donation->setAmount(number_format(round($donation->getAmount(), 2),2));
  88.             $donation->setOrderId("PATP-".$this->generateRandomString());
  89.             $donation->setTime(new \DateTime());
  90.             $donation->setTimeUpdated(new \DateTime());
  91.             if (!empty($this->getUser())) {
  92.                 $donation->setUserId($this->getUser()->getId());
  93.                 $donation->setUsername($this->getUser()->getUsername());
  94.             }
  95.             if ($_ENV['SITE_TYPE'] == 'live') {
  96.                 $site_type 'www';
  97.             } else {
  98.                 $site_type $_ENV['SITE_TYPE'];
  99.             }
  100.             $payment $mollie->payments->create([
  101.                 "amount" => [
  102.                     "currency" => "EUR",
  103.                     "value" => $donation->getAmount() // You must send the correct number of decimals, thus we enforce the use of strings
  104.                 ],
  105.                 "description" => "Donation #".$donation->getOrderId(),
  106.                 "redirectUrl" => "https://".$site_type.".paleontica.org/donations/checkout/{$donation->getOrderId()}",
  107.                 "webhookUrl" => "https://".$site_type.".paleontica.org/donations/webhook",
  108.                 "metadata" => [
  109.                     "order_id" => $donation->getOrderId(),
  110.                     "user_id" => $donation->getUserId(),
  111.                     "username" => $donation->getUsername(),
  112.                 ],
  113.             ]);
  114.             $donation->setStatus($payment->status);
  115.             $em->persist($donation);
  116.             $em->flush();
  117.             return $this->redirect($payment->getCheckoutUrl(),303);
  118.         }
  119.     }
  120.     /**
  121.      * @Route("/donations/webhook", name="donation_webhook")
  122.      */
  123.     public function webhook(Request $requestEntityManagerInterface $em) {
  124.         $mollie = new \Mollie\Api\MollieApiClient();
  125.         $mollie->setApiKey($_ENV['MOLLIE_API_KEY']);
  126.         $payment $mollie->payments->get($request->request->get("id"));
  127.         $repository $this->getDoctrine()->getRepository(Donations::class);
  128.         $donation $repository->findOneBy(array('order_id' => $payment->metadata->order_id));
  129.         $donation->setTimeUpdated(new \DateTime());
  130.         if($donation->getStatus() != 'paid') {
  131.             $donation->setStatus($payment->status);
  132.         }
  133.         $em->persist($donation);
  134.         $em->flush();
  135.         $response = new Response();
  136.         $response->headers->set('Content-Type''text/plain');
  137.         $response->setStatusCode(Response::HTTP_OK);
  138.         return $response;
  139.     
  140.         #if ($payment->isPaid() && !$payment->hasRefunds() && !$payment->hasChargebacks()) {
  141.             /*
  142.              * The payment is paid and isn't refunded or charged back.
  143.              * At this point you'd probably want to start the process of delivering the product to the customer.
  144.              */
  145.         #} elseif ($payment->isOpen()) {
  146.             /*
  147.              * The payment is open.
  148.              */
  149.         #} elseif ($payment->isPending()) {
  150.             /*
  151.              * The payment is pending.
  152.              */
  153.         #} elseif ($payment->isFailed()) {
  154.             /*
  155.              * The payment has failed.
  156.              */
  157.         #} elseif ($payment->isExpired()) {
  158.             /*
  159.              * The payment is expired.
  160.              */
  161.         #} elseif ($payment->isCanceled()) {
  162.             /*
  163.              * The payment has been canceled.
  164.              */
  165.         #} elseif ($payment->hasRefunds()) {
  166.             /*
  167.              * The payment has been (partially) refunded.
  168.              * The status of the payment is still "paid"
  169.              */
  170.         #} elseif ($payment->hasChargebacks()) {
  171.             /*
  172.              * The payment has been (partially) charged back.
  173.              * The status of the payment is still "paid"
  174.              */
  175.         #}
  176.     }
  177.     /**
  178.      * @Route("/donations/checkout/{order_id}", name="donation_checkout")
  179.      */
  180.     public function donated(string $order_id) {
  181.         $repository $this->getDoctrine()->getRepository(Donations::class);
  182.         if ($this->isGranted('ROLE_USER')) {
  183.             $donation $repository->findOneBy(array('order_id' => $order_id'user_id' => $this->getUser()->getId()));
  184.         } else {
  185.             $donation $repository->findOneBy(array('order_id' => $order_id'user_id' => 0));
  186.         }
  187.         if($donation) {
  188.             return $this->render('donations_checkout.html.twig',[ 'not_authorized' => false'donation' => $donation ]);
  189.         } else {
  190.             return $this->render('donations_checkout.html.twig',[ 'not_authorized' => true'donation' => false ]);
  191.         }
  192.     }
  193.     /**
  194.      * @Route("/moderate/donations", name="donations_moderate")
  195.      */
  196.     public function manage_donations(Request $request)
  197.     {
  198.         $this->denyAccessUnlessGranted('ROLE_MOD_DONATIONS');
  199.         $repository $this->getDoctrine()->getRepository(Donations::class);
  200.         $donations $repository->findBy(array('status' => 'paid'),array('time' => 'DESC'));
  201.         return $this->render('donations_moderate.html.twig', [
  202.             'donations' => $donations,
  203.         ]);
  204.     }
  205. }