src/Controller/Website/CartController.php line 4191

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