<?php
namespace App\Controller;
use App\Entity\Ads;
use App\Entity\Order;
use App\Entity\ShippingAddress;
use App\Payment\PayPalClient;
//use http\Env\Response;
use http\Client\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="homepage")
*/
public function index()
{
return $this->redirectToRoute('app_login');
}
/**
* @Route("/email")
*/
public function checkMailTemplate(Request $request){
$baseurlPublic = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath().'/uploads/images/';
$em = $this->getDoctrine()->getManager();
/*Send Email to user for order place*/
$data = [];
$data['firstName'] = '';
$data['lastname'] = '';
$data['address'] = '';
$shippingAddress = $em->getRepository(ShippingAddress::class)->find(1);
$order = $em->getRepository(Order::class)->find(13);
$data['firstName'] = $shippingAddress->getFirstName();
$data['lastName'] = $shippingAddress->getLastName();
$data['address'] = $shippingAddress->getAddress();
$data['city'] = $shippingAddress->getCity();
$data['state'] = $shippingAddress->getState();
$data['mobile'] = $shippingAddress->getMobile();
$data['country'] = $shippingAddress->getCountry();
$data['zip'] = $shippingAddress->getZipcode();
$data['orderId'] = $order->getOrderId();
$data['date'] = date("d M Y");
$data['total'] = $order->getAmount();
$data['products'] = $order->getProduct();
$data['image_base_url'] = $baseurlPublic;
$data['text'] = 'order placed successfully';
return $this->render('api/emails/user-orders-template.html.twig', [
'data' => $data,
]);
}
/**
* Order update Success
*
* @Route("order-update-success")
* @param MailerInterface $mailer
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function orderUpdateSuccess(MailerInterface $mailer, Request $request)
{
/*Update order status*/
$baseurlPublic = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath().'/uploads/images/';
$em = $this->getDoctrine()->getManager();
$token = $request->query->get('token', null);
try {
/*Update order status*/
if($token){
$order = $em->getRepository(Order::class)->findOneBy(['token'=>$token]);
if($order){
/*Order capture For mobile app*/
$request = new OrdersCaptureRequest($order->getOrderId());
$client = PayPalClient::client();
$response = $client->execute($request);
if($response->result->status === Order::COMPLETED){
$order->setStatus(Order::COMPLETED);
$order->setResponseMeta(null);
if(!empty($order->getAds())){
$adId = $order->getAds()->getId();
/*Added add in order for premimum post*/
$ad = $em->getRepository(Ads::class)->find($adId);
$ad->setUrgent(true);
$em->persist($ad);
}
$em->persist($order);
$em->flush();
$user = $order->getUser();
$type = $order->getType();
if($type === 'CART'){
$shippingId = $order->getShipping()->getID();
/*Send Email to user for order place*/
$subject = 'Order Placed Successfully';
$text = 'Your Order has been placed successfully #'.$order->getOrderId();
$this->sendUserEmail($mailer, $user->getEmail(), $shippingId, $order, $text, $subject, $baseurlPublic);
/*Send Email to user for order place*/
$adminEmail = $_ENV['ADMIN_EMAIL'];
$subject = 'Order received';
$text = 'You have received order #'.$order->getOrderId();
$this->sendUserEmail($mailer, $adminEmail, $shippingId, $order, $text, $subject, $baseurlPublic);
}
$url = 'freshyhelpline://order/'.$order->getId();
return $this->render('api/order/payment-success.html.twig', [
'redirectUrl' => $url,
'msg' => 'Your payment has been done successfully',
]);
//return $this->success('Order update successully', ['Default']);
}else{
$url = 'freshyhelpline://order/';
return $this->render('api/order/payment-error.html.twig', [
'redirectUrl' => $url,
'msg' => 'Something went wrong, Please try again later',
]);
}
}
}
$url = 'freshyhelpline://order/';
return $this->render('api/order/payment-error.html.twig', [
'redirectUrl' => $url,
'msg' => 'Token id missing',
]);
} catch (\Throwable $e) {
$url = 'freshyhelpline://order/';
return $this->render('api/order/payment-error.html.twig', [
'redirectUrl' => $url,
'msg' => $e->getMessage(),
]);
}
}
/**
* Order update Error
*
* @Route("order-update-error")
*/
public function orderUpdateError(Request $request)
{
$em = $this->getDoctrine()->getManager();
$token = $request->query->get('token', null);
try {
/*Update order status*/
if($token){
$order = $em->getRepository(Order::class)->findOneBy(['token'=> $token]);
if($order){
$order->setStatus(Order::DECLINED);
$order->setResponseMeta(null);
$em->persist($order);
$em->flush();
$url = 'freshyhelpline://order/';
return $this->render('api/order/payment-error.html.twig', [
'redirectUrl' => $url,
'msg' => 'Something went wrong, Please try again later',
]);
}
}else{
$url = 'freshyhelpline://order/';
return $this->render('api/order/payment-error.html.twig', [
'redirectUrl' => $url,
'msg' => 'Token is missing',
]);
}
//return $this->failure(401, 'Order id missing');
} catch (\Throwable $e) {
$url = 'freshyhelpline://order/';
return $this->render('api/order/payment-error.html.twig', [
'redirectUrl' => $url,
'msg' => $e->getMessage(),
]);
}
}
public function sendUserEmail($mailer, $email, $shippingId, $order, $text, $subject, $baseurlPublic){
$em = $this->getDoctrine()->getManager();
/*Send Email to user for order place*/
$data = [];
$data['firstName'] = '';
$data['lastname'] = '';
$data['address'] = '';
$data['city'] = '';
$data['state'] = '';
$data['mobile'] = '';
$data['country'] = '';
$data['zip'] = '';
if($shippingId){
$shippingAddress = $em->getRepository(ShippingAddress::class)->find($shippingId);
$data['firstName'] = $shippingAddress->getFirstName();
$data['lastName'] = $shippingAddress->getLastName();
$data['address'] = $shippingAddress->getAddress();
$data['city'] = $shippingAddress->getCity();
$data['state'] = $shippingAddress->getState();
$data['mobile'] = $shippingAddress->getMobile();
$data['country'] = $shippingAddress->getCountry();
$data['zip'] = $shippingAddress->getZipcode();
}
$data['orderId'] = $order->getOrderId();
$data['date'] = date("d M Y");
$data['total'] = $order->getAmount();
$data['products'] = $order->getProduct();
$data['image_base_url'] = $baseurlPublic;
$data['text'] = $text;
$userEmail = $email;
$this->sendEmail($mailer, $userEmail, $subject, $text, $data);
}
public function sendEmail($mailer, $email, $subject, $text, $data){
$email = (new TemplatedEmail())
->from($_ENV['ADMIN_EMAIL'])
->to($email)
->cc($_ENV['ADMIN_EMAIL'])
->subject($subject)
->text($text)
->htmlTemplate('api/emails/user-orders-template.html.twig')
->context([
'data' => $data,
]);
$mailer->send($email);
}
/**
* Order update Error
*
* @Route("test")
*/
public function test(){
return $this->render('api/order/payment-success.html.twig', [
'redirectUrl' => 'sdasda',
'msg' => 'paymentdf fjkfbd',
]);
}
/**
* @Route("/update-premium-ads", name="update_premium_ads")
*/
public function updatePremiumAds()
{
$em = $this->getDoctrine()->getManager();
$ads = $em->getRepository(Ads::class)->findBy(['urgent' => true]);
$em = $this->getDoctrine()->getManager();
foreach ($ads as $ad) {
$premiumExpiryDate = $ad->getPremiumExpiryDate();
if($premiumExpiryDate){
$premiumExpiryDate = $premiumExpiryDate->format('Y-m-d');
$curdate=strtotime(date("Y-m-d"));
$mydate=strtotime($premiumExpiryDate);
if($curdate > $mydate)
{
$ad->setUrgent(false);
$em->persist($ad);
$em->flush();
}
}
}
return new JsonResponse('Ads Update Successfully');
}
}