src/Controller/Website/CartController.php line 1158

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([ "success" => ( $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.             $this->data['couponKeyValid'] = $cartRepository->applyCouponUserCart(
  970.                 $couponKey
  971.                 $this->user
  972.                 $hashId
  973.             );
  974.         }
  975.         $this->data['formName'] = "formRegisterUserCart";
  976.         $this->data['carts'] = null;
  977.         $this->data['cartSubtotal'] = 0;
  978.         $this->data['cartDisplaySubtotal'] = 0;
  979.         $this->data['couponKey'] = $couponKey;
  980.         $this->data['showInfoStep'] = true;
  981.         $cartRepository $this->em->getRepository(Cart::class);
  982.         $customCart false;
  983.         $displayTotal 0;
  984.         $cartSubtotal 0;
  985.         $paymentConfig $this->configuration->getPaymentConfig();
  986.         if($this->user){
  987.             $userCheckoutInfoRepository $this->em->getRepository(
  988.                 UserCheckoutInfo::class
  989.             );
  990.             $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  991.                 "user" => $this->user->getId(),
  992.                 "default" => UserCheckoutInfoEnum::YES,
  993.                 "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
  994.             ]);
  995.             if($userCheckoutInfo){
  996.                 $this->data['showInfoStep'] = false;
  997.             }
  998.             $userId $this->user->getId();
  999.             $carts $cartRepository->getUserValidCarts($userId);
  1000.             $userCards $this->em->getRepository(UserCard::class)->getValidUserCard();
  1001.             $customCart = (
  1002.                 $cartRepository->countCustomCheckout($userId) > true false
  1003.             );
  1004.             $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($userId);
  1005.             $cartSubtotal $cartRepository->getUserSubTotal($userId);
  1006.             $pagarMeTransaction $this->generalService->getService(
  1007.                 'PagarMe\\PagarMeTransaction'
  1008.             );
  1009.             $numberUtil $this->generalService->getUtil('NumberUtil');
  1010.             $methods = [
  1011.                 ProductOfferEnum::CARD,
  1012.                 ProductOfferEnum::BILL,
  1013.                 ProductOfferEnum::PIX,
  1014.             ];
  1015.             $cardOptions = [
  1016.                 ProductOfferEnum::ALL_METHODS,
  1017.                 ProductOfferEnum::CARD,
  1018.                 ProductOfferEnum::CARD_PIX,
  1019.                 ProductOfferEnum::CARD_BILL,
  1020.             ];
  1021.             $pixOptions = [
  1022.                 ProductOfferEnum::ALL_METHODS,
  1023.                 ProductOfferEnum::PIX,
  1024.                 ProductOfferEnum::CARD_PIX,
  1025.                 ProductOfferEnum::BILL_PIX,
  1026.             ];
  1027.             $billOptions = [
  1028.                 ProductOfferEnum::ALL_METHODS,
  1029.                 ProductOfferEnum::BILL,
  1030.                 ProductOfferEnum::CARD_BILL,
  1031.                 ProductOfferEnum::BILL_PIX,
  1032.             ];
  1033.             $getPaymentOption = function(ProductOffer $productOffer) use (
  1034.                 $productOfferRepository
  1035.                 $cardOptions
  1036.                 $pixOptions
  1037.                 $billOptions
  1038.             ){
  1039.                 $billTypeExpire ProductOfferEnum::BILL_PERIOD;
  1040.                 $billPeriodExpire 7;
  1041.                 $billDateExpire null;
  1042.                 $pixTypeExpire ProductOfferEnum::PIX_PERIOD;
  1043.                 $pixPeriodExpire 7;
  1044.                 $pixDateExpire null;
  1045.                 $installmentNumberMax 12;
  1046.                 $installmentNumberInterest 1;
  1047.                 $freeInstallments 1;
  1048.                 $paymentMethodOption $productOffer->getPaymentMethod();
  1049.                 if(in_array($paymentMethodOption$cardOptions)){
  1050.                     $installmentNumberMax $productOffer->getInstallmentNumberMax();
  1051.                     $installmentNumberInterest $productOffer->getInstallmentNumberInterest();
  1052.                     $freeInstallments $productOfferRepository->getFreeInstallment(
  1053.                         $productOffer
  1054.                     );
  1055.                 }else if(in_array($paymentMethodOption$pixOptions)){
  1056.                     $pixTypeExpire $productOffer->getPixTypeExpire();
  1057.                     $pixPeriodExpire $productOffer->getPixPeriodExpire();
  1058.                     $pixDateExpire $productOffer->getPixDateExpire();
  1059.                 }else if(in_array($paymentMethodOption$billOptions)){
  1060.                     $billTypeExpire $productOffer->getBillTypeExpire();
  1061.                     $billPeriodExpire $productOffer->getBillPeriodExpire();
  1062.                     $billDateExpire $productOffer->getBillDateExpire();
  1063.                 }
  1064.                 return (object)[
  1065.                     "typeCheckout" => $productOffer->getTypeCheckout(),
  1066.                     "saleOption" => $productOffer->getSaleOption(),
  1067.                     "paymentMethod" => $paymentMethodOption,
  1068.                     "billTypeExpire" => $billTypeExpire,
  1069.                     "billPeriodExpire" => $billPeriodExpire,
  1070.                     "billDateExpire" => $billDateExpire,
  1071.                     "pixTypeExpire" => $pixTypeExpire,
  1072.                     "pixPeriodExpire" => $pixPeriodExpire,
  1073.                     "pixDateExpire" => $pixDateExpire,
  1074.                     "installmentNumberMax" => $installmentNumberMax,
  1075.                     "installmentNumberInterest" => $installmentNumberInterest,
  1076.                     "freeInstallments" => $freeInstallments,
  1077.                     "items" => [],
  1078.                 ];
  1079.             };
  1080.             $offerPaymentOptions = [];
  1081.             $internalOptions = [
  1082.                 ProductOfferEnum::INTERNAL_EAD,
  1083.                 ProductOfferEnum::INTERNAL_PAYPAL,
  1084.                 ProductOfferEnum::INTERNAL_PAGSEGURO,
  1085.                 ProductOfferEnum::INTERNAL_MERCADOPAGO,
  1086.             ];
  1087.             foreach ($carts as $key => $cart) {
  1088.                 $productOffer $cart->getProductOffer();
  1089.                 $offerInternalOptions json_decode($productOffer->getSaleOptionInternal());
  1090.                 if(is_array($offerInternalOptions)){
  1091.                     $internalOptions array_intersect(
  1092.                         $internalOptions
  1093.                         $offerInternalOptions
  1094.                     );
  1095.                 }
  1096.                 $productOfferCouponTotal $pcRepository->countPublicCouponByProductOffer(
  1097.                     $productOffer
  1098.                 );
  1099.                 $cart->offerPaymentOptions null;
  1100.                 if($customCart){
  1101.                     $cart->offerPaymentOptions $getPaymentOption($productOffer);
  1102.                     $offerPaymentOptions[] = $cart->offerPaymentOptions;
  1103.                 }
  1104.                 $cart->productOfferCouponTotal $productOfferCouponTotal;
  1105.                 $carts[$key] = $cart;
  1106.                 //tags
  1107.                 $mkgService $this->generalService->getService('Marketing\\MarketingService');
  1108.                 $mkgService->setTag(TagsMarketingEnum::TAG_ADD_CART);
  1109.                 $mkgService->setTextComplement($cart->getProduct()->getTitle());
  1110.                 $mkgService->setUser($cart->getUser());
  1111.                 $mkgService->setProductOffer($productOffer);
  1112.                 $mkgService->setOpportunity(ProductOpportunityEnum::TAG_ADD_CART);
  1113.                 $mkgService->send();
  1114.             }
  1115.             $customCartSame true;
  1116.             $optionCustomDefault null;
  1117.             if(count($offerPaymentOptions) == count($carts)){
  1118.                 foreach ($offerPaymentOptions as $key => $option) {
  1119.                     if(empty($optionCustomDefault)){
  1120.                         $optionCustomDefault $option;
  1121.                     }
  1122.                     if($optionCustomDefault != $option){
  1123.                         $customCartSame false;
  1124.                         break;
  1125.                     }
  1126.                 }
  1127.             }
  1128.             $this->data['use_card'] = $this->configuration->get('use_card');
  1129.             $this->data['use_pix'] = $this->configuration->get('use_pix');
  1130.             $this->data['use_bill'] = $this->configuration->get('use_bill');
  1131.             $this->data['use_paypal'] = CartEnum::NO;
  1132.             $this->data['use_pagseguro'] = CartEnum::NO;
  1133.             $this->data['use_mercadopago'] = CartEnum::NO;
  1134.                 
  1135.             if(is_array($internalOptions)){
  1136.                 $this->data['use_paypal'] = (
  1137.                     in_array(ProductOfferEnum::INTERNAL_PAYPAL$internalOptions) ? 
  1138.                     CartEnum::YES 
  1139.                     CartEnum::NO
  1140.                 );
  1141.                 $this->data['use_pagseguro'] = (
  1142.                     in_array(ProductOfferEnum::INTERNAL_PAGSEGURO$internalOptions) ? 
  1143.                     CartEnum::YES 
  1144.                     CartEnum::NO
  1145.                 );
  1146.                 $this->data['use_mercadopago'] = (
  1147.                     in_array(ProductOfferEnum::INTERNAL_MERCADOPAGO$internalOptions) ? 
  1148.                     CartEnum::YES 
  1149.                     CartEnum::NO
  1150.                 );
  1151.             }
  1152.             $this->data['isOneMethod'] = CartEnum::NO;
  1153.             $this->data['isOneMethodType'] = null;
  1154.             if($customCartSame && $optionCustomDefault){
  1155.                 $customCart false;
  1156.                 $methodsInUse = [];
  1157.                 $this->data['use_card'] = CartEnum::NO;
  1158.                 $this->data['use_pix'] = CartEnum::NO;
  1159.                 $this->data['use_bill'] = CartEnum::NO;
  1160.                 if(in_array($optionCustomDefault->paymentMethod$cardOptions)){
  1161.                     $this->data['use_card'] = CartEnum::YES;
  1162.                     $methodsInUse[] = CartEnum::PAYMENT_CARD;
  1163.                 }
  1164.                 if(in_array($optionCustomDefault->paymentMethod$pixOptions)){
  1165.                     $this->data['use_pix'] = CartEnum::YES;
  1166.                     $methodsInUse[] = CartEnum::PAYMENT_PIX;
  1167.                 }
  1168.                 if(in_array($optionCustomDefault->paymentMethod$billOptions)){
  1169.                     $this->data['use_bill'] = CartEnum::YES;
  1170.                     $methodsInUse[] = CartEnum::PAYMENT_BILL;
  1171.                 }
  1172.                 if(count($methodsInUse) == CartEnum::YES){
  1173.                     $this->data['isOneMethod'] = CartEnum::YES;
  1174.                     $this->data['isOneMethodType'] = end($methodsInUse);
  1175.                 }
  1176.                 $freeInstallments $optionCustomDefault->installmentNumberInterest;
  1177.                 /*if($paymentConfig->assumeInstallmentInterest == ProductOfferEnum::YES){
  1178.                     $freeInstallments = 12;
  1179.                 }*/
  1180.                 $parcelInfo $numberUtil->getNumberMaxParcel(
  1181.                     $cartSubtotal 100,
  1182.                     $optionCustomDefault->installmentNumberMax
  1183.                     $freeInstallments
  1184.                     ProductOfferEnum::EAD,
  1185.                     ProductOfferEnum::CHECKOUT_CUSTOM
  1186.                 );
  1187.                 $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1188.                     'amount' => $numberUtil->numberToCents($cartSubtotal),
  1189.                     'free_installments' => $freeInstallments,
  1190.                     'max_installments' => $parcelInfo->maxInstallments,
  1191.                     'interest_rate' => $paymentConfig->installmentInterest
  1192.                 ]);
  1193.                 $this->data['installmentsOptions'] = (array)$installmentsOptions;
  1194.             }else if($customCart){
  1195.                 $steps = [];
  1196.                 $auxOption = [];
  1197.                 foreach ($offerPaymentOptions as $key => $value) {
  1198.                     if(!in_array($value$auxOption)){
  1199.                         $auxOption[] = $value;
  1200.                     }
  1201.                 }
  1202.                 $offerPaymentOptions $auxOption;
  1203.                 $defaultObj = (object)[
  1204.                     "amountStep" => 0,
  1205.                     "amountStepCent" => 0,
  1206.                     "items" => [],
  1207.                     "installmentsOptions" => null,
  1208.                     "freeInstallments" => 1,
  1209.                     "useCard" => $this->configuration->get('use_card'),
  1210.                     "usePix" => $this->configuration->get('use_pix'),
  1211.                     "useBill" => $this->configuration->get('use_bill'),
  1212.                     "installmentNumberMax" => $paymentConfig->installmentNumberMax,
  1213.                     "assumeInstallmentInterest" => $paymentConfig->assumeInstallmentInterest,
  1214.                     "installmentNumberInterest" => $paymentConfig->installmentNumberInterest,
  1215.                     "default" => true,
  1216.                 ];
  1217.                 $defaultSubtotalCent 0;
  1218.                 $defaultInstallmentsOptions null;
  1219.                 foreach ($carts as $key => $cart) {
  1220.                     $cart->setAble(CartEnum::YES);
  1221.                     $productOffer $cart->getProductOffer();
  1222.                     if($productOffer->getTypeCheckout() == ProductOfferEnum::CHECKOUT_DEFAULT){
  1223.                         $price $cart->getPrice() + $cart->getMembershipFee();
  1224.                         $priceCents $cart->getPrice(true) + $cart->getMembershipFee(true);
  1225.                         $defaultObj->amountStep $defaultObj->amountStep $price;
  1226.                         $defaultObj->amountStepCent $defaultObj->amountStepCent $priceCents;
  1227.                         $defaultObj->items[] = $cart;
  1228.                     }else{
  1229.                         $indexOptions array_search(
  1230.                             $cart->offerPaymentOptions,
  1231.                             $offerPaymentOptions
  1232.                         );
  1233.                         if(isset($offerPaymentOptions[$indexOptions])){
  1234.                             $offerPaymentOptions[$indexOptions]->items[] = $cart;
  1235.                         }
  1236.                     }
  1237.                 }
  1238.                 foreach ($offerPaymentOptions as $key => $option) {
  1239.                     if(count($option->items)){
  1240.                         $option->useCard in_array($option->paymentMethod$cardOptions);
  1241.                         $option->usePix in_array($option->paymentMethod$pixOptions);
  1242.                         $option->useBill in_array($option->paymentMethod$billOptions);
  1243.                         $amountOption 0;
  1244.                         $amountOptionCents 0;
  1245.                         foreach ($option->items as $keyItem => $item) {
  1246.                             $productOffer $item->getProductOffer();
  1247.                             $amountOption $amountOption $item->getPrice() + $item->getMembershipFee();
  1248.                             $amountOptionCents $amountOptionCents $item->getPrice(true) + $item->getMembershipFee(true);
  1249.                         }
  1250.                         
  1251.                         $installmentsOptions null;
  1252.                         if($option->useCard){
  1253.                             $parcelInfo $numberUtil->getNumberMaxParcel(
  1254.                                 $amountOptionCents,
  1255.                                 $option->installmentNumberMax,
  1256.                                 $option->freeInstallments,
  1257.                                 $option->saleOption,
  1258.                                 $option->typeCheckout
  1259.                             );
  1260.                             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1261.                                 'amount' => $amountOptionCents,
  1262.                                 'free_installments' => $option->freeInstallments,
  1263.                                 'max_installments' => $parcelInfo->maxInstallments,
  1264.                                 'interest_rate' => $paymentConfig->installmentInterest
  1265.                             ]);
  1266.                             $installmentsOptions = (array)$installmentsOptions;
  1267.                         }
  1268.                         $newStep = (object)[
  1269.                             'amountStep' => $amountOption,
  1270.                             'amountStepCent' => $amountOptionCents,
  1271.                             "items" => $option->items,
  1272.                             "installmentsOptions" => $installmentsOptions,
  1273.                             "freeInstallments" => 1,
  1274.                             "useCard" => $option->useCard,
  1275.                             "usePix" => $option->usePix,
  1276.                             "useBill" => $option->useBill,
  1277.                             "installmentNumberMax" => $option->installmentNumberMax,
  1278.                             "assumeInstallmentInterest" => ProductOfferEnum::NO,
  1279.                             "installmentNumberInterest" => $option->installmentNumberInterest,
  1280.                             "default" => false
  1281.                         ];
  1282.                         if($option->useCard){
  1283.                             $this->data['use_card'] = CartEnum::YES;
  1284.                         }
  1285.                         if($option->usePix){
  1286.                             $this->data['use_pix'] = CartEnum::YES;
  1287.                         }
  1288.                         if($option->useBill){
  1289.                             $this->data['use_bill'] = CartEnum::YES;
  1290.                         }
  1291.                         $steps[] = $newStep;
  1292.                     }
  1293.                 }
  1294.                 if(count($defaultObj->items)){
  1295.                     
  1296.                     $defaultObj->freeInstallments $paymentConfig->installmentNumberInterest;
  1297.                     if($paymentConfig->assumeInstallmentInterest == ProductOfferEnum::YES){
  1298.                         $defaultObj->freeInstallments 12;
  1299.                     }
  1300.                     $parcelInfo $numberUtil->getNumberMaxParcel(
  1301.                         $defaultObj->amountStepCent
  1302.                         $paymentConfig->installmentNumberMax
  1303.                         $defaultObj->freeInstallments
  1304.                         ProductOfferEnum::EAD,
  1305.                         ProductOfferEnum::CHECKOUT_DEFAULT
  1306.                     );
  1307.                     $defaultOptions = (array)$pagarMeTransaction->calculateInstallments([
  1308.                         'amount' => $defaultObj->amountStepCent,
  1309.                         'free_installments' => $defaultObj->freeInstallments,
  1310.                         'max_installments' => $parcelInfo->maxInstallments,
  1311.                         'interest_rate' => $paymentConfig->installmentInterest
  1312.                     ]);
  1313.                     $defaultObj->installmentsOptions $defaultOptions;
  1314.                     
  1315.                     array_unshift($steps$defaultObj);
  1316.                 }
  1317.                 $this->data['steps'] = $steps;
  1318.             }else{
  1319.                 $freeInstallments $paymentConfig->installmentNumberInterest;
  1320.                 if($paymentConfig->assumeInstallmentInterest == ProductOfferEnum::YES){
  1321.                     $freeInstallments 12;
  1322.                 }
  1323.                 $parcelInfo $numberUtil->getNumberMaxParcel(
  1324.                     $cartSubtotal 100,
  1325.                     $paymentConfig->installmentNumberMax
  1326.                     $freeInstallments
  1327.                     ProductOfferEnum::EAD,
  1328.                     ProductOfferEnum::CHECKOUT_DEFAULT
  1329.                 );
  1330.                 $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1331.                     'amount' => $numberUtil->numberToCents($cartSubtotal),
  1332.                     'free_installments' => $freeInstallments,
  1333.                     'max_installments' => $parcelInfo->maxInstallments,
  1334.                     'interest_rate' => $paymentConfig->installmentInterest
  1335.                 ]);
  1336.                 $this->data['installmentsOptions'] = (array)$installmentsOptions;
  1337.             }
  1338.             $receiverSchoolEad $this->configuration->getReceiverSchool(
  1339.                 ReceiverEnum::EAD_CHECKOUT
  1340.             );
  1341.             $receiverSchoolPayPal $this->configuration->getReceiverSchool(
  1342.                 ReceiverEnum::PAYPAL
  1343.             );
  1344.             $receiverSchoolPagSeguro $this->configuration->getReceiverSchool(
  1345.                 ReceiverEnum::PAGSEGURO
  1346.             );
  1347.             $receiverSchoolMercadoPago $this->configuration->getReceiverSchool(
  1348.                 ReceiverEnum::MERCADO_PAGO
  1349.             );
  1350.             $displayTotal $cartRepository->getUserDisplaySubTotal($userId);
  1351.             $this->data['receiverSchoolEad'] = $receiverSchoolEad;
  1352.             $this->data['receiverSchoolPayPal'] = $receiverSchoolPayPal;
  1353.             $this->data['receiverSchoolPagSeguro'] = $receiverSchoolPagSeguro;
  1354.             $this->data['receiverSchoolMercadoPago'] = $receiverSchoolMercadoPago;
  1355.             $this->data['cartSubtotal'] = $cartSubtotal;
  1356.             $this->data['cartDisplaySubtotal'] = $displayTotal;
  1357.             $this->data['userCards'] = $userCards;
  1358.             $this->data['hasOnlyCourseInCart'] = $hasOnlyCourseInCart;
  1359.             $this->data['carts'] = $carts;
  1360.         }else if(!empty($hashId)){
  1361.             $cartInfo $cartRepository->getHashValidCartsInfo($hashId);
  1362.             $customCart = ($cartInfo->totalCustom true false);
  1363.             $carts $cartInfo->data;
  1364.             foreach ($carts as $key => $cart) {
  1365.                 $cart->setAble(CartEnum::YES);
  1366.                 $productOffer $cart->getProductOffer();
  1367.                 $productOfferCouponTotal $pcRepository->countPublicCouponByProductOffer(
  1368.                     $productOffer
  1369.                 );
  1370.                 $cart->productOfferCouponTotal $productOfferCouponTotal;
  1371.                 $carts[$key] = $cart;
  1372.             }
  1373.             $this->data['carts'] = $carts;
  1374.             $this->data['cartSubtotal'] = $cartInfo->totalPrice;
  1375.             $this->data['cartDisplaySubtotal'] = $cartInfo->totalDisplay;
  1376.         }
  1377.         $this->data['keyCaptcha'] = $this->createCaptchaKey($request);
  1378.         $this->data['customCart'] = $customCart;
  1379.         $this->data['cartSubtotalDiff'] = $displayTotal $cartSubtotal;
  1380.         $this->data['isCart'] = true;
  1381.         $this->data['urlPost'] = $this->generalService->generateUrl("cartCheckoutDefault");
  1382.         $this->data['urlConclusion'] = $this->generalService->generateUrl("cartConclusion");
  1383.         $this->em->flush();
  1384.         if(empty($this->data['carts'])){
  1385.             $this->data['carts'] = [];
  1386.         }
  1387.         return $this->renderEAD('cart/cart.html.twig');
  1388.     }
  1389.     /**
  1390.      * @Route(
  1391.      *      path          = "/cart/indvidual/{poHash}/{pcKey}",
  1392.      *      name          = "cartIndividual",
  1393.      *      methods       = {"GET"},
  1394.      *      defaults      = { "pcKey" = "" }
  1395.      * )
  1396.      */
  1397.     public function cartPageIndvidual(Request $request) {
  1398.         $cartRepository $this->em->getRepository(Cart::class);
  1399.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  1400.         $productCouponRepository $this->em->getRepository(ProductCoupon::class);
  1401.         $productRepository $this->em->getRepository(Product::class);
  1402.         $offerHash $request->get('poHash');
  1403.         $productOffer $productOfferRepository->getProductByOfferLink($offerHash);
  1404.         if(!$productOffer){
  1405.             return $this->redirectToRoute('notFound');
  1406.         }
  1407.         $product $productOffer->getProduct();
  1408.         if($productOffer->getAllowTrial() == ProductOfferEnum::NO){
  1409.             return $this->redirectToRoute('notFound');
  1410.         }
  1411.         $productPage $productOffer->getProductPage();
  1412.         if(!$productPage){
  1413.             return $this->redirectToRoute('notFound');
  1414.         }
  1415.         if($productOffer->getSaleChannel() == ProductOfferEnum::EXTERNAL){
  1416.             $externalCheckoutLink $productOffer->getExternalCheckoutLink();
  1417.             return $this->redirect($externalCheckoutLink301);
  1418.         }
  1419.         
  1420.         $externalPage $productPage->getExternalPage();
  1421.         $externalPageLink $productPage->getExternalPageLink();
  1422.         if($externalPage == ProductEnum::YES && !empty($externalPageLink)){
  1423.             //return $this->redirect($externalPageLink, 301);
  1424.         }
  1425.         
  1426.         $productCoupon null;
  1427.         $couponKey $request->get('pcKey');
  1428.         $couponKeyValid false;
  1429.         
  1430.         if(!empty($couponKey)){
  1431.             $productCoupon $productCouponRepository->findValidProductCouponByProductOffer(
  1432.                 $couponKey,
  1433.                 $productOffer
  1434.             );
  1435.             if($productCoupon){
  1436.                 $amount $productOffer->getPriceReal();
  1437.                 if(
  1438.                     $productCouponRepository->checkApplyCoupon($productCoupon$productOffer)
  1439.                 ){
  1440.                     $amount $productCouponRepository->applyDiscount(
  1441.                         $productCoupon,
  1442.                         $amount
  1443.                     );
  1444.                     $couponKeyValid true;
  1445.                 }
  1446.                 if(!empty($amount)){
  1447.                     $productOffer->setPriceRealCopy($amount);
  1448.                 }
  1449.             }
  1450.         }
  1451.         if(
  1452.             $this->user &&
  1453.             (
  1454.                 $product->getType() == ProductEnum::SUBSCRIPTION || 
  1455.                 $productOffer->getAllowRecurrency() == ProductEnum::YES
  1456.             )
  1457.         ){
  1458.             $userSubscriptionRepository $this->em->getRepository(UserSubscription::class);
  1459.             $userSubscription $userSubscriptionRepository->findOneBy([
  1460.                 "user" => $this->user->getId(),
  1461.                 "product" => $product->getId(),
  1462.                 "deleted" => UserSubscriptionEnum::ITEM_NO_DELETED,
  1463.             ]);
  1464.             if($userSubscription){
  1465.                 if(
  1466.                     $userSubscription->getStatus() == UserSubscriptionEnum::STATUS_COMPLETED ||
  1467.                     $userSubscription->getStatus() == UserSubscriptionEnum::STATUS_CANCELED
  1468.                 ){
  1469.                     $params = [
  1470.                         "poID" => $productOffer->getId(),
  1471.                         "pcID" => ( $productCoupon $productCoupon->getId() : ),
  1472.                     ];
  1473.                     return $this->redirectToRoute('cartAdd'$params);
  1474.                 }
  1475.                 return $this->redirectToRoute('notFound');
  1476.             }
  1477.         }
  1478.         $this->data['couponKey'] = $couponKey;
  1479.         $this->data['couponKeyValid'] = $couponKeyValid;
  1480.         $this->data['showInfoStep'] = true;
  1481.         $customCart false;
  1482.         $displayTotal $productOffer->getPriceDisplay();
  1483.         $cartSubtotal $productOffer->getPriceRealCopy();
  1484.         $hasOnlyCourseInCart true;
  1485.         $paymentConfig $this->configuration->getPaymentConfig();
  1486.         $userCards = [];
  1487.         if($this->user){
  1488.             $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  1489.             $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  1490.                 "user" => $this->user->getId(),
  1491.                 "default" => UserCheckoutInfoEnum::YES,
  1492.                 "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED
  1493.             ]);
  1494.             if($userCheckoutInfo){
  1495.                 $this->data['showInfoStep'] = false;
  1496.             }
  1497.             $userCards $this->em->getRepository(UserCard::class)->getValidUserCard();
  1498.         }
  1499.         $pagarMeTransaction $this->generalService->getService('PagarMe\\PagarMeTransaction');
  1500.         $numberUtil $this->generalService->getUtil('NumberUtil');
  1501.         $useCard $productOfferRepository->produtOfferAllowCard($productOffer);
  1502.         $useBill $productOfferRepository->produtOfferAllowBill($productOffer);
  1503.         $usePix $productOfferRepository->produtOfferAllowPix($productOffer);
  1504.         $internalOptions json_decode($productOffer->getSaleOptionInternal());
  1505.         
  1506.         $this->data['use_paypal'] = CartEnum::NO;
  1507.         $this->data['use_pagseguro'] = CartEnum::NO;
  1508.         $this->data['use_mercadopago'] = CartEnum::NO;
  1509.         if(is_array($internalOptions)){
  1510.             $this->data['use_paypal'] = (
  1511.                 in_array(ProductOfferEnum::INTERNAL_PAYPAL$internalOptions) ? 
  1512.                 CartEnum::YES 
  1513.                 CartEnum::NO
  1514.             );
  1515.             $this->data['use_pagseguro'] = (
  1516.                 in_array(ProductOfferEnum::INTERNAL_PAGSEGURO$internalOptions) ? 
  1517.                 CartEnum::YES 
  1518.                 CartEnum::NO
  1519.             );
  1520.             $this->data['use_mercadopago'] = (
  1521.                 in_array(ProductOfferEnum::INTERNAL_MERCADOPAGO$internalOptions) ? 
  1522.                 CartEnum::YES 
  1523.                 CartEnum::NO
  1524.             );
  1525.         }
  1526.         $hasTrue = [
  1527.             $useCard == CartEnum::YES && $useBill == CartEnum::NO && $usePix == CartEnum::NO,
  1528.             $useCard == CartEnum::NO && $useBill == CartEnum::YES && $usePix == CartEnum::NO,
  1529.             $useCard == CartEnum::NO && $useBill == CartEnum::NO && $usePix == CartEnum::YES,
  1530.         ];
  1531.         $paymentMethod = [
  1532.             CartEnum::PAYMENT_CARD,
  1533.             CartEnum::PAYMENT_BILL,
  1534.             CartEnum::PAYMENT_PIX,
  1535.         ];
  1536.         $this->data['use_card'] = $useCard;
  1537.         $this->data['use_bill'] = $useBill;
  1538.         $this->data['use_pix'] = $usePix;
  1539.         $key array_search(true$hasTrue);
  1540.         $this->data['isOneMethod'] = (!empty($key) ? CartEnum::YES CartEnum::NO);
  1541.         $this->data['isOneMethodType'] = (!empty($key) ? $paymentMethod[$key] : null);
  1542.         $freeInstallments $productOfferRepository->getFreeInstallment($productOffer);
  1543.         $maxInstallments $productOfferRepository->getInstallmentNumberMax($productOffer);
  1544.         $parcelInfo $numberUtil->getNumberMaxParcel(
  1545.             $cartSubtotal 100,
  1546.             $maxInstallments
  1547.             $freeInstallments
  1548.             ProductOfferEnum::EAD,
  1549.             $productOffer->getTypeCheckout()
  1550.         );
  1551.         $installmentsOptions $pagarMeTransaction->calculateInstallments([
  1552.             'amount' => $numberUtil->numberToCents($cartSubtotal),
  1553.             'free_installments' => $freeInstallments,
  1554.             'max_installments' => $parcelInfo->maxInstallments,
  1555.             'interest_rate' => $paymentConfig->installmentInterest
  1556.         ]);
  1557.         $this->data['installmentsOptions'] = (array)$installmentsOptions;
  1558.         $receiverSchoolEad $this->configuration->getReceiverSchool(
  1559.             ReceiverEnum::EAD_CHECKOUT
  1560.         );
  1561.         $receiverSchoolPayPal $this->configuration->getReceiverSchool(
  1562.             ReceiverEnum::PAYPAL
  1563.         );
  1564.         $receiverSchoolPagSeguro $this->configuration->getReceiverSchool(
  1565.             ReceiverEnum::PAGSEGURO
  1566.         );
  1567.         $receiverSchoolMercadoPago $this->configuration->getReceiverSchool(
  1568.             ReceiverEnum::MERCADO_PAGO
  1569.         );
  1570.         $this->data['receiverSchoolEad'] = $receiverSchoolEad;
  1571.         $this->data['receiverSchoolPayPal'] = $receiverSchoolPayPal;
  1572.         $this->data['receiverSchoolPagSeguro'] = $receiverSchoolPagSeguro;
  1573.         $this->data['receiverSchoolMercadoPago'] = $receiverSchoolMercadoPago;
  1574.         $this->data['cartSubtotal'] = CartEnum::NO;//$cartSubtotal;
  1575.         $this->data['cartDisplaySubtotal'] = CartEnum::NO;//$displayTotal;
  1576.         $this->data['cartSubtotalDiff'] = CartEnum::NO;//$displayTotal - $cartSubtotal;
  1577.         $this->data['userCards'] = $userCards;
  1578.         $this->data['hasOnlyCourseInCart'] = $hasOnlyCourseInCart;
  1579.         $cart = new Cart();
  1580.         $hashIdentify $this->generalService->getCookieHashIdentify();
  1581.         
  1582.         $cart->setProductOffer($productOffer);
  1583.         $cart->setProduct($product);
  1584.         if($productCoupon && $couponKeyValid){
  1585.             $cart->setProductCoupon($productCoupon);
  1586.         }
  1587.         if($this->user){
  1588.             $cart->setUser($this->user);
  1589.             $cart->setHashIdentify($this->user->getHashIdentify());
  1590.         }else if(!empty($hashIdentify)){
  1591.             $cart->setHashIdentify($hashIdentify);
  1592.         }
  1593.         $cart->setPrice($productOffer->getPriceRealCopy());
  1594.         if(
  1595.             $product->getType() == ProductEnum::SUBSCRIPTION || 
  1596.             $productOffer->getAllowRecurrency() == ProductEnum::YES
  1597.         ){
  1598.             $cart->setMembershipFee($productOffer->getMembershipFeeCopy());
  1599.         }
  1600.         $cart->setProductType($product->getType());
  1601.         $productOfferCouponTotal $productCouponRepository->countPublicCouponByProductOffer(
  1602.             $productOffer
  1603.         );
  1604.         $cart->offerPaymentOptions null;
  1605.         $cart->productOfferCouponTotal $productOfferCouponTotal;
  1606.         $this->data['carts'] = [
  1607.             $cart
  1608.         ];
  1609.         $this->data['keyCaptcha'] = $this->createCaptchaKey($request);
  1610.         $this->data['customCart'] = $customCart;
  1611.         $this->data['isCart'] = false;
  1612.         $this->data['urlPost'] = $this->generalService->generateUrl(
  1613.             "cartCheckoutPlanTrial"
  1614.             [
  1615.                 "poID" => $productOffer->getId(),
  1616.                 "pcID" => ($productCoupon $productCoupon->getId() : null),
  1617.             ]
  1618.         );
  1619.         $this->data['urlRedirect'] = $this->generalService->generateUrl(
  1620.             "cartIndividual"
  1621.             [
  1622.                 "poHash" => $offerHash,
  1623.                 "pcKey" => ($couponKey $couponKey null),
  1624.             ]
  1625.         ) . "#cart-step-info";
  1626.         $this->data['urlConclusion'] = $this->generalService->generateUrl("cartConclusion");
  1627.         $this->em->flush();
  1628.         if(empty($this->data['carts'])){
  1629.             $this->data['carts'] = [];
  1630.         }
  1631.         return $this->renderEAD('cart/cart.html.twig');
  1632.     }
  1633.     /**
  1634.      * @Route(
  1635.      *      path    = "/admin/cart/user/valid",
  1636.      *      name    = "getUserValidCarts",
  1637.      *      methods = {"GET"}
  1638.      * )
  1639.      */
  1640.     public function getUserValidCarts(Request $request){
  1641.         if (!$this->user) {
  1642.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  1643.         }
  1644.         $cartRepository $this->em->getRepository(Cart::class);
  1645.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  1646.         $userId $this->user->getId();
  1647.         $carts $cartRepository->getUserValidCarts($userId);
  1648.         $paymentConfig $this->configuration->getPaymentConfig();
  1649.         $fileService $this->generalService->getService('FileService');
  1650.         $data = [];
  1651.         foreach ($carts as $key => $cart) {
  1652.             $productOffer $cart->getProductOffer();
  1653.             $billDateExpire $productOfferRepository->getDateExpireBill($productOffer);
  1654.             $pixDateExpire $productOfferRepository->getDateExpirePix($productOffer);
  1655.             $photo $fileService->getFilePathComplete(
  1656.                 $cart->getProductOffer()->getProductPage()->getPhoto(),
  1657.                 ProductEnum::PATH_PRODUCT_PHOTO,
  1658.                 true,
  1659.                 false,
  1660.                 'product-photo',
  1661.                 true
  1662.             );
  1663.             $onErrorPhoto $fileService->getFilePathComplete(
  1664.                 $cart->getProductOffer()->getProductPage()->getPhoto(),
  1665.                 ProductEnum::PATH_PRODUCT_PHOTO,
  1666.                 true,
  1667.                 false,
  1668.                 'product-photo',
  1669.                 false
  1670.             );
  1671.             $price = (float)($cart->getPrice() + $cart->getMembershipFee());
  1672.             $membershipFee = (float)$productOffer->getMembershipFee();
  1673.             $priceReal = (float)($productOffer->getPriceReal() + $membershipFee);
  1674.             $priceDiscount = (float)$price $priceReal;
  1675.             $priceDisplay = (float)$productOffer->getPriceDisplay();
  1676.             
  1677.             if(!empty($priceDisplay)){
  1678.                 $priceDisplay = (float)($priceDisplay $membershipFee);
  1679.                 $priceDiscount $price $priceDisplay;
  1680.             }
  1681.             $recurrency = (
  1682.                 $cart->getProduct()->getType() == ProductEnum::SUBSCRIPTION ||
  1683.                 $productOffer->getAllowRecurrency() == ProductEnum::YES
  1684.             )? true false;
  1685.             $item = (object)[
  1686.                 "id" => $cart->getId(),
  1687.                 "title" => $cart->getProduct()->getTitle(),
  1688.                 "photo" => $photo,
  1689.                 "onErrorPhoto" => $onErrorPhoto,
  1690.                 "able" => ($cart->getAble() == CartEnum::YES),
  1691.                 "priceOrigin" => (float)$cart->getPrice(),
  1692.                 "membershipFeeOrigin" => (float)$productOffer->getMembershipFee(),
  1693.                 "membershipFeeReal" => (float)$cart->getMembershipFee(),
  1694.                 "priceReal" => $priceReal,
  1695.                 "price" => (float)$price,
  1696.                 "priceReal" => $priceReal,
  1697.                 "priceDisplay" => (float)$priceDisplay,
  1698.                 "priceDiscount" => (float)$priceDiscount,
  1699.                 "couponKey" => (
  1700.                     $cart->getProductCoupon() ? $cart->getProductCoupon()->getKey() : null
  1701.                 ),
  1702.                 "couponMembershipFee" => (int)(
  1703.                     $cart->getProductCoupon() ?
  1704.                     $cart->getProductCoupon()->getApplyMembershipFee() : 
  1705.                     ProductEnum::NO
  1706.                 ),
  1707.                 "billDateExpire" => $billDateExpire,
  1708.                 "pixDateExpire" => $pixDateExpire,
  1709.                 "interestFee" => $paymentConfig->installmentInterest,
  1710.                 "currencySymbol" => $productOffer->getCurrencySymbol(),
  1711.                 "paymentCycle" => $productOffer->getPlanCycle(),
  1712.                 "recurrency" => $recurrency,
  1713.             ];
  1714.             $data[] = $item;
  1715.         }
  1716.         return $this->eadResponse($data);
  1717.     }
  1718.     /**
  1719.      * @Route(
  1720.      *      path          = "/admin/cart/change/status",
  1721.      *      name          = "cartChangeStatus",
  1722.      *      methods       = {"PUT"}
  1723.      * )
  1724.      */
  1725.     public function updateCartStatus(Request $request){
  1726.         if (!$this->user) {
  1727.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  1728.         }
  1729.         $this->requestUtil->setRequest($request)->setData();
  1730.         $cartId $this->requestUtil->getField('cart');
  1731.         $able $this->requestUtil->getField('able');
  1732.         $cartRepository $this->em->getRepository(Cart::class);
  1733.         $userId $this->user->getId();
  1734.         $cart $cartRepository->findOneBy([
  1735.             "id" => $cartId,
  1736.             "user" => $userId,
  1737.             "deleted" => CartEnum::ITEM_NO_DELETED,
  1738.         ]);
  1739.         if(!$cart){
  1740.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  1741.         }
  1742.         $cart->setAble($able);
  1743.         $this->em->flush();
  1744.         return $this->eadResponse([ "success" => ]);
  1745.     }
  1746.     /**
  1747.      * @Route(
  1748.      *      path          = "/cart/checkout/paypal",
  1749.      *      name          = "cartCheckoutPaypal",
  1750.      *      methods       = {"GET"}
  1751.      * )
  1752.      */
  1753.     public function checkoutPayPal(Request $request){
  1754.         $this->checkUserSession($request);
  1755.         $receiverSchool $this->configuration->getReceiverSchool(ReceiverEnum::PAYPAL);
  1756.         if(!$receiverSchool){
  1757.             return $this->redirectToRoute('notFound');
  1758.         }
  1759.         $this->requestUtil->setRequest($request)->setData();
  1760.         $cartRepository $this->em->getRepository(Cart::class);
  1761.         $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($this->user->getId());
  1762.         if(!$hasOnlyCourseInCart){
  1763.             return $this->redirectToRoute('notFound');
  1764.         }
  1765.         $currencyCode $this->configuration->get('currency_code');
  1766.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  1767.         $transactionData = [
  1768.             "cmd" => "_cart",
  1769.             "business" => $receiverSchool->getEmail(),
  1770.             "upload" => 1,
  1771.             "currency_code" => ( $currencyCode $currencyCode "BRL" ),
  1772.             "charset" => "UTF-8",
  1773.         ];
  1774.         $dataCustom = [];
  1775.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  1776.         if(empty($carts)){
  1777.             return $this->redirectToRoute('notFound');
  1778.         }
  1779.         foreach ($carts as $key => $cart) {
  1780.             $index $key 1;
  1781.             $product $cart->getProduct();
  1782.             $productOffer $cart->getProductOffer();
  1783.             $title $this->stringUtil->shortTextClean($product->getTitle(), 30);
  1784.             $title $this->stringUtil->cleanString2($title);
  1785.             $transactionData["item_number_{$index}"] = $product->getId();
  1786.             $transactionData["item_name_{$index}"] = $title;
  1787.             $transactionData["amount_{$index}"] = $cart->getPrice();
  1788.             $transactionData["quantity_{$index}"] = 1;
  1789.             $dataCustom["cart_{$index}"] = $cart->getId();
  1790.             $dataCustom["amount_{$index}"] = $cart->getPrice();
  1791.             $dataCustom["offer_{$index}"] = $productOffer->getId();
  1792.             
  1793.             $productCoupon $cart->getProductCoupon();
  1794.             if($productCoupon){
  1795.                 $dataCustom["coupon_{$index}"] = $productCoupon->getId();
  1796.             }
  1797.         }
  1798.         $dataCustom["new"] = CartEnum::YES;
  1799.         $dataCustom["userId"] = $this->user->getId();
  1800.         $dataCustom["numberItens"] = count($carts);
  1801.         $dataCustom["userCheckoutInfoId"] = $userCheckoutInfoId;
  1802.         $transactionData["custom"] = json_encode($dataCustom);
  1803.         $transactionData["return"] = "https://{$this->eadDomain}/resume/";
  1804.         $transactionData["notify_url"] = "https://{$this->eadDomain}/paypal/";
  1805.         $transactionData http_build_query($transactionData);
  1806.         $payPal $this->generalService->getService('PayPal\\PayPal');
  1807.         $url "{$payPal->getPayPalAddress()}?{$transactionData}";
  1808.         return $this->redirect($url301);
  1809.     }
  1810.     /**
  1811.      * @Route(
  1812.      *      path          = "/cart/checkout/pagseguro",
  1813.      *      name          = "cartCheckoutPagseguro",
  1814.      *      methods       = {"GET"}
  1815.      * )
  1816.      */
  1817.     public function checkoutPagSeguro(Request $request){
  1818.         $this->checkUserSession($request);
  1819.         $receiverSchool $this->configuration->getReceiverSchool(ReceiverEnum::PAGSEGURO);
  1820.         if(!$receiverSchool){
  1821.             return $this->redirectToRoute('notFound');
  1822.         }
  1823.         $this->requestUtil->setRequest($request)->setData();
  1824.         $cartRepository $this->em->getRepository(Cart::class);
  1825.         $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($this->user->getId());
  1826.         if(!$hasOnlyCourseInCart){
  1827.             return $this->redirectToRoute('notFound');
  1828.         }
  1829.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  1830.         $transactionData = [
  1831.             "cmd" => "_cart",
  1832.             "business" => $receiverSchool->getEmail(),
  1833.             //"upload" => 1,
  1834.             "currency" => "BRL",
  1835.             "charset" => "ISO-8859-1",
  1836.         ];
  1837.         $dataCustom = [];
  1838.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  1839.         if(empty($carts)){
  1840.             return $this->redirectToRoute('notFound');
  1841.         }
  1842.         foreach ($carts as $key => $cart) {
  1843.             $index $key 1;
  1844.             $product $cart->getProduct();
  1845.             $productOffer $cart->getProductOffer();
  1846.             $title $this->stringUtil->shortTextClean($product->getTitle(), 30);
  1847.             $title $this->stringUtil->cleanString2($title);
  1848.             $transactionData["itemId{$index}"] = $productOffer->getId();
  1849.             $transactionData["itemDescription{$index}"] = $title;
  1850.             $transactionData["itemAmount{$index}"] = $cart->getPrice();
  1851.             $transactionData["itemQuantity{$index}"] = 1;
  1852.             $dataCustom["cart_{$index}"] = $cart->getId();
  1853.             $dataCustom["amount_{$index}"] = $cart->getPrice();
  1854.             $dataCustom["offer_{$index}"] = $productOffer->getId();
  1855.             
  1856.             $productCoupon $cart->getProductCoupon();
  1857.             if($productCoupon){
  1858.                 $dataCustom["coupon_{$index}"] = $productCoupon->getId();
  1859.             }
  1860.         }
  1861.         $dataCustom["userId"] = $this->user->getId();
  1862.         $dataCustom["numberItens"] = count($carts);
  1863.         $dataCustom["userCheckoutInfoId"] = $userCheckoutInfoId;
  1864.         $transactionData["reference"] = $this->user->getId();
  1865.         $transactionData["custom"] = json_encode($dataCustom);
  1866.         $transactionData["redirectURL"] = "https://{$this->eadDomain}/resume/";
  1867.         $transactionData["notificationURL"] = "https://{$this->eadDomain}/pagseguro/";
  1868.         $pagSeguro $this->generalService->getService('PagSeguro\\PagSeguro');
  1869.         $url $pagSeguro->getCheckoutPagseguro($transactionData);
  1870.         return $this->redirect($url301);
  1871.     }
  1872.     /**
  1873.      * @Route(
  1874.      *      path          = "/cart/checkout/mercadopago",
  1875.      *      name          = "cartCheckoutMercadopago",
  1876.      *      methods       = {"GET"}
  1877.      * )
  1878.      */
  1879.     public function checkoutMercadopago(Request $request) {
  1880.         $this->checkUserSession($request);
  1881.         $receiverSchool $this->configuration->getReceiverSchool(
  1882.             ReceiverEnum::MERCADO_PAGO
  1883.         );
  1884.         if(!$receiverSchool){
  1885.             return $this->redirectToRoute('notFound');
  1886.         }
  1887.         $this->requestUtil->setRequest($request)->setData();
  1888.         $cartRepository $this->em->getRepository(Cart::class);
  1889.         $hasOnlyCourseInCart $cartRepository->hasOnlyCourseInCart($this->user->getId());
  1890.         if(!$hasOnlyCourseInCart){
  1891.             return $this->redirectToRoute('notFound');
  1892.         }
  1893.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  1894.         if(empty($carts)){
  1895.             return $this->redirectToRoute('notFound');
  1896.         }
  1897.         foreach ($carts as $key => $cart) {
  1898.             $product $cart->getProduct();
  1899.             $productOffer $cart->getProductOffer();
  1900.             $title $this->stringUtil->shortTextClean($product->getTitle(), 30);
  1901.             $title $this->stringUtil->cleanString2($title);
  1902.             $description $this->stringUtil->shortTextClean($product->getDescription(), 30);
  1903.             $description $this->stringUtil->cleanString2($description);
  1904.             $items[] = [
  1905.                 "id" => $productOffer->getId(),
  1906.                 "title" => $title,
  1907.                 "description" => $description,
  1908.                 "quantity" => 1,
  1909.                 "unit_price" => $cart->getPrice()
  1910.             ];
  1911.         }
  1912.         $transactionData["items"] = $items;
  1913.         $transactionData["back_urls"] = [
  1914.             "success" => "https://{$this->eadDomain}/resume/",
  1915.             "failure" => "https://{$this->eadDomain}/cart#mercadopago/"
  1916.             "pending" => "https://{$this->eadDomain}/cart#mercadopago/"
  1917.         ];
  1918.         $transactionData["external_reference"] = $this->user->getId();
  1919.         $transactionData["notification_url"] = "https://{$this->eadDomain}/mercadopago?debug=1";
  1920.         $data json_encode($transactionData);
  1921.         $mercadoPago $this->generalService->getService('MercadoPago\\MercadoPago');
  1922.         $url $mercadoPago->getCheckoutMercadoPago($data);
  1923.         return $this->redirect($url301);
  1924.     }
  1925.     /**
  1926.      * @Route(
  1927.      *      path          = "/admin/cart/checkout/charge",
  1928.      *      name          = "cartCheckoutCharge",
  1929.      *      methods       = {"POST"}
  1930.      * )
  1931.      */
  1932.     public function checkoutCharge(Request $request){
  1933.         $receiverSchool $this->configuration->getReceiverSchool(
  1934.             ReceiverEnum::EAD_CHECKOUT
  1935.         );
  1936.         if(!$receiverSchool){
  1937.             return $this->eadResponse(
  1938.                 [ "message" => "Receiver not found "],
  1939.                 ErrorEnum::ACTION_INVALID
  1940.             );
  1941.         }
  1942.         $transactionService $this->generalService->getService(
  1943.             'Transaction\\TransactionService'
  1944.         );
  1945.         $pagarMeTransaction $this->generalService->getService(
  1946.             'PagarMe\\PagarMeTransaction'
  1947.         );
  1948.         $this->requestUtil->setRequest($request)->setData();
  1949.         $productChargeId = (int)$this->requestUtil->getField('productChargeId');
  1950.         $userCheckoutInfoId = (int)$this->requestUtil->getField('userCheckoutInfoId');
  1951.         $paymentMethod = (int)$this->requestUtil->getField('paymentMethod');
  1952.         $installments = (int)$this->requestUtil->getField('installments');
  1953.         $debug = (int)$request->get('debug');
  1954.         $productCharge $this->em->getRepository(ProductCharge::class)->findOneBy([
  1955.             "id" => $productChargeId,
  1956.             "deleted" => ProductChargeEnum::ITEM_NO_DELETED
  1957.         ]);
  1958.         
  1959.         if (!$productCharge) {
  1960.             return $this->eadResponse(
  1961.                 [ "message" => "Charge not found "], 
  1962.                 ErrorEnum::ACTION_INVALID
  1963.             );
  1964.         }
  1965.         if ($productCharge->getPublish() == ProductChargeEnum::NO) {
  1966.             return $this->eadResponse(
  1967.                 [ "message" => "Charge is not publish "], 
  1968.                 ErrorEnum::ACTION_INVALID
  1969.             );
  1970.         }
  1971.         $userCardRepository $this->em->getRepository(UserCard::class);
  1972.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  1973.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  1974.             "id" => $userCheckoutInfoId,
  1975.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  1976.         ]);
  1977.         if(!$userCheckoutInfo){
  1978.             return $this->eadResponse([ 
  1979.                 "message" => "UserCheckoutInfo is empty"
  1980.             ], ErrorEnum::ACTION_INVALID);
  1981.         }
  1982.         $paymentConfig $this->configuration->getPaymentConfig();
  1983.         $product $productCharge->getProduct();
  1984.         $productOffer $productCharge->getProductOffer();
  1985.         $items = [
  1986.             (object)[
  1987.                 "courseId" => null,
  1988.                 "productOfferId" => $productOffer->getId(),
  1989.                 "productId" => $product->getId(),
  1990.                 "amount" => $productCharge->getAmount(),
  1991.                 "cartId" => null,
  1992.                 "productCouponId" => null,
  1993.             ]
  1994.         ];
  1995.         $userCard null;
  1996.         $amount $productCharge->getAmount(true);
  1997.         $dateExpire $productCharge->getDateExpire();
  1998.         if(empty($paymentMethod)){
  1999.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  2000.         }
  2001.         if($paymentMethod == CartEnum::PAYMENT_CARD){ // get user card
  2002.             if(empty($installments)){
  2003.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  2004.             }
  2005.             $userCardId $this->requestUtil->getField('userCardId');
  2006.             if(empty($userCardId)){
  2007.                 return $this->eadResponse([ 'userCardId' ], ErrorEnum::FIELD_EMPTY);
  2008.             }
  2009.             if($userCardId 0){
  2010.                 $userCard $userCardRepository->findOneBy([
  2011.                     "id" => $userCardId,
  2012.                     "user" => $this->user->getId(),
  2013.                     "deleted" => CartEnum::ITEM_NO_DELETED
  2014.                 ]);
  2015.             }
  2016.         }
  2017.         if($paymentMethod == CartEnum::PAYMENT_CARD && !$userCard){
  2018.             return $this->eadResponse(
  2019.                 [ "message" => "Card not found "], 
  2020.                 ErrorEnum::ACTION_INVALID
  2021.             );
  2022.         }
  2023.         $freeInstallments $productCharge->getInstallmentNumberInterest();
  2024.         if($paymentMethod == CartEnum::PAYMENT_CARD){
  2025.             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2026.                 'amount' => $amount,
  2027.                 'free_installments' => $freeInstallments,
  2028.                 'max_installments' => $installments,
  2029.                 'interest_rate' => $paymentConfig->installmentInterest
  2030.             ]);
  2031.             $amount $installmentsOptions->{$installments}->amount;
  2032.         }
  2033.         if($amount $this->configuration->getMinValueProduct()){
  2034.             return $this->eadResponse([ "message" => "Amount invalid "], ErrorEnum::ACTION_INVALID);
  2035.         }
  2036.         if($this->user && $this->user->getId() == CartEnum::YES){
  2037.             $transactionService->setDebug(true);
  2038.         }
  2039.         if($debug == CartEnum::YES){
  2040.             $transactionService->setDebug(true);
  2041.         }
  2042.         $transactionOrigin TransactionEnum::ORIGIN_CHARGE;
  2043.         $data $transactionService->createTransactionEAD(
  2044.             $productCharge->getUser(),
  2045.             $userCheckoutInfo,
  2046.             $amount,
  2047.             $paymentMethod,
  2048.             $installments,
  2049.             $items,
  2050.             $userCard,
  2051.             $dateExpire,
  2052.             null,
  2053.             $productChargeId,
  2054.             (!empty($freeInstallments) ? (int)$freeInstallments null),
  2055.             $transactionOrigin
  2056.         );
  2057.         if(!empty($data['errorMessage'])){
  2058.             $productCharge->setErrorMessage($data['errorMessage']);
  2059.             $this->em->flush();
  2060.             return $this->eadResponse([ 
  2061.                 "message" => $data['errorMessage']
  2062.             ], ErrorEnum::ACTION_INVALID);
  2063.         }
  2064.         return $this->eadResponse($data);
  2065.     }
  2066.     /**
  2067.      * @Route(
  2068.      *      path          = "/cart/checkout/clean",
  2069.      *      name          = "checkoutClean",
  2070.      *      methods       = {"POST"}
  2071.      * )
  2072.      */
  2073.     public function checkoutClean(Request $request){
  2074.         $pixelService $this->generalService->getService('Marketing\\PixelService');
  2075.         $pixelService->sendConversion('InitiateCheckout');
  2076.         $receiverSchool $this->configuration->getReceiverSchool(
  2077.             ReceiverEnum::EAD_CHECKOUT
  2078.         );
  2079.         if(!$receiverSchool){
  2080.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2081.         }
  2082.         $transactionService $this->generalService->getService(
  2083.             'Transaction\\TransactionService'
  2084.         );
  2085.         $pagarMeTransaction $this->generalService->getService(
  2086.             'PagarMe\\PagarMeTransaction'
  2087.         );
  2088.         $this->requestUtil->setRequest($request)->setData();
  2089.         $data $this->requestUtil->getData();
  2090.         $dataUtml = [
  2091.             "utm_source" => $request->get('utm_source'),
  2092.             "utm_medium" => $request->get('utm_medium'),
  2093.             "utm_campaign" => $request->get('utm_campaign'),
  2094.             "utm_term" => $request->get('utm_term'),
  2095.             "utm_content" => $request->get('utm_content'),
  2096.         ];
  2097.         $utmsUrl json_encode($dataUtml);
  2098.         $cartRepository $this->em->getRepository(Cart::class);
  2099.         $courseRepository $this->em->getRepository(Course::class);
  2100.         $userCardRepository $this->em->getRepository(UserCard::class);
  2101.         $userRepository $this->em->getRepository(User::class);
  2102.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2103.         $poRepository $this->em->getRepository(ProductOffer::class);
  2104.         $pcRepository $this->em->getRepository(ProductCoupon::class);
  2105.         //user
  2106.         $name $this->requestUtil->getField('name');
  2107.         $email $this->requestUtil->getField('email');
  2108.         $document $this->requestUtil->getField('document');
  2109.         $phone $this->requestUtil->getField('phone');
  2110.         $zipCode $this->requestUtil->getField('zipCode');
  2111.         $stateId = (int)$this->requestUtil->getField('state');
  2112.         $cityId = (int)$this->requestUtil->getField('city');
  2113.         $address $this->requestUtil->getField('address');
  2114.         $addressNeighborhood $this->requestUtil->getField('addressNeighborhood');
  2115.         $addressNumber $this->requestUtil->getField('addressNumber');
  2116.         $addressComplement $this->requestUtil->getField('addressComplement');
  2117.         $foreigner = (int)$this->requestUtil->getField('foreigner');
  2118.         //userCheckoutInfo
  2119.         $checkoutName $this->requestUtil->getField('checkout-name');
  2120.         $checkoutEmail $this->requestUtil->getField('checkout-email');
  2121.         $checkoutDocument $this->requestUtil->getField('checkout-document');
  2122.         $checkoutPhone $this->requestUtil->getField('checkout-phone');
  2123.         $checkoutZipCode $this->requestUtil->getField('checkout-zipCode');
  2124.         $checkoutStateId = (int)$this->requestUtil->getField('checkout-state');
  2125.         $checkoutCityId = (int)$this->requestUtil->getField('checkout-city');
  2126.         $checkoutAddress $this->requestUtil->getField('checkout-address');
  2127.         $checkoutAddressNeighborhood $this->requestUtil->getField(
  2128.             'checkout-addressNeighborhood'
  2129.         );
  2130.         $checkoutAddressNumber $this->requestUtil->getField('checkout-addressNumber');
  2131.         $checkoutAddressComplement $this->requestUtil->getField(
  2132.             'checkout-addressComplement'
  2133.         );
  2134.         $checkoutNotify = (int)$this->requestUtil->getField('checkout-notify');
  2135.         $checkoutForeigner = (int)$this->requestUtil->getField('checkout-foreigner');
  2136.         
  2137.         $checkoutNew = (int)$this->requestUtil->getField('checkout-new');
  2138.         $cardNew = (int)$this->requestUtil->getField('card-new');
  2139.         $userNew = (int)$this->requestUtil->getField('user-new');
  2140.         $installments = (int)$this->requestUtil->getField('installments');
  2141.         $paymentMethod = (int)$this->requestUtil->getField('payment-method');
  2142.         $cardId = (int)$this->requestUtil->getField('card-id');
  2143.         $cardHash $this->requestUtil->getField('card-hash');
  2144.         $userId = (int)$this->requestUtil->getField('user-id');
  2145.         $userCheckoutInfoId = (int)$this->requestUtil->getField('checkout-id');
  2146.         $offerDefaultId = (int)$this->requestUtil->getField('offer-default');
  2147.         $offers $this->requestUtil->getField('added-ids');
  2148.         $offers explode(","$offers);
  2149.         if(empty($offers)){
  2150.             return $this->eadResponse([ 'added-ids' ], ErrorEnum::FIELD_EMPTY);
  2151.         }
  2152.         if(empty($paymentMethod)){
  2153.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  2154.         }
  2155.         $couponKey $this->requestUtil->getField('couponKey');
  2156.         $user null;
  2157.         $userCheckoutInfo null;
  2158.         $userCard null;
  2159.         
  2160.         if(!empty($userId)){
  2161.             $user $userRepository->findOneBy([
  2162.                 "id" => $userId,
  2163.             ]);
  2164.             //update user data ???
  2165.             
  2166.         }else if($userNew == UserEnum::YES){
  2167.             $pixelService $this->generalService->getService('Marketing\\PixelService');
  2168.             $pixelService->sendConversion('Lead');
  2169.             //check user exist
  2170.             $user $userRepository->findOneBy([
  2171.                 "email" => $email,
  2172.             ], [ "id" => "DESC" ]);
  2173.             if(!$user || ($user && $user->isDeleted())){
  2174.                 $dataUser = [
  2175.                     "name" => $name,
  2176.                     "email" => $email,
  2177.                     "password" => null,
  2178.                     "phone" => $phone,
  2179.                     "notify" => UserEnum::YES,
  2180.                     "invited" => UserEnum::YES
  2181.                 ];
  2182.                 $userDTO = new UserDTO($dataUser);
  2183.                 $data $userRepository->newUser($userDTOUserEnum::YES);
  2184.                 if($data->errors){
  2185.                     return $this->eadResponse($data->errorsErrorEnum::FIELD_EMPTY);
  2186.                 }
  2187.                 $user $data->user;
  2188.             }
  2189.             $user->setDocument($document);
  2190.             $user->setPhone($phone);
  2191.             $user->setZipCode($zipCode);
  2192.             if($foreigner == UserCheckoutInfoEnum::NO){
  2193.                 $country $this->em->getRepository(Country::class)->findOneBy([ 
  2194.                     "deleted" => GeoDataEnum::ITEM_NO_DELETED ,
  2195.                     "id" => GeoDataEnum::YES,
  2196.                 ]);
  2197.                 if($country){
  2198.                     $user->setCountry($country);
  2199.                 }
  2200.             }
  2201.             if(!empty($stateId)){
  2202.                 $state $this->em->getRepository(State::class)->findOneBy([
  2203.                     "id" => $stateId,
  2204.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2205.                 ]);
  2206.                 if($state){
  2207.                     $user->setState($state);
  2208.                 }
  2209.             }
  2210.             if(!empty($cityId)){
  2211.                 $city $this->em->getRepository(City::class)->findOneBy([
  2212.                     "id" => $cityId,
  2213.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2214.                 ]);
  2215.                 if($city){
  2216.                     $user->setCity($city);
  2217.                 }
  2218.             }
  2219.             $user->setAddress($address);
  2220.             $user->setAddressNeighborhood($addressNeighborhood);
  2221.             $user->setAddressNumber($addressNumber);
  2222.             $user->setAddressComplement($addressComplement);
  2223.             $user->restore();
  2224.         }
  2225.         if(!$user){
  2226.             return $this->eadResponse([ 'user' ], ErrorEnum::FIELD_EMPTY);
  2227.         }
  2228.         if($checkoutNew == CartEnum::NO && empty($userCheckoutInfoId)){
  2229.             $userCheckoutInfo = new UserCheckoutInfo();
  2230.             $userCheckoutInfo->setName($user->getName());
  2231.             $userCheckoutInfo->setEmail($user->getEmail());
  2232.             $userCheckoutInfo->setDocument($user->getDocument());
  2233.             $userCheckoutInfo->setPhone($user->getPhone());
  2234.             $userCheckoutInfo->setZipCode($user->getZipCode());
  2235.             $userCheckoutInfo->setAddress($user->getAddress());
  2236.             $userCheckoutInfo->setAddressNumber($user->getAddressNumber());
  2237.             $userCheckoutInfo->setAddressComplement($user->getAddressComplement());
  2238.             $userCheckoutInfo->setAddressNeighborhood($user->getAddressNeighborhood());
  2239.             $userCheckoutInfo->setCity($user->getCity());
  2240.             $userCheckoutInfo->setState($user->getState());
  2241.             $userCheckoutInfo->setCountry($user->getCountry());
  2242.             $userCheckoutInfo->setUser($user);
  2243.             $userCheckoutInfo->setReceiveEmail(UserCheckoutInfoEnum::NO);
  2244.             $userCheckoutInfo->setDefault(UserCheckoutInfoEnum::YES);
  2245.             $this->em->persist($userCheckoutInfo);
  2246.         }else if($checkoutNew == CartEnum::NO && !empty($userCheckoutInfoId)){
  2247.             $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2248.                 "id" => $userCheckoutInfoId,
  2249.                 "user" => $user->getId(),
  2250.                 "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2251.             ]);
  2252.         }else if($checkoutNew == CartEnum::YES){
  2253.             $userCheckoutInfo = new UserCheckoutInfo();
  2254.             $userCheckoutInfo->setName($checkoutName);
  2255.             $userCheckoutInfo->setEmail($checkoutEmail);
  2256.             $userCheckoutInfo->setDocument($checkoutDocument);
  2257.             $userCheckoutInfo->setPhone($checkoutPhone);
  2258.             $userCheckoutInfo->setZipCode($checkoutZipCode);
  2259.             $userCheckoutInfo->setAddress($checkoutAddress);
  2260.             $userCheckoutInfo->setAddressNumber($checkoutAddressNumber);
  2261.             $userCheckoutInfo->setAddressComplement($checkoutAddressComplement);
  2262.             $userCheckoutInfo->setAddressNeighborhood($checkoutAddressNeighborhood);
  2263.             if($checkoutForeigner == UserCheckoutInfoEnum::NO){
  2264.                 $country $this->em->getRepository(Country::class)->findOneBy([ 
  2265.                     "deleted" => GeoDataEnum::ITEM_NO_DELETED ,
  2266.                     "id" => GeoDataEnum::YES,
  2267.                 ]);
  2268.                 if($country){
  2269.                     $userCheckoutInfo->setCountry($country);
  2270.                 }
  2271.             }
  2272.             if(!empty($checkoutStateId)){
  2273.                 $state $this->em->getRepository(State::class)->findOneBy([
  2274.                     "id" => $checkoutStateId,
  2275.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2276.                 ]);
  2277.                 if($state){
  2278.                     $userCheckoutInfo->setState($state);
  2279.                 }
  2280.             }
  2281.             if(!empty($checkoutCityId)){
  2282.                 $city $this->em->getRepository(City::class)->findOneBy([
  2283.                     "id" => $checkoutCityId,
  2284.                     "deleted" => UserEnum::ITEM_NO_DELETED
  2285.                 ]);
  2286.                 if($city){
  2287.                     $userCheckoutInfo->setCity($city);
  2288.                 }
  2289.             }
  2290.             $userCheckoutInfo->setUser($user);
  2291.             $userCheckoutInfo->setReceiveEmail($checkoutNotify);
  2292.             $userCheckoutInfo->setDefault(UserCheckoutInfoEnum::YES);
  2293.             $this->em->persist($userCheckoutInfo);
  2294.         }
  2295.         if(!$userCheckoutInfo){
  2296.             return $this->eadResponse([ 'userCheckoutInfo' ], ErrorEnum::FIELD_EMPTY);
  2297.         }
  2298.         if($paymentMethod == CartEnum::PAYMENT_CARD){
  2299.             if($cardNew == CartEnum::NO && !empty($cardId)){
  2300.                 $userCard $userCardRepository->findOneBy([
  2301.                     "id" => $cardId,
  2302.                     "user" => $user->getId(),
  2303.                     "deleted" => CartEnum::ITEM_NO_DELETED
  2304.                 ]);
  2305.             }else if($cardNew == CartEnum::YES){
  2306.                 if(empty($cardHash)){
  2307.                     return $this->eadResponse([ 'cardHash' ], ErrorEnum::FIELD_EMPTY);
  2308.                 }
  2309.                 $userCard $userCardRepository->createByHash($cardHash$user);
  2310.             }
  2311.             if(!$userCard){
  2312.                 return $this->eadResponse([ 'userCard' ], ErrorEnum::FIELD_EMPTY);
  2313.             }
  2314.             if(empty($installments)){
  2315.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  2316.             }
  2317.         }
  2318.         $productOfferDefault null;
  2319.         $amount null;
  2320.         $items = [];
  2321.         $courses null;
  2322.         $coursesAux = [];
  2323.         foreach ($offers as $key => $offerId) {
  2324.             $productOffer $poRepository->findOneBy([
  2325.                 "id" => $offerId,
  2326.             ]);
  2327.             if($productOffer){
  2328.                 $product $productOffer->getProduct();
  2329.                 if($offerId == $offerDefaultId){
  2330.                     $productOfferDefault $productOffer;
  2331.                     if(
  2332.                         $product->getType() == ProductEnum::COURSE || 
  2333.                         $product->getType() == ProductEnum::COMBO
  2334.                     ){
  2335.                         $courses $courseRepository->getCoursesByProduct(
  2336.                             $product->getId()
  2337.                         );
  2338.                         foreach ($courses as $key => $course) {
  2339.                             $coursesAux[] = $course->getId();
  2340.                         }
  2341.                     }
  2342.                 }
  2343.                 if(!empty($couponKey)){
  2344.                     $productCoupon $pcRepository->findValidProductCouponByProductOffer(
  2345.                         $couponKey
  2346.                         $productOffer
  2347.                     );
  2348.                 }
  2349.                 $cart $cartRepository->addCartByProductOffer(
  2350.                     $productOffer,
  2351.                     $user,
  2352.                     null,
  2353.                     $productCoupon,
  2354.                     $utmsUrl,
  2355.                     ($productOffer->getProduct()->isTypeResource() ? $courses null)
  2356.                 );
  2357.                 if($cart){
  2358.                     $items[] = (object)[
  2359.                         "courses" => $coursesAux,
  2360.                         "productOfferId" => $productOffer->getId(),
  2361.                         "productId" => $product->getId(),
  2362.                         "amount" => $cart->getPrice() + $cart->getMembershipFee(),
  2363.                         "cartId" => $cart->getId(),
  2364.                         "productCouponId" => (
  2365.                             $productCoupon $productCoupon->getId() : null
  2366.                         ),
  2367.                     ];
  2368.                     $amount $amount $cart->getPrice(true) + $cart->getMembershipFee(true);
  2369.                 }
  2370.             }
  2371.         }
  2372.         if(count($items) != count($offers)){
  2373.             return $this->eadResponse([ "added-ids" ], ErrorEnum::FIELD_EMPTY);
  2374.         }
  2375.         if(!$productOfferDefault){
  2376.             return $this->eadResponse([ "offer-default" ], ErrorEnum::FIELD_EMPTY);
  2377.         }
  2378.         $this->em->flush();
  2379.         $paymentConfig $this->configuration->getPaymentConfig();
  2380.         $dateExpire null;
  2381.         if($paymentMethod == CartEnum::PAYMENT_CARD){
  2382.             $freeInstallments $poRepository->getFreeInstallment($productOfferDefault);
  2383.             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2384.                 'amount' => $amount,
  2385.                 'free_installments' => $freeInstallments,
  2386.                 'max_installments' => $installments,
  2387.                 'interest_rate' => $paymentConfig->installmentInterest
  2388.             ]);
  2389.             $amount $installmentsOptions->{$installments}->amount;
  2390.         }else if($paymentMethod == CartEnum::PAYMENT_BILL){
  2391.             $dateExpire $poRepository->getDateExpireBill($productOffer);
  2392.         }else if($paymentMethod == CartEnum::PAYMENT_PIX){
  2393.             $dateExpire $poRepository->getDateExpirePix($productOffer);
  2394.         }
  2395.         if($amount $this->configuration->getMinValueProduct()){
  2396.             return $this->eadResponse([ 
  2397.                 "message" => "Min Value" 
  2398.             ], ErrorEnum::ACTION_INVALID);
  2399.         }
  2400.         if($user && $user->getId() == CartEnum::YES){
  2401.             $transactionService->setDebug(true);
  2402.         }
  2403.         $transactionOrigin TransactionEnum::ORIGIN_CHECKOUT_PAGE;
  2404.         $data $transactionService->createTransactionEAD(
  2405.             $user,
  2406.             $userCheckoutInfo,
  2407.             $amount,
  2408.             $paymentMethod,
  2409.             $installments,
  2410.             $items,
  2411.             $userCard,
  2412.             $dateExpire,
  2413.             null,
  2414.             null,
  2415.             null,
  2416.             $transactionOrigin
  2417.         );
  2418.         if(!empty($data['errorMessage'])){
  2419.             foreach ($offers as $key => $offerId) {
  2420.                 $productOffer $poRepository->findOneBy([
  2421.                     "id" => $offerId,
  2422.                 ]);
  2423.                 if($productOffer){
  2424.                     if($cart){
  2425.                         $cart->setErrorMessage($data['errorMessage']);
  2426.                     }
  2427.                 }
  2428.             }
  2429.             $this->em->flush();
  2430.             return $this->eadResponse([ 
  2431.                 "message" => $data['errorMessage']
  2432.             ], ErrorEnum::ACTION_INVALID);
  2433.         }
  2434.         
  2435.         return $this->eadResponse($data);
  2436.     }
  2437.     /**
  2438.      * @Route(
  2439.      *      path          = "/admin/cart/checkout/default",
  2440.      *      name          = "cartCheckoutDefault",
  2441.      *      methods       = {"POST"}
  2442.      * )
  2443.      */
  2444.     public function checkoutDefault(Request $request){
  2445.         if(!$this->user){
  2446.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2447.         }
  2448.         
  2449.         $receiverSchool $this->configuration->getReceiverSchool(
  2450.             ReceiverEnum::EAD_CHECKOUT
  2451.         );
  2452.         if(!$receiverSchool){
  2453.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2454.         }
  2455.         $transactionService $this->generalService->getService(
  2456.             'Transaction\\TransactionService'
  2457.         );
  2458.         $pagarMeTransaction $this->generalService->getService(
  2459.             'PagarMe\\PagarMeTransaction'
  2460.         );
  2461.         $this->requestUtil->setRequest($request)->setData();
  2462.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  2463.         $paymentMethod $this->requestUtil->getField('paymentMethod');
  2464.         $installments $this->requestUtil->getField('installments');
  2465.         $cartRepository $this->em->getRepository(Cart::class);
  2466.         $productRepository $this->em->getRepository(Product::class);
  2467.         $userCardRepository $this->em->getRepository(UserCard::class);
  2468.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  2469.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2470.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2471.             "id" => $userCheckoutInfoId,
  2472.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2473.         ]);
  2474.         if(!$userCheckoutInfo){
  2475.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2476.         }
  2477.         $paymentConfig $this->configuration->getPaymentConfig();
  2478.         $items = [];
  2479.         $amount 0;
  2480.         $userCard null;
  2481.         $dateExpire null;
  2482.         if(empty($paymentMethod)){
  2483.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  2484.         }
  2485.         if($paymentMethod == CartEnum::PAYMENT_CARD){ // get user card
  2486.             if(empty($installments)){
  2487.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  2488.             }
  2489.             $userCardId $this->requestUtil->getField('userCardId');
  2490.             if(empty($userCardId)){
  2491.                 return $this->eadResponse([ 'userCardId' ], ErrorEnum::FIELD_EMPTY);
  2492.             }
  2493.             if($userCardId 0){
  2494.                 $userCard $userCardRepository->findOneBy([
  2495.                     "id" => $userCardId,
  2496.                     "user" => $this->user->getId(),
  2497.                     "deleted" => CartEnum::ITEM_NO_DELETED
  2498.                 ]);
  2499.             }
  2500.         }
  2501.         if($paymentMethod == CartEnum::PAYMENT_CARD && !$userCard){
  2502.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2503.         }
  2504.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  2505.         if(empty($carts)){
  2506.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2507.         }
  2508.         $freeInstallments $paymentConfig->installmentNumberInterest;
  2509.         if($paymentConfig->assumeInstallmentInterest == CartEnum::YES){
  2510.             $freeInstallments 12;
  2511.         }
  2512.         $billDateExpire null;
  2513.         $pixDateExpire null;
  2514.         $hasOneItem = (count($carts) == CartEnum::YES);
  2515.         
  2516.         foreach ($carts as $key => $cart) {
  2517.             $productOffer $cart->getProductOffer();
  2518.             $product $cart->getProduct();
  2519.             if($hasOneItem){
  2520.                 $billDateExpire $productOfferRepository->getDateExpireBill($productOffer);
  2521.                 $pixDateExpire $productOfferRepository->getDateExpirePix($productOffer);
  2522.                 $freeInstallments $productOfferRepository->getFreeInstallment(
  2523.                     $productOffer
  2524.                 );
  2525.             }
  2526.             $productCoupon $cart->getProductCoupon();
  2527.             $items[] = (object)[
  2528.                 "courses" => $cart->getCourse(),
  2529.                 "productOfferId" => $productOffer->getId(),
  2530.                 "productId" => $product->getId(),
  2531.                 "amount" => $cart->getPrice() + $cart->getMembershipFee(),
  2532.                 "cartId" => $cart->getId(),
  2533.                 "productCouponId" => ($productCoupon $productCoupon->getId() : null),
  2534.             ];
  2535.             $amount $amount $cart->getPrice(true) + $cart->getMembershipFee(true);
  2536.         }
  2537.         if($paymentMethod == CartEnum::PAYMENT_BILL){
  2538.             $dateExpire $billDateExpire;
  2539.             if(empty($dateExpire)){
  2540.                 $period = (int)$this->configuration->get('bill_period_expire') + 1;
  2541.                 if($period == 1){
  2542.                     $period 8;
  2543.                 }
  2544.                 $dateExpire date("Y-m-d"strtotime("+ {$period} day"));
  2545.             }
  2546.         }else if($paymentMethod == CartEnum::PAYMENT_PIX){
  2547.             $dateExpire $pixDateExpire;
  2548.             if(empty($dateExpire)){
  2549.                 $period = (int)$this->configuration->get('pix_period_expire') + 1;
  2550.                 if($period == 1){
  2551.                     $period 8;
  2552.                 }
  2553.                 $dateExpire date("Y-m-d"strtotime("+ {$period} day"));
  2554.             }
  2555.         }else if($paymentMethod == CartEnum::PAYMENT_CARD){
  2556.             $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2557.                 'amount' => $amount,
  2558.                 'free_installments' => $freeInstallments,
  2559.                 'max_installments' => $installments,
  2560.                 'interest_rate' => $paymentConfig->installmentInterest
  2561.             ]);
  2562.             $amount $installmentsOptions->{$installments}->amount;
  2563.         }
  2564.         if($amount $this->configuration->getMinValueProduct()){
  2565.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2566.         }
  2567.         if($this->user && $this->user->getId() == CartEnum::YES){
  2568.             $transactionService->setDebug(true);
  2569.         }
  2570.         $transactionOrigin TransactionEnum::ORIGIN_CHECKOUT_DEFAULT;
  2571.         $data $transactionService->createTransactionEAD(
  2572.             $this->user,
  2573.             $userCheckoutInfo,
  2574.             $amount,
  2575.             $paymentMethod,
  2576.             $installments,
  2577.             $items,
  2578.             $userCard,
  2579.             $dateExpire,
  2580.             null,
  2581.             null,
  2582.             (!empty($freeInstallments) ? (int)$freeInstallments null),
  2583.             $transactionOrigin
  2584.         );
  2585.         if(!empty($data['errorMessage'])){
  2586.             foreach ($carts as $key => $cart) {
  2587.                 $cart->setErrorMessage($data['errorMessage']);
  2588.             }
  2589.             $this->em->flush();
  2590.             return $this->eadResponse([ 
  2591.                 "message" => $data['errorMessage']
  2592.             ], ErrorEnum::ACTION_INVALID);
  2593.         }
  2594.         return $this->eadResponse($data);
  2595.     }
  2596.     /**
  2597.      * @Route(
  2598.      *      path          = "/admin/cart/checkout/custom",
  2599.      *      name          = "cartCheckoutCustom",
  2600.      *      methods       = {"POST"}
  2601.      * )
  2602.      */
  2603.     public function cartCheckoutCustom(Request $request){
  2604.         if(!$this->user){
  2605.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2606.         }
  2607.         $receiverSchool $this->configuration->getReceiverSchool(
  2608.             ReceiverEnum::EAD_CHECKOUT
  2609.         );
  2610.         if(!$receiverSchool){
  2611.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2612.         }
  2613.         $transactionService $this->generalService->getService(
  2614.             'Transaction\\TransactionService'
  2615.         );
  2616.         $pagarMeTransaction $this->generalService->getService(
  2617.             'PagarMe\\PagarMeTransaction'
  2618.         );
  2619.         $this->requestUtil->setRequest($request)->setData();
  2620.         $items json_decode($this->requestUtil->getField('items'));
  2621.         $userCardId $this->requestUtil->getField('userCardId');
  2622.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  2623.         if(count($items) == CartEnum::NO){
  2624.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2625.         }
  2626.         $cartRepository $this->em->getRepository(Cart::class);
  2627.         $productRepository $this->em->getRepository(Product::class);
  2628.         $userCardRepository $this->em->getRepository(UserCard::class);
  2629.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  2630.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2631.         $carts $cartRepository->getUserValidCarts($this->user->getId());
  2632.         if(empty($carts)){
  2633.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2634.         }
  2635.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2636.             "id" => $userCheckoutInfoId,
  2637.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2638.         ]);
  2639.         if(!$userCheckoutInfo){
  2640.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2641.         }
  2642.         $groupedItems = [];
  2643.         $ref null;
  2644.         $group = [];
  2645.         foreach ($items as $key => $item) {
  2646.             
  2647.             if(empty($ref)){
  2648.                 $ref $item->parentRef;
  2649.             }
  2650.             if($ref != $item->parentRef){
  2651.                 if(!empty($group)){
  2652.                     $groupedItems[] = $group;
  2653.                 }
  2654.                 $ref $item->parentRef;
  2655.                 $group = [];
  2656.             }
  2657.             
  2658.             $group[] = $item;
  2659.             if(($key 1) == count($items)){
  2660.                 if(!empty($group)){
  2661.                     $groupedItems[] = $group;
  2662.                 }
  2663.             }
  2664.         }
  2665.         $userCard null;
  2666.         if($userCardId 0){
  2667.             $userCard $userCardRepository->findOneBy([
  2668.                 "id" => $userCardId,
  2669.                 "user" => $this->user->getId(),
  2670.                 "deleted" => CartEnum::ITEM_NO_DELETED
  2671.             ]);
  2672.         }
  2673.         $hashReference md5(
  2674.             date('Y-m-d H:i:s') . $this->user->getId() . $this->client->getClientId()
  2675.         );
  2676.         $paymentConfig $this->configuration->getPaymentConfig();
  2677.         foreach ($groupedItems as $key => $group) {
  2678.             $items = [];
  2679.             $paymentMethod null;
  2680.             $installments 1;
  2681.             $dateExpire null;
  2682.             $amount 0;
  2683.             foreach ($group as $key => $itemReq) {
  2684.                 $itemReq = (object)$itemReq;
  2685.                 if($itemReq->able){
  2686.                     $cart $cartRepository->findOneBy([
  2687.                         "id" => $itemReq->id,
  2688.                         "deleted" => CartEnum::ITEM_NO_DELETED
  2689.                     ]);
  2690.                     $productOffer $cart->getProductOffer();
  2691.                     $product $cart->getProduct();
  2692.                     $productCoupon $cart->getProductCoupon();
  2693.                     $items[] = (object)[
  2694.                         "courses" => $cart->getCourse(),
  2695.                         "productOfferId" => $productOffer->getId(),
  2696.                         "productId" => $product->getId(),
  2697.                         "amount" => $cart->getPrice() + $productOffer->getMembershipFee(),
  2698.                         "cartId" => $cart->getId(),
  2699.                         "productCouponId" => (
  2700.                             $productCoupon $productCoupon->getId() : null
  2701.                         ),
  2702.                     ];
  2703.                     $paymentMethod $itemReq->paymentMethod;
  2704.                     $installments $itemReq->installment;
  2705.                     if($paymentMethod == CartEnum::PAYMENT_BILL){
  2706.                         $dateExpire $itemReq->billDateExpire;
  2707.                     }else if($paymentMethod == CartEnum::PAYMENT_PIX){
  2708.                         $dateExpire $itemReq->pixDateExpire;
  2709.                     }
  2710.                     $amountItem $cart->getPrice(true) + $productOffer->getMembershipFee(
  2711.                         true
  2712.                     );
  2713.                     if($paymentMethod == CartEnum::PAYMENT_CARD){
  2714.                         $freeInstallments $productOfferRepository->getFreeInstallment(
  2715.                             $productOffer
  2716.                         );
  2717.                         $installmentsOptions $pagarMeTransaction->calculateInstallments([
  2718.                             'amount' => $amountItem,
  2719.                             'free_installments' => $freeInstallments,
  2720.                             'max_installments' => $installments,
  2721.                             'interest_rate' => $paymentConfig->installmentInterest
  2722.                         ]);
  2723.                         $amountItem $installmentsOptions->{$installments}->amount;
  2724.                     }
  2725.                     $amount $amount $amountItem;
  2726.                 }
  2727.             }
  2728.             if($amount $this->configuration->getMinValueProduct()){
  2729.                 return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2730.             }
  2731.             if($this->user && $this->user->getId() == CartEnum::YES){
  2732.                 $transactionService->setDebug(true);
  2733.             }
  2734.             $transactionOrigin TransactionEnum::ORIGIN_CHECKOUT_CUSTOM;
  2735.             $data $transactionService->createTransactionEAD(
  2736.                 $this->user,
  2737.                 $userCheckoutInfo,
  2738.                 $amount
  2739.                 $paymentMethod,
  2740.                 $installments,
  2741.                 $items,
  2742.                 $userCard,
  2743.                 $dateExpire,
  2744.                 $hashReference,
  2745.                 null,
  2746.                 null,
  2747.                 $transactionOrigin
  2748.             );
  2749.             if(!empty($data['errorMessage'])){
  2750.                 foreach ($group as $key => $itemReq) {
  2751.                     if($itemReq->able){
  2752.                         $cart $cartRepository->findOneBy([
  2753.                             "id" => $itemReq->id,
  2754.                             "deleted" => CartEnum::ITEM_NO_DELETED
  2755.                         ]);
  2756.                         $cart->setErrorMessage($data['errorMessage']);
  2757.                     }
  2758.                 }
  2759.                 $this->em->flush();
  2760.     
  2761.                 return $this->eadResponse([ 
  2762.                     "message" => $data['errorMessage']
  2763.                 ], ErrorEnum::ACTION_INVALID);
  2764.             }
  2765.         }
  2766.         $dataFinal = [
  2767.             "hashReference" => $hashReference,
  2768.         ];
  2769.         return $this->eadResponse($dataFinal);
  2770.     }
  2771.     /**
  2772.      * @Route(
  2773.      *      path          = "/cart/buy/one/click/{productOfferId}/{courseId}",
  2774.      *      name          = "cartOneClickBuy",
  2775.      *      methods       = {"GET"},
  2776.      *      defaults      = { "courseId" = 0 }
  2777.      * )
  2778.      */
  2779.     public function cartOneClickBuy(Request $request){
  2780.         $this->requestUtil->setRequest($request)->setData();
  2781.         $productOfferId $request->get('productOfferId');
  2782.         $courseId = (int)$request->get('courseId');
  2783.         $dataUtml = [
  2784.             "utm_source" => $request->get('utm_source'),
  2785.             "utm_medium" => $request->get('utm_medium'),
  2786.             "utm_campaign" => $request->get('utm_campaign'),
  2787.             "utm_term" => $request->get('utm_term'),
  2788.             "utm_content" => $request->get('utm_content'),
  2789.         ];
  2790.         $utmsUrl json_encode($dataUtml);
  2791.         $params = [
  2792.             "poID" => $productOfferId,
  2793.             "courseId" => $courseId,
  2794.         ];
  2795.         $params array_merge($params$dataUtml);
  2796.         $cartRepository $this->em->getRepository(Cart::class);
  2797.         $productOffer $this->em->getRepository(ProductOffer::class)->findOneBy([ 
  2798.             "id" => $productOfferId
  2799.             "status" => ProductOfferEnum::PUBLISHED,
  2800.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  2801.         ]);
  2802.         if(!$productOffer){
  2803.             return $this->redirectToRoute('notFound');
  2804.         }
  2805.         $product $productOffer->getProduct();
  2806.         if($product->isTypeResource() && empty($courseId)){
  2807.             return $this->redirectToRoute('notFound');
  2808.         }
  2809.         if(
  2810.             $product->getType() == ProductEnum::SUBSCRIPTION ||
  2811.             (
  2812.                 $product->getType() != ProductEnum::SUBSCRIPTION && 
  2813.                 $productOffer->getAllowRecurrency() == ProductOfferEnum::YES
  2814.             )
  2815.         ){
  2816.             if($productOffer->getAllowTrial() == ProductOfferEnum::YES){
  2817.                 $params = [
  2818.                     "poHash" => $productOffer->getOfferLink(),
  2819.                     "pcKey" => null,
  2820.                 ];
  2821.                 $params array_merge($params$dataUtml);
  2822.                 return $this->redirectToRoute('cartIndividual'$params);
  2823.             }
  2824.         }
  2825.         if(!$this->user){
  2826.             return $this->redirectToRoute('cartAdd'$params);
  2827.         }
  2828.         
  2829.         $receiverSchool $this->configuration->getReceiverSchool(
  2830.             ReceiverEnum::EAD_CHECKOUT
  2831.         );
  2832.         if(!$receiverSchool){
  2833.             return $this->redirectToRoute('cartAdd'$params);
  2834.         }
  2835.         if($this->user->getAllowOneClickBuy() == UserEnum::NO){
  2836.             return $this->redirectToRoute('cartAdd'$params);
  2837.         }
  2838.         $userCardRepository $this->em->getRepository(UserCard::class);
  2839.         $userCardDefault $userCardRepository->getDefaultValidUserCard();
  2840.         if(!$userCardDefault){
  2841.             return $this->redirectToRoute('cartAdd'$params);
  2842.         }
  2843.         if($productOffer->getSaleChannel() == ProductOfferEnum::EXTERNAL){
  2844.             $externalCheckoutLink $productOffer->getExternalCheckoutLink();
  2845.             return $this->redirect($externalCheckoutLink301);
  2846.         }
  2847.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2848.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2849.             "user" => $this->user->getId(),
  2850.             "default" => UserCheckoutInfoEnum::YES,
  2851.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2852.         ]);
  2853.         if(!$userCheckoutInfo){
  2854.             return $this->redirectToRoute('cartAdd'$params);
  2855.         }
  2856.         $productSuggestion null;
  2857.         if(!empty($productSuggestionId)){
  2858.             $productSuggestion $psRepository->findOneBy([ 
  2859.                 "id" => $productSuggestionId
  2860.                 "status" => ProductSuggestionEnum::PUBLISHED,
  2861.                 "deleted" => ProductSuggestionEnum::ITEM_NO_DELETED
  2862.             ]);
  2863.         }
  2864.         $course null;
  2865.         $courseRepository $this->em->getRepository(Course::class);
  2866.         if(!empty($courseId)){
  2867.             $course $courseRepository->findOneBy([
  2868.                 "id" => $courseId,
  2869.                 "deleted" => CourseEnum::ITEM_NO_DELETED,
  2870.             ]);
  2871.         }
  2872.         $cart $cartRepository->addCartByProductOffer(
  2873.             $productOffer
  2874.             $this->user
  2875.             $productSuggestion,
  2876.             null,
  2877.             $utmsUrl,
  2878.             ($course ? [ $course ] : null)
  2879.         );
  2880.         
  2881.         if(!$cart){
  2882.             return $this->redirectToRoute('cartAdd'$params);
  2883.         }
  2884.         $productCoupon $cart->getProductCoupon();
  2885.         $items = [
  2886.             (object)[
  2887.                 "courseId" => ($course $course->getId() : null),
  2888.                 "productOfferId" => $productOffer->getId(),
  2889.                 "productId" => $productOffer->getProduct()->getId(),
  2890.                 "amount" => $cart->getPrice() + $cart->getMembershipFee(),
  2891.                 "cartId" => $cart->getId(),
  2892.                 "productCouponId" => ($productCoupon $productCoupon->getId() : null),
  2893.             ]
  2894.         ];
  2895.         $transactionService $this->generalService->getService(
  2896.             'Transaction\\TransactionService'
  2897.         );
  2898.         if($this->user && $this->user->getId() == CartEnum::YES){
  2899.             $transactionService->setDebug(true);
  2900.         }
  2901.         $transactionOrigin TransactionEnum::ORIGIN_ONE_CLICK;
  2902.         $data = (object)$transactionService->createTransactionEAD(
  2903.             $this->user,
  2904.             $userCheckoutInfo,
  2905.             $cart->getPrice(true) + $cart->getMembershipFee(true),
  2906.             CartEnum::PAYMENT_CARD,
  2907.             1,
  2908.             $items,
  2909.             $userCardDefault,
  2910.             null,
  2911.             null,
  2912.             null,
  2913.             null,
  2914.             $transactionOrigin
  2915.         );
  2916.         if(!empty($data->errorMessage)){
  2917.             $cart->setErrorMessage($data->errorMessage);
  2918.             $this->em->flush();
  2919.             //return $this->eadResponse([ 
  2920.             //    "message" => $data->errorMessage
  2921.             //], ErrorEnum::ACTION_INVALID);
  2922.             return $this->redirectToRoute('cart'); 
  2923.         }
  2924.         $paramsConclusion = [
  2925.             "hashReference" => $data->hashReference,
  2926.         ];
  2927.         return $this->redirectToRoute('cartConclusion'$paramsConclusion);
  2928.     }
  2929.     /**
  2930.      * @Route(
  2931.      *      path          = "/admin/cart/checkout/plan/trial/{poID}/{pcID}",
  2932.      *      name          = "cartCheckoutPlanTrial",
  2933.      *      methods       = {"POST"},
  2934.      *      defaults      = { "pcID" = 0 }
  2935.      * )
  2936.      */
  2937.     public function cartCheckoutPlanTrial(Request $request){
  2938.         if(!$this->user){
  2939.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2940.         }
  2941.         
  2942.         $receiverSchool $this->configuration->getReceiverSchool(
  2943.             ReceiverEnum::EAD_CHECKOUT
  2944.         );
  2945.         if(!$receiverSchool){
  2946.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2947.         }
  2948.         $productOfferId = (int)$request->get('poID');
  2949.         $productCouponId = (int)$request->get('pcID');
  2950.         $this->requestUtil->setRequest($request)->setData();
  2951.         $userCheckoutInfoId $this->requestUtil->getField('userCheckoutInfoId');
  2952.         $paymentMethod $this->requestUtil->getField('paymentMethod');
  2953.         $installments $this->requestUtil->getField('installments');
  2954.         $userCardRepository $this->em->getRepository(UserCard::class);
  2955.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  2956.         $productCouponRepository $this->em->getRepository(ProductCoupon::class);
  2957.         $userCheckoutInfoRepository $this->em->getRepository(UserCheckoutInfo::class);
  2958.         $productOffer $productOfferRepository->findOneBy([ 
  2959.             "id" => $productOfferId
  2960.             "status" => ProductOfferEnum::PUBLISHED,
  2961.             "deleted" => ProductOfferEnum::ITEM_NO_DELETED
  2962.         ]);
  2963.         if(!$productOffer){
  2964.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  2965.         }
  2966.         if($productOffer->getAllowTrial() == ProductOfferEnum::NO){
  2967.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2968.         }
  2969.         
  2970.         $product $productOffer->getProduct();
  2971.         $pagarMeTransaction $this->generalService->getService(
  2972.             'PagarMe\\PagarMeTransaction'
  2973.         );
  2974.         $userCheckoutInfo $userCheckoutInfoRepository->findOneBy([
  2975.             "id" => $userCheckoutInfoId,
  2976.             "deleted" => UserCheckoutInfoEnum::ITEM_NO_DELETED,
  2977.         ]);
  2978.         if(!$userCheckoutInfo){
  2979.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  2980.         }
  2981.         $paymentConfig $this->configuration->getPaymentConfig();
  2982.         $userCard null;
  2983.         if(empty($paymentMethod)){
  2984.             return $this->eadResponse([ 'paymentMethod' ], ErrorEnum::FIELD_EMPTY);
  2985.         }
  2986.         if($paymentMethod == CartEnum::PAYMENT_CARD){ // get user card
  2987.             if(empty($installments)){
  2988.                 return $this->eadResponse([ 'installments' ], ErrorEnum::FIELD_EMPTY);
  2989.             }
  2990.             $userCardId $this->requestUtil->getField('userCardId');
  2991.             if(empty($userCardId)){
  2992.                 return $this->eadResponse([ 'userCardId' ], ErrorEnum::FIELD_EMPTY);
  2993.             }
  2994.             if($userCardId 0){
  2995.                 $userCard $userCardRepository->findOneBy([
  2996.                     "id" => $userCardId,
  2997.                     "user" => $this->user->getId(),
  2998.                     "deleted" => CartEnum::ITEM_NO_DELETED
  2999.                 ]);
  3000.             }
  3001.         }
  3002.         if($paymentMethod == CartEnum::PAYMENT_CARD && !$userCard){
  3003.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3004.         }
  3005.         $productCoupon null;
  3006.         $amount $productOffer->getPriceReal();
  3007.         if(!empty($productCouponId)){
  3008.             $productCoupon $productCouponRepository->findOneBy([
  3009.                 "deleted" => ProductCouponEnum::ITEM_NO_DELETED,
  3010.                 "status" => ProductCouponEnum::PUBLISHED,
  3011.                 "id" => $productCouponId,
  3012.             ]);
  3013.             if($productCoupon){
  3014.                 if($productCouponRepository->checkApplyCoupon($productCoupon$productOffer)){
  3015.                     $amount $productCouponRepository->applyDiscount(
  3016.                         $productCoupon
  3017.                         $amount
  3018.                     );
  3019.                 }
  3020.             }
  3021.         }
  3022.         if($amount $this->configuration->getMinValueProduct()){
  3023.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3024.         }
  3025.         $userSubscriptionRepository $this->em->getRepository(UserSubscription::class);
  3026.         $userSubscription $userSubscriptionRepository->findOneBy([
  3027.             "user" => $this->user->getId(),
  3028.             "product" => $product->getId()
  3029.         ]);
  3030.         if($userSubscription && $userSubscription->isLive()){
  3031.             return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3032.         }
  3033.         if(!$userSubscription){
  3034.             $userSubscription = new UserSubscription();
  3035.         }else{
  3036.             $userSubscriptionRepository->restore($userSubscriptionTrashEnum::USER_SUBSCRIPTION);
  3037.         }
  3038.         $userSubscription->setStatus(UserSubscriptionEnum::STATUS_TRIAL);
  3039.         $userSubscription->setDateStart(date('Y-m-d H:i:s'));
  3040.         $userSubscription->setAutomaticRenewal($productOffer->getPlanRenew());
  3041.         $userSubscription->setLifetime($productOffer->getPlanLifetime());
  3042.         $userSubscription->setPrice($productOffer->getPriceReal());
  3043.         $userSubscription->setCycle($productOffer->getPlanCycle());
  3044.         $userSubscription->setChargeNumberMax($productOffer->getPlanChargeNumberMax());
  3045.         $userSubscription->setMembershipFee($productOffer->getMembershipFee());
  3046.         $userSubscription->setProduct($product);
  3047.         $userSubscription->setProductOffer($productOffer);
  3048.         $userSubscription->setUser($this->user);
  3049.         $userSubscription->setUsedTrial(UserSubscriptionEnum::YES);
  3050.         if($productCoupon){
  3051.             $userSubscription->setProductCoupon($productCoupon);
  3052.             $userSubscription->setCouponKey($productCoupon->getKey());
  3053.             $userSubscription->setCouponType($productCoupon->getDiscountType());
  3054.             $userSubscription->setCouponApplyMembershipFee(
  3055.                 $productCoupon->getApplyMembershipFee()
  3056.             );
  3057.             $userSubscription->setCouponLifetime($productCoupon->getPlanLifetime());
  3058.             $userSubscription->setCouponNumberCharges($productCoupon->getPlanCharges());
  3059.             $userSubscription->setCouponDiscount($productCoupon->getDiscount());
  3060.         }
  3061.         $userSubscription->setPaymentMethod($paymentMethod);
  3062.         $monthCycles = [
  3063.             UserSubscriptionEnum::CYCLE_MONTHLY,
  3064.             UserSubscriptionEnum::CYCLE_WEEKLY,
  3065.             UserSubscriptionEnum::CYCLE_BIWEEKLY,
  3066.         ];
  3067.         if(!in_array($userSubscription->getCycle(), $monthCycles)){
  3068.             $freeInstallmentNumber $productOfferRepository->getFreeInstallment(
  3069.                 $productOffer
  3070.             );
  3071.             $userSubscription->setInstallments($installments);
  3072.             $userSubscription->setInstallmentsFree($freeInstallmentNumber);
  3073.         }
  3074.         
  3075.         //define date renew
  3076.         if(
  3077.             $userSubscription->getLifetime() == UserSubscriptionEnum::NO && 
  3078.             $userSubscription->getAutomaticRenewal() == UserSubscriptionEnum::YES
  3079.         ){
  3080.             $periodRenew $userSubscriptionRepository->getPlanCycle(
  3081.                 $userSubscription->getCycle(),
  3082.                 $userSubscription->getChargeNumberMax()
  3083.             );
  3084.             $userSubscription->setDateRenew(date('Y-m-d'strtotime($periodRenew)));
  3085.         }
  3086.         $days $productOffer->getTrialPeriod();
  3087.         $dateNextPayment date('Y-m-d'strtotime("+ {$days} day"));
  3088.         $dateAccess date('Y-m-d H:i:s'strtotime("+ {$days} day"));
  3089.         $userSubscription->setDateNextPayment($dateNextPayment);
  3090.         $userSubscription->setDateExpiration($dateNextPayment);
  3091.         $userSubscription->setUserCard($userCard);
  3092.         $userSubscription->setUserCard($userCard);
  3093.         $userSubscription->setTrialDateEnd($dateAccess);
  3094.         $this->em->persist($userSubscription);
  3095.         $this->userLogService->logInsert(
  3096.             "user_subscription"
  3097.             $userSubscription->getId(), 
  3098.             $userSubscription->toReturn()
  3099.         );
  3100.         $enrollmentService $this->generalService->getService('EnrollmentService');
  3101.         $enrollmentService->setNotification(true);
  3102.         $enrollmentService->setEmail(false);
  3103.         $enrollmentService->setOrigin(EnrollmentEnum::ORIGIN_SUBSCRIPTION);
  3104.         if($userSubscription->getPaymentMethod() == UserSubscriptionEnum::PAYMENT_BILL){
  3105.             $dateAccess date('Y-m-d H:i:s'strtotime("{$dateAccess} + 3 days"));
  3106.         }
  3107.         $enrollmentService->setAccessDate($dateAccess);
  3108.         $enrollmentService->setSupportDate($dateAccess);
  3109.         $enrollmentService->setUserSubscription($userSubscription);
  3110.         $enrollmentService->enrollUserByProduct(
  3111.             $userSubscription->getUser(), 
  3112.             $userSubscription->getProduct()
  3113.         );
  3114.         $marketingService $this->generalService->getService('Marketing\MarketingService');
  3115.         $marketingService->setTag(TagsMarketingEnum::TAG_START_SUBSCRIPTION);
  3116.         $marketingService->setTextComplement($product->getTitle());
  3117.         $marketingService->setUser($this->user);
  3118.         $marketingService->send();
  3119.         $pixelService $this->generalService->getService('Marketing\\PixelService');
  3120.         $pixelService->sendConversion('StartTrial', (object)[
  3121.             "value" => $userSubscription->getPrice(),
  3122.             "currency" => $productOffer->getCurrencyCode(),
  3123.             "contents" => [
  3124.                 (object)[
  3125.                     "id" => $product->getId(),
  3126.                     "quantity" => 1,
  3127.                     "item_price" => $userSubscription->getPrice(),
  3128.                     //"name" => $product->getTitle(),
  3129.                     //"type" => $product->getType()
  3130.                 ]
  3131.             ],
  3132.             "num_items" => 1,
  3133.             "content_type" => "product",
  3134.             "transaction_id" => $userSubscription->getId(),
  3135.         ]);
  3136.         $this->em->flush();
  3137.         if($userSubscription->getUser() && $userSubscription->getProduct()->getUser()){
  3138.             sleep(1);
  3139.             
  3140.             if($userSubscription->getId()){
  3141.                 $notificationService $this->generalService->getService('NotificationService');
  3142.                 $notificationService->create(
  3143.                     $userSubscription->getUser(), 
  3144.                     $userSubscription->getProduct()->getUser(),
  3145.                     NotificationEnum::ORIGIN_USER_SUBSCRIPTION_NEW,
  3146.                     $userSubscription->getId()
  3147.                 );
  3148.                 $notificationService->create(
  3149.                     $userSubscription->getProduct()->getUser(),
  3150.                     $userSubscription->getUser(),
  3151.                     NotificationEnum::ORIGIN_USER_SUBSCRIPTION_NEW,
  3152.                     $userSubscription->getId()
  3153.                 );
  3154.             }
  3155.         }
  3156.         $userWebhook $this->em->getRepository(User::class)->getToWebhook(
  3157.             $userSubscription->getUser()
  3158.         );
  3159.         
  3160.         //webhook
  3161.         $dataObj = (object)[
  3162.             "user" => $userWebhook,
  3163.             "subscription" => $userSubscription->toWebhook(),
  3164.             "product" => (object)[
  3165.                 "id" => (string)$userSubscription->getProduct()->getId(),
  3166.                 "name" => $userSubscription->getProduct()->getTitle(),
  3167.                 "offer" => (object)[
  3168.                     "id" => (string)$userSubscription->getProductOffer()->getId(),
  3169.                     "name" => $userSubscription->getProductOffer()->getTitle(),
  3170.                     "price" => (string)$userSubscription->getProductOffer()->getPriceReal(),
  3171.                     "membership"=> (string)$userSubscription->getMembershipFee(),
  3172.                 ],
  3173.             ],
  3174.         ];
  3175.         $webhookService $this->generalService->getService('WebhookService');
  3176.         $webhookService->addItemList(WebhookEnum::SUBSCRIPTION$dataObj);
  3177.         return $this->eadResponse([
  3178.             "billLink" => null,
  3179.             "pixCode" => null,
  3180.             "userId" => $this->user->getId(),
  3181.             "transactionHash" => "success",
  3182.             "hashReference" => "success",
  3183.         ]);
  3184.     }
  3185.     /**
  3186.      * @Route(
  3187.      *      path          = "/cart/checkout/new/try/{transactionHash}",
  3188.      *      name          = "cartCheckoutNewTry",
  3189.      *      methods       = {"GET"}
  3190.      * )
  3191.      */
  3192.     public function cartCheckoutNewTry(Request $request){
  3193.         $this->checkUserSession($request);
  3194.         $transactionHash $request->get('transactionHash');
  3195.         $cartRepository $this->em->getRepository(Cart::class);
  3196.         $transactionRepository $this->em->getRepository(Transaction::class);
  3197.         $transactionItemRepository $this->em->getRepository(TransactionItem::class);
  3198.         $transaction $transactionRepository->findOneBy([
  3199.             "hash" => $transactionHash,
  3200.             "status" => TransactionEnum::CANCELED,
  3201.             "deleted" => TransactionEnum::ITEM_NO_DELETED
  3202.         ]);
  3203.         if(!$transaction){
  3204.             return $this->redirectToRoute('notFound');
  3205.         }
  3206.         $transactionItems $transactionItemRepository->findBy([
  3207.             "transaction" => $transaction->getId(),
  3208.             "deleted" => TransactionItemEnum::ITEM_NO_DELETED
  3209.         ]);
  3210.         if(empty($transactionItems)){
  3211.             return $this->redirectToRoute('notFound');
  3212.         }
  3213.         foreach ($transactionItems as $key => $transactionItem) {
  3214.             $courses = [];
  3215.             foreach ($transactionItem->getCourse() as $keyC => $course) {
  3216.                 $courses[] = $course;
  3217.             }
  3218.             $cartRepository->addCartByProductOffer(
  3219.                 $transactionItem->getProductOffer(),
  3220.                 $transaction->getUser(),
  3221.                 null,
  3222.                 $transactionItem->getProductCoupon(),
  3223.                 $transactionItem->getUtmsUrl(),
  3224.                 $courses
  3225.             );
  3226.         }
  3227.         $this->em->flush();
  3228.         return $this->redirectToRoute('cart');
  3229.     }
  3230.     /**
  3231.      * @Route(
  3232.      *      path          = "/cart/conclusion/{hashReference}",
  3233.      *      name          = "cartConclusion",
  3234.      *      methods       = {"GET"},
  3235.      *      defaults      = { "hashReference" = null }
  3236.      * )
  3237.      * @Route(
  3238.      *      path          = "/cart/conclusao/{hashReference}",
  3239.      *      name          = "cartConclusionOld",
  3240.      *      methods       = {"GET"},
  3241.      *      defaults      = { "hashReference" = null }
  3242.      * )
  3243.      */
  3244.     public function cartConclusionPage(Request $request) {
  3245.         $cartRepository $this->em->getRepository(Cart::class);
  3246.         $productRepository $this->em->getRepository(Product::class);
  3247.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  3248.         $transactionRepository $this->em->getRepository(Transaction::class);
  3249.         $ti $this->em->getRepository(TransactionItem::class);
  3250.         $psRepository $this->em->getRepository(ProductSuggestion::class);
  3251.         $hashReference $request->get('hashReference');
  3252.         $this->data['hashReference'] = $hashReference;
  3253.         $this->data['transactionsCard'] = null;
  3254.         $this->data['transactionsPix'] = null;
  3255.         $this->data['transactionsBill'] = null;
  3256.         $this->data['productOfferSuggestions'] = null;
  3257.         
  3258.         $this->data['hasWaiting'] = true;
  3259.         $this->data['hasApproved'] = false;
  3260.         $this->data['hasCanceled'] = false;
  3261.         $this->data['showInfoStep'] = true;
  3262.         $this->data['hasContracts'] = true;
  3263.         $this->data['sOffers'] = [];
  3264.         
  3265.         $this->data['showSteps'] = true;
  3266.         if(!empty($hashReference)){
  3267.             $isCheckoutPage $transactionRepository->existByReferenceAndOrigin(
  3268.                 $hashReference
  3269.                 TransactionEnum::ORIGIN_CHECKOUT_PAGE
  3270.             );
  3271.             if($isCheckoutPage){
  3272.                 $this->data['showSteps'] = false;
  3273.             }
  3274.             $hasWaiting $transactionRepository->existByReferenceAndStatus(
  3275.                 $hashReference
  3276.                 TransactionEnum::WAITING
  3277.             );
  3278.             $hasApproved $transactionRepository->existByReferenceAndStatus(
  3279.                 $hashReference
  3280.                 TransactionEnum::APPROVED
  3281.             );
  3282.             $hasCanceled $transactionRepository->existByReferenceAndStatus(
  3283.                 $hashReference
  3284.                 TransactionEnum::CANCELED
  3285.             );
  3286.             $hasWaitingCard $transactionRepository->existByReferenceAndStatus(
  3287.                 $hashReference
  3288.                 TransactionEnum::WAITING,
  3289.                 TransactionEnum::PAYMENT_CARD
  3290.             );
  3291.             $hasApprovedCard $transactionRepository->existByReferenceAndStatus(
  3292.                 $hashReference
  3293.                 TransactionEnum::APPROVED,
  3294.                 TransactionEnum::PAYMENT_CARD
  3295.             );
  3296.             $hasCanceledCard $transactionRepository->existByReferenceAndStatus(
  3297.                 $hashReference
  3298.                 TransactionEnum::CANCELED,
  3299.                 TransactionEnum::PAYMENT_CARD
  3300.             );
  3301.             $hasWaitingBill $transactionRepository->existByReferenceAndStatus(
  3302.                 $hashReference
  3303.                 TransactionEnum::WAITING,
  3304.                 TransactionEnum::PAYMENT_BILL
  3305.             );
  3306.             $hasApprovedBill $transactionRepository->existByReferenceAndStatus(
  3307.                 $hashReference
  3308.                 TransactionEnum::APPROVED,
  3309.                 TransactionEnum::PAYMENT_BILL
  3310.             );
  3311.             $hasCanceledBill $transactionRepository->existByReferenceAndStatus(
  3312.                 $hashReference
  3313.                 TransactionEnum::CANCELED,
  3314.                 TransactionEnum::PAYMENT_BILL
  3315.             );
  3316.             $hasWaitingPix $transactionRepository->existByReferenceAndStatus(
  3317.                 $hashReference,
  3318.                 TransactionEnum::WAITING,
  3319.                 TransactionEnum::PAYMENT_PIX
  3320.             );
  3321.             $hasApprovedPix $transactionRepository->existByReferenceAndStatus(
  3322.                 $hashReference,
  3323.                 TransactionEnum::APPROVED,
  3324.                 TransactionEnum::PAYMENT_PIX
  3325.             );
  3326.             $hasCanceledPix $transactionRepository->existByReferenceAndStatus(
  3327.                 $hashReference
  3328.                 TransactionEnum::CANCELED,
  3329.                 TransactionEnum::PAYMENT_PIX
  3330.             );
  3331.             $transactionsCard $transactionRepository->getToCartConclusion(
  3332.                 $hashReference
  3333.                 TransactionEnum::PAYMENT_CARD
  3334.             );
  3335.             $transactionsPix $transactionRepository->getToCartConclusion(
  3336.                 $hashReference
  3337.                 TransactionEnum::PAYMENT_PIX
  3338.             );
  3339.             $transactionsBill $transactionRepository->getToCartConclusion(
  3340.                 $hashReference
  3341.                 TransactionEnum::PAYMENT_BILL
  3342.             );
  3343.             $transactionOffers $productOfferRepository->getByTransactionHashReference(
  3344.                 $hashReference
  3345.             );
  3346.             if(count($transactionOffers) == TransactionEnum::YES){
  3347.                 $offer $transactionOffers[0];
  3348.                 $productPage $offer->getProductPage();
  3349.                 $externalPageLink $productPage->getExternalConclusionLink();
  3350.                 if(
  3351.                     $productPage->getAllowExternalConclusion() &&
  3352.                     !empty($externalPageLink) &&
  3353.                     $hasApproved
  3354.                 ){
  3355.                     return $this->redirect($externalPageLink301);
  3356.                 }
  3357.             }
  3358.             $sOffers = [];
  3359.             foreach ($transactionOffers as $keyOffer => $offer) {
  3360.                 $productOfferSuggestions $psRepository->getByOfferOriginSimply(
  3361.                     $offer,
  3362.                     ProductSuggestionEnum::EXECUTE_IN_CONCLUSION_PAGE
  3363.                 );
  3364.                 foreach ($productOfferSuggestions as $key => $productSuggestion) {
  3365.                     $productOffer $productSuggestion->getProductOffer();
  3366.                     $product $productOffer->getProduct();
  3367.                     if($this->user && !$productRepository->userHasProduct($this->user$product)){
  3368.                         $productOffer->productSuggestionId $productSuggestion->getId();
  3369.                         $productOffer->productSuggestionItem $productSuggestion;
  3370.                         
  3371.                         if($productOffer->getAllowSaleLimit() == ProductOfferEnum::YES){
  3372.                             $saleNumber $ti->countSalesApprovedByProductOffer(
  3373.                                 $productOffer
  3374.                             );
  3375.                             if($productOffer->getSaleNumberLimit() > $saleNumber){
  3376.                                 $sOffers[$productOffer->getId()] = $productOffer;
  3377.                             }
  3378.                         }else{
  3379.                             $sOffers[$productOffer->getId()] = $productOffer;
  3380.                         }
  3381.                     }
  3382.                 }
  3383.                 $cartRepository->sendConclusionConversion($hashReference);
  3384.             }
  3385.             try{
  3386.             
  3387.                 $transactions array_merge(
  3388.                     $transactionsCard,
  3389.                     $transactionsPix,
  3390.                     $transactionsBill
  3391.                 );
  3392.                 $pagarMeTransaction $this->generalService->getService(
  3393.                     'PagarMe\\PagarMeTransaction'
  3394.                 );
  3395.                 foreach ($transactions as $key => $tr) {
  3396.                     if($tr->getGateway() == TransactionEnum::EAD_CHECKOUT){
  3397.                         if($tr->getPaymentMethod() == TransactionEnum::PAYMENT_BILL){
  3398.                             $transactionPagarme $pagarMeTransaction->get($tr->getHash());
  3399.                             $dateExpire date(
  3400.                                 'Y-m-d'
  3401.                                 strtotime($transactionPagarme->boleto_expiration_date)
  3402.                             );
  3403.                             $tr->setBillCode($transactionPagarme->boleto_barcode);
  3404.                             $tr->setBillLink($transactionPagarme->boleto_url);
  3405.                             $tr->setDateExpire($dateExpire);
  3406.                         }else if($tr->getPaymentMethod() == TransactionEnum::PAYMENT_PIX){
  3407.                             $transactionPagarme $pagarMeTransaction->get($tr->getHash());
  3408.                             $dateExpire date(
  3409.                                 'Y-m-d'
  3410.                                 strtotime($transactionPagarme->pix_expiration_date)
  3411.                             );
  3412.                             $tr->setPixCode($transactionPagarme->pix_qr_code);
  3413.                             $tr->setDateExpire($dateExpire);
  3414.                         }
  3415.                     }
  3416.                 }
  3417.                 $this->em->flush();
  3418.             }catch(Exception $e){
  3419.             }
  3420.             $this->data['hasWaiting'] = $hasWaiting;
  3421.             $this->data['hasApproved'] = $hasApproved;
  3422.             $this->data['hasCanceled'] = $hasCanceled;
  3423.             $this->data['hasWaitingCard'] = $hasWaitingCard;
  3424.             $this->data['hasApprovedCard'] = $hasApprovedCard;
  3425.             $this->data['hasCanceledCard'] = $hasCanceledCard;
  3426.             $this->data['hasWaitingBill'] = $hasWaitingBill;
  3427.             $this->data['hasApprovedBill'] = $hasApprovedBill;
  3428.             $this->data['hasCanceledBill'] = $hasCanceledBill;
  3429.             $this->data['hasWaitingPix'] = $hasWaitingPix;
  3430.             $this->data['hasApprovedPix'] = $hasApprovedPix;
  3431.             $this->data['hasCanceledPix'] = $hasCanceledPix;
  3432.             $this->data['transactionsCard'] = $transactionsCard;
  3433.             $this->data['transactionsPix'] = $transactionsPix;
  3434.             $this->data['transactionsBill'] = $transactionsBill;
  3435.             $this->data['productOfferSuggestions'] = $sOffers;
  3436.         }
  3437.         $this->data['customCart'] = true;
  3438.         $this->data['cartConclusion'] = true;
  3439.         return $this->renderEAD('cart/cart-conclusion.html.twig');
  3440.     }
  3441.     /**
  3442.      * @Route(
  3443.      *      path          = "/cart/conclusion/status/{hashReference}",
  3444.      *      name          = "cartConclusionCheckStatus",
  3445.      *      methods       = {"GET"},
  3446.      *      defaults      = { "hashReference" = null }
  3447.      * )
  3448.      */
  3449.     public function cartConclusionCheckStatus(Request $request) {
  3450.         $cartRepository $this->em->getRepository(Cart::class);
  3451.         $transactionRepository $this->em->getRepository(Transaction::class);
  3452.         $transactionItemRepository $this->em->getRepository(TransactionItem::class);
  3453.         $pagarMeTransaction $this->generalService->getService(
  3454.             'PagarMe\\PagarMeTransaction'
  3455.         );
  3456.         $processEad $this->generalService->getService('Transaction\\ProcessEad');
  3457.         $hashReference $request->get('hashReference');
  3458.         if(empty($hashReference)){
  3459.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3460.         }
  3461.         $transactions $transactionRepository->findBy([
  3462.             "hashReference" => $hashReference,
  3463.             "deleted" => TransactionEnum::ITEM_NO_DELETED
  3464.         ]);
  3465.         $refresh = [];
  3466.         foreach ($transactions as $key => $transaction) {
  3467.             $transactionPagarme $pagarMeTransaction->get($transaction->getHash());
  3468.             $newStatus $pagarMeTransaction->getTransactionStatus(
  3469.                 $transactionPagarme->status
  3470.             );
  3471.             if($newStatus == TransactionEnum::APPROVED){
  3472.                 $refresh[] = true;
  3473.             }
  3474.         }
  3475.         $this->em->flush();
  3476.         $data = [
  3477.             "refresh" => (count($refresh) == count($transactions))
  3478.         ];
  3479.         return $this->eadResponse($dataErrorEnum::SUCCESS);
  3480.     }
  3481.     /**
  3482.      * @Route(
  3483.      *      path          = "/cart/remove/coupon/{id}",
  3484.      *      name          = "cartCouponRemove",
  3485.      *      methods       = {"GET"},
  3486.      *      requirements  = { "id" = "\d+"}
  3487.      * )
  3488.      */
  3489.     public function removeCouponProduct(Request $request) {
  3490.         $cartId $request->get('id');
  3491.         $cartRepository $this->em->getRepository(Cart::class);
  3492.         $cart $cartRepository->findOneBy([
  3493.             "id" => $cartId,
  3494.             "deleted" => CartEnum::ITEM_NO_DELETED
  3495.         ]);
  3496.         
  3497.         if(!$cart){
  3498.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3499.         }
  3500.         if($this->user){
  3501.             if($this->user->getId() != $cart->getUser()->getId()){
  3502.                 return $this->eadResponse(nullErrorEnum::ACTION_INVALID);
  3503.             }   
  3504.         }
  3505.         if($cart->getProductCoupon()){
  3506.             $product $cart->getProduct();
  3507.             $productOffer $cart->getProductOffer();
  3508.             $cart->setPrice($productOffer->getPriceReal());
  3509.             $cart->setMembershipFee(ProductEnum::NO);
  3510.             if(
  3511.                 $product->getType() == ProductEnum::SUBSCRIPTION ||
  3512.                 $productOffer->getAllowRecurrency() == ProductEnum::YES
  3513.             ){
  3514.                 $cart->setMembershipFee($productOffer->getMembershipFee());
  3515.             }
  3516.             $cart->setProductCoupon(null);
  3517.         }
  3518.         $this->em->flush();
  3519.         // return $this->eadResponse($cart->toReturn(), ErrorEnum::SUCCESS);
  3520.         return $this->redirectToRoute('cart');
  3521.     }
  3522.     /**
  3523.      * @Route(
  3524.      *      path          = "/delete/{id}/{isAjaxRequest}",
  3525.      *      name          = "cartDelete",
  3526.      *      methods       = {"GET"},
  3527.      *      requirements  = { "id" = "\d+" },
  3528.      *      defaults      = { "isAjaxRequest" = 0 }
  3529.      * )
  3530.      */
  3531.     public function deleteCart(Request $request) {
  3532.         $isAjaxRequest = (int)$request->get('isAjaxRequest');
  3533.         $cartId $request->get('id');
  3534.         $cartRepository $this->em->getRepository(Cart::class);
  3535.         $cart $cartRepository->findOneBy([
  3536.             "id" => $cartId,
  3537.             "deleted" => CartEnum::ITEM_NO_DELETED
  3538.         ]);
  3539.         
  3540.         if(!$cart){
  3541.             if($isAjaxRequest == CartEnum::YES){
  3542.                 return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3543.             }
  3544.             return $this->redirectToRoute('notFound');
  3545.         }
  3546.         if($this->user){
  3547.             if($this->user->getId() != $cart->getUser()->getId()){
  3548.                 if($isAjaxRequest == CartEnum::YES){
  3549.                     return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3550.                 }
  3551.                 return $this->redirectToRoute('notFound');
  3552.             }
  3553.         }else{
  3554.             $cookieName 'hashcartoff';
  3555.             $hashId $this->generalService->getCookie($cookieName);
  3556.             if(empty($hashId) || $hashId != $cart->getHashIdentify()){
  3557.                 if($isAjaxRequest == CartEnum::YES){
  3558.                     return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  3559.                 }
  3560.                 return $this->redirectToRoute('notFound');
  3561.             }
  3562.         }
  3563.         $cartRepository->updateCartProductSuggestion($cart);
  3564.         if($this->user){
  3565.             $whishlistIsActive $this->em->getRepository(Whishlist::class)->findOneBy([
  3566.                 "product" => $cart->getProduct()->getId(), 
  3567.                 "user" => $cart->getUser()->getId(),
  3568.                 "deleted" => WhishlistEnum::ITEM_NO_DELETED
  3569.             ]);
  3570.     
  3571.             $crmService $this->generalService->getService('CRM\\CrmService');
  3572.             $dealId $cart->getPipedriveDeal();
  3573.             $crmService->deleteDeal($dealId);
  3574.     
  3575.             if($whishlistIsActive){
  3576.                 $backWhishlist $crmService->saveDeal(
  3577.                     $dealId,
  3578.                     $cart->getUser(),
  3579.                     $cart->getProduct(),
  3580.                     'open',
  3581.                     $this->configuration->get("pipedrive_wishlist"),
  3582.                     $cart->getPrice(),
  3583.                     $cart->getProductOffer()->getCurrencyCode()
  3584.                 );
  3585.             }    
  3586.         }
  3587.         $cart->setUtmsUrl(null);
  3588.         $cart->setProductCoupon(null);
  3589.         $cart->delete();
  3590.         $this->em->flush();
  3591.         // AJAX
  3592.         // if($isAjaxRequest == CartEnum::YES){
  3593.             // return $this->redirectToRoute('cart');
  3594.         // }
  3595.         // NAVBAR
  3596.         return $this->redirectToRoute('cart');
  3597.     }
  3598. }