src/Controller/Website/CartController.php line 4210

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Website;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use EADPlataforma\Entity\Cart;
  8. use EADPlataforma\Entity\Product;
  9. use EADPlataforma\Entity\ProductCharge;
  10. use EADPlataforma\Entity\ProductOffer;
  11. use EADPlataforma\Entity\ProductCoupon;
  12. use EADPlataforma\Entity\ProductOpportunity;
  13. use EADPlataforma\Entity\ProductSuggestion;
  14. use EADPlataforma\Entity\Whishlist;
  15. use EADPlataforma\Entity\Transaction;
  16. use EADPlataforma\Entity\TransactionItem;
  17. use EADPlataforma\Entity\User;
  18. use EADPlataforma\Entity\Country;
  19. use EADPlataforma\Entity\State;
  20. use EADPlataforma\Entity\City;
  21. use EADPlataforma\Entity\UserCard;
  22. use EADPlataforma\Entity\UserCheckoutInfo;
  23. use EADPlataforma\Entity\UserSubscription;
  24. use EADPlataforma\Entity\UserCustomField;
  25. use EADPlataforma\Entity\Course;
  26. use EADPlataforma\DataTransferObject\UserDTO;
  27. use EADPlataforma\Enum\CartEnum;
  28. use EADPlataforma\Enum\ProductEnum;
  29. use EADPlataforma\Enum\ProductChargeEnum;
  30. use EADPlataforma\Enum\GeoDataEnum;
  31. use EADPlataforma\Enum\ReceiverEnum;
  32. use EADPlataforma\Enum\TransactionEnum;
  33. use EADPlataforma\Enum\TransactionItemEnum;
  34. use EADPlataforma\Enum\ProductOfferEnum;
  35. use EADPlataforma\Enum\ProductCouponEnum;
  36. use EADPlataforma\Enum\ProductSuggestionEnum;
  37. use EADPlataforma\Enum\ProductOpportunityEnum;
  38. use EADPlataforma\Enum\TagsMarketingEnum;
  39. use EADPlataforma\Enum\UserEnum;
  40. use EADPlataforma\Enum\UserCardEnum;
  41. use EADPlataforma\Enum\UserCheckoutInfoEnum;
  42. use EADPlataforma\Enum\UserSubscriptionEnum;
  43. use EADPlataforma\Enum\WhishlistEnum;
  44. use EADPlataforma\Enum\EnrollmentEnum;
  45. use EADPlataforma\Enum\NotificationEnum;
  46. use EADPlataforma\Enum\WebhookEnum;
  47. use EADPlataforma\Enum\UserCustomFieldEnum;
  48. use EADPlataforma\Enum\CourseEnum;
  49. use EADPlataforma\Enum\TrashEnum;
  50. use EADPlataforma\Enum\ErrorEnum;
  51. /**
  52.  * @Route(
  53.  *      schemes         = {"http|https"}
  54.  * )
  55.  * @Cache(
  56.  *      maxage          = "0",
  57.  *      smaxage         = "0",
  58.  *      expires         = "now",
  59.  *      public          = false
  60.  * )
  61.  */
  62. class CartController extends AbstractWebsiteController {
  63.     /**
  64.      * @Route(
  65.      *      path          = "/installment/interest",
  66.      *      name          = "getInstallmentInterest",
  67.      *      methods       = {"GET"}
  68.      * )
  69.      */
  70.     public function getInstallmentInterest(Request $request) {
  71.         $this->requestUtil->setRequest($request)->setData();
  72.         $numberUtil $this->generalService->getUtil('NumberUtil');
  73.         $installments = (int)$this->requestUtil->getField('installments');
  74.         $freeInstallments = (int)$this->requestUtil->getField('freeInstallments');
  75.         $amount = (float)$this->requestUtil->getField('amount');
  76.         $productOfferIds $this->requestUtil->getField('productIds');
  77.         $productOfferIds json_decode($productOfferIds);
  78.         $poRepository $this->em->getRepository(ProductOffer::class);
  79.         
  80.         if(empty($freeInstallments)){
  81.             $freeInstallments CartEnum::YES;
  82.         }
  83.         $pagarMeTransaction $this->generalService->getService(
  84.             'PagarMe\\PagarMeTransaction'
  85.         );
  86.         $paymentConfig $this->configuration->getPaymentConfig();
  87.         $parcelItems = [];
  88.         if(!empty($productOfferIds)){
  89.             foreach ($productOfferIds as $key => $offerId) {
  90.                 $productOffer $poRepository->find($offerId);
  91.                 $parcels = (array) $pagarMeTransaction->calculateInstallments([
  92.                     'amount' => $numberUtil->numberToCents($productOffer->getPriceReal()),
  93.                     'free_installments' => $freeInstallments,
  94.                     'max_installments' => $installments,
  95.                     'interest_rate' => $paymentConfig->installmentInterest
  96.                 ]);
  97.                 foreach ($parcels as $key => $value) {
  98.                     $value->amount $numberUtil->centsToNumber($value->amount);
  99.                     $value->installment_amount $numberUtil->centsToNumber(
  100.                         $value->installment_amount
  101.                     );
  102.                     $parcels[$key] = $value;
  103.                 }
  104.                 $parcelItems[$offerId] = $parcels;
  105.             }
  106.         }
  107.         $installmentsOptions = (array) $pagarMeTransaction->calculateInstallments([
  108.             'amount' => $numberUtil->numberToCents($amount),
  109.             'free_installments' => $freeInstallments,
  110.             'max_installments' => $installments,
  111.             'interest_rate' => $paymentConfig->installmentInterest
  112.         ]);
  113.         if(empty($installmentsOptions[$installments])){
  114.             return $this->eadResponse([
  115.                 "parcelItems" => $parcelItems,
  116.                 "installmentsOptions" => [
  117.                     "1" => [
  118.                         "installment" => 1,
  119.                         "amount" => $amount,
  120.                         "installment_amount" => $amount,
  121.                     ]
  122.                 ],
  123.                 "amountTotal" => $amount,
  124.                 "amountParcel" => $amount
  125.             ]);
  126.         }
  127.         foreach ($installmentsOptions as $key => $value) {
  128.             $value->amount $numberUtil->centsToNumber($value->amount);
  129.             $value->installment_amount $numberUtil->centsToNumber(
  130.                 $value->installment_amount
  131.             );
  132.             $installmentsOptions[$key] = $value;
  133.         }
  134.         return $this->eadResponse([
  135.             "parcelItems" => $parcelItems,
  136.             "installmentsOptions" => $installmentsOptions,
  137.             "amountTotal" => $installmentsOptions[$installments]->amount,
  138.             "amountParcel" => $installmentsOptions[$installments]->installment_amount
  139.         ]);
  140.     }
  141.     /**
  142.      * @Route(
  143.      *      path          = "/charge/payment/{hash}",
  144.      *      name          = "getChargePage",
  145.      *      methods       = {"GET"}
  146.      * )
  147.      */
  148.     public function getChargePage(Request $request) {
  149.         if(!$this->configuration->isModuleActive("product_charge_module")){
  150.             return $this->redirectToRoute('notFound');
  151.         }
  152.         
  153.         $hash $request->get('hash');
  154.         $debug = (int)$request->get('debug');
  155.         $productCharge $this->em->getRepository(ProductCharge::class)->findOneBy([
  156.             "chargeKey" => $hash,
  157.             "deleted" => ProductChargeEnum::ITEM_NO_DELETED
  158.         ]);
  159.         
  160.         if (!$productCharge) {
  161.             return $this->redirectToRoute('notFound');
  162.         }
  163.         if($productCharge->getPublish() == ProductChargeEnum::NO){
  164.             return $this->redirectToRoute('notFound');
  165.         }
  166.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  167.         $user $productCharge->getUser();
  168.         $numberUserCheckoutInfo $userCheckoutInfoRepository->count([
  169.             "user" => $user->getId(),
  170.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
  171.         ]);
  172.         if(empty($numberUserCheckoutInfo)){
  173.             $userCheckoutInfo = new UserCheckoutInfo();
  174.             $userCheckoutInfo->setName($user->getName());
  175.             $userCheckoutInfo->setEmail($user->getEmail());
  176.             $userCheckoutInfo->setDocument($user->getDocument());
  177.             $userCheckoutInfo->setPhone($user->getPhone());
  178.             $userCheckoutInfo->setZipCode($user->getZipCode());
  179.             $userCheckoutInfo->setAddress($user->getAddress());
  180.             $userCheckoutInfo->setAddressNumber($user->getAddressNumber());
  181.             $userCheckoutInfo->setAddressComplement($user->getAddressComplement());
  182.             $userCheckoutInfo->setAddressNeighborhood($user->getAddressNeighborhood());
  183.             $userCheckoutInfo->setCity($user->getCity());
  184.             $userCheckoutInfo->setState($user->getState());
  185.             $userCheckoutInfo->setCountry($user->getCountry());
  186.             $userCheckoutInfo->setUser($user);
  187.             $userCheckoutInfo->setReceiveEmail(UserCheckoutInfoEnum::NO);
  188.             $userCheckoutInfo->setDefault(UserCheckoutInfoEnum::YES);
  189.             $this->em->persist($userCheckoutInfo);
  190.         }
  191.         
  192.         if($productCharge->getStatus() == ProductChargeEnum::WAITING){
  193.             $today strtotime(date('Y-m-d'));
  194.             $dateExpire strtotime($productCharge->getDateExpire());
  195.             if($today $dateExpire){
  196.                 $productCharge->setStatus(ProductChargeEnum::EXPIRED_PAYMENT);
  197.             }
  198.         }
  199.         $this->em->flush();
  200.         $userCheckoutInfos $userCheckoutInfoRepository->findBy([
  201.             "user" => $user->getId(),
  202.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
  203.         ]);
  204.         $userCardRepository $this->em->getRepository(UserCard::class);
  205.         
  206.         $userCards null;
  207.         if($this->user && $this->user->getId() == $user->getId()){
  208.             $userCards $userCardRepository->getValidUserCard();
  209.         }
  210.         $pagarMeTransaction $this->generalService->getService(
  211.             'PagarMe\\PagarMeTransaction'
  212.         );
  213.         $paymentConfig $this->configuration->getPaymentConfig();
  214.         $this->data['installmentsOptions'] = (array) $pagarMeTransaction->calculateInstallments([
  215.             'amount' => $productCharge->getAmount(true),
  216.             'free_installments' => $productCharge->getInstallmentNumberInterest(),
  217.             'max_installments' => $productCharge->getInstallmentNumberMax(),
  218.             'interest_rate' => $paymentConfig->installmentInterest
  219.         ]);
  220.         $this->data['productCharge'] = $productCharge;
  221.         $this->data['userCheckoutInfos'] = $userCheckoutInfos;
  222.         $this->data['userCards'] = $userCards;
  223.         $this->data['debug'] = $debug;
  224.         return $this->renderEAD('cart/charge/charge.html.twig');
  225.     }
  226.     /**
  227.      * @Route(
  228.      *      path          = "/cart/opportunity/{hash}",
  229.      *      name          = "getCartOpportunity",
  230.      *      methods       = {"GET"}
  231.      * )
  232.      */
  233.     public function getCartOpportunity(Request $request) {
  234.         
  235.         $hash $request->get('hash');
  236.         $cartRepository $this->em->getRepository(Cart::class);
  237.         $userRepository $this->em->getRepository(User::class);
  238.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  239.         $productOpportunityRepository $this->em->getRepository(ProductOpportunity::class);
  240.         $op = (object)$productOpportunityRepository->getDetailProductOpportunityByHash(
  241.             $hash
  242.         );
  243.         $user $userRepository->findOneBy([
  244.             "id" => $op->user->userId,
  245.             "deleted" => UserEnum::ITEM_NO_DELETED,
  246.         ]);
  247.         if($user){
  248.             $this->generalService->setCookie('hashcartoff'$user->getHashIdentify());
  249.             if($this->user && $this->user->getId() != $user->getId()){
  250.                 return $this->redirectToRoute('notFound');
  251.             }
  252.         }
  253.         $this->requestUtil->setRequest($request)->setData();
  254.         $data $this->requestUtil->getData();
  255.         
  256.         $dataUtml = [
  257.             "utm_source"   => $request->get('utm_source'),
  258.             "utm_medium"   => $request->get('utm_medium'),
  259.             "utm_campaign" => $request->get('utm_campaign'),
  260.             "utm_term"     => $request->get('utm_term'),
  261.             "utm_content"  => $request->get('utm_content'),
  262.         ];
  263.         $utmsUrl json_encode($dataUtml);
  264.         foreach ($op->order as $key => $value){
  265.             $productOffer $productOfferRepository->findOneBy([
  266.                 "id" => $value->offerId,
  267.                 "deleted" => ProductOfferEnum::ITEM_NO_DELETED,
  268.             ]);
  269.             if($productOffer){
  270.                 $cartRepository->addCartByProductOffer(
  271.                     $productOffer
  272.                     $user
  273.                     null,
  274.                     null,
  275.                     $utmsUrl
  276.                 );
  277.             }
  278.         }
  279.         
  280.         return $this->redirectToRoute('cart');
  281.     }
  282.     /**
  283.      * @Route(
  284.      *      path          = "/cart/items/menu",
  285.      *      name          = "getCartItemsMenu",
  286.      *      methods       = {"GET"}
  287.      * )
  288.      */
  289.     public function getCartItemsMenu(Request $request) {
  290.         $itemsNumber = (int)$request->get('itemsNumber');
  291.         $this->data['itemsNumber'] = $itemsNumber;
  292.         return $this->renderEAD('includes/submenu-cart-items.html.twig');
  293.     }
  294.     /**
  295.      * @Route(
  296.      *      path          = "/cart/initiate/checkout",
  297.      *      name          = "cartInitiateCheckout",
  298.      *      methods       = {"GET"}
  299.      * )
  300.      */
  301.     public function cartInitiateCheckout(Request $request){
  302.         $pixelService $this->generalService->getService('Marketing\\PixelService');
  303.         $pixelService->sendConversion('InitiateCheckout');
  304.         return $this->eadResponse([ "message" => "Success" ]);
  305.     }
  306.     /**
  307.      * @Route(
  308.      *      path          = "/checkout/{type}/{productId}/{couponKey}/{productSuggestionId}",
  309.      *      name          = "checkoutOldCart",
  310.      *      methods       = {"GET"},
  311.      *      defaults      = { "couponKey" = 0, "productSuggestionId" = 0 },
  312.      *      requirements  = { "productId" = "\d+" }
  313.      * )
  314.      */
  315.     public function getCheckoutOldCart(Request $request) {
  316.         $couponKey $request->get('couponKey');
  317.         $dataUtml = [
  318.             "utm_source" => $request->get('utm_source'),
  319.             "utm_medium" => $request->get('utm_medium'),
  320.             "utm_campaign" => $request->get('utm_campaign'),
  321.             "utm_term" => $request->get('utm_term'),
  322.             "utm_content" => $request->get('utm_content'),
  323.         ];
  324.         $product $this->em->getRepository(Product::class)->findOneBy([ 
  325.             "id" => (int)$request->get('productId')
  326.         ]);
  327.         if(!$product){
  328.             return $this->redirectToRoute('notFound');
  329.         }
  330.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  331.         $productOffer $productOfferRepository->getProductOfferDefaultByProduct(
  332.             $product
  333.         );
  334.         if(!$productOffer){
  335.             return $this->redirectToRoute('notFound');
  336.         }
  337.         $productCoupon null;
  338.         if(!empty($couponKey)){
  339.             $productCouponRepository $this->em->getRepository(ProductCoupon::class);
  340.             $productCoupon $productCouponRepository->findValidProductCouponByProductOffer(
  341.                 $couponKey
  342.                 $productOffer
  343.             );
  344.         }
  345.         if(
  346.             $product->getType() == ProductEnum::SUBSCRIPTION ||
  347.             (
  348.                 $product->getType() != ProductEnum::SUBSCRIPTION && 
  349.                 $productOffer->getAllowRecurrency() == ProductOfferEnum::YES
  350.             )
  351.         ){
  352.             if($productOffer->getAllowTrial() == ProductOfferEnum::YES){
  353.                 $params = [
  354.                     "poHash" => $productOffer->getOfferLink(),
  355.                     "pcKey" => $couponKey,
  356.                 ];
  357.                 $params array_merge($params$dataUtml);
  358.                 return $this->redirectToRoute('cartIndividual'$params);
  359.             }
  360.         }
  361.         $params = [
  362.             "poID" => $productOffer->getId(),
  363.             "pcID" => ( $productCoupon $productCoupon->getId() : ),
  364.             "psID" => $request->get('productSuggestionId'),
  365.         ];
  366.         $params array_merge($params$dataUtml);
  367.         return $this->redirectToRoute('cartAdd'$params);
  368.     }
  369.     /**
  370.      * @Route(
  371.      *      path          = "/checkout/offer/{type}/{productId}/{offerLink}/{courseId}",
  372.      *      name          = "checkoutNewCart",
  373.      *      methods       = {"GET"},
  374.      *      defaults      = { "courseId" = 0 },
  375.      *      requirements  = { "productId" = "\d+", "courseId" = "\d+" }
  376.      * )
  377.      */
  378.     public function getCheckoutNewCart(Request $request) {
  379.         $offerLink $request->get('offerLink');
  380.         $courseId = (int)$request->get('courseId');
  381.         $product $this->em->getRepository(Product::class)->findOneBy([ 
  382.             "id" => $request->get('productId')
  383.         ]);
  384.         if(!$product){
  385.             return $this->redirectToRoute('notFound');
  386.         }
  387.         $dataUtml = [
  388.             "utm_source" => $request->get('utm_source'),
  389.             "utm_medium" => $request->get('utm_medium'),
  390.             "utm_campaign" => $request->get('utm_campaign'),
  391.             "utm_term" => $request->get('utm_term'),
  392.             "utm_content" => $request->get('utm_content'),
  393.         ];
  394.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  395.         $productOffer $productOfferRepository->getProductOfferDefaultByProduct(
  396.             $product
  397.             $offerLink
  398.         );
  399.         if(!$productOffer){
  400.             return $this->redirectToRoute('notFound');
  401.         }
  402.         if(
  403.             $product->getType() == ProductEnum::SUBSCRIPTION || 
  404.             (
  405.                 $product->getType() != ProductEnum::SUBSCRIPTION && 
  406.                 $productOffer->getAllowRecurrency() == ProductOfferEnum::YES
  407.             )
  408.         ){
  409.             if($productOffer->getAllowTrial() == ProductOfferEnum::YES){
  410.                 $params = [
  411.                     "poHash" => $productOffer->getOfferLink(),
  412.                     "pcKey" => null,
  413.                 ];
  414.                 $params array_merge($params$dataUtml);
  415.                 return $this->redirectToRoute('cartIndividual'$params);
  416.             }
  417.         }
  418.         $params = [
  419.             "poID" => $productOffer->getId(),
  420.             "courseId" => $courseId,
  421.         ];
  422.         $params array_merge($params$dataUtml);
  423.         return $this->redirectToRoute('cartAdd'$params);
  424.     }
  425.     /**
  426.      * @Route(
  427.      *      path          = "/checkout/coupon/{type}/{productId}/{productCouponId}",
  428.      *      name          = "checkoutCartWithCoupon",
  429.      *      methods       = {"GET"},
  430.      *      requirements  = { "productId" = "\d+", "productCouponId" = "\d+" }
  431.      * )
  432.      */
  433.     public function getCheckoutCartWithCoupon(Request $request) {
  434.         $product $this->em->getRepository(Product::class)->findOneBy([ 
  435.             "id" => (int)$request->get('productId')
  436.         ]);
  437.         if(!$product){
  438.             return $this->redirectToRoute('notFound');
  439.         }
  440.         $dataUtml = [
  441.             "utm_source" => $request->get('utm_source'),
  442.             "utm_medium" => $request->get('utm_medium'),
  443.             "utm_campaign" => $request->get('utm_campaign'),
  444.             "utm_term" => $request->get('utm_term'),
  445.             "utm_content" => $request->get('utm_content'),
  446.         ];
  447.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  448.         $productOffer $productOfferRepository->getProductOfferDefaultByProduct(
  449.             $product 
  450.         );
  451.         if(!$productOffer){
  452.             return $this->redirectToRoute('notFound');
  453.         }
  454.         if(
  455.             $product->getType() == ProductEnum::SUBSCRIPTION ||
  456.             (
  457.                 $product->getType() != ProductEnum::SUBSCRIPTION && 
  458.                 $productOffer->getAllowRecurrency() == ProductOfferEnum::YES
  459.             )
  460.         ){
  461.             if($productOffer->getAllowTrial() == ProductOfferEnum::YES){
  462.                 $params = [
  463.                     "poHash" => $productOffer->getOfferLink(),
  464.                     "pcKey" => null,
  465.                 ];
  466.                 $params array_merge($params$dataUtml);
  467.                 return $this->redirectToRoute('cartIndividual'$params);
  468.             }
  469.         }
  470.         $params = [
  471.             "poID" => $productOffer->getId(),
  472.             "pcID" => (int)$request->get('productCouponId'),
  473.         ];
  474.         $params array_merge($params$dataUtml);
  475.         return $this->redirectToRoute('cartAdd'$params);
  476.     }
  477.     /**
  478.      * @Route(
  479.      *      path          = "/cart/add/{poID}/{psID}/{pcID}/{isAjaxRequest}/{courseId}",
  480.      *      name          = "cartAdd",
  481.      *      methods       = {"GET"},
  482.      *      defaults      = { 
  483.      *                          "psID" = 0, 
  484.      *                          "pcID" = 0, 
  485.      *                          "isAjaxRequest" = 0, 
  486.      *                          "courseId" = 0 
  487.      *                      }
  488.      * )
  489.      */
  490.     public function registerCart(Request $request) {
  491.         $cartRepository $this->em->getRepository(Cart::class);
  492.         $productOfferId = (int)$request->get('poID');
  493.         $productSuggestionId = (int)$request->get('psID');
  494.         $productCouponId = (int)$request->get('pcID');
  495.         $isAjaxRequest = (int)$request->get('isAjaxRequest');
  496.         $courseId = (int)$request->get('courseId');
  497.         $hash base64_encode($request->getUri());
  498.         $this->requestUtil->setRequest($request)->setData();
  499.         $data $this->requestUtil->getData();
  500.         
  501.         $dataUtml = [
  502.             "utm_source" => $request->get('utm_source'),
  503.             "utm_medium" => $request->get('utm_medium'),
  504.             "utm_campaign" => $request->get('utm_campaign'),
  505.             "utm_term" => $request->get('utm_term'),
  506.             "utm_content" => $request->get('utm_content'),
  507.         ];
  508.         $utmsUrl json_encode($dataUtml);
  509.         $course $this->em->getRepository(Course::class)->findOneBy([ 
  510.             "id" => $courseId
  511.             "status" => CourseEnum::PUBLISHED,
  512.             "deleted" => CourseEnum::ITEM_NO_DELETED
  513.         ]);
  514.         $productOffer $this->em->getRepository(ProductOffer::class)->findOneBy([ 
  515.             "id" => $productOfferId
  516.             "status" => ProductOfferEnum::PUBLISHED,
  517.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  518.         ]);
  519.         if(!$productOffer){
  520.             if($isAjaxRequest == CartEnum::YES){
  521.                 return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  522.             }
  523.             return $this->redirectToRoute('notFound');
  524.         }
  525.         $hasModule $this->configuration->isModuleActive("product_coupon_module");
  526.         $enrollmentService $this->generalService->getService('EnrollmentService');
  527.         $pcRepository $this->em->getRepository(ProductCoupon::class);
  528.         $psRepository $this->em->getRepository(ProductSuggestion::class);
  529.         $productSuggestion null;
  530.         if(!empty($productSuggestionId)){
  531.             $productSuggestion $psRepository->findOneBy([ 
  532.                 "id" => $productSuggestionId
  533.                 "status" => ProductSuggestionEnum::PUBLISHED,
  534.                 "deleted" => ProductSuggestionEnum::ITEM_NO_DELETED
  535.             ]);
  536.         }
  537.         $productCoupon null;
  538.         $product $productOffer->getProduct();
  539.         if($hasModule){
  540.             $productCoupon $pcRepository->findValidProductCouponByIdAndProductOffer(
  541.                 $productCouponId,
  542.                 $productOffer
  543.             );
  544.             if($productCoupon && $this->user){
  545.                 if($pcRepository->checkApplyCoupon($productCoupon$productOffer)){
  546.                     $price $pcRepository->applyDiscount(
  547.                         $productCoupon,
  548.                         $productOffer->getPriceReal()
  549.                     );
  550.                     if(empty($price)){
  551.                         $enrollmentService->setOrigin(EnrollmentEnum::ORIGIN_COUPOM);
  552.                         $enrollmentService->setCouponKey($productCoupon->getKey());
  553.                         $enrollmentService->enrollUserByProduct(
  554.                             $this->user
  555.                             $product,
  556.                             ($product->isTypeResource() ? $course null)
  557.                         );
  558.                         return $this->redirectToRoute('resume');
  559.                     }
  560.                 }
  561.             }
  562.         }
  563.         if($productOffer->getSaleChannel() == ProductOfferEnum::EXTERNAL){
  564.             $externalCheckoutLink $productOffer->getExternalCheckoutLink();
  565.             if(!empty($externalCheckoutLink)){
  566.                 return $this->redirect($externalCheckoutLink301);
  567.             }
  568.         }
  569.         $cartRepository->addCartByProductOffer(
  570.             $productOffer
  571.             $this->user
  572.             $productSuggestion,
  573.             $productCoupon,
  574.             $utmsUrl,
  575.             ($course ? [ $course ] : null)
  576.         );
  577.         $allowSuggestion = (
  578.             !$product->isTypeResource() || 
  579.             ($course && $product->isTypeResource())
  580.         );
  581.         $psRedirect null;
  582.         if($allowSuggestion){
  583.             $psRedirect $psRepository->getProductSuggestionRedirectByProductOffer(
  584.                 $productOffer
  585.                 ProductSuggestionEnum::EXECUTE_ON_ADD_CART
  586.             );
  587.         }
  588.         if($isAjaxRequest == CartEnum::YES){
  589.             return $this->eadResponse([
  590.                 "cartItemsQuantity" => $cartRepository->countUserOnCart(),
  591.                 "urlRedirect" => ( $psRedirect $psRedirect->getLinkRedirect() : null ),
  592.                 "success" => CartEnum::YES
  593.             ]);
  594.         }
  595.         if($psRedirect){
  596.             return $this->redirect($psRedirect->getLinkRedirect());
  597.         }
  598.         $productOfferSuggestions null;
  599.         if($allowSuggestion){
  600.             $productOfferSuggestions $psRepository->getProductSuggestionsByOfferOrigin(
  601.                 $productOffer
  602.                 ProductSuggestionEnum::EXECUTE_ON_ADD_CART
  603.             );
  604.         }
  605.         if(!empty($productOfferSuggestions)){
  606.             if($productCoupon && $hasModule){
  607.                 foreach ($productOfferSuggestions as $key => $offerSuggestion) {
  608.                     $pcSuggestion $pcRepository->findValidProductCouponByIdAndProductOffer(
  609.                         $productCoupon->getId(), 
  610.                         $offerSuggestion,
  611.                         true
  612.                     );
  613.                     if($pcSuggestion){
  614.                         $amount $offerSuggestion->getPriceReal();
  615.                         if($pcRepository->checkApplyCoupon($pcSuggestion$offerSuggestion)){
  616.                             $amount $pcRepository->applyDiscount($pcSuggestion$amount);
  617.                         }
  618.                         if(!empty($amount)){
  619.                             $offerSuggestion->setPriceRealCopy($amount);
  620.                             $productOfferSuggestions[$key] = $offerSuggestion;
  621.                         }else{
  622.                             if($this->user){
  623.                                 $pcSuggestion->setUsageNumber(
  624.                                     $pcSuggestion->getUsageNumber() + 1
  625.                                 );
  626.                                 $this->em->flush();
  627.                                 $offerProd $offerSuggestion->getProduct();
  628.                                 $enrollmentService->setOrigin(EnrollmentEnum::ORIGIN_COUPOM);
  629.                                 $enrollmentService->setCouponKey($couponKey);
  630.                                 $enrollmentService->enrollUserByProduct(
  631.                                     $this->user,
  632.                                     $offerProd,
  633.                                     ($offerProd->isTypeResource() ? $course null)
  634.                                 );
  635.                                 unset($productOfferSuggestions[$key]);
  636.                             }
  637.                         }
  638.                     }
  639.                 }
  640.             }
  641.             $this->data['productOfferId'] = $productOfferId;
  642.             $this->data['productCoupon'] = $productCoupon;
  643.             $this->data['productOfferSuggestions'] = $productOfferSuggestions;
  644.             return $this->renderEAD('cart/suggestions.html.twig');
  645.         }
  646.         
  647.         if($productCoupon){
  648.             return $this->redirectToRoute('cart', [ 
  649.                 "coupon" => $productCoupon->getKey()
  650.             ]);
  651.         }
  652.         return $this->redirectToRoute('cart');
  653.     }
  654.     /**
  655.      * @Route(
  656.      *      path          = "/cart/add/suggestion/{productOfferId}/{productCouponId}",
  657.      *      name          = "cartAddSuggestion",
  658.      *      methods       = {"POST"},
  659.      *      defaults      = { "productCouponId" = 0 }
  660.      * )
  661.      */
  662.     public function registerCartBySuggestion(Request $request) {
  663.         $this->requestUtil->setRequest($request)->setData();
  664.         $cartRepository $this->em->getRepository(Cart::class);
  665.         $productSuggestions json_decode(
  666.             $this->requestUtil->getField('productSuggestions')
  667.         );
  668.         if(empty($productSuggestions)){
  669.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  670.         }
  671.         $productCouponId = (int)$request->get('productCouponId');
  672.         $productOfferId = (int)$request->get('productOfferId');
  673.         $hasModule $this->configuration->isModuleActive("product_coupon_module");
  674.         $enrollmentService $this->generalService->getService('EnrollmentService');
  675.         $pcRepository $this->em->getRepository(ProductCoupon::class);
  676.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  677.         $productOffer $productOfferRepository->findOneBy([ 
  678.             "id" => $productOfferId,
  679.             "status" => ProductOfferEnum::PUBLISHED,
  680.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  681.         ]);
  682.         if(!$productOffer){
  683.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  684.         }
  685.         if($productOffer->getSaleChannel() == ProductOfferEnum::EXTERNAL){
  686.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  687.         }
  688.         $product $productOffer->getProduct();
  689.         $productCouponOffer null;
  690.         $course null;
  691.         if(
  692.             $product->getType() == ProductEnum::COURSE ||
  693.             $product->getType() == ProductEnum::COMBO
  694.         ){
  695.             $courseRepository $this->em->getRepository(Course::class);
  696.             $courses $courseRepository->getCoursesByProduct($product->getId());
  697.         }
  698.         
  699.         if($hasModule){
  700.             $productCouponOffer $pcRepository->findValidProductCouponByIdAndProductOffer(
  701.                 $productCouponId
  702.                 $productOffer
  703.             );
  704.             if($productCouponOffer && $this->user){
  705.                 if($pcRepository->checkApplyCoupon($productCouponOffer$productOffer)){
  706.                     $priceOffer $pcRepository->applyDiscount(
  707.                         $productCouponOffer
  708.                         $priceOffer
  709.                     );
  710.                     if(empty($priceOffer)){
  711.                         $enrollmentService->setOrigin(EnrollmentEnum::ORIGIN_COUPOM);
  712.                         $enrollmentService->setCouponKey($productCouponOffer->getKey());
  713.                         $enrollmentService->enrollUserByProduct(
  714.                             $this->user
  715.                             $product,
  716.                             null,
  717.                             ($product->isTypeResource() ? $courses null)
  718.                         );
  719.                     }
  720.                 }
  721.             }
  722.         }
  723.         $cartRepository->addCartByProductOffer(
  724.             $productOffer,
  725.             $this->user,
  726.             null,
  727.             $productCouponOffer,
  728.             null,
  729.             ($product->isTypeResource() ? $courses null)
  730.         );
  731.         $psRepository $this->em->getRepository(ProductSuggestion::class);
  732.         foreach ($productSuggestions as $key => $productSuggestionId) {
  733.             $productSuggestion $psRepository->findOneBy([ 
  734.                 "id" => $productSuggestionId
  735.                 "status" => ProductSuggestionEnum::PUBLISHED,
  736.                 "deleted" => ProductSuggestionEnum::ITEM_NO_DELETED
  737.             ]);
  738.             if($productSuggestion){
  739.                 $offerSuggestion $productSuggestion->getProductOffer();
  740.                 $pcSuggestion null;
  741.                 $priceOfferSuggestion $offerSuggestion->getPriceReal();
  742.                 $offerProd $offerSuggestion->getProduct();
  743.                 if($hasModule){
  744.                     $pcSuggestion $pcRepository->findValidProductCouponByIdAndProductOffer(
  745.                         $productCouponId
  746.                         $offerSuggestion,
  747.                         true
  748.                     );
  749.                     if($pcSuggestion && $this->user){
  750.                         if($pcRepository->checkApplyCoupon($pcSuggestion$offerSuggestion)){
  751.                             $priceOfferSuggestion $pcRepository->applyDiscount(
  752.                                 $pcSuggestion
  753.                                 $priceOfferSuggestion
  754.                             );
  755.                             if(empty($priceOfferSuggestion)){
  756.                                 $enrollmentService->setOrigin(EnrollmentEnum::ORIGIN_COUPOM);
  757.                                 $enrollmentService->setCouponKey($pcSuggestion->getKey());
  758.                                 $enrollmentService->enrollUserByProduct(
  759.                                     $this->user,
  760.                                     $offerProd,
  761.                                     ($offerProd->isTypeResource() ? $courses null)
  762.                                 );
  763.                             }
  764.                         }
  765.                     }
  766.                 }
  767.                 $cartRepository->addCartByProductOffer(
  768.                     $offerSuggestion
  769.                     $this->user
  770.                     $productSuggestion,
  771.                     $pcSuggestion,
  772.                     null,
  773.                     ($offerProd->isTypeResource() ? $courses null)
  774.                 );
  775.             }
  776.         }
  777.         return $this->eadResponse([
  778.             "cartItemsQuantity" => $cartRepository->countUserOnCart(),
  779.             "success" => CartEnum::YES
  780.         ]);
  781.     }
  782.     /**
  783.      * @Route(
  784.      *      path          = "/cart/enroll/{productOfferId}",
  785.      *      name          = "cartCreateEnroll",
  786.      *      methods       = {"GET"}
  787.      * )
  788.      */
  789.     public function cartCreateEnroll(Request $request){
  790.         $this->checkUserSession($request);
  791.         $productOfferId $request->get('productOfferId');
  792.         $productOffer $this->em->getRepository(ProductOffer::class)->findOneBy([ 
  793.             "id" => $productOfferId
  794.             "status" => ProductOfferEnum::PUBLISHED,
  795.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  796.         ]);
  797.         if(!$productOffer){
  798.             return $this->redirectToRoute('notFound');
  799.         }
  800.         $product $productOffer->getProduct();
  801.         if($product->isDisable()){
  802.             return $this->redirectToRoute('notFound');
  803.         }
  804.         if($productOffer->getSaleOption() != ProductOfferEnum::FREE){
  805.             return $this->redirectToRoute('notFound');
  806.         }
  807.         $enrollmentService $this->generalService->getService('EnrollmentService');
  808.         $enrollmentService->setNotification(true);
  809.         $enrollmentService->enrollUserByProduct($this->user$product);
  810.         return $this->redirectToRoute('resume');
  811.     }
  812.     /**
  813.      * @Route(
  814.      *      path          = "/whishlist/add/{productId}",
  815.      *      name          = "addProductWhishlist",
  816.      *      methods       = {"GET"}
  817.      * )
  818.      */
  819.     public function addProductWhishlist(Request $request){
  820.         $this->checkUserSession($request);
  821.         $productId $request->get('productId');
  822.         $product $this->em->getRepository(Product::class)->findOneBy([ 
  823.             "id" => $productId
  824.             "status" => ProductEnum::PUBLISHED,
  825.             "deleted" => ProductEnum::ITEM_NO_DELETED
  826.         ]);
  827.         if(!$product){
  828.             return $this->redirectToRoute('notFound');
  829.         }
  830.         if($product->isDisable()){
  831.             return $this->redirectToRoute('notFound');
  832.         }
  833.         $productOfferId $request->get('productOfferId');
  834.         $productOffer $this->em->getRepository(ProductOffer::class)->findOneBy([ 
  835.             "id" => $productOfferId
  836.             "status" => ProductOfferEnum::PUBLISHED,
  837.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  838.         ]);
  839.         $whishlist = new Whishlist();
  840.         $whishlistSave $this->em->getRepository(Whishlist::class)->findOneBy([ 
  841.             "product" => $productId,
  842.             "user" => $this->user->getId(),
  843.         ]);
  844.         if($whishlistSave){
  845.             $whishlist $whishlistSave;
  846.         }
  847.         $whishlist->setUser($this->user);
  848.         $whishlist->setProduct($product);
  849.         if($productOffer){
  850.             $whishlist->setProductOffer($productOffer);
  851.         }
  852.         $errors $this->validateEntity($whishlist);
  853.         if($errors){
  854.             return $this->redirectToRoute('notFound');
  855.         }
  856.         if(!$whishlistSave) {
  857.             $this->em->persist($whishlist);
  858.         }
  859.         $this->em->flush();
  860.         return $this->redirectToRoute('resume');
  861.     }
  862.     /**
  863.      * @Route(
  864.      *      path          = "/apply",
  865.      *      name          = "applyCartCoupon",
  866.      *      methods       = {"POST"},
  867.      * )
  868.      */
  869.     public function applyProductCoupon(Request $request) {
  870.         if(!$this->configuration->isModuleActive("product_coupon_module")){
  871.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  872.         }
  873.         $this->requestUtil->setRequest($request)->setData();
  874.         
  875.         $couponKey $this->requestUtil->getField('couponKey');
  876.         $cookieName 'hashcartoff';
  877.         $hashId $this->generalService->getCookie($cookieName);
  878.         $cartRepository $this->em->getRepository(Cart::class);
  879.         $return $cartRepository->applyCouponUserCart($couponKey$this->user$hashId);
  880.         return $this->eadResponse($return);
  881.     }
  882.     /**
  883.      * @Route(
  884.      *      path          = "/apply/offer",
  885.      *      name          = "applyProductCouponOnOffer",
  886.      *      methods       = {"POST"},
  887.      * )
  888.      */
  889.     public function applyProductCouponOnOffer(Request $request) {
  890.         if(!$this->configuration->isModuleActive("product_coupon_module")){
  891.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  892.         }
  893.         $this->requestUtil->setRequest($request)->setData();
  894.         
  895.         $couponKey $this->requestUtil->getField('couponKey');
  896.         $productOfferIds $this->requestUtil->getField('productIds');
  897.         $cartIds $this->requestUtil->getField('cartIds');
  898.         if(empty($couponKey)){
  899.             return $this->eadResponse([ "couponKey" ], ErrorEnum::FIELD_EMPTY);
  900.         }
  901.         $cartIds json_decode($cartIds);
  902.         $productOfferIds json_decode($productOfferIds);
  903.         if(empty($productOfferIds) || !is_array($productOfferIds)){
  904.             return $this->eadResponse([ "productIds" ], ErrorEnum::FIELD_EMPTY);
  905.         }
  906.         $poRepository $this->em->getRepository(ProductOffer::class);
  907.         $pcRepository $this->em->getRepository(ProductCoupon::class);
  908.         $items = [];
  909.         $total 0;
  910.         $totalWithDiscount 0;
  911.         $find false;
  912.         foreach ($productOfferIds as $key => $offerId) {
  913.             $productOffer $poRepository->find($offerId);
  914.             $productCoupon $pcRepository->findValidProductCouponByProductOffer(
  915.                 $couponKey,
  916.                 $productOffer
  917.             );
  918.             $amountDisplay = (float)$productOffer->getPriceDisplay();
  919.             $amount = (float)$productOffer->getPriceReal();
  920.             if(in_array($offerId$cartIds)){
  921.                 $total $total + (!empty($amountDisplay) ? $amountDisplay $amount);
  922.             }
  923.             
  924.             if($productCoupon){
  925.                 $find true;
  926.                 $amount $pcRepository->applyDiscount($productCoupon$amount);
  927.             }
  928.             if(in_array($offerId$cartIds)){
  929.                 $totalWithDiscount $totalWithDiscount $amount;
  930.             }
  931.             $items[] = (object)[
  932.                 "id" => $offerId,
  933.                 "amount" => $amount,
  934.             ];
  935.         }
  936.         if(!$find){
  937.             return $this->eadResponse([ 
  938.                 "message" => "Invalid Coupon" 
  939.             ], ErrorEnum::ACTION_INVALID);
  940.         }
  941.         $data = [
  942.             "total" => $total,
  943.             "totalDiscount" => $total $totalWithDiscount,
  944.             "totalWithDiscount" => $totalWithDiscount,
  945.             "items" => $items,
  946.         ];
  947.         return $this->eadResponse($data);
  948.     }
  949.     /**
  950.      * @Route(
  951.      *      path          = "/cart",
  952.      *      name          = "cart",
  953.      *      methods       = {"GET"}
  954.      * )
  955.      */
  956.     public function cartPage(Request $request) {
  957.         $cookieName 'hashcartoff';
  958.         $hashId $this->generalService->getCookie($cookieName);
  959.         $cartRepository $this->em->getRepository(Cart::class);
  960.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  961.         $pcRepository $this->em->getRepository(ProductCoupon::class);
  962.         $cartRepository->updateCartsWithProductCouponExpired(
  963.             ($this->user $this->user->getId() : null),
  964.             $hashId
  965.         );
  966.         $couponKey $request->get('coupon');
  967.         $this->data['couponKeyValid'] = false;
  968.         if(!empty($couponKey)){
  969.             $return $cartRepository->applyCouponUserCart(
  970.                 $couponKey
  971.                 $this->user
  972.                 $hashId
  973.             );
  974.             $this->data['couponKeyValid'] = $return['apply'];
  975.         }
  976.         $this->data['formName'] = "formRegisterUserCart";
  977.         $this->data['carts'] = null;
  978.         $this->data['cartSubtotal'] = 0;
  979.         $this->data['cartDisplaySubtotal'] = 0;
  980.         $this->data['couponKey'] = $couponKey;
  981.         $this->data['showInfoStep'] = true;
  982.         $cartRepository $this->em->getRepository(Cart::class);
  983.         $customCart false;
  984.         $displayTotal 0;
  985.         $cartSubtotal 0;
  986.         $paymentConfig $this->configuration->getPaymentConfig();
  987.         if($this->user){
  988.             $userCheckoutInfoRepository $this->em->getRepository(
  989.                 UserCheckoutInfo::class
  990.             );
  991.             $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  992.                 "user" => $this->user->getId(),
  993.                 "default" => UserCheckoutInfoEnum::YES,
  994.                 "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
  995.             ]);
  996.             if($userCheckoutInfo){
  997.                 $this->data['showInfoStep'] = false;
  998.             }
  999.             $userId $this->user->getId();
  1000.             $carts $cartRepository->getUserValidCarts($userId);
  1001.             $userCards $this->em->getRepository(UserCard::class)->getValidUserCard();
  1002.             $customCart = (
  1003.                 $cartRepository->countCustomCheckout($userId) > true false
  1004.             );
  1005.             $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($userId);
  1006.             $cartSubtotal $cartRepository->getUserSubTotal($userId);
  1007.             $pagarMeTransaction $this->generalService->getService(
  1008.                 'PagarMe\\PagarMeTransaction'
  1009.             );
  1010.             $numberUtil $this->generalService->getUtil('NumberUtil');
  1011.             $methods = [
  1012.                 ProductOfferEnum::CARD,
  1013.                 ProductOfferEnum::BILL,
  1014.                 ProductOfferEnum::PIX,
  1015.             ];
  1016.             $cardOptions = [
  1017.                 ProductOfferEnum::ALL_METHODS,
  1018.                 ProductOfferEnum::CARD,
  1019.                 ProductOfferEnum::CARD_PIX,
  1020.                 ProductOfferEnum::CARD_BILL,
  1021.             ];
  1022.             $pixOptions = [
  1023.                 ProductOfferEnum::ALL_METHODS,
  1024.                 ProductOfferEnum::PIX,
  1025.                 ProductOfferEnum::CARD_PIX,
  1026.                 ProductOfferEnum::BILL_PIX,
  1027.             ];
  1028.             $billOptions = [
  1029.                 ProductOfferEnum::ALL_METHODS,
  1030.                 ProductOfferEnum::BILL,
  1031.                 ProductOfferEnum::CARD_BILL,
  1032.                 ProductOfferEnum::BILL_PIX,
  1033.             ];
  1034.             $getPaymentOption = function(ProductOffer $productOffer) use (
  1035.                 $productOfferRepository
  1036.                 $cardOptions
  1037.                 $pixOptions
  1038.                 $billOptions
  1039.             ){
  1040.                 $billTypeExpire ProductOfferEnum::BILL_PERIOD;
  1041.                 $billPeriodExpire 7;
  1042.                 $billDateExpire null;
  1043.                 $pixTypeExpire ProductOfferEnum::PIX_PERIOD;
  1044.                 $pixPeriodExpire 7;
  1045.                 $pixDateExpire null;
  1046.                 $installmentNumberMax 12;
  1047.                 $installmentNumberInterest 1;
  1048.                 $freeInstallments 1;
  1049.                 $paymentMethodOption $productOffer->getPaymentMethod();
  1050.                 if(in_array($paymentMethodOption$cardOptions)){
  1051.                     $installmentNumberMax $productOffer->getInstallmentNumberMax();
  1052.                     $installmentNumberInterest $productOffer->getInstallmentNumberInterest();
  1053.                     $freeInstallments $productOfferRepository->getFreeInstallment(
  1054.                         $productOffer
  1055.                     );
  1056.                 }else if(in_array($paymentMethodOption$pixOptions)){
  1057.                     $pixTypeExpire $productOffer->getPixTypeExpire();
  1058.                     $pixPeriodExpire $productOffer->getPixPeriodExpire();
  1059.                     $pixDateExpire $productOffer->getPixDateExpire();
  1060.                 }else if(in_array($paymentMethodOption$billOptions)){
  1061.                     $billTypeExpire $productOffer->getBillTypeExpire();
  1062.                     $billPeriodExpire $productOffer->getBillPeriodExpire();
  1063.                     $billDateExpire $productOffer->getBillDateExpire();
  1064.                 }
  1065.                 return (object)[
  1066.                     "typeCheckout" => $productOffer->getTypeCheckout(),
  1067.                     "saleOption" => $productOffer->getSaleOption(),
  1068.                     "paymentMethod" => $paymentMethodOption,
  1069.                     "billTypeExpire" => $billTypeExpire,
  1070.                     "billPeriodExpire" => $billPeriodExpire,
  1071.                     "billDateExpire" => $billDateExpire,
  1072.                     "pixTypeExpire" => $pixTypeExpire,
  1073.                     "pixPeriodExpire" => $pixPeriodExpire,
  1074.                     "pixDateExpire" => $pixDateExpire,
  1075.                     "installmentNumberMax" => $installmentNumberMax,
  1076.                     "installmentNumberInterest" => $installmentNumberInterest,
  1077.                     "freeInstallments" => $freeInstallments,
  1078.                     "items" => [],
  1079.                 ];
  1080.             };
  1081.             $offerPaymentOptions = [];
  1082.             $internalOptions = [
  1083.                 ProductOfferEnum::INTERNAL_EAD,
  1084.                 ProductOfferEnum::INTERNAL_PAYPAL,
  1085.                 ProductOfferEnum::INTERNAL_PAGSEGURO,
  1086.                 ProductOfferEnum::INTERNAL_MERCADOPAGO,
  1087.             ];
  1088.             foreach ($carts as $key => $cart) {
  1089.                 $productOffer $cart->getProductOffer();
  1090.                 $offerInternalOptions json_decode($productOffer->getSaleOptionInternal());
  1091.                 if(is_array($offerInternalOptions)){
  1092.                     $internalOptions array_intersect(
  1093.                         $internalOptions
  1094.                         $offerInternalOptions
  1095.                     );
  1096.                 }
  1097.                 $productOfferCouponTotal $pcRepository->countPublicCouponByProductOffer(
  1098.                     $productOffer
  1099.                 );
  1100.                 $cart->offerPaymentOptions null;
  1101.                 if($customCart){
  1102.                     $cart->offerPaymentOptions $getPaymentOption($productOffer);
  1103.                     $offerPaymentOptions[] = $cart->offerPaymentOptions;
  1104.                 }
  1105.                 $cart->productOfferCouponTotal $productOfferCouponTotal;
  1106.                 $carts[$key] = $cart;
  1107.             }
  1108.             $customCartSame true;
  1109.             $optionCustomDefault null;
  1110.             if(count($offerPaymentOptions) == count($carts)){
  1111.                 foreach ($offerPaymentOptions as $key => $option) {
  1112.                     if(empty($optionCustomDefault)){
  1113.                         $optionCustomDefault $option;
  1114.                     }
  1115.                     if($optionCustomDefault != $option){
  1116.                         $customCartSame false;
  1117.                         break;
  1118.                     }
  1119.                 }
  1120.             }
  1121.             $this->data['use_card'] = $this->configuration->get('use_card');
  1122.             $this->data['use_pix'] = $this->configuration->get('use_pix');
  1123.             $this->data['use_bill'] = $this->configuration->get('use_bill');
  1124.             $this->data['use_paypal'] = CartEnum::NO;
  1125.             $this->data['use_pagseguro'] = CartEnum::NO;
  1126.             $this->data['use_mercadopago'] = CartEnum::NO;
  1127.                 
  1128.             if(is_array($internalOptions)){
  1129.                 $this->data['use_paypal'] = (
  1130.                     in_array(ProductOfferEnum::INTERNAL_PAYPAL$internalOptions) ? 
  1131.                     CartEnum::YES 
  1132.                     CartEnum::NO
  1133.                 );
  1134.                 $this->data['use_pagseguro'] = (
  1135.                     in_array(ProductOfferEnum::INTERNAL_PAGSEGURO$internalOptions) ? 
  1136.                     CartEnum::YES 
  1137.                     CartEnum::NO
  1138.                 );
  1139.                 $this->data['use_mercadopago'] = (
  1140.                     in_array(ProductOfferEnum::INTERNAL_MERCADOPAGO$internalOptions) ? 
  1141.                     CartEnum::YES 
  1142.                     CartEnum::NO
  1143.                 );
  1144.             }
  1145.             $this->data['isOneMethod'] = CartEnum::NO;
  1146.             $this->data['isOneMethodType'] = null;
  1147.             if($customCartSame && $optionCustomDefault){
  1148.                 $customCart false;
  1149.                 $methodsInUse = [];
  1150.                 $this->data['use_card'] = CartEnum::NO;
  1151.                 $this->data['use_pix'] = CartEnum::NO;
  1152.                 $this->data['use_bill'] = CartEnum::NO;
  1153.                 if(in_array($optionCustomDefault->paymentMethod$cardOptions)){
  1154.                     $this->data['use_card'] = CartEnum::YES;
  1155.                     $methodsInUse[] = CartEnum::PAYMENT_CARD;
  1156.                 }
  1157.                 if(in_array($optionCustomDefault->paymentMethod$pixOptions)){
  1158.                     $this->data['use_pix'] = CartEnum::YES;
  1159.                     $methodsInUse[] = CartEnum::PAYMENT_PIX;
  1160.                 }
  1161.                 if(in_array($optionCustomDefault->paymentMethod$billOptions)){
  1162.                     $this->data['use_bill'] = CartEnum::YES;
  1163.                     $methodsInUse[] = CartEnum::PAYMENT_BILL;
  1164.                 }
  1165.                 if(count($methodsInUse) == CartEnum::YES){
  1166.                     $this->data['isOneMethod'] = CartEnum::YES;
  1167.                     $this->data['isOneMethodType'] = end($methodsInUse);
  1168.                 }
  1169.                 $freeInstallments $optionCustomDefault->installmentNumberInterest;
  1170.                 /*if($paymentConfig->assumeInstallmentInterest == ProductOfferEnum::YES){
  1171.                     $freeInstallments = 12;
  1172.                 }*/
  1173.                 $parcelInfo $numberUtil->getNumberMaxParcel(
  1174.                     $cartSubtotal 100,
  1175.                     $optionCustomDefault->installmentNumberMax
  1176.                     $freeInstallments
  1177.                     ProductOfferEnum::EAD,
  1178.                     ProductOfferEnum::CHECKOUT_CUSTOM
  1179.                 );
  1180.                 $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1181.                     'amount' => $numberUtil->numberToCents($cartSubtotal),
  1182.                     'free_installments' => $freeInstallments,
  1183.                     'max_installments' => $parcelInfo->maxInstallments,
  1184.                     'interest_rate' => $paymentConfig->installmentInterest
  1185.                 ]);
  1186.                 $this->data['installmentsOptions'] = (array)$installmentsOptions;
  1187.             }else if($customCart){
  1188.                 $steps = [];
  1189.                 $auxOption = [];
  1190.                 foreach ($offerPaymentOptions as $key => $value) {
  1191.                     if(!in_array($value$auxOption)){
  1192.                         $auxOption[] = $value;
  1193.                     }
  1194.                 }
  1195.                 $offerPaymentOptions $auxOption;
  1196.                 $defaultObj = (object)[
  1197.                     "amountStep" => 0,
  1198.                     "amountStepCent" => 0,
  1199.                     "items" => [],
  1200.                     "installmentsOptions" => null,
  1201.                     "freeInstallments" => 1,
  1202.                     "useCard" => $this->configuration->get('use_card'),
  1203.                     "usePix" => $this->configuration->get('use_pix'),
  1204.                     "useBill" => $this->configuration->get('use_bill'),
  1205.                     "installmentNumberMax" => $paymentConfig->installmentNumberMax,
  1206.                     "assumeInstallmentInterest" => $paymentConfig->assumeInstallmentInterest,
  1207.                     "installmentNumberInterest" => $paymentConfig->installmentNumberInterest,
  1208.                     "default" => true,
  1209.                 ];
  1210.                 $defaultSubtotalCent 0;
  1211.                 $defaultInstallmentsOptions null;
  1212.                 foreach ($carts as $key => $cart) {
  1213.                     $cart->setAble(CartEnum::YES);
  1214.                     $productOffer $cart->getProductOffer();
  1215.                     if($productOffer->getTypeCheckout() == ProductOfferEnum::CHECKOUT_DEFAULT){
  1216.                         $price $cart->getPrice() + $cart->getMembershipFee();
  1217.                         $priceCents $cart->getPrice(true) + $cart->getMembershipFee(true);
  1218.                         $defaultObj->amountStep $defaultObj->amountStep $price;
  1219.                         $defaultObj->amountStepCent $defaultObj->amountStepCent $priceCents;
  1220.                         $defaultObj->items[] = $cart;
  1221.                     }else{
  1222.                         $indexOptions array_search(
  1223.                             $cart->offerPaymentOptions,
  1224.                             $offerPaymentOptions
  1225.                         );
  1226.                         if(isset($offerPaymentOptions[$indexOptions])){
  1227.                             $offerPaymentOptions[$indexOptions]->items[] = $cart;
  1228.                         }
  1229.                     }
  1230.                 }
  1231.                 foreach ($offerPaymentOptions as $key => $option) {
  1232.                     if(count($option->items)){
  1233.                         $option->useCard in_array($option->paymentMethod$cardOptions);
  1234.                         $option->usePix in_array($option->paymentMethod$pixOptions);
  1235.                         $option->useBill in_array($option->paymentMethod$billOptions);
  1236.                         $amountOption 0;
  1237.                         $amountOptionCents 0;
  1238.                         foreach ($option->items as $keyItem => $item) {
  1239.                             $productOffer $item->getProductOffer();
  1240.                             $amountOption $amountOption $item->getPrice() + $item->getMembershipFee();
  1241.                             $amountOptionCents $amountOptionCents $item->getPrice(true) + $item->getMembershipFee(true);
  1242.                         }
  1243.                         
  1244.                         $installmentsOptions null;
  1245.                         if($option->useCard){
  1246.                             $parcelInfo $numberUtil->getNumberMaxParcel(
  1247.                                 $amountOptionCents,
  1248.                                 $option->installmentNumberMax,
  1249.                                 $option->freeInstallments,
  1250.                                 $option->saleOption,
  1251.                                 $option->typeCheckout
  1252.                             );
  1253.                             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1254.                                 'amount' => $amountOptionCents,
  1255.                                 'free_installments' => $option->freeInstallments,
  1256.                                 'max_installments' => $parcelInfo->maxInstallments,
  1257.                                 'interest_rate' => $paymentConfig->installmentInterest
  1258.                             ]);
  1259.                             $installmentsOptions = (array)$installmentsOptions;
  1260.                         }
  1261.                         $newStep = (object)[
  1262.                             'amountStep' => $amountOption,
  1263.                             'amountStepCent' => $amountOptionCents,
  1264.                             "items" => $option->items,
  1265.                             "installmentsOptions" => $installmentsOptions,
  1266.                             "freeInstallments" => 1,
  1267.                             "useCard" => $option->useCard,
  1268.                             "usePix" => $option->usePix,
  1269.                             "useBill" => $option->useBill,
  1270.                             "installmentNumberMax" => $option->installmentNumberMax,
  1271.                             "assumeInstallmentInterest" => ProductOfferEnum::NO,
  1272.                             "installmentNumberInterest" => $option->installmentNumberInterest,
  1273.                             "default" => false
  1274.                         ];
  1275.                         if($option->useCard){
  1276.                             $this->data['use_card'] = CartEnum::YES;
  1277.                         }
  1278.                         if($option->usePix){
  1279.                             $this->data['use_pix'] = CartEnum::YES;
  1280.                         }
  1281.                         if($option->useBill){
  1282.                             $this->data['use_bill'] = CartEnum::YES;
  1283.                         }
  1284.                         $steps[] = $newStep;
  1285.                     }
  1286.                 }
  1287.                 if(count($defaultObj->items)){
  1288.                     
  1289.                     $defaultObj->freeInstallments $paymentConfig->installmentNumberInterest;
  1290.                     if($paymentConfig->assumeInstallmentInterest == ProductOfferEnum::YES){
  1291.                         $defaultObj->freeInstallments 12;
  1292.                     }
  1293.                     $parcelInfo $numberUtil->getNumberMaxParcel(
  1294.                         $defaultObj->amountStepCent
  1295.                         $paymentConfig->installmentNumberMax
  1296.                         $defaultObj->freeInstallments
  1297.                         ProductOfferEnum::EAD,
  1298.                         ProductOfferEnum::CHECKOUT_DEFAULT
  1299.                     );
  1300.                     $defaultOptions = (array)$pagarMeTransaction->calculateInstallments([
  1301.                         'amount' => $defaultObj->amountStepCent,
  1302.                         'free_installments' => $defaultObj->freeInstallments,
  1303.                         'max_installments' => $parcelInfo->maxInstallments,
  1304.                         'interest_rate' => $paymentConfig->installmentInterest
  1305.                     ]);
  1306.                     $defaultObj->installmentsOptions $defaultOptions;
  1307.                     
  1308.                     array_unshift($steps$defaultObj);
  1309.                 }
  1310.                 $this->data['steps'] = $steps;
  1311.             }else{
  1312.                 $freeInstallments $paymentConfig->installmentNumberInterest;
  1313.                 if($paymentConfig->assumeInstallmentInterest == ProductOfferEnum::YES){
  1314.                     $freeInstallments 12;
  1315.                 }
  1316.                 $parcelInfo $numberUtil->getNumberMaxParcel(
  1317.                     $cartSubtotal 100,
  1318.                     $paymentConfig->installmentNumberMax
  1319.                     $freeInstallments
  1320.                     ProductOfferEnum::EAD,
  1321.                     ProductOfferEnum::CHECKOUT_DEFAULT
  1322.                 );
  1323.                 $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1324.                     'amount' => $numberUtil->numberToCents($cartSubtotal),
  1325.                     'free_installments' => $freeInstallments,
  1326.                     'max_installments' => $parcelInfo->maxInstallments,
  1327.                     'interest_rate' => $paymentConfig->installmentInterest
  1328.                 ]);
  1329.                 $this->data['installmentsOptions'] = (array)$installmentsOptions;
  1330.             }
  1331.             $receiverSchoolEad $this->configuration->getReceiverSchool(
  1332.                 ReceiverEnum::EAD_CHECKOUT
  1333.             );
  1334.             $receiverSchoolPayPal $this->configuration->getReceiverSchool(
  1335.                 ReceiverEnum::PAYPAL
  1336.             );
  1337.             $receiverSchoolPagSeguro $this->configuration->getReceiverSchool(
  1338.                 ReceiverEnum::PAGSEGURO
  1339.             );
  1340.             $receiverSchoolMercadoPago $this->configuration->getReceiverSchool(
  1341.                 ReceiverEnum::MERCADO_PAGO
  1342.             );
  1343.             $displayTotal $cartRepository->getUserDisplaySubTotal($userId);
  1344.             $this->data['receiverSchoolEad'] = $receiverSchoolEad;
  1345.             $this->data['receiverSchoolPayPal'] = $receiverSchoolPayPal;
  1346.             $this->data['receiverSchoolPagSeguro'] = $receiverSchoolPagSeguro;
  1347.             $this->data['receiverSchoolMercadoPago'] = $receiverSchoolMercadoPago;
  1348.             $this->data['cartSubtotal'] = $cartSubtotal;
  1349.             $this->data['cartDisplaySubtotal'] = $displayTotal;
  1350.             $this->data['userCards'] = $userCards;
  1351.             $this->data['hasOnlyCourseInCart'] = $hasOnlyCourseInCart;
  1352.             $this->data['carts'] = $carts;
  1353.         }else if(!empty($hashId)){
  1354.             $cartInfo $cartRepository->getHashValidCartsInfo($hashId);
  1355.             $customCart = ($cartInfo->totalCustom true false);
  1356.             $carts $cartInfo->data;
  1357.             foreach ($carts as $key => $cart) {
  1358.                 $cart->setAble(CartEnum::YES);
  1359.                 $productOffer $cart->getProductOffer();
  1360.                 $productOfferCouponTotal $pcRepository->countPublicCouponByProductOffer(
  1361.                     $productOffer
  1362.                 );
  1363.                 $cart->productOfferCouponTotal $productOfferCouponTotal;
  1364.                 $carts[$key] = $cart;
  1365.             }
  1366.             $this->data['carts'] = $carts;
  1367.             $this->data['cartSubtotal'] = $cartInfo->totalPrice;
  1368.             $this->data['cartDisplaySubtotal'] = $cartInfo->totalDisplay;
  1369.         }
  1370.         $this->data['keyCaptcha'] = $this->createCaptchaKey($request);
  1371.         $this->data['customCart'] = $customCart;
  1372.         $this->data['cartSubtotalDiff'] = $displayTotal $cartSubtotal;
  1373.         $this->data['isCart'] = true;
  1374.         $this->data['urlPost'] = $this->generalService->generateUrl("cartCheckoutDefault");
  1375.         $this->data['urlConclusion'] = $this->generalService->generateUrl("cartConclusion");
  1376.         $this->em->flush();
  1377.         if(empty($this->data['carts'])){
  1378.             $this->data['carts'] = [];
  1379.         }
  1380.         return $this->renderEAD('cart/cart.html.twig');
  1381.     }
  1382.     /**
  1383.      * @Route(
  1384.      *      path          = "/cart/indvidual/{poHash}/{pcKey}",
  1385.      *      name          = "cartIndividual",
  1386.      *      methods       = {"GET"},
  1387.      *      defaults      = { "pcKey" = "" }
  1388.      * )
  1389.      */
  1390.     public function cartPageIndvidual(Request $request) {
  1391.         $cartRepository $this->em->getRepository(Cart::class);
  1392.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  1393.         $productCouponRepository $this->em->getRepository(ProductCoupon::class);
  1394.         $productRepository $this->em->getRepository(Product::class);
  1395.         $offerHash $request->get('poHash');
  1396.         $productOffer $productOfferRepository->getProductByOfferLink($offerHash);
  1397.         if(!$productOffer){
  1398.             return $this->redirectToRoute('notFound');
  1399.         }
  1400.         $product $productOffer->getProduct();
  1401.         if($productOffer->getAllowTrial() == ProductOfferEnum::NO){
  1402.             return $this->redirectToRoute('notFound');
  1403.         }
  1404.         $productPage $productOffer->getProductPage();
  1405.         if(!$productPage){
  1406.             return $this->redirectToRoute('notFound');
  1407.         }
  1408.         if($productOffer->getSaleChannel() == ProductOfferEnum::EXTERNAL){
  1409.             $externalCheckoutLink $productOffer->getExternalCheckoutLink();
  1410.             return $this->redirect($externalCheckoutLink301);
  1411.         }
  1412.         
  1413.         $externalPage $productPage->getExternalPage();
  1414.         $externalPageLink $productPage->getExternalPageLink();
  1415.         if($externalPage == ProductEnum::YES && !empty($externalPageLink)){
  1416.             //return $this->redirect($externalPageLink, 301);
  1417.         }
  1418.         
  1419.         $productCoupon null;
  1420.         $couponKey $request->get('pcKey');
  1421.         $couponKeyValid false;
  1422.         
  1423.         if(!empty($couponKey)){
  1424.             $productCoupon $productCouponRepository->findValidProductCouponByProductOffer(
  1425.                 $couponKey,
  1426.                 $productOffer
  1427.             );
  1428.             if($productCoupon){
  1429.                 $amount $productOffer->getPriceReal();
  1430.                 if(
  1431.                     $productCouponRepository->checkApplyCoupon($productCoupon$productOffer)
  1432.                 ){
  1433.                     $amount $productCouponRepository->applyDiscount(
  1434.                         $productCoupon,
  1435.                         $amount
  1436.                     );
  1437.                     $couponKeyValid true;
  1438.                 }
  1439.                 if(!empty($amount)){
  1440.                     $productOffer->setPriceRealCopy($amount);
  1441.                 }
  1442.             }
  1443.         }
  1444.         if(
  1445.             $this->user &&
  1446.             (
  1447.                 $product->getType() == ProductEnum::SUBSCRIPTION || 
  1448.                 $productOffer->getAllowRecurrency() == ProductEnum::YES
  1449.             )
  1450.         ){
  1451.             $userSubscriptionRepository $this->em->getRepository(UserSubscription::class);
  1452.             $userSubscription $userSubscriptionRepository->findOneBy([
  1453.                 "user" => $this->user->getId(),
  1454.                 "product" => $product->getId(),
  1455.                 "deleted" => UserSubscriptionEnum::ITEM_NO_DELETED,
  1456.             ]);
  1457.             if($userSubscription){
  1458.                 if(
  1459.                     $userSubscription->getStatus() == UserSubscriptionEnum::STATUS_COMPLETED ||
  1460.                     $userSubscription->getStatus() == UserSubscriptionEnum::STATUS_CANCELED
  1461.                 ){
  1462.                     $params = [
  1463.                         "poID" => $productOffer->getId(),
  1464.                         "pcID" => ( $productCoupon $productCoupon->getId() : ),
  1465.                     ];
  1466.                     return $this->redirectToRoute('cartAdd'$params);
  1467.                 }
  1468.                 return $this->redirectToRoute('notFound');
  1469.             }
  1470.         }
  1471.         $this->data['couponKey'] = $couponKey;
  1472.         $this->data['couponKeyValid'] = $couponKeyValid;
  1473.         $this->data['showInfoStep'] = true;
  1474.         $customCart false;
  1475.         $displayTotal $productOffer->getPriceDisplay();
  1476.         $cartSubtotal $productOffer->getPriceRealCopy();
  1477.         $hasOnlyCourseInCart true;
  1478.         $paymentConfig $this->configuration->getPaymentConfig();
  1479.         $userCards = [];
  1480.         if($this->user){
  1481.             $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  1482.             $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  1483.                 "user" => $this->user->getId(),
  1484.                 "default" => UserCheckoutInfoEnum::YES,
  1485.                 "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
  1486.             ]);
  1487.             if($userCheckoutInfo){
  1488.                 $this->data['showInfoStep'] = false;
  1489.             }
  1490.             $userCards $this->em->getRepository(UserCard::class)->getValidUserCard();
  1491.         }
  1492.         $pagarMeTransaction $this->generalService->getService('PagarMe\\PagarMeTransaction');
  1493.         $numberUtil $this->generalService->getUtil('NumberUtil');
  1494.         $useCard $productOfferRepository->produtOfferAllowCard($productOffer);
  1495.         $useBill $productOfferRepository->produtOfferAllowBill($productOffer);
  1496.         $usePix $productOfferRepository->produtOfferAllowPix($productOffer);
  1497.         $internalOptions json_decode($productOffer->getSaleOptionInternal());
  1498.         
  1499.         $this->data['use_paypal'] = CartEnum::NO;
  1500.         $this->data['use_pagseguro'] = CartEnum::NO;
  1501.         $this->data['use_mercadopago'] = CartEnum::NO;
  1502.         if(is_array($internalOptions)){
  1503.             $this->data['use_paypal'] = (
  1504.                 in_array(ProductOfferEnum::INTERNAL_PAYPAL$internalOptions) ? 
  1505.                 CartEnum::YES 
  1506.                 CartEnum::NO
  1507.             );
  1508.             $this->data['use_pagseguro'] = (
  1509.                 in_array(ProductOfferEnum::INTERNAL_PAGSEGURO$internalOptions) ? 
  1510.                 CartEnum::YES 
  1511.                 CartEnum::NO
  1512.             );
  1513.             $this->data['use_mercadopago'] = (
  1514.                 in_array(ProductOfferEnum::INTERNAL_MERCADOPAGO$internalOptions) ? 
  1515.                 CartEnum::YES 
  1516.                 CartEnum::NO
  1517.             );
  1518.         }
  1519.         $hasTrue = [
  1520.             $useCard == CartEnum::YES && $useBill == CartEnum::NO && $usePix == CartEnum::NO,
  1521.             $useCard == CartEnum::NO && $useBill == CartEnum::YES && $usePix == CartEnum::NO,
  1522.             $useCard == CartEnum::NO && $useBill == CartEnum::NO && $usePix == CartEnum::YES,
  1523.         ];
  1524.         $paymentMethod = [
  1525.             CartEnum::PAYMENT_CARD,
  1526.             CartEnum::PAYMENT_BILL,
  1527.             CartEnum::PAYMENT_PIX,
  1528.         ];
  1529.         $this->data['use_card'] = $useCard;
  1530.         $this->data['use_bill'] = $useBill;
  1531.         $this->data['use_pix'] = $usePix;
  1532.         $key array_search(true$hasTrue);
  1533.         $this->data['isOneMethod'] = (!empty($key) ? CartEnum::YES CartEnum::NO);
  1534.         $this->data['isOneMethodType'] = (!empty($key) ? $paymentMethod[$key] : null);
  1535.         $freeInstallments $productOfferRepository->getFreeInstallment($productOffer);
  1536.         $maxInstallments $productOfferRepository->getInstallmentNumberMax($productOffer);
  1537.         $parcelInfo $numberUtil->getNumberMaxParcel(
  1538.             $cartSubtotal 100,
  1539.             $maxInstallments
  1540.             $freeInstallments
  1541.             ProductOfferEnum::EAD,
  1542.             $productOffer->getTypeCheckout()
  1543.         );
  1544.         $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1545.             'amount' => $numberUtil->numberToCents($cartSubtotal),
  1546.             'free_installments' => $freeInstallments,
  1547.             'max_installments' => $parcelInfo->maxInstallments,
  1548.             'interest_rate' => $paymentConfig->installmentInterest
  1549.         ]);
  1550.         $this->data['installmentsOptions'] = (array)$installmentsOptions;
  1551.         $receiverSchoolEad $this->configuration->getReceiverSchool(
  1552.             ReceiverEnum::EAD_CHECKOUT
  1553.         );
  1554.         $receiverSchoolPayPal $this->configuration->getReceiverSchool(
  1555.             ReceiverEnum::PAYPAL
  1556.         );
  1557.         $receiverSchoolPagSeguro $this->configuration->getReceiverSchool(
  1558.             ReceiverEnum::PAGSEGURO
  1559.         );
  1560.         $receiverSchoolMercadoPago $this->configuration->getReceiverSchool(
  1561.             ReceiverEnum::MERCADO_PAGO
  1562.         );
  1563.         $this->data['receiverSchoolEad'] = $receiverSchoolEad;
  1564.         $this->data['receiverSchoolPayPal'] = $receiverSchoolPayPal;
  1565.         $this->data['receiverSchoolPagSeguro'] = $receiverSchoolPagSeguro;
  1566.         $this->data['receiverSchoolMercadoPago'] = $receiverSchoolMercadoPago;
  1567.         $this->data['cartSubtotal'] = CartEnum::NO;//$cartSubtotal;
  1568.         $this->data['cartDisplaySubtotal'] = CartEnum::NO;//$displayTotal;
  1569.         $this->data['cartSubtotalDiff'] = CartEnum::NO;//$displayTotal - $cartSubtotal;
  1570.         $this->data['userCards'] = $userCards;
  1571.         $this->data['hasOnlyCourseInCart'] = $hasOnlyCourseInCart;
  1572.         $cart = new Cart();
  1573.         $hashIdentify $this->generalService->getCookieHashIdentify();
  1574.         
  1575.         $cart->setProductOffer($productOffer);
  1576.         $cart->setProduct($product);
  1577.         if($productCoupon && $couponKeyValid){
  1578.             $cart->setProductCoupon($productCoupon);
  1579.         }
  1580.         if($this->user){
  1581.             $cart->setUser($this->user);
  1582.             $cart->setHashIdentify($this->user->getHashIdentify());
  1583.         }else if(!empty($hashIdentify)){
  1584.             $cart->setHashIdentify($hashIdentify);
  1585.         }
  1586.         $cart->setPrice($productOffer->getPriceRealCopy());
  1587.         if(
  1588.             $product->getType() == ProductEnum::SUBSCRIPTION || 
  1589.             $productOffer->getAllowRecurrency() == ProductEnum::YES
  1590.         ){
  1591.             $cart->setMembershipFee($productOffer->getMembershipFeeCopy());
  1592.         }
  1593.         $cart->setProductType($product->getType());
  1594.         $productOfferCouponTotal $productCouponRepository->countPublicCouponByProductOffer(
  1595.             $productOffer
  1596.         );
  1597.         $cart->offerPaymentOptions null;
  1598.         $cart->productOfferCouponTotal $productOfferCouponTotal;
  1599.         $this->data['carts'] = [
  1600.             $cart
  1601.         ];
  1602.         $this->data['keyCaptcha'] = $this->createCaptchaKey($request);
  1603.         $this->data['customCart'] = $customCart;
  1604.         $this->data['isCart'] = false;
  1605.         $this->data['urlPost'] = $this->generalService->generateUrl(
  1606.             "cartCheckoutPlanTrial"
  1607.             [
  1608.                 "poID" => $productOffer->getId(),
  1609.                 "pcID" => ($productCoupon $productCoupon->getId() : null),
  1610.             ]
  1611.         );
  1612.         $this->data['urlRedirect'] = $this->generalService->generateUrl(
  1613.             "cartIndividual"
  1614.             [
  1615.                 "poHash" => $offerHash,
  1616.                 "pcKey" => ($couponKey $couponKey null),
  1617.             ]
  1618.         ) . "#cart-step-info";
  1619.         $this->data['urlConclusion'] = $this->generalService->generateUrl("cartConclusion");
  1620.         $this->em->flush();
  1621.         if(empty($this->data['carts'])){
  1622.             $this->data['carts'] = [];
  1623.         }
  1624.         return $this->renderEAD('cart/cart.html.twig');
  1625.     }
  1626.     /**
  1627.      * @Route(
  1628.      *      path    = "/admin/cart/user/valid",
  1629.      *      name    = "getUserValidCarts",
  1630.      *      methods = {"GET"}
  1631.      * )
  1632.      */
  1633.     public function getUserValidCarts(Request $request){
  1634.         if (!$this->user) {
  1635.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  1636.         }
  1637.         $cartRepository $this->em->getRepository(Cart::class);
  1638.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  1639.         $userId $this->user->getId();
  1640.         $carts $cartRepository->getUserValidCarts($userId);
  1641.         $paymentConfig $this->configuration->getPaymentConfig();
  1642.         $fileService $this->generalService->getService('FileService');
  1643.         $data = [];
  1644.         foreach ($carts as $key => $cart) {
  1645.             $productOffer $cart->getProductOffer();
  1646.             $billDateExpire $productOfferRepository->getDateExpireBill($productOffer);
  1647.             $pixDateExpire $productOfferRepository->getDateExpirePix($productOffer);
  1648.             $photo $fileService->getFilePathComplete(
  1649.                 $cart->getProductOffer()->getProductPage()->getPhoto(),
  1650.                 ProductEnum::PATH_PRODUCT_PHOTO,
  1651.                 true,
  1652.                 false,
  1653.                 'product-photo',
  1654.                 true
  1655.             );
  1656.             $onErrorPhoto $fileService->getFilePathComplete(
  1657.                 $cart->getProductOffer()->getProductPage()->getPhoto(),
  1658.                 ProductEnum::PATH_PRODUCT_PHOTO,
  1659.                 true,
  1660.                 false,
  1661.                 'product-photo',
  1662.                 false
  1663.             );
  1664.             $price = (float)($cart->getPrice() + $cart->getMembershipFee());
  1665.             $membershipFee = (float)$productOffer->getMembershipFee();
  1666.             $priceReal = (float)($productOffer->getPriceReal() + $membershipFee);
  1667.             $priceDiscount = (float)$price $priceReal;
  1668.             $priceDisplay = (float)$productOffer->getPriceDisplay();
  1669.             
  1670.             if(!empty($priceDisplay)){
  1671.                 $priceDisplay = (float)($priceDisplay $membershipFee);
  1672.                 $priceDiscount $price $priceDisplay;
  1673.             }
  1674.             $recurrency = (
  1675.                 $cart->getProduct()->getType() == ProductEnum::SUBSCRIPTION ||
  1676.                 $productOffer->getAllowRecurrency() == ProductEnum::YES
  1677.             )? true false;
  1678.             $item = (object)[
  1679.                 "id" => $cart->getId(),
  1680.                 "title" => $cart->getProduct()->getTitle(),
  1681.                 "photo" => $photo,
  1682.                 "onErrorPhoto" => $onErrorPhoto,
  1683.                 "able" => ($cart->getAble() == CartEnum::YES),
  1684.                 "priceOrigin" => (float)$cart->getPrice(),
  1685.                 "membershipFeeOrigin" => (float)$productOffer->getMembershipFee(),
  1686.                 "membershipFeeReal" => (float)$cart->getMembershipFee(),
  1687.                 "priceReal" => $priceReal,
  1688.                 "price" => (float)$price,
  1689.                 "priceReal" => $priceReal,
  1690.                 "priceDisplay" => (float)$priceDisplay,
  1691.                 "priceDiscount" => (float)$priceDiscount,
  1692.                 "couponKey" => (
  1693.                     $cart->getProductCoupon() ? $cart->getProductCoupon()->getKey() : null
  1694.                 ),
  1695.                 "couponMembershipFee" => (int)(
  1696.                     $cart->getProductCoupon() ?
  1697.                     $cart->getProductCoupon()->getApplyMembershipFee() : 
  1698.                     ProductEnum::NO
  1699.                 ),
  1700.                 "billDateExpire" => $billDateExpire,
  1701.                 "pixDateExpire" => $pixDateExpire,
  1702.                 "interestFee" => $paymentConfig->installmentInterest,
  1703.                 "currencySymbol" => $productOffer->getCurrencySymbol(),
  1704.                 "paymentCycle" => $productOffer->getPlanCycle(),
  1705.                 "recurrency" => $recurrency,
  1706.             ];
  1707.             $data[] = $item;
  1708.         }
  1709.         return $this->eadResponse($data);
  1710.     }
  1711.     /**
  1712.      * @Route(
  1713.      *      path          = "/admin/cart/change/status",
  1714.      *      name          = "cartChangeStatus",
  1715.      *      methods       = {"PUT"}
  1716.      * )
  1717.      */
  1718.     public function updateCartStatus(Request $request){
  1719.         if (!$this->user) {
  1720.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  1721.         }
  1722.         $this->requestUtil->setRequest($request)->setData();
  1723.         $cartId $this->requestUtil->getField('cart');
  1724.         $able $this->requestUtil->getField('able');
  1725.         $cartRepository $this->em->getRepository(Cart::class);
  1726.         $userId $this->user->getId();
  1727.         $cart $cartRepository->findOneBy([
  1728.             "id" => $cartId,
  1729.             "user" => $userId,
  1730.             "deleted" => CartEnum::ITEM_NO_DELETED,
  1731.         ]);
  1732.         if(!$cart){
  1733.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  1734.         }
  1735.         $cart->setAble($able);
  1736.         $this->em->flush();
  1737.         return $this->eadResponse([ "success" => ]);
  1738.     }
  1739.     /**
  1740.      * @Route(
  1741.      *      path          = "/cart/checkout/paypal",
  1742.      *      name          = "cartCheckoutPaypal",
  1743.      *      methods       = {"GET"}
  1744.      * )
  1745.      */
  1746.     public function checkoutPayPal(Request $request){
  1747.         $this->checkUserSession($request);
  1748.         $receiverSchool $this->configuration->getReceiverSchool(ReceiverEnum::PAYPAL);
  1749.         if(!$receiverSchool){
  1750.             return $this->redirectToRoute('notFound');
  1751.         }
  1752.         $this->requestUtil->setRequest($request)->setData();
  1753.         $cartRepository $this->em->getRepository(Cart::class);
  1754.         $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($this->user->getId());
  1755.         if(!$hasOnlyCourseInCart){
  1756.             return $this->redirectToRoute('notFound');
  1757.         }
  1758.         $currencyCode $this->configuration->get('currency_code');
  1759.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  1760.         $transactionData = [
  1761.             "cmd" => "_cart",
  1762.             "business" => $receiverSchool->getEmail(),
  1763.             "upload" => 1,
  1764.             "currency_code" => ( $currencyCode $currencyCode "BRL" ),
  1765.             "charset" => "UTF-8",
  1766.         ];
  1767.         $dataCustom = [];
  1768.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  1769.         if(empty($carts)){
  1770.             return $this->redirectToRoute('notFound');
  1771.         }
  1772.         foreach ($carts as $key => $cart) {
  1773.             $index $key 1;
  1774.             $product $cart->getProduct();
  1775.             $productOffer $cart->getProductOffer();
  1776.             $title $this->stringUtil->shortTextClean($product->getTitle(), 30);
  1777.             $title $this->stringUtil->cleanString2($title);
  1778.             $transactionData["item_number_{$index}"] = $product->getId();
  1779.             $transactionData["item_name_{$index}"] = $title;
  1780.             $transactionData["amount_{$index}"] = $cart->getPrice();
  1781.             $transactionData["quantity_{$index}"] = 1;
  1782.             $dataCustom["cart_{$index}"] = $cart->getId();
  1783.             $dataCustom["amount_{$index}"] = $cart->getPrice();
  1784.             $dataCustom["offer_{$index}"] = $productOffer->getId();
  1785.             
  1786.             $productCoupon $cart->getProductCoupon();
  1787.             if($productCoupon){
  1788.                 $dataCustom["coupon_{$index}"] = $productCoupon->getId();
  1789.             }
  1790.         }
  1791.         $dataCustom["new"] = CartEnum::YES;
  1792.         $dataCustom["userId"] = $this->user->getId();
  1793.         $dataCustom["numberItens"] = count($carts);
  1794.         $dataCustom["userCheckoutInfoId"] = $userCheckoutInfoId;
  1795.         $transactionData["custom"] = json_encode($dataCustom);
  1796.         $transactionData["return"] = "https://{$this->eadDomain}/resume/";
  1797.         $transactionData["notify_url"] = "https://{$this->eadDomain}/paypal/";
  1798.         $transactionData http_build_query($transactionData);
  1799.         $payPal $this->generalService->getService('PayPal\\PayPal');
  1800.         $url "{$payPal->getPayPalAddress()}?{$transactionData}";
  1801.         return $this->redirect($url301);
  1802.     }
  1803.     /**
  1804.      * @Route(
  1805.      *      path          = "/cart/checkout/pagseguro",
  1806.      *      name          = "cartCheckoutPagseguro",
  1807.      *      methods       = {"GET"}
  1808.      * )
  1809.      */
  1810.     public function checkoutPagSeguro(Request $request){
  1811.         $this->checkUserSession($request);
  1812.         $receiverSchool $this->configuration->getReceiverSchool(ReceiverEnum::PAGSEGURO);
  1813.         if(!$receiverSchool){
  1814.             return $this->redirectToRoute('notFound');
  1815.         }
  1816.         $this->requestUtil->setRequest($request)->setData();
  1817.         $cartRepository $this->em->getRepository(Cart::class);
  1818.         $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($this->user->getId());
  1819.         if(!$hasOnlyCourseInCart){
  1820.             return $this->redirectToRoute('notFound');
  1821.         }
  1822.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  1823.         $transactionData = [
  1824.             "cmd" => "_cart",
  1825.             "business" => $receiverSchool->getEmail(),
  1826.             //"upload" => 1,
  1827.             "currency" => "BRL",
  1828.             "charset" => "ISO-8859-1",
  1829.         ];
  1830.         $dataCustom = [];
  1831.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  1832.         if(empty($carts)){
  1833.             return $this->redirectToRoute('notFound');
  1834.         }
  1835.         foreach ($carts as $key => $cart) {
  1836.             $index $key 1;
  1837.             $product $cart->getProduct();
  1838.             $productOffer $cart->getProductOffer();
  1839.             $title $this->stringUtil->shortTextClean($product->getTitle(), 30);
  1840.             $title $this->stringUtil->cleanString2($title);
  1841.             $transactionData["itemId{$index}"] = $productOffer->getId();
  1842.             $transactionData["itemDescription{$index}"] = $title;
  1843.             $transactionData["itemAmount{$index}"] = $cart->getPrice();
  1844.             $transactionData["itemQuantity{$index}"] = 1;
  1845.             $dataCustom["cart_{$index}"] = $cart->getId();
  1846.             $dataCustom["amount_{$index}"] = $cart->getPrice();
  1847.             $dataCustom["offer_{$index}"] = $productOffer->getId();
  1848.             
  1849.             $productCoupon $cart->getProductCoupon();
  1850.             if($productCoupon){
  1851.                 $dataCustom["coupon_{$index}"] = $productCoupon->getId();
  1852.             }
  1853.         }
  1854.         $dataCustom["userId"] = $this->user->getId();
  1855.         $dataCustom["numberItens"] = count($carts);
  1856.         $dataCustom["userCheckoutInfoId"] = $userCheckoutInfoId;
  1857.         $transactionData["reference"] = $this->user->getId();
  1858.         $transactionData["custom"] = json_encode($dataCustom);
  1859.         $transactionData["redirectURL"] = "https://{$this->eadDomain}/resume/";
  1860.         $transactionData["notificationURL"] = "https://{$this->eadDomain}/pagseguro/";
  1861.         $pagSeguro $this->generalService->getService('PagSeguro\\PagSeguro');
  1862.         $url $pagSeguro->getCheckoutPagseguro($transactionData);
  1863.         return $this->redirect($url301);
  1864.     }
  1865.     /**
  1866.      * @Route(
  1867.      *      path          = "/cart/checkout/mercadopago",
  1868.      *      name          = "cartCheckoutMercadopago",
  1869.      *      methods       = {"GET"}
  1870.      * )
  1871.      */
  1872.     public function checkoutMercadopago(Request $request) {
  1873.         $this->checkUserSession($request);
  1874.         $receiverSchool $this->configuration->getReceiverSchool(
  1875.             ReceiverEnum::MERCADO_PAGO
  1876.         );
  1877.         if(!$receiverSchool){
  1878.             return $this->redirectToRoute('notFound');
  1879.         }
  1880.         $this->requestUtil->setRequest($request)->setData();
  1881.         $cartRepository $this->em->getRepository(Cart::class);
  1882.         $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($this->user->getId());
  1883.         if(!$hasOnlyCourseInCart){
  1884.             return $this->redirectToRoute('notFound');
  1885.         }
  1886.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  1887.         if(empty($carts)){
  1888.             return $this->redirectToRoute('notFound');
  1889.         }
  1890.         foreach ($carts as $key => $cart) {
  1891.             $product $cart->getProduct();
  1892.             $productOffer $cart->getProductOffer();
  1893.             $title $this->stringUtil->shortTextClean($product->getTitle(), 30);
  1894.             $title $this->stringUtil->cleanString2($title);
  1895.             $description $this->stringUtil->shortTextClean($product->getDescription(), 30);
  1896.             $description $this->stringUtil->cleanString2($description);
  1897.             $items[] = [
  1898.                 "id" => $productOffer->getId(),
  1899.                 "title" => $title,
  1900.                 "description" => $description,
  1901.                 "quantity" => 1,
  1902.                 "unit_price" => $cart->getPrice()
  1903.             ];
  1904.         }
  1905.         $transactionData["items"] = $items;
  1906.         $transactionData["back_urls"] = [
  1907.             "success" => "https://{$this->eadDomain}/resume/",
  1908.             "failure" => "https://{$this->eadDomain}/cart#mercadopago/"
  1909.             "pending" => "https://{$this->eadDomain}/cart#mercadopago/"
  1910.         ];
  1911.         $transactionData["external_reference"] = $this->user->getId();
  1912.         $transactionData["notification_url"] = "https://{$this->eadDomain}/mercadopago?debug=1";
  1913.         $data json_encode($transactionData);
  1914.         $mercadoPago $this->generalService->getService('MercadoPago\\MercadoPago');
  1915.         $url $mercadoPago->getCheckoutMercadoPago($data);
  1916.         return $this->redirect($url301);
  1917.     }
  1918.     /**
  1919.      * @Route(
  1920.      *      path          = "/admin/cart/checkout/charge",
  1921.      *      name          = "cartCheckoutCharge",
  1922.      *      methods       = {"POST"}
  1923.      * )
  1924.      */
  1925.     public function checkoutCharge(Request $request){
  1926.         $receiverSchool $this->configuration->getReceiverSchool(
  1927.             ReceiverEnum::EAD_CHECKOUT
  1928.         );
  1929.         if(!$receiverSchool){
  1930.             return $this->eadResponse(
  1931.                 [ "message" => "Receiver not found "],
  1932.                 ErrorEnum::ACTION_INVALID
  1933.             );
  1934.         }
  1935.         $transactionService $this->generalService->getService(
  1936.             'Transaction\\TransactionService'
  1937.         );
  1938.         $pagarMeTransaction $this->generalService->getService(
  1939.             'PagarMe\\PagarMeTransaction'
  1940.         );
  1941.         $this->requestUtil->setRequest($request)->setData();
  1942.         $productChargeId = (int)$this->requestUtil->getField('productChargeId');
  1943.         $userCheckoutInfoId = (int)$this->requestUtil->getField('userCheckoutInfoId');
  1944.         $paymentMethod = (int)$this->requestUtil->getField('paymentMethod');
  1945.         $installments = (int)$this->requestUtil->getField('installments');
  1946.         $debug = (int)$request->get('debug');
  1947.         $productCharge $this->em->getRepository(ProductCharge::class)->findOneBy([
  1948.             "id" => $productChargeId,
  1949.             "deleted" => ProductChargeEnum::ITEM_NO_DELETED
  1950.         ]);
  1951.         
  1952.         if (!$productCharge) {
  1953.             return $this->eadResponse(
  1954.                 [ "message" => "Charge not found "], 
  1955.                 ErrorEnum::ACTION_INVALID
  1956.             );
  1957.         }
  1958.         if ($productCharge->getPublish() == ProductChargeEnum::NO) {
  1959.             return $this->eadResponse(
  1960.                 [ "message" => "Charge is not publish "], 
  1961.                 ErrorEnum::ACTION_INVALID
  1962.             );
  1963.         }
  1964.         $userCardRepository $this->em->getRepository(UserCard::class);
  1965.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  1966.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  1967.             "id" => $userCheckoutInfoId,
  1968.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  1969.         ]);
  1970.         if(!$userCheckoutInfo){
  1971.             return $this->eadResponse([ 
  1972.                 "message" => "UserCheckoutInfo is empty"
  1973.             ], ErrorEnum::ACTION_INVALID);
  1974.         }
  1975.         $paymentConfig $this->configuration->getPaymentConfig();
  1976.         $product $productCharge->getProduct();
  1977.         $productOffer $productCharge->getProductOffer();
  1978.         $items = [
  1979.             (object)[
  1980.                 "courseId" => null,
  1981.                 "productOfferId" => $productOffer->getId(),
  1982.                 "productId" => $product->getId(),
  1983.                 "amount" => $productCharge->getAmount(),
  1984.                 "cartId" => null,
  1985.                 "productCouponId" => null,
  1986.             ]
  1987.         ];
  1988.         $userCard null;
  1989.         $amount $productCharge->getAmount(true);
  1990.         $dateExpire $productCharge->getDateExpire();
  1991.         if(empty($paymentMethod)){
  1992.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  1993.         }
  1994.         if($paymentMethod == CartEnum::PAYMENT_CARD){ // get user card
  1995.             if(empty($installments)){
  1996.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  1997.             }
  1998.             $userCardId $this->requestUtil->getField('userCardId');
  1999.             if(empty($userCardId)){
  2000.                 return $this->eadResponse([ 'userCardId' ], ErrorEnum::FIELD_EMPTY);
  2001.             }
  2002.             if($userCardId 0){
  2003.                 $userCard $userCardRepository->findOneBy([
  2004.                     "id" => $userCardId,
  2005.                     "user" => $this->user->getId(),
  2006.                     "deleted" => CartEnum::ITEM_NO_DELETED
  2007.                 ]);
  2008.             }
  2009.         }
  2010.         if($paymentMethod == CartEnum::PAYMENT_CARD && !$userCard){
  2011.             return $this->eadResponse(
  2012.                 [ "message" => "Card not found "], 
  2013.                 ErrorEnum::ACTION_INVALID
  2014.             );
  2015.         }
  2016.         $freeInstallments $productCharge->getInstallmentNumberInterest();
  2017.         if($paymentMethod == CartEnum::PAYMENT_CARD){
  2018.             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2019.                 'amount' => $amount,
  2020.                 'free_installments' => $freeInstallments,
  2021.                 'max_installments' => $installments,
  2022.                 'interest_rate' => $paymentConfig->installmentInterest
  2023.             ]);
  2024.             $amount $installmentsOptions->{$installments}->amount;
  2025.         }
  2026.         if($amount $this->configuration->getMinValueProduct()){
  2027.             return $this->eadResponse([ "message" => "Amount invalid "], ErrorEnum::ACTION_INVALID);
  2028.         }
  2029.         if($this->user && $this->user->getId() == CartEnum::YES){
  2030.             $transactionService->setDebug(true);
  2031.         }
  2032.         if($debug == CartEnum::YES){
  2033.             $transactionService->setDebug(true);
  2034.         }
  2035.         $transactionOrigin TransactionEnum::ORIGIN_CHARGE;
  2036.         $data $transactionService->createTransactionEAD(
  2037.             $productCharge->getUser(),
  2038.             $userCheckoutInfo,
  2039.             $amount,
  2040.             $paymentMethod,
  2041.             $installments,
  2042.             $items,
  2043.             $userCard,
  2044.             $dateExpire,
  2045.             null,
  2046.             $productChargeId,
  2047.             (!empty($freeInstallments) ? (int)$freeInstallments null),
  2048.             $transactionOrigin
  2049.         );
  2050.         if(!empty($data['errorMessage'])){
  2051.             $productCharge->setErrorMessage($data['errorMessage']);
  2052.             $this->em->flush();
  2053.             return $this->eadResponse([ 
  2054.                 "message" => $data['errorMessage']
  2055.             ], ErrorEnum::ACTION_INVALID);
  2056.         }
  2057.         return $this->eadResponse($data);
  2058.     }
  2059.     /**
  2060.      * @Route(
  2061.      *      path          = "/cart/checkout/clean",
  2062.      *      name          = "checkoutClean",
  2063.      *      methods       = {"POST"}
  2064.      * )
  2065.      */
  2066.     public function checkoutClean(Request $request){
  2067.         $pixelService $this->generalService->getService('Marketing\\PixelService');
  2068.         $pixelService->sendConversion('InitiateCheckout');
  2069.         $receiverSchool $this->configuration->getReceiverSchool(
  2070.             ReceiverEnum::EAD_CHECKOUT
  2071.         );
  2072.         if(!$receiverSchool){
  2073.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2074.         }
  2075.         $transactionService $this->generalService->getService(
  2076.             'Transaction\\TransactionService'
  2077.         );
  2078.         $pagarMeTransaction $this->generalService->getService(
  2079.             'PagarMe\\PagarMeTransaction'
  2080.         );
  2081.         $this->requestUtil->setRequest($request)->setData();
  2082.         $data $this->requestUtil->getData();
  2083.         $dataUtml = [
  2084.             "utm_source" => $request->get('utm_source'),
  2085.             "utm_medium" => $request->get('utm_medium'),
  2086.             "utm_campaign" => $request->get('utm_campaign'),
  2087.             "utm_term" => $request->get('utm_term'),
  2088.             "utm_content" => $request->get('utm_content'),
  2089.         ];
  2090.         $utmsUrl json_encode($dataUtml);
  2091.         $cartRepository $this->em->getRepository(Cart::class);
  2092.         $courseRepository $this->em->getRepository(Course::class);
  2093.         $userCardRepository $this->em->getRepository(UserCard::class);
  2094.         $userRepository $this->em->getRepository(User::class);
  2095.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2096.         $poRepository $this->em->getRepository(ProductOffer::class);
  2097.         $pcRepository $this->em->getRepository(ProductCoupon::class);
  2098.         //user
  2099.         $name $this->requestUtil->getField('name');
  2100.         $email $this->requestUtil->getField('email');
  2101.         $document $this->requestUtil->getField('document');
  2102.         $phone $this->requestUtil->getField('phone');
  2103.         $zipCode $this->requestUtil->getField('zipCode');
  2104.         $stateId = (int)$this->requestUtil->getField('state');
  2105.         $cityId = (int)$this->requestUtil->getField('city');
  2106.         $address $this->requestUtil->getField('address');
  2107.         $addressNeighborhood $this->requestUtil->getField('addressNeighborhood');
  2108.         $addressNumber $this->requestUtil->getField('addressNumber');
  2109.         $addressComplement $this->requestUtil->getField('addressComplement');
  2110.         $foreigner = (int)$this->requestUtil->getField('foreigner');
  2111.         //userCheckoutInfo
  2112.         $checkoutName $this->requestUtil->getField('checkout-name');
  2113.         $checkoutEmail $this->requestUtil->getField('checkout-email');
  2114.         $checkoutDocument $this->requestUtil->getField('checkout-document');
  2115.         $checkoutPhone $this->requestUtil->getField('checkout-phone');
  2116.         $checkoutZipCode $this->requestUtil->getField('checkout-zipCode');
  2117.         $checkoutStateId = (int)$this->requestUtil->getField('checkout-state');
  2118.         $checkoutCityId = (int)$this->requestUtil->getField('checkout-city');
  2119.         $checkoutAddress $this->requestUtil->getField('checkout-address');
  2120.         $checkoutAddressNeighborhood $this->requestUtil->getField(
  2121.             'checkout-addressNeighborhood'
  2122.         );
  2123.         $checkoutAddressNumber $this->requestUtil->getField('checkout-addressNumber');
  2124.         $checkoutAddressComplement $this->requestUtil->getField(
  2125.             'checkout-addressComplement'
  2126.         );
  2127.         $checkoutNotify = (int)$this->requestUtil->getField('checkout-notify');
  2128.         $checkoutForeigner = (int)$this->requestUtil->getField('checkout-foreigner');
  2129.         
  2130.         $checkoutNew = (int)$this->requestUtil->getField('checkout-new');
  2131.         $cardNew = (int)$this->requestUtil->getField('card-new');
  2132.         $userNew = (int)$this->requestUtil->getField('user-new');
  2133.         $installments = (int)$this->requestUtil->getField('installments');
  2134.         $paymentMethod = (int)$this->requestUtil->getField('payment-method');
  2135.         $cardId = (int)$this->requestUtil->getField('card-id');
  2136.         $cardHash $this->requestUtil->getField('card-hash');
  2137.         $userId = (int)$this->requestUtil->getField('user-id');
  2138.         $userCheckoutInfoId = (int)$this->requestUtil->getField('checkout-id');
  2139.         $offerDefaultId = (int)$this->requestUtil->getField('offer-default');
  2140.         $offers $this->requestUtil->getField('added-ids');
  2141.         $offers explode(","$offers);
  2142.         if(empty($offers)){
  2143.             return $this->eadResponse([ 'added-ids' ], ErrorEnum::FIELD_EMPTY);
  2144.         }
  2145.         if(empty($paymentMethod)){
  2146.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  2147.         }
  2148.         $couponKey $this->requestUtil->getField('couponKey');
  2149.         $user null;
  2150.         $userCheckoutInfo null;
  2151.         $userCard null;
  2152.         
  2153.         if(!empty($userId)){
  2154.             $user $userRepository->findOneBy([
  2155.                 "id" => $userId,
  2156.             ]);
  2157.             //update user data ???
  2158.             
  2159.         }else if($userNew == UserEnum::YES){
  2160.             $pixelService $this->generalService->getService('Marketing\\PixelService');
  2161.             $pixelService->sendConversion('Lead');
  2162.             //check user exist
  2163.             $user $userRepository->findOneBy([
  2164.                 "email" => $email,
  2165.             ], [ "id" => "DESC" ]);
  2166.             if(!$user || ($user && $user->isDeleted())){
  2167.                 $dataUser = [
  2168.                     "name" => $name,
  2169.                     "email" => $email,
  2170.                     "password" => null,
  2171.                     "phone" => $phone,
  2172.                     "notify" => UserEnum::YES,
  2173.                     "invited" => UserEnum::YES
  2174.                 ];
  2175.                 $userDTO = new UserDTO($dataUser);
  2176.                 $data $userRepository->newUser($userDTOUserEnum::YES);
  2177.                 if($data->errors){
  2178.                     return $this->eadResponse($data->errorsErrorEnum::FIELD_EMPTY);
  2179.                 }
  2180.                 $user $data->user;
  2181.             }
  2182.             $user->setDocument($document);
  2183.             $user->setPhone($phone);
  2184.             $user->setZipCode($zipCode);
  2185.             if($foreigner == UserCheckoutInfoEnum::NO){
  2186.                 $country $this->em->getRepository(Country::class)->findOneBy([ 
  2187.                     "deleted" => GeoDataEnum::ITEM_NO_DELETED ,
  2188.                     "id" => GeoDataEnum::YES,
  2189.                 ]);
  2190.                 if($country){
  2191.                     $user->setCountry($country);
  2192.                 }
  2193.             }
  2194.             if(!empty($stateId)){
  2195.                 $state $this->em->getRepository(State::class)->findOneBy([
  2196.                     "id" => $stateId,
  2197.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2198.                 ]);
  2199.                 if($state){
  2200.                     $user->setState($state);
  2201.                 }
  2202.             }
  2203.             if(!empty($cityId)){
  2204.                 $city $this->em->getRepository(City::class)->findOneBy([
  2205.                     "id" => $cityId,
  2206.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2207.                 ]);
  2208.                 if($city){
  2209.                     $user->setCity($city);
  2210.                 }
  2211.             }
  2212.             $user->setAddress($address);
  2213.             $user->setAddressNeighborhood($addressNeighborhood);
  2214.             $user->setAddressNumber($addressNumber);
  2215.             $user->setAddressComplement($addressComplement);
  2216.             $user->restore();
  2217.         }
  2218.         if(!$user){
  2219.             return $this->eadResponse([ 'user' ], ErrorEnum::FIELD_EMPTY);
  2220.         }
  2221.         if($checkoutNew == CartEnum::NO && empty($userCheckoutInfoId)){
  2222.             $userCheckoutInfo = new UserCheckoutInfo();
  2223.             $userCheckoutInfo->setName($user->getName());
  2224.             $userCheckoutInfo->setEmail($user->getEmail());
  2225.             $userCheckoutInfo->setDocument($user->getDocument());
  2226.             $userCheckoutInfo->setPhone($user->getPhone());
  2227.             $userCheckoutInfo->setZipCode($user->getZipCode());
  2228.             $userCheckoutInfo->setAddress($user->getAddress());
  2229.             $userCheckoutInfo->setAddressNumber($user->getAddressNumber());
  2230.             $userCheckoutInfo->setAddressComplement($user->getAddressComplement());
  2231.             $userCheckoutInfo->setAddressNeighborhood($user->getAddressNeighborhood());
  2232.             $userCheckoutInfo->setCity($user->getCity());
  2233.             $userCheckoutInfo->setState($user->getState());
  2234.             $userCheckoutInfo->setCountry($user->getCountry());
  2235.             $userCheckoutInfo->setUser($user);
  2236.             $userCheckoutInfo->setReceiveEmail(UserCheckoutInfoEnum::NO);
  2237.             $userCheckoutInfo->setDefault(UserCheckoutInfoEnum::YES);
  2238.             $this->em->persist($userCheckoutInfo);
  2239.         }else if($checkoutNew == CartEnum::NO && !empty($userCheckoutInfoId)){
  2240.             $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2241.                 "id" => $userCheckoutInfoId,
  2242.                 "user" => $user->getId(),
  2243.                 "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2244.             ]);
  2245.         }else if($checkoutNew == CartEnum::YES){
  2246.             $userCheckoutInfo = new UserCheckoutInfo();
  2247.             $userCheckoutInfo->setName($checkoutName);
  2248.             $userCheckoutInfo->setEmail($checkoutEmail);
  2249.             $userCheckoutInfo->setDocument($checkoutDocument);
  2250.             $userCheckoutInfo->setPhone($checkoutPhone);
  2251.             $userCheckoutInfo->setZipCode($checkoutZipCode);
  2252.             $userCheckoutInfo->setAddress($checkoutAddress);
  2253.             $userCheckoutInfo->setAddressNumber($checkoutAddressNumber);
  2254.             $userCheckoutInfo->setAddressComplement($checkoutAddressComplement);
  2255.             $userCheckoutInfo->setAddressNeighborhood($checkoutAddressNeighborhood);
  2256.             if($checkoutForeigner == UserCheckoutInfoEnum::NO){
  2257.                 $country $this->em->getRepository(Country::class)->findOneBy([ 
  2258.                     "deleted" => GeoDataEnum::ITEM_NO_DELETED ,
  2259.                     "id" => GeoDataEnum::YES,
  2260.                 ]);
  2261.                 if($country){
  2262.                     $userCheckoutInfo->setCountry($country);
  2263.                 }
  2264.             }
  2265.             if(!empty($checkoutStateId)){
  2266.                 $state $this->em->getRepository(State::class)->findOneBy([
  2267.                     "id" => $checkoutStateId,
  2268.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2269.                 ]);
  2270.                 if($state){
  2271.                     $userCheckoutInfo->setState($state);
  2272.                 }
  2273.             }
  2274.             if(!empty($checkoutCityId)){
  2275.                 $city $this->em->getRepository(City::class)->findOneBy([
  2276.                     "id" => $checkoutCityId,
  2277.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2278.                 ]);
  2279.                 if($city){
  2280.                     $userCheckoutInfo->setCity($city);
  2281.                 }
  2282.             }
  2283.             $userCheckoutInfo->setUser($user);
  2284.             $userCheckoutInfo->setReceiveEmail($checkoutNotify);
  2285.             $userCheckoutInfo->setDefault(UserCheckoutInfoEnum::YES);
  2286.             $this->em->persist($userCheckoutInfo);
  2287.         }
  2288.         if(!$userCheckoutInfo){
  2289.             return $this->eadResponse([ 'userCheckoutInfo' ], ErrorEnum::FIELD_EMPTY);
  2290.         }
  2291.         if($paymentMethod == CartEnum::PAYMENT_CARD){
  2292.             if($cardNew == CartEnum::NO && !empty($cardId)){
  2293.                 $userCard $userCardRepository->findOneBy([
  2294.                     "id" => $cardId,
  2295.                     "user" => $user->getId(),
  2296.                     "deleted" => CartEnum::ITEM_NO_DELETED
  2297.                 ]);
  2298.             }else if($cardNew == CartEnum::YES){
  2299.                 if(empty($cardHash)){
  2300.                     return $this->eadResponse([ 'cardHash' ], ErrorEnum::FIELD_EMPTY);
  2301.                 }
  2302.                 $userCard $userCardRepository->createByHash($cardHash$user);
  2303.             }
  2304.             if(!$userCard){
  2305.                 return $this->eadResponse([ 'userCard' ], ErrorEnum::FIELD_EMPTY);
  2306.             }
  2307.             if(empty($installments)){
  2308.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  2309.             }
  2310.         }
  2311.         $productOfferDefault null;
  2312.         $amount null;
  2313.         $items = [];
  2314.         $courses null;
  2315.         $coursesAux = [];
  2316.         foreach ($offers as $key => $offerId) {
  2317.             $productOffer $poRepository->findOneBy([
  2318.                 "id" => $offerId,
  2319.             ]);
  2320.             if($productOffer){
  2321.                 $product $productOffer->getProduct();
  2322.                 if($offerId == $offerDefaultId){
  2323.                     $productOfferDefault $productOffer;
  2324.                     if(
  2325.                         $product->getType() == ProductEnum::COURSE || 
  2326.                         $product->getType() == ProductEnum::COMBO
  2327.                     ){
  2328.                         $courses $courseRepository->getCoursesByProduct(
  2329.                             $product->getId()
  2330.                         );
  2331.                         foreach ($courses as $key => $course) {
  2332.                             $coursesAux[] = $course->getId();
  2333.                         }
  2334.                     }
  2335.                 }
  2336.                 if(!empty($couponKey)){
  2337.                     $productCoupon $pcRepository->findValidProductCouponByProductOffer(
  2338.                         $couponKey
  2339.                         $productOffer
  2340.                     );
  2341.                 }
  2342.                 $cart $cartRepository->addCartByProductOffer(
  2343.                     $productOffer,
  2344.                     $user,
  2345.                     null,
  2346.                     $productCoupon,
  2347.                     $utmsUrl,
  2348.                     ($productOffer->getProduct()->isTypeResource() ? $courses null)
  2349.                 );
  2350.                 if($cart){
  2351.                     $items[] = (object)[
  2352.                         "courses" => $coursesAux,
  2353.                         "productOfferId" => $productOffer->getId(),
  2354.                         "productId" => $product->getId(),
  2355.                         "amount" => $cart->getPrice() + $cart->getMembershipFee(),
  2356.                         "cartId" => $cart->getId(),
  2357.                         "productCouponId" => (
  2358.                             $productCoupon $productCoupon->getId() : null
  2359.                         ),
  2360.                     ];
  2361.                     $amount $amount $cart->getPrice(true) + $cart->getMembershipFee(true);
  2362.                 }
  2363.             }
  2364.         }
  2365.         if(count($items) != count($offers)){
  2366.             return $this->eadResponse([ "added-ids" ], ErrorEnum::FIELD_EMPTY);
  2367.         }
  2368.         if(!$productOfferDefault){
  2369.             return $this->eadResponse([ "offer-default" ], ErrorEnum::FIELD_EMPTY);
  2370.         }
  2371.         $this->em->flush();
  2372.         $paymentConfig $this->configuration->getPaymentConfig();
  2373.         $dateExpire null;
  2374.         if($paymentMethod == CartEnum::PAYMENT_CARD){
  2375.             $freeInstallments $poRepository->getFreeInstallment($productOfferDefault);
  2376.             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2377.                 'amount' => $amount,
  2378.                 'free_installments' => $freeInstallments,
  2379.                 'max_installments' => $installments,
  2380.                 'interest_rate' => $paymentConfig->installmentInterest
  2381.             ]);
  2382.             $amount $installmentsOptions->{$installments}->amount;
  2383.         }else if($paymentMethod == CartEnum::PAYMENT_BILL){
  2384.             $dateExpire $poRepository->getDateExpireBill($productOffer);
  2385.         }else if($paymentMethod == CartEnum::PAYMENT_PIX){
  2386.             $dateExpire $poRepository->getDateExpirePix($productOffer);
  2387.         }
  2388.         if($amount $this->configuration->getMinValueProduct()){
  2389.             return $this->eadResponse([ 
  2390.                 "message" => "Min Value" 
  2391.             ], ErrorEnum::ACTION_INVALID);
  2392.         }
  2393.         if($user && $user->getId() == CartEnum::YES){
  2394.             $transactionService->setDebug(true);
  2395.         }
  2396.         $transactionOrigin TransactionEnum::ORIGIN_CHECKOUT_PAGE;
  2397.         $data $transactionService->createTransactionEAD(
  2398.             $user,
  2399.             $userCheckoutInfo,
  2400.             $amount,
  2401.             $paymentMethod,
  2402.             $installments,
  2403.             $items,
  2404.             $userCard,
  2405.             $dateExpire,
  2406.             null,
  2407.             null,
  2408.             null,
  2409.             $transactionOrigin
  2410.         );
  2411.         if(!empty($data['errorMessage'])){
  2412.             foreach ($offers as $key => $offerId) {
  2413.                 $productOffer $poRepository->findOneBy([
  2414.                     "id" => $offerId,
  2415.                 ]);
  2416.                 if($productOffer){
  2417.                     if($cart){
  2418.                         $cart->setErrorMessage($data['errorMessage']);
  2419.                     }
  2420.                 }
  2421.             }
  2422.             $this->em->flush();
  2423.             return $this->eadResponse([ 
  2424.                 "message" => $data['errorMessage']
  2425.             ], ErrorEnum::ACTION_INVALID);
  2426.         }
  2427.         
  2428.         return $this->eadResponse($data);
  2429.     }
  2430.     /**
  2431.      * @Route(
  2432.      *      path          = "/admin/cart/checkout/default",
  2433.      *      name          = "cartCheckoutDefault",
  2434.      *      methods       = {"POST"}
  2435.      * )
  2436.      */
  2437.     public function checkoutDefault(Request $request){
  2438.         if(!$this->user){
  2439.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2440.         }
  2441.         
  2442.         $receiverSchool $this->configuration->getReceiverSchool(
  2443.             ReceiverEnum::EAD_CHECKOUT
  2444.         );
  2445.         if(!$receiverSchool){
  2446.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2447.         }
  2448.         $transactionService $this->generalService->getService(
  2449.             'Transaction\\TransactionService'
  2450.         );
  2451.         $pagarMeTransaction $this->generalService->getService(
  2452.             'PagarMe\\PagarMeTransaction'
  2453.         );
  2454.         $this->requestUtil->setRequest($request)->setData();
  2455.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  2456.         $paymentMethod $this->requestUtil->getField('paymentMethod');
  2457.         $installments $this->requestUtil->getField('installments');
  2458.         $cartRepository $this->em->getRepository(Cart::class);
  2459.         $productRepository $this->em->getRepository(Product::class);
  2460.         $userCardRepository $this->em->getRepository(UserCard::class);
  2461.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  2462.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2463.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2464.             "id" => $userCheckoutInfoId,
  2465.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2466.         ]);
  2467.         if(!$userCheckoutInfo){
  2468.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2469.         }
  2470.         $paymentConfig $this->configuration->getPaymentConfig();
  2471.         $items = [];
  2472.         $amount 0;
  2473.         $userCard null;
  2474.         $dateExpire null;
  2475.         if(empty($paymentMethod)){
  2476.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  2477.         }
  2478.         if($paymentMethod == CartEnum::PAYMENT_CARD){ // get user card
  2479.             if(empty($installments)){
  2480.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  2481.             }
  2482.             $userCardId $this->requestUtil->getField('userCardId');
  2483.             if(empty($userCardId)){
  2484.                 return $this->eadResponse([ 'userCardId' ], ErrorEnum::FIELD_EMPTY);
  2485.             }
  2486.             if($userCardId 0){
  2487.                 $userCard $userCardRepository->findOneBy([
  2488.                     "id" => $userCardId,
  2489.                     "user" => $this->user->getId(),
  2490.                     "deleted" => CartEnum::ITEM_NO_DELETED
  2491.                 ]);
  2492.             }
  2493.         }
  2494.         if($paymentMethod == CartEnum::PAYMENT_CARD && !$userCard){
  2495.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2496.         }
  2497.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  2498.         if(empty($carts)){
  2499.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2500.         }
  2501.         $freeInstallments $paymentConfig->installmentNumberInterest;
  2502.         if($paymentConfig->assumeInstallmentInterest == CartEnum::YES){
  2503.             $freeInstallments 12;
  2504.         }
  2505.         $billDateExpire null;
  2506.         $pixDateExpire null;
  2507.         $hasOneItem = (count($carts) == CartEnum::YES);
  2508.         
  2509.         foreach ($carts as $key => $cart) {
  2510.             $productOffer $cart->getProductOffer();
  2511.             $product $cart->getProduct();
  2512.             if($hasOneItem){
  2513.                 $billDateExpire $productOfferRepository->getDateExpireBill($productOffer);
  2514.                 $pixDateExpire $productOfferRepository->getDateExpirePix($productOffer);
  2515.                 $freeInstallments $productOfferRepository->getFreeInstallment(
  2516.                     $productOffer
  2517.                 );
  2518.             }
  2519.             $productCoupon $cart->getProductCoupon();
  2520.             $coursesAux = [];
  2521.             foreach ($cart->getCourse() as $key => $course) {
  2522.                 $coursesAux[] = $course->getId();
  2523.             }
  2524.             $items[] = (object)[
  2525.                 "courses" => $coursesAux,
  2526.                 "productOfferId" => $productOffer->getId(),
  2527.                 "productId" => $product->getId(),
  2528.                 "amount" => $cart->getPrice() + $cart->getMembershipFee(),
  2529.                 "cartId" => $cart->getId(),
  2530.                 "productCouponId" => ($productCoupon $productCoupon->getId() : null),
  2531.             ];
  2532.             $amount $amount $cart->getPrice(true) + $cart->getMembershipFee(true);
  2533.         }
  2534.         if($paymentMethod == CartEnum::PAYMENT_BILL){
  2535.             $dateExpire $billDateExpire;
  2536.             if(empty($dateExpire)){
  2537.                 $period = (int)$this->configuration->get('bill_period_expire') + 1;
  2538.                 if($period == 1){
  2539.                     $period 8;
  2540.                 }
  2541.                 $dateExpire date("Y-m-d"strtotime("+ {$period} day"));
  2542.             }
  2543.         }else if($paymentMethod == CartEnum::PAYMENT_PIX){
  2544.             $dateExpire $pixDateExpire;
  2545.             if(empty($dateExpire)){
  2546.                 $period = (int)$this->configuration->get('pix_period_expire') + 1;
  2547.                 if($period == 1){
  2548.                     $period 8;
  2549.                 }
  2550.                 $dateExpire date("Y-m-d"strtotime("+ {$period} day"));
  2551.             }
  2552.         }else if($paymentMethod == CartEnum::PAYMENT_CARD){
  2553.             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2554.                 'amount' => $amount,
  2555.                 'free_installments' => $freeInstallments,
  2556.                 'max_installments' => $installments,
  2557.                 'interest_rate' => $paymentConfig->installmentInterest
  2558.             ]);
  2559.             $amount $installmentsOptions->{$installments}->amount;
  2560.         }
  2561.         if($amount $this->configuration->getMinValueProduct()){
  2562.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2563.         }
  2564.         if($this->user && $this->user->getId() == CartEnum::YES){
  2565.             $transactionService->setDebug(true);
  2566.         }
  2567.         $transactionOrigin TransactionEnum::ORIGIN_CHECKOUT_DEFAULT;
  2568.         $data $transactionService->createTransactionEAD(
  2569.             $this->user,
  2570.             $userCheckoutInfo,
  2571.             $amount,
  2572.             $paymentMethod,
  2573.             $installments,
  2574.             $items,
  2575.             $userCard,
  2576.             $dateExpire,
  2577.             null,
  2578.             null,
  2579.             (!empty($freeInstallments) ? (int)$freeInstallments null),
  2580.             $transactionOrigin
  2581.         );
  2582.         if(!empty($data['errorMessage'])){
  2583.             foreach ($carts as $key => $cart) {
  2584.                 $cart->setErrorMessage($data['errorMessage']);
  2585.             }
  2586.             $this->em->flush();
  2587.             return $this->eadResponse([ 
  2588.                 "message" => $data['errorMessage']
  2589.             ], ErrorEnum::ACTION_INVALID);
  2590.         }
  2591.         return $this->eadResponse($data);
  2592.     }
  2593.     /**
  2594.      * @Route(
  2595.      *      path          = "/admin/cart/checkout/custom",
  2596.      *      name          = "cartCheckoutCustom",
  2597.      *      methods       = {"POST"}
  2598.      * )
  2599.      */
  2600.     public function cartCheckoutCustom(Request $request){
  2601.         if(!$this->user){
  2602.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2603.         }
  2604.         $receiverSchool $this->configuration->getReceiverSchool(
  2605.             ReceiverEnum::EAD_CHECKOUT
  2606.         );
  2607.         if(!$receiverSchool){
  2608.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2609.         }
  2610.         $transactionService $this->generalService->getService(
  2611.             'Transaction\\TransactionService'
  2612.         );
  2613.         $pagarMeTransaction $this->generalService->getService(
  2614.             'PagarMe\\PagarMeTransaction'
  2615.         );
  2616.         $this->requestUtil->setRequest($request)->setData();
  2617.         $items json_decode($this->requestUtil->getField('items'));
  2618.         $userCardId $this->requestUtil->getField('userCardId');
  2619.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  2620.         if(count($items) == CartEnum::NO){
  2621.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2622.         }
  2623.         $cartRepository $this->em->getRepository(Cart::class);
  2624.         $productRepository $this->em->getRepository(Product::class);
  2625.         $userCardRepository $this->em->getRepository(UserCard::class);
  2626.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  2627.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2628.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  2629.         if(empty($carts)){
  2630.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2631.         }
  2632.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2633.             "id" => $userCheckoutInfoId,
  2634.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2635.         ]);
  2636.         if(!$userCheckoutInfo){
  2637.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2638.         }
  2639.         $groupedItems = [];
  2640.         $ref null;
  2641.         $group = [];
  2642.         foreach ($items as $key => $item) {
  2643.             
  2644.             /*if(empty($ref)){
  2645.                 $ref = $item->parentRef;
  2646.             }
  2647.             if($ref != $item->parentRef){
  2648.                 if(!empty($group)){
  2649.                     $groupedItems[] = $group;
  2650.                 }
  2651.                 $ref = $item->parentRef;
  2652.                 $group = [];
  2653.             }*/
  2654.             
  2655.             $group[] = $item;
  2656.             if(($key 1) == count($items)){
  2657.                 if(!empty($group)){
  2658.                     $groupedItems[] = $group;
  2659.                 }
  2660.             }
  2661.         }
  2662.         $userCard null;
  2663.         if($userCardId 0){
  2664.             $userCard $userCardRepository->findOneBy([
  2665.                 "id" => $userCardId,
  2666.                 "user" => $this->user->getId(),
  2667.                 "deleted" => CartEnum::ITEM_NO_DELETED
  2668.             ]);
  2669.         }
  2670.         $hashReference md5(
  2671.             date('Y-m-d H:i:s') . $this->user->getId() . $this->client->getClientId()
  2672.         );
  2673.         $paymentConfig $this->configuration->getPaymentConfig();
  2674.         foreach ($groupedItems as $key => $group) {
  2675.             $items = [];
  2676.             $paymentMethod null;
  2677.             $installments 1;
  2678.             $dateExpire null;
  2679.             $amount 0;
  2680.             foreach ($group as $key => $itemReq) {
  2681.                 $itemReq = (object)$itemReq;
  2682.                 if($itemReq->able){
  2683.                     $cart $cartRepository->findOneBy([
  2684.                         "id" => $itemReq->id,
  2685.                         "deleted" => CartEnum::ITEM_NO_DELETED
  2686.                     ]);
  2687.                     $productOffer $cart->getProductOffer();
  2688.                     $product $cart->getProduct();
  2689.                     $productCoupon $cart->getProductCoupon();
  2690.                     $coursesAux = [];
  2691.                     foreach ($cart->getCourse() as $key => $course) {
  2692.                         $coursesAux[] = $course->getId();
  2693.                     }
  2694.                     $items[] = (object)[
  2695.                         "courses" => $coursesAux,
  2696.                         "productOfferId" => $productOffer->getId(),
  2697.                         "productId" => $product->getId(),
  2698.                         "amount" => $cart->getPrice() + $productOffer->getMembershipFee(),
  2699.                         "cartId" => $cart->getId(),
  2700.                         "productCouponId" => (
  2701.                             $productCoupon $productCoupon->getId() : null
  2702.                         ),
  2703.                     ];
  2704.                     $paymentMethod $itemReq->paymentMethod;
  2705.                     $installments $itemReq->installment;
  2706.                     if($paymentMethod == CartEnum::PAYMENT_BILL){
  2707.                         $dateExpire $itemReq->billDateExpire;
  2708.                     }else if($paymentMethod == CartEnum::PAYMENT_PIX){
  2709.                         $dateExpire $itemReq->pixDateExpire;
  2710.                     }
  2711.                     $amountItem $cart->getPrice(true) + $productOffer->getMembershipFee(
  2712.                         true
  2713.                     );
  2714.                     if($paymentMethod == CartEnum::PAYMENT_CARD){
  2715.                         $freeInstallments $productOfferRepository->getFreeInstallment(
  2716.                             $productOffer
  2717.                         );
  2718.                         $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2719.                             'amount' => $amountItem,
  2720.                             'free_installments' => $freeInstallments,
  2721.                             'max_installments' => $installments,
  2722.                             'interest_rate' => $paymentConfig->installmentInterest
  2723.                         ]);
  2724.                         $amountItem $installmentsOptions->{$installments}->amount;
  2725.                     }
  2726.                     $amount $amount $amountItem;
  2727.                 }
  2728.             }
  2729.             if($amount $this->configuration->getMinValueProduct()){
  2730.                 return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2731.             }
  2732.             if($this->user && $this->user->getId() == CartEnum::YES){
  2733.                 $transactionService->setDebug(true);
  2734.             }
  2735.             $transactionOrigin TransactionEnum::ORIGIN_CHECKOUT_CUSTOM;
  2736.             $data $transactionService->createTransactionEAD(
  2737.                 $this->user,
  2738.                 $userCheckoutInfo,
  2739.                 $amount
  2740.                 $paymentMethod,
  2741.                 $installments,
  2742.                 $items,
  2743.                 $userCard,
  2744.                 $dateExpire,
  2745.                 $hashReference,
  2746.                 null,
  2747.                 null,
  2748.                 $transactionOrigin
  2749.             );
  2750.             if(!empty($data['errorMessage'])){
  2751.                 foreach ($group as $key => $itemReq) {
  2752.                     if($itemReq->able){
  2753.                         $cart $cartRepository->findOneBy([
  2754.                             "id" => $itemReq->id,
  2755.                             "deleted" => CartEnum::ITEM_NO_DELETED
  2756.                         ]);
  2757.                         $cart->setErrorMessage($data['errorMessage']);
  2758.                     }
  2759.                 }
  2760.                 $this->em->flush();
  2761.     
  2762.                 return $this->eadResponse([ 
  2763.                     "message" => $data['errorMessage']
  2764.                 ], ErrorEnum::ACTION_INVALID);
  2765.             }
  2766.         }
  2767.         $dataFinal = [
  2768.             "hashReference" => $hashReference,
  2769.         ];
  2770.         return $this->eadResponse($dataFinal);
  2771.     }
  2772.     /**
  2773.      * @Route(
  2774.      *      path          = "/cart/buy/one/click/{productOfferId}/{courseId}",
  2775.      *      name          = "cartOneClickBuy",
  2776.      *      methods       = {"GET"},
  2777.      *      defaults      = { "courseId" = 0 }
  2778.      * )
  2779.      */
  2780.     public function cartOneClickBuy(Request $request){
  2781.         $this->requestUtil->setRequest($request)->setData();
  2782.         $productOfferId $request->get('productOfferId');
  2783.         $courseId = (int)$request->get('courseId');
  2784.         $dataUtml = [
  2785.             "utm_source" => $request->get('utm_source'),
  2786.             "utm_medium" => $request->get('utm_medium'),
  2787.             "utm_campaign" => $request->get('utm_campaign'),
  2788.             "utm_term" => $request->get('utm_term'),
  2789.             "utm_content" => $request->get('utm_content'),
  2790.         ];
  2791.         $utmsUrl json_encode($dataUtml);
  2792.         $params = [
  2793.             "poID" => $productOfferId,
  2794.             "courseId" => $courseId,
  2795.         ];
  2796.         $params array_merge($params$dataUtml);
  2797.         $cartRepository $this->em->getRepository(Cart::class);
  2798.         $productOffer $this->em->getRepository(ProductOffer::class)->findOneBy([ 
  2799.             "id" => $productOfferId
  2800.             "status" => ProductOfferEnum::PUBLISHED,
  2801.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  2802.         ]);
  2803.         if(!$productOffer){
  2804.             return $this->redirectToRoute('notFound');
  2805.         }
  2806.         $product $productOffer->getProduct();
  2807.         if($product->isTypeResource() && empty($courseId)){
  2808.             return $this->redirectToRoute('notFound');
  2809.         }
  2810.         if(
  2811.             $product->getType() == ProductEnum::SUBSCRIPTION ||
  2812.             (
  2813.                 $product->getType() != ProductEnum::SUBSCRIPTION && 
  2814.                 $productOffer->getAllowRecurrency() == ProductOfferEnum::YES
  2815.             )
  2816.         ){
  2817.             if($productOffer->getAllowTrial() == ProductOfferEnum::YES){
  2818.                 $params = [
  2819.                     "poHash" => $productOffer->getOfferLink(),
  2820.                     "pcKey" => null,
  2821.                 ];
  2822.                 $params array_merge($params$dataUtml);
  2823.                 return $this->redirectToRoute('cartIndividual'$params);
  2824.             }
  2825.         }
  2826.         if(!$this->user){
  2827.             return $this->redirectToRoute('cartAdd'$params);
  2828.         }
  2829.         
  2830.         $receiverSchool $this->configuration->getReceiverSchool(
  2831.             ReceiverEnum::EAD_CHECKOUT
  2832.         );
  2833.         if(!$receiverSchool){
  2834.             return $this->redirectToRoute('cartAdd'$params);
  2835.         }
  2836.         if($this->user->getAllowOneClickBuy() == UserEnum::NO){
  2837.             return $this->redirectToRoute('cartAdd'$params);
  2838.         }
  2839.         $userCardRepository $this->em->getRepository(UserCard::class);
  2840.         $userCardDefault $userCardRepository->getDefaultValidUserCard();
  2841.         if(!$userCardDefault){
  2842.             return $this->redirectToRoute('cartAdd'$params);
  2843.         }
  2844.         if($productOffer->getSaleChannel() == ProductOfferEnum::EXTERNAL){
  2845.             $externalCheckoutLink $productOffer->getExternalCheckoutLink();
  2846.             return $this->redirect($externalCheckoutLink301);
  2847.         }
  2848.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2849.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2850.             "user" => $this->user->getId(),
  2851.             "default" => UserCheckoutInfoEnum::YES,
  2852.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2853.         ]);
  2854.         if(!$userCheckoutInfo){
  2855.             return $this->redirectToRoute('cartAdd'$params);
  2856.         }
  2857.         $productSuggestion null;
  2858.         if(!empty($productSuggestionId)){
  2859.             $productSuggestion $psRepository->findOneBy([ 
  2860.                 "id" => $productSuggestionId
  2861.                 "status" => ProductSuggestionEnum::PUBLISHED,
  2862.                 "deleted" => ProductSuggestionEnum::ITEM_NO_DELETED
  2863.             ]);
  2864.         }
  2865.         $course null;
  2866.         $courseRepository $this->em->getRepository(Course::class);
  2867.         if(!empty($courseId)){
  2868.             $course $courseRepository->findOneBy([
  2869.                 "id" => $courseId,
  2870.                 "deleted" => CourseEnum::ITEM_NO_DELETED,
  2871.             ]);
  2872.         }
  2873.         $cart $cartRepository->addCartByProductOffer(
  2874.             $productOffer
  2875.             $this->user
  2876.             $productSuggestion,
  2877.             null,
  2878.             $utmsUrl,
  2879.             ($course ? [ $course ] : null)
  2880.         );
  2881.         
  2882.         if(!$cart){
  2883.             return $this->redirectToRoute('cartAdd'$params);
  2884.         }
  2885.         $productCoupon $cart->getProductCoupon();
  2886.         $items = [
  2887.             (object)[
  2888.                 "courseId" => ($course $course->getId() : null),
  2889.                 "productOfferId" => $productOffer->getId(),
  2890.                 "productId" => $productOffer->getProduct()->getId(),
  2891.                 "amount" => $cart->getPrice() + $cart->getMembershipFee(),
  2892.                 "cartId" => $cart->getId(),
  2893.                 "productCouponId" => ($productCoupon $productCoupon->getId() : null),
  2894.             ]
  2895.         ];
  2896.         $transactionService $this->generalService->getService(
  2897.             'Transaction\\TransactionService'
  2898.         );
  2899.         if($this->user && $this->user->getId() == CartEnum::YES){
  2900.             $transactionService->setDebug(true);
  2901.         }
  2902.         $transactionOrigin TransactionEnum::ORIGIN_ONE_CLICK;
  2903.         $data = (object)$transactionService->createTransactionEAD(
  2904.             $this->user,
  2905.             $userCheckoutInfo,
  2906.             $cart->getPrice(true) + $cart->getMembershipFee(true),
  2907.             CartEnum::PAYMENT_CARD,
  2908.             1,
  2909.             $items,
  2910.             $userCardDefault,
  2911.             null,
  2912.             null,
  2913.             null,
  2914.             null,
  2915.             $transactionOrigin
  2916.         );
  2917.         if(!empty($data->errorMessage)){
  2918.             $cart->setErrorMessage($data->errorMessage);
  2919.             $this->em->flush();
  2920.             //return $this->eadResponse([ 
  2921.             //    "message" => $data->errorMessage
  2922.             //], ErrorEnum::ACTION_INVALID);
  2923.             return $this->redirectToRoute('cart'); 
  2924.         }
  2925.         $paramsConclusion = [
  2926.             "hashReference" => $data->hashReference,
  2927.         ];
  2928.         return $this->redirectToRoute('cartConclusion'$paramsConclusion);
  2929.     }
  2930.     /**
  2931.      * @Route(
  2932.      *      path          = "/admin/cart/checkout/plan/trial/{poID}/{pcID}",
  2933.      *      name          = "cartCheckoutPlanTrial",
  2934.      *      methods       = {"POST"},
  2935.      *      defaults      = { "pcID" = 0 }
  2936.      * )
  2937.      */
  2938.     public function cartCheckoutPlanTrial(Request $request){
  2939.         if(!$this->user){
  2940.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2941.         }
  2942.         
  2943.         $receiverSchool $this->configuration->getReceiverSchool(
  2944.             ReceiverEnum::EAD_CHECKOUT
  2945.         );
  2946.         if(!$receiverSchool){
  2947.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2948.         }
  2949.         $productOfferId = (int)$request->get('poID');
  2950.         $productCouponId = (int)$request->get('pcID');
  2951.         $this->requestUtil->setRequest($request)->setData();
  2952.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  2953.         $paymentMethod $this->requestUtil->getField('paymentMethod');
  2954.         $installments $this->requestUtil->getField('installments');
  2955.         $userCardRepository $this->em->getRepository(UserCard::class);
  2956.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  2957.         $productCouponRepository $this->em->getRepository(ProductCoupon::class);
  2958.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2959.         $productOffer $productOfferRepository->findOneBy([ 
  2960.             "id" => $productOfferId
  2961.             "status" => ProductOfferEnum::PUBLISHED,
  2962.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  2963.         ]);
  2964.         if(!$productOffer){
  2965.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  2966.         }
  2967.         if($productOffer->getAllowTrial() == ProductOfferEnum::NO){
  2968.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2969.         }
  2970.         
  2971.         $product $productOffer->getProduct();
  2972.         $pagarMeTransaction $this->generalService->getService(
  2973.             'PagarMe\\PagarMeTransaction'
  2974.         );
  2975.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2976.             "id" => $userCheckoutInfoId,
  2977.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2978.         ]);
  2979.         if(!$userCheckoutInfo){
  2980.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2981.         }
  2982.         $paymentConfig $this->configuration->getPaymentConfig();
  2983.         $userCard null;
  2984.         if(empty($paymentMethod)){
  2985.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  2986.         }
  2987.         if($paymentMethod == CartEnum::PAYMENT_CARD){ // get user card
  2988.             if(empty($installments)){
  2989.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  2990.             }
  2991.             $userCardId $this->requestUtil->getField('userCardId');
  2992.             if(empty($userCardId)){
  2993.                 return $this->eadResponse([ 'userCardId' ], ErrorEnum::FIELD_EMPTY);
  2994.             }
  2995.             if($userCardId 0){
  2996.                 $userCard $userCardRepository->findOneBy([
  2997.                     "id" => $userCardId,
  2998.                     "user" => $this->user->getId(),
  2999.                     "deleted" => CartEnum::ITEM_NO_DELETED
  3000.                 ]);
  3001.             }
  3002.         }
  3003.         if($paymentMethod == CartEnum::PAYMENT_CARD && !$userCard){
  3004.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3005.         }
  3006.         $productCoupon null;
  3007.         $amount $productOffer->getPriceReal();
  3008.         if(!empty($productCouponId)){
  3009.             $productCoupon $productCouponRepository->findOneBy([
  3010.                 "deleted" => ProductCouponEnum::ITEM_NO_DELETED,
  3011.                 "status" => ProductCouponEnum::PUBLISHED,
  3012.                 "id" => $productCouponId,
  3013.             ]);
  3014.             if($productCoupon){
  3015.                 if($productCouponRepository->checkApplyCoupon($productCoupon$productOffer)){
  3016.                     $amount $productCouponRepository->applyDiscount(
  3017.                         $productCoupon
  3018.                         $amount
  3019.                     );
  3020.                 }
  3021.             }
  3022.         }
  3023.         if($amount $this->configuration->getMinValueProduct()){
  3024.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3025.         }
  3026.         $userSubscriptionRepository $this->em->getRepository(UserSubscription::class);
  3027.         $userSubscription $userSubscriptionRepository->findOneBy([
  3028.             "user" => $this->user->getId(),
  3029.             "product" => $product->getId()
  3030.         ]);
  3031.         if($userSubscription && $userSubscription->isLive()){
  3032.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3033.         }
  3034.         if(!$userSubscription){
  3035.             $userSubscription = new UserSubscription();
  3036.         }else{
  3037.             $userSubscriptionRepository->restore($userSubscriptionTrashEnum::USER_SUBSCRIPTION);
  3038.         }
  3039.         $userSubscription->setStatus(UserSubscriptionEnum::STATUS_TRIAL);
  3040.         $userSubscription->setDateStart(date('Y-m-d H:i:s'));
  3041.         $userSubscription->setAutomaticRenewal($productOffer->getPlanRenew());
  3042.         $userSubscription->setLifetime($productOffer->getPlanLifetime());
  3043.         $userSubscription->setPrice($productOffer->getPriceReal());
  3044.         $userSubscription->setCycle($productOffer->getPlanCycle());
  3045.         $userSubscription->setChargeNumberMax($productOffer->getPlanChargeNumberMax());
  3046.         $userSubscription->setMembershipFee($productOffer->getMembershipFee());
  3047.         $userSubscription->setProduct($product);
  3048.         $userSubscription->setProductOffer($productOffer);
  3049.         $userSubscription->setUser($this->user);
  3050.         $userSubscription->setUsedTrial(UserSubscriptionEnum::YES);
  3051.         if($productCoupon){
  3052.             $userSubscription->setProductCoupon($productCoupon);
  3053.             $userSubscription->setCouponKey($productCoupon->getKey());
  3054.             $userSubscription->setCouponType($productCoupon->getDiscountType());
  3055.             $userSubscription->setCouponApplyMembershipFee(
  3056.                 $productCoupon->getApplyMembershipFee()
  3057.             );
  3058.             $userSubscription->setCouponLifetime($productCoupon->getPlanLifetime());
  3059.             $userSubscription->setCouponNumberCharges($productCoupon->getPlanCharges());
  3060.             $userSubscription->setCouponDiscount($productCoupon->getDiscount());
  3061.         }
  3062.         $userSubscription->setPaymentMethod($paymentMethod);
  3063.         $monthCycles = [
  3064.             UserSubscriptionEnum::CYCLE_MONTHLY,
  3065.             UserSubscriptionEnum::CYCLE_WEEKLY,
  3066.             UserSubscriptionEnum::CYCLE_BIWEEKLY,
  3067.         ];
  3068.         if(!in_array($userSubscription->getCycle(), $monthCycles)){
  3069.             $freeInstallmentNumber $productOfferRepository->getFreeInstallment(
  3070.                 $productOffer
  3071.             );
  3072.             $userSubscription->setInstallments($installments);
  3073.             $userSubscription->setInstallmentsFree($freeInstallmentNumber);
  3074.         }
  3075.         
  3076.         //define date renew
  3077.         if(
  3078.             $userSubscription->getLifetime() == UserSubscriptionEnum::NO && 
  3079.             $userSubscription->getAutomaticRenewal() == UserSubscriptionEnum::YES
  3080.         ){
  3081.             $periodRenew $userSubscriptionRepository->getPlanCycle(
  3082.                 $userSubscription->getCycle(),
  3083.                 $userSubscription->getChargeNumberMax()
  3084.             );
  3085.             $userSubscription->setDateRenew(date('Y-m-d'strtotime($periodRenew)));
  3086.         }
  3087.         $days $productOffer->getTrialPeriod();
  3088.         $dateNextPayment date('Y-m-d'strtotime("+ {$days} day"));
  3089.         $dateAccess date('Y-m-d H:i:s'strtotime("+ {$days} day"));
  3090.         $userSubscription->setDateNextPayment($dateNextPayment);
  3091.         $userSubscription->setDateExpiration($dateNextPayment);
  3092.         $userSubscription->setUserCard($userCard);
  3093.         $userSubscription->setUserCard($userCard);
  3094.         $userSubscription->setTrialDateEnd($dateAccess);
  3095.         $this->em->persist($userSubscription);
  3096.         $this->userLogService->logInsert(
  3097.             "user_subscription"
  3098.             $userSubscription->getId(), 
  3099.             $userSubscription->toReturn()
  3100.         );
  3101.         $enrollmentService $this->generalService->getService('EnrollmentService');
  3102.         $enrollmentService->setNotification(true);
  3103.         $enrollmentService->setEmail(false);
  3104.         $enrollmentService->setOrigin(EnrollmentEnum::ORIGIN_SUBSCRIPTION);
  3105.         if($userSubscription->getPaymentMethod() == UserSubscriptionEnum::PAYMENT_BILL){
  3106.             $dateAccess date('Y-m-d H:i:s'strtotime("{$dateAccess} + 3 days"));
  3107.         }
  3108.         $enrollmentService->setAccessDate($dateAccess);
  3109.         $enrollmentService->setSupportDate($dateAccess);
  3110.         $enrollmentService->setUserSubscription($userSubscription);
  3111.         $enrollmentService->enrollUserByProduct(
  3112.             $userSubscription->getUser(), 
  3113.             $userSubscription->getProduct()
  3114.         );
  3115.         $marketingService $this->generalService->getService('Marketing\MarketingService');
  3116.         $marketingService->setTag(TagsMarketingEnum::TAG_START_SUBSCRIPTION);
  3117.         $marketingService->setTextComplement($product->getTitle());
  3118.         $marketingService->setUser($this->user);
  3119.         $marketingService->send();
  3120.         $pixelService $this->generalService->getService('Marketing\\PixelService');
  3121.         $pixelService->sendConversion('StartTrial', (object)[
  3122.             "value" => $userSubscription->getPrice(),
  3123.             "currency" => $productOffer->getCurrencyCode(),
  3124.             "contents" => [
  3125.                 (object)[
  3126.                     "id" => $product->getId(),
  3127.                     "quantity" => 1,
  3128.                     "item_price" => $userSubscription->getPrice(),
  3129.                     //"name" => $product->getTitle(),
  3130.                     //"type" => $product->getType()
  3131.                 ]
  3132.             ],
  3133.             "num_items" => 1,
  3134.             "content_type" => "product",
  3135.             "transaction_id" => $userSubscription->getId(),
  3136.         ]);
  3137.         $this->em->flush();
  3138.         if($userSubscription->getUser() && $userSubscription->getProduct()->getUser()){
  3139.             sleep(1);
  3140.             
  3141.             if($userSubscription->getId()){
  3142.                 $notificationService $this->generalService->getService('NotificationService');
  3143.                 $notificationService->create(
  3144.                     $userSubscription->getUser(), 
  3145.                     $userSubscription->getProduct()->getUser(),
  3146.                     NotificationEnum::ORIGIN_USER_SUBSCRIPTION_NEW,
  3147.                     $userSubscription->getId()
  3148.                 );
  3149.                 $notificationService->create(
  3150.                     $userSubscription->getProduct()->getUser(),
  3151.                     $userSubscription->getUser(),
  3152.                     NotificationEnum::ORIGIN_USER_SUBSCRIPTION_NEW,
  3153.                     $userSubscription->getId()
  3154.                 );
  3155.             }
  3156.         }
  3157.         $userWebhook $this->em->getRepository(User::class)->getToWebhook(
  3158.             $userSubscription->getUser()
  3159.         );
  3160.         
  3161.         //webhook
  3162.         $dataObj = (object)[
  3163.             "user" => $userWebhook,
  3164.             "subscription" => $userSubscription->toWebhook(),
  3165.             "product" => (object)[
  3166.                 "id" => (string)$userSubscription->getProduct()->getId(),
  3167.                 "name" => $userSubscription->getProduct()->getTitle(),
  3168.                 "offer" => (object)[
  3169.                     "id" => (string)$userSubscription->getProductOffer()->getId(),
  3170.                     "name" => $userSubscription->getProductOffer()->getTitle(),
  3171.                     "price" => (string)$userSubscription->getProductOffer()->getPriceReal(),
  3172.                     "membership"=> (string)$userSubscription->getMembershipFee(),
  3173.                 ],
  3174.             ],
  3175.         ];
  3176.         $webhookService $this->generalService->getService('WebhookService');
  3177.         $webhookService->addItemList(WebhookEnum::SUBSCRIPTION$dataObj);
  3178.         return $this->eadResponse([
  3179.             "billLink" => null,
  3180.             "pixCode" => null,
  3181.             "userId" => $this->user->getId(),
  3182.             "transactionHash" => "success",
  3183.             "hashReference" => "success",
  3184.         ]);
  3185.     }
  3186.     /**
  3187.      * @Route(
  3188.      *      path          = "/cart/checkout/new/try/{transactionHash}",
  3189.      *      name          = "cartCheckoutNewTry",
  3190.      *      methods       = {"GET"}
  3191.      * )
  3192.      */
  3193.     public function cartCheckoutNewTry(Request $request){
  3194.         $this->checkUserSession($request);
  3195.         $transactionHash $request->get('transactionHash');
  3196.         $cartRepository $this->em->getRepository(Cart::class);
  3197.         $transactionRepository $this->em->getRepository(Transaction::class);
  3198.         $transactionItemRepository $this->em->getRepository(TransactionItem::class);
  3199.         $transaction $transactionRepository->findOneBy([
  3200.             "hash" => $transactionHash,
  3201.             "status" => TransactionEnum::CANCELED,
  3202.             "deleted" => TransactionEnum::ITEM_NO_DELETED
  3203.         ]);
  3204.         if(!$transaction){
  3205.             return $this->redirectToRoute('notFound');
  3206.         }
  3207.         $transactionItems $transactionItemRepository->findBy([
  3208.             "transaction" => $transaction->getId(),
  3209.             "deleted" => TransactionItemEnum::ITEM_NO_DELETED
  3210.         ]);
  3211.         if(empty($transactionItems)){
  3212.             return $this->redirectToRoute('notFound');
  3213.         }
  3214.         foreach ($transactionItems as $key => $transactionItem) {
  3215.             $courses = [];
  3216.             foreach ($transactionItem->getCourse() as $keyC => $course) {
  3217.                 $courses[] = $course;
  3218.             }
  3219.             $cartRepository->addCartByProductOffer(
  3220.                 $transactionItem->getProductOffer(),
  3221.                 $transaction->getUser(),
  3222.                 null,
  3223.                 $transactionItem->getProductCoupon(),
  3224.                 $transactionItem->getUtmsUrl(),
  3225.                 $courses
  3226.             );
  3227.         }
  3228.         $this->em->flush();
  3229.         return $this->redirectToRoute('cart');
  3230.     }
  3231.     /**
  3232.      * @Route(
  3233.      *      path          = "/cart/conclusion/{hashReference}",
  3234.      *      name          = "cartConclusion",
  3235.      *      methods       = {"GET"},
  3236.      *      defaults      = { "hashReference" = null }
  3237.      * )
  3238.      * @Route(
  3239.      *      path          = "/cart/conclusao/{hashReference}",
  3240.      *      name          = "cartConclusionOld",
  3241.      *      methods       = {"GET"},
  3242.      *      defaults      = { "hashReference" = null }
  3243.      * )
  3244.      */
  3245.     public function cartConclusionPage(Request $request) {
  3246.         $cartRepository $this->em->getRepository(Cart::class);
  3247.         $productRepository $this->em->getRepository(Product::class);
  3248.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  3249.         $transactionRepository $this->em->getRepository(Transaction::class);
  3250.         $ti $this->em->getRepository(TransactionItem::class);
  3251.         $psRepository $this->em->getRepository(ProductSuggestion::class);
  3252.         $hashReference $request->get('hashReference');
  3253.         $this->data['hashReference'] = $hashReference;
  3254.         $this->data['transactionsCard'] = null;
  3255.         $this->data['transactionsPix'] = null;
  3256.         $this->data['transactionsBill'] = null;
  3257.         $this->data['productOfferSuggestions'] = null;
  3258.         
  3259.         $this->data['hasWaiting'] = true;
  3260.         $this->data['hasApproved'] = false;
  3261.         $this->data['hasCanceled'] = false;
  3262.         $this->data['showInfoStep'] = true;
  3263.         $this->data['hasContracts'] = true;
  3264.         $this->data['sOffers'] = [];
  3265.         
  3266.         $this->data['showSteps'] = true;
  3267.         if(!empty($hashReference)){
  3268.             $isCheckoutPage $transactionRepository->existByReferenceAndOrigin(
  3269.                 $hashReference
  3270.                 TransactionEnum::ORIGIN_CHECKOUT_PAGE
  3271.             );
  3272.             if($isCheckoutPage){
  3273.                 $this->data['showSteps'] = false;
  3274.             }
  3275.             $hasWaiting $transactionRepository->existByReferenceAndStatus(
  3276.                 $hashReference
  3277.                 TransactionEnum::WAITING
  3278.             );
  3279.             $hasApproved $transactionRepository->existByReferenceAndStatus(
  3280.                 $hashReference
  3281.                 TransactionEnum::APPROVED
  3282.             );
  3283.             $hasCanceled $transactionRepository->existByReferenceAndStatus(
  3284.                 $hashReference
  3285.                 TransactionEnum::CANCELED
  3286.             );
  3287.             $hasWaitingCard $transactionRepository->existByReferenceAndStatus(
  3288.                 $hashReference
  3289.                 TransactionEnum::WAITING,
  3290.                 TransactionEnum::PAYMENT_CARD
  3291.             );
  3292.             $hasApprovedCard $transactionRepository->existByReferenceAndStatus(
  3293.                 $hashReference
  3294.                 TransactionEnum::APPROVED,
  3295.                 TransactionEnum::PAYMENT_CARD
  3296.             );
  3297.             $hasCanceledCard $transactionRepository->existByReferenceAndStatus(
  3298.                 $hashReference
  3299.                 TransactionEnum::CANCELED,
  3300.                 TransactionEnum::PAYMENT_CARD
  3301.             );
  3302.             $hasWaitingBill $transactionRepository->existByReferenceAndStatus(
  3303.                 $hashReference
  3304.                 TransactionEnum::WAITING,
  3305.                 TransactionEnum::PAYMENT_BILL
  3306.             );
  3307.             $hasApprovedBill $transactionRepository->existByReferenceAndStatus(
  3308.                 $hashReference
  3309.                 TransactionEnum::APPROVED,
  3310.                 TransactionEnum::PAYMENT_BILL
  3311.             );
  3312.             $hasCanceledBill $transactionRepository->existByReferenceAndStatus(
  3313.                 $hashReference
  3314.                 TransactionEnum::CANCELED,
  3315.                 TransactionEnum::PAYMENT_BILL
  3316.             );
  3317.             $hasWaitingPix $transactionRepository->existByReferenceAndStatus(
  3318.                 $hashReference,
  3319.                 TransactionEnum::WAITING,
  3320.                 TransactionEnum::PAYMENT_PIX
  3321.             );
  3322.             $hasApprovedPix $transactionRepository->existByReferenceAndStatus(
  3323.                 $hashReference,
  3324.                 TransactionEnum::APPROVED,
  3325.                 TransactionEnum::PAYMENT_PIX
  3326.             );
  3327.             $hasCanceledPix $transactionRepository->existByReferenceAndStatus(
  3328.                 $hashReference
  3329.                 TransactionEnum::CANCELED,
  3330.                 TransactionEnum::PAYMENT_PIX
  3331.             );
  3332.             $transactionsCard $transactionRepository->getToCartConclusion(
  3333.                 $hashReference
  3334.                 TransactionEnum::PAYMENT_CARD
  3335.             );
  3336.             $transactionsPix $transactionRepository->getToCartConclusion(
  3337.                 $hashReference
  3338.                 TransactionEnum::PAYMENT_PIX
  3339.             );
  3340.             $transactionsBill $transactionRepository->getToCartConclusion(
  3341.                 $hashReference
  3342.                 TransactionEnum::PAYMENT_BILL
  3343.             );
  3344.             $transactionOffers $productOfferRepository->getByTransactionHashReference(
  3345.                 $hashReference
  3346.             );
  3347.             if(count($transactionOffers) == TransactionEnum::YES){
  3348.                 $offer $transactionOffers[0];
  3349.                 $productPage $offer->getProductPage();
  3350.                 $externalPageLink $productPage->getExternalConclusionLink();
  3351.                 if(
  3352.                     $productPage->getAllowExternalConclusion() &&
  3353.                     !empty($externalPageLink) &&
  3354.                     $hasApproved
  3355.                 ){
  3356.                     return $this->redirect($externalPageLink301);
  3357.                 }
  3358.             }
  3359.             $sOffers = [];
  3360.             foreach ($transactionOffers as $keyOffer => $offer) {
  3361.                 $productOfferSuggestions $psRepository->getByOfferOriginSimply(
  3362.                     $offer,
  3363.                     ProductSuggestionEnum::EXECUTE_IN_CONCLUSION_PAGE
  3364.                 );
  3365.                 foreach ($productOfferSuggestions as $key => $productSuggestion) {
  3366.                     $productOffer $productSuggestion->getProductOffer();
  3367.                     $product $productOffer->getProduct();
  3368.                     if($this->user && !$productRepository->userHasProduct($this->user$product)){
  3369.                         $productOffer->productSuggestionId $productSuggestion->getId();
  3370.                         $productOffer->productSuggestionItem $productSuggestion;
  3371.                         
  3372.                         if($productOffer->getAllowSaleLimit() == ProductOfferEnum::YES){
  3373.                             $saleNumber $ti->countSalesApprovedByProductOffer(
  3374.                                 $productOffer
  3375.                             );
  3376.                             if($productOffer->getSaleNumberLimit() > $saleNumber){
  3377.                                 $sOffers[$productOffer->getId()] = $productOffer;
  3378.                             }
  3379.                         }else{
  3380.                             $sOffers[$productOffer->getId()] = $productOffer;
  3381.                         }
  3382.                     }
  3383.                 }
  3384.                 $cartRepository->sendConclusionConversion($hashReference);
  3385.             }
  3386.             try{
  3387.             
  3388.                 $transactions array_merge(
  3389.                     $transactionsCard,
  3390.                     $transactionsPix,
  3391.                     $transactionsBill
  3392.                 );
  3393.                 $pagarMeTransaction $this->generalService->getService(
  3394.                     'PagarMe\\PagarMeTransaction'
  3395.                 );
  3396.                 foreach ($transactions as $key => $tr) {
  3397.                     if($tr->getGateway() == TransactionEnum::EAD_CHECKOUT){
  3398.                         if($tr->getPaymentMethod() == TransactionEnum::PAYMENT_BILL){
  3399.                             $transactionPagarme $pagarMeTransaction->get($tr->getHash());
  3400.                             $dateExpire date(
  3401.                                 'Y-m-d'
  3402.                                 strtotime($transactionPagarme->boleto_expiration_date)
  3403.                             );
  3404.                             $tr->setBillCode($transactionPagarme->boleto_barcode);
  3405.                             $tr->setBillLink($transactionPagarme->boleto_url);
  3406.                             $tr->setDateExpire($dateExpire);
  3407.                         }else if($tr->getPaymentMethod() == TransactionEnum::PAYMENT_PIX){
  3408.                             $transactionPagarme $pagarMeTransaction->get($tr->getHash());
  3409.                             $dateExpire date(
  3410.                                 'Y-m-d'
  3411.                                 strtotime($transactionPagarme->pix_expiration_date)
  3412.                             );
  3413.                             $tr->setPixCode($transactionPagarme->pix_qr_code);
  3414.                             $tr->setDateExpire($dateExpire);
  3415.                         }
  3416.                     }
  3417.                 }
  3418.                 $this->em->flush();
  3419.             }catch(Exception $e){
  3420.             }
  3421.             $this->data['hasWaiting'] = $hasWaiting;
  3422.             $this->data['hasApproved'] = $hasApproved;
  3423.             $this->data['hasCanceled'] = $hasCanceled;
  3424.             $this->data['hasWaitingCard'] = $hasWaitingCard;
  3425.             $this->data['hasApprovedCard'] = $hasApprovedCard;
  3426.             $this->data['hasCanceledCard'] = $hasCanceledCard;
  3427.             $this->data['hasWaitingBill'] = $hasWaitingBill;
  3428.             $this->data['hasApprovedBill'] = $hasApprovedBill;
  3429.             $this->data['hasCanceledBill'] = $hasCanceledBill;
  3430.             $this->data['hasWaitingPix'] = $hasWaitingPix;
  3431.             $this->data['hasApprovedPix'] = $hasApprovedPix;
  3432.             $this->data['hasCanceledPix'] = $hasCanceledPix;
  3433.             $this->data['transactionsCard'] = $transactionsCard;
  3434.             $this->data['transactionsPix'] = $transactionsPix;
  3435.             $this->data['transactionsBill'] = $transactionsBill;
  3436.             $this->data['productOfferSuggestions'] = $sOffers;
  3437.         }
  3438.         $this->data['customCart'] = true;
  3439.         $this->data['cartConclusion'] = true;
  3440.         return $this->renderEAD('cart/cart-conclusion.html.twig');
  3441.     }
  3442.     /**
  3443.      * @Route(
  3444.      *      path          = "/cart/conclusion/status/{hashReference}/{charge}",
  3445.      *      name          = "cartConclusionCheckStatus",
  3446.      *      methods       = {"GET"},
  3447.      *      defaults      = { "hashReference" = null, "charge" = 0 }
  3448.      * )
  3449.      */
  3450.     public function cartConclusionCheckStatus(Request $request) {
  3451.         $cartRepository $this->em->getRepository(Cart::class);
  3452.         $transactionRepository $this->em->getRepository(Transaction::class);
  3453.         $transactionItemRepository $this->em->getRepository(TransactionItem::class);
  3454.         $pagarMeTransaction $this->generalService->getService(
  3455.             'PagarMe\\PagarMeTransaction'
  3456.         );
  3457.         $processEad $this->generalService->getService('Transaction\\ProcessEad');
  3458.         $charge $request->get('charge');
  3459.         $hashReference $request->get('hashReference');
  3460.         if(empty($hashReference)){
  3461.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3462.         }
  3463.         $transactions $transactionRepository->findBy([
  3464.             "hashReference" => $hashReference,
  3465.             "deleted" => TransactionEnum::ITEM_NO_DELETED
  3466.         ]);
  3467.         $refresh = [];
  3468.         foreach ($transactions as $key => $transaction) {
  3469.             $transactionPagarme $pagarMeTransaction->get($transaction->getHash());
  3470.             $newStatus $pagarMeTransaction->getTransactionStatus(
  3471.                 $transactionPagarme->status
  3472.             );
  3473.             if(
  3474.                 $newStatus == TransactionEnum::APPROVED || 
  3475.                 $charge == TransactionEnum::YES && (
  3476.                     !empty($transactionPagarme->boleto_barcode) ||
  3477.                     !empty($transactionPagarme->pix_expiration_date)
  3478.                 )
  3479.             ){
  3480.                 $refresh[] = true;
  3481.             }
  3482.         }
  3483.         $this->em->flush();
  3484.         $data = [
  3485.             "refresh" => (count($refresh) == count($transactions))
  3486.         ];
  3487.         return $this->eadResponse($dataErrorEnum::SUCCESS);
  3488.     }
  3489.     /**
  3490.      * @Route(
  3491.      *      path          = "/cart/remove/coupon/{id}",
  3492.      *      name          = "cartCouponRemove",
  3493.      *      methods       = {"GET"},
  3494.      *      requirements  = { "id" = "\d+"}
  3495.      * )
  3496.      */
  3497.     public function removeCouponProduct(Request $request) {
  3498.         $cartId $request->get('id');
  3499.         $cartRepository $this->em->getRepository(Cart::class);
  3500.         $cart $cartRepository->findOneBy([
  3501.             "id" => $cartId,
  3502.             "deleted" => CartEnum::ITEM_NO_DELETED
  3503.         ]);
  3504.         
  3505.         if(!$cart){
  3506.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3507.         }
  3508.         if($this->user){
  3509.             if($this->user->getId() != $cart->getUser()->getId()){
  3510.                 return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3511.             }   
  3512.         }
  3513.         if($cart->getProductCoupon()){
  3514.             $product $cart->getProduct();
  3515.             $productOffer $cart->getProductOffer();
  3516.             $cart->setPrice($productOffer->getPriceReal());
  3517.             $cart->setMembershipFee(ProductEnum::NO);
  3518.             if(
  3519.                 $product->getType() == ProductEnum::SUBSCRIPTION ||
  3520.                 $productOffer->getAllowRecurrency() == ProductEnum::YES
  3521.             ){
  3522.                 $cart->setMembershipFee($productOffer->getMembershipFee());
  3523.             }
  3524.             $cart->setProductCoupon(null);
  3525.         }
  3526.         $this->em->flush();
  3527.         // return $this->eadResponse($cart->toReturn(), ErrorEnum::SUCCESS);
  3528.         return $this->redirectToRoute('cart');
  3529.     }
  3530.     /**
  3531.      * @Route(
  3532.      *      path          = "/delete/{id}/{isAjaxRequest}",
  3533.      *      name          = "cartDelete",
  3534.      *      methods       = {"GET"},
  3535.      *      requirements  = { "id" = "\d+" },
  3536.      *      defaults      = { "isAjaxRequest" = 0 }
  3537.      * )
  3538.      */
  3539.     public function deleteCart(Request $request) {
  3540.         $isAjaxRequest = (int)$request->get('isAjaxRequest');
  3541.         $cartId $request->get('id');
  3542.         $cartRepository $this->em->getRepository(Cart::class);
  3543.         $cart $cartRepository->findOneBy([
  3544.             "id" => $cartId,
  3545.             "deleted" => CartEnum::ITEM_NO_DELETED
  3546.         ]);
  3547.         
  3548.         if(!$cart){
  3549.             if($isAjaxRequest == CartEnum::YES){
  3550.                 return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3551.             }
  3552.             return $this->redirectToRoute('notFound');
  3553.         }
  3554.         if($this->user){
  3555.             if($this->user->getId() != $cart->getUser()->getId()){
  3556.                 if($isAjaxRequest == CartEnum::YES){
  3557.                     return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3558.                 }
  3559.                 return $this->redirectToRoute('notFound');
  3560.             }
  3561.         }else{
  3562.             $cookieName 'hashcartoff';
  3563.             $hashId $this->generalService->getCookie($cookieName);
  3564.             if(empty($hashId) || $hashId != $cart->getHashIdentify()){
  3565.                 if($isAjaxRequest == CartEnum::YES){
  3566.                     return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3567.                 }
  3568.                 return $this->redirectToRoute('notFound');
  3569.             }
  3570.         }
  3571.         $cartRepository->updateCartProductSuggestion($cart);
  3572.         if($this->user){
  3573.             $whishlistIsActive $this->em->getRepository(Whishlist::class)->findOneBy([
  3574.                 "product" => $cart->getProduct()->getId(), 
  3575.                 "user" => $cart->getUser()->getId(),
  3576.                 "deleted" => WhishlistEnum::ITEM_NO_DELETED
  3577.             ]);
  3578.     
  3579.             $crmService $this->generalService->getService('CRM\\CrmService');
  3580.             $dealId $cart->getPipedriveDeal();
  3581.             $crmService->deleteDeal($dealId);
  3582.     
  3583.             if($whishlistIsActive){
  3584.                 $backWhishlist $crmService->saveDeal(
  3585.                     $dealId,
  3586.                     $cart->getUser(),
  3587.                     $cart->getProduct(),
  3588.                     'open',
  3589.                     $this->configuration->get("pipedrive_wishlist"),
  3590.                     $cart->getPrice(),
  3591.                     $cart->getProductOffer()->getCurrencyCode()
  3592.                 );
  3593.             }    
  3594.         }
  3595.         $cart->setUtmsUrl(null);
  3596.         $cart->setProductCoupon(null);
  3597.         $cart->delete();
  3598.         $this->em->flush();
  3599.         $mkgService $this->generalService->getService('Marketing\\MarketingService');
  3600.         $mkgService->setTag(TagsMarketingEnum::TAG_REMOVE_CART);
  3601.         $mkgService->setTextComplement($cart->getProduct()->getTitle());
  3602.         $mkgService->setUser($this->user);
  3603.         $mkgService->send();
  3604.         // AJAX
  3605.         // if($isAjaxRequest == CartEnum::YES){
  3606.             // return $this->redirectToRoute('cart');
  3607.         // }
  3608.         // NAVBAR
  3609.         return $this->redirectToRoute('cart');
  3610.     }
  3611. }