src/Controller/DefaultController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Ads;
  4. use App\Entity\Order;
  5. use App\Entity\ShippingAddress;
  6. use App\Payment\PayPalClient;
  7. //use http\Env\Response;
  8. use http\Client\Response;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class DefaultController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="homepage")
  20.      */
  21.     public function index()
  22.     {
  23.         return $this->redirectToRoute('app_login');
  24.     }
  25.     /**
  26.      * @Route("/email")
  27.      */
  28.     public function checkMailTemplate(Request $request){
  29.         $baseurlPublic   $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath().'/uploads/images/';
  30.         $em             $this->getDoctrine()->getManager();
  31.         /*Send Email to user for order place*/
  32.         $data = [];
  33.         $data['firstName'] = '';
  34.         $data['lastname'] = '';
  35.         $data['address'] = '';
  36.         $shippingAddress $em->getRepository(ShippingAddress::class)->find(1);
  37.         $order $em->getRepository(Order::class)->find(13);
  38.         $data['firstName'] = $shippingAddress->getFirstName();
  39.         $data['lastName'] = $shippingAddress->getLastName();
  40.         $data['address'] = $shippingAddress->getAddress();
  41.         $data['city'] = $shippingAddress->getCity();
  42.         $data['state'] = $shippingAddress->getState();
  43.         $data['mobile'] = $shippingAddress->getMobile();
  44.         $data['country'] = $shippingAddress->getCountry();
  45.         $data['zip'] = $shippingAddress->getZipcode();
  46.         $data['orderId'] = $order->getOrderId();
  47.         $data['date'] = date("d M Y");
  48.         $data['total'] = $order->getAmount();
  49.         $data['products'] = $order->getProduct();
  50.         $data['image_base_url'] = $baseurlPublic;
  51.         $data['text'] = 'order placed successfully';
  52.         return $this->render('api/emails/user-orders-template.html.twig', [
  53.             'data' => $data,
  54.         ]);
  55.     }
  56.     /**
  57.      * Order update Success
  58.      *
  59.      * @Route("order-update-success")
  60.      * @param MailerInterface $mailer
  61.      * @param Request $request
  62.      * @return \Symfony\Component\HttpFoundation\Response
  63.      */
  64.     public function orderUpdateSuccess(MailerInterface $mailerRequest $request)
  65.     {
  66.         /*Update order status*/
  67.         $baseurlPublic   $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath().'/uploads/images/';
  68.         $em             $this->getDoctrine()->getManager();
  69.         $token          $request->query->get('token'null);
  70.         try {
  71.             /*Update order status*/
  72.             if($token){
  73.                 $order $em->getRepository(Order::class)->findOneBy(['token'=>$token]);
  74.                 if($order){
  75.                     /*Order capture For mobile app*/
  76.                     $request = new OrdersCaptureRequest($order->getOrderId());
  77.                     $client PayPalClient::client();
  78.                     $response $client->execute($request);
  79.                     if($response->result->status === Order::COMPLETED){
  80.                         $order->setStatus(Order::COMPLETED);
  81.                         $order->setResponseMeta(null);
  82.                         if(!empty($order->getAds())){
  83.                             $adId $order->getAds()->getId();
  84.                             /*Added add in order for premimum post*/
  85.                             $ad $em->getRepository(Ads::class)->find($adId);
  86.                             $ad->setUrgent(true);
  87.                             $em->persist($ad);
  88.                         }
  89.                         $em->persist($order);
  90.                         $em->flush();
  91.                         $user $order->getUser();
  92.                         $type $order->getType();
  93.                         if($type === 'CART'){
  94.                             $shippingId $order->getShipping()->getID();
  95.                             /*Send Email to user for order place*/
  96.                             $subject    'Order Placed Successfully';
  97.                             $text       'Your Order has been placed successfully #'.$order->getOrderId();
  98.                             $this->sendUserEmail($mailer$user->getEmail(), $shippingId$order$text$subject$baseurlPublic);
  99.                             /*Send Email to user for order place*/
  100.                             $adminEmail  $_ENV['ADMIN_EMAIL'];
  101.                             $subject    'Order received';
  102.                             $text       'You have received order #'.$order->getOrderId();
  103.                             $this->sendUserEmail($mailer$adminEmail$shippingId$order$text$subject$baseurlPublic);
  104.                         }
  105.                         $url 'freshyhelpline://order/'.$order->getId();
  106.                         return $this->render('api/order/payment-success.html.twig', [
  107.                             'redirectUrl' => $url,
  108.                             'msg' => 'Your payment has been done successfully',
  109.                         ]);
  110.                         //return $this->success('Order update successully', ['Default']);
  111.                     }else{
  112.                         $url 'freshyhelpline://order/';
  113.                         return $this->render('api/order/payment-error.html.twig', [
  114.                             'redirectUrl' => $url,
  115.                             'msg' => 'Something went wrong, Please try again later',
  116.                         ]);
  117.                     }
  118.                 }
  119.             }
  120.             $url 'freshyhelpline://order/';
  121.             return $this->render('api/order/payment-error.html.twig', [
  122.                 'redirectUrl' => $url,
  123.                 'msg' => 'Token id missing',
  124.             ]);
  125.         } catch (\Throwable $e) {
  126.             $url 'freshyhelpline://order/';
  127.             return $this->render('api/order/payment-error.html.twig', [
  128.                 'redirectUrl' => $url,
  129.                 'msg' => $e->getMessage(),
  130.             ]);
  131.         }
  132.     }
  133.     /**
  134.      * Order update Error
  135.      *
  136.      * @Route("order-update-error")
  137.      */
  138.     public function orderUpdateError(Request $request)
  139.     {
  140.         $em     $this->getDoctrine()->getManager();
  141.         $token  $request->query->get('token'null);
  142.         try {
  143.             /*Update order status*/
  144.             if($token){
  145.                 $order $em->getRepository(Order::class)->findOneBy(['token'=> $token]);
  146.                 if($order){
  147.                     $order->setStatus(Order::DECLINED);
  148.                     $order->setResponseMeta(null);
  149.                     $em->persist($order);
  150.                     $em->flush();
  151.                     $url 'freshyhelpline://order/';
  152.                     return $this->render('api/order/payment-error.html.twig', [
  153.                         'redirectUrl' => $url,
  154.                         'msg' => 'Something went wrong, Please try again later',
  155.                     ]);
  156.                 }
  157.             }else{
  158.                 $url 'freshyhelpline://order/';
  159.                 return $this->render('api/order/payment-error.html.twig', [
  160.                     'redirectUrl' => $url,
  161.                     'msg' => 'Token is missing',
  162.                 ]);
  163.             }
  164.             //return $this->failure(401, 'Order id missing');
  165.         } catch (\Throwable $e) {
  166.             $url 'freshyhelpline://order/';
  167.             return $this->render('api/order/payment-error.html.twig', [
  168.                 'redirectUrl' => $url,
  169.                 'msg' => $e->getMessage(),
  170.             ]);
  171.         }
  172.     }
  173.     public function sendUserEmail($mailer$email$shippingId$order$text$subject$baseurlPublic){
  174.         $em $this->getDoctrine()->getManager();
  175.         /*Send Email to user for order place*/
  176.         $data = [];
  177.         $data['firstName'] = '';
  178.         $data['lastname'] = '';
  179.         $data['address'] = '';
  180.         $data['city'] = '';
  181.         $data['state'] = '';
  182.         $data['mobile'] = '';
  183.         $data['country'] = '';
  184.         $data['zip'] = '';
  185.         if($shippingId){
  186.             $shippingAddress $em->getRepository(ShippingAddress::class)->find($shippingId);
  187.             $data['firstName'] = $shippingAddress->getFirstName();
  188.             $data['lastName'] = $shippingAddress->getLastName();
  189.             $data['address'] = $shippingAddress->getAddress();
  190.             $data['city'] = $shippingAddress->getCity();
  191.             $data['state'] = $shippingAddress->getState();
  192.             $data['mobile'] = $shippingAddress->getMobile();
  193.             $data['country'] = $shippingAddress->getCountry();
  194.             $data['zip'] = $shippingAddress->getZipcode();
  195.         }
  196.         $data['orderId'] = $order->getOrderId();
  197.         $data['date'] = date("d M Y");
  198.         $data['total'] = $order->getAmount();
  199.         $data['products'] = $order->getProduct();
  200.         $data['image_base_url'] = $baseurlPublic;
  201.         $data['text'] = $text;
  202.         $userEmail  $email;
  203.         $this->sendEmail($mailer$userEmail$subject$text$data);
  204.     }
  205.     public function sendEmail($mailer$email$subject$text$data){
  206.         $email = (new TemplatedEmail())
  207.             ->from($_ENV['ADMIN_EMAIL'])
  208.             ->to($email)
  209.             ->cc($_ENV['ADMIN_EMAIL'])
  210.             ->subject($subject)
  211.             ->text($text)
  212.             ->htmlTemplate('api/emails/user-orders-template.html.twig')
  213.             ->context([
  214.                 'data' => $data,
  215.             ]);
  216.         $mailer->send($email);
  217.     }
  218.     /**
  219.      * Order update Error
  220.      *
  221.      * @Route("test")
  222.      */
  223.     public function test(){
  224.         return $this->render('api/order/payment-success.html.twig', [
  225.             'redirectUrl' => 'sdasda',
  226.             'msg' => 'paymentdf fjkfbd',
  227.         ]);
  228.     }
  229.     /**
  230.      * @Route("/update-premium-ads", name="update_premium_ads")
  231.      */
  232.     public function updatePremiumAds()
  233.     {
  234.         $em $this->getDoctrine()->getManager();
  235.         $ads $em->getRepository(Ads::class)->findBy(['urgent' => true]);
  236.         $em  $this->getDoctrine()->getManager();
  237.         foreach ($ads as $ad) {
  238.             $premiumExpiryDate $ad->getPremiumExpiryDate();
  239.             if($premiumExpiryDate){
  240.                 $premiumExpiryDate $premiumExpiryDate->format('Y-m-d');
  241.                 $curdate=strtotime(date("Y-m-d"));
  242.                 $mydate=strtotime($premiumExpiryDate);
  243.                 if($curdate $mydate)
  244.                 {
  245.                     $ad->setUrgent(false);
  246.                     $em->persist($ad);
  247.                     $em->flush();
  248.                 }
  249.             }
  250.         }
  251.         return new JsonResponse('Ads Update Successfully');
  252.     }
  253. }