src/Controller/IndexController.php line 610

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\App\AppMenu;
  4. use App\Entity\App\AppProfile;
  5. use App\Entity\App\AppUser;
  6. use App\Entity\Rcd\RcdDonation;
  7. use App\Entity\Rcd\RcdInvoice;
  8. use App\Entity\Rcd\RcdLogs;
  9. use App\Entity\Rcd\RcdLogsDonations;
  10. use App\Entity\Rcd\RcdOrganization;
  11. use App\Entity\Rcd\RcdProject;
  12. use App\Entity\Rcd\RcdQuota;
  13. use App\Entity\Rcd\RcdUserBkToken;
  14. use App\Form\Rcd\PersonLegal\RcdUserLegalType;
  15. use App\Form\Rcd\PersonNatural\RcdUserNaturalType;
  16. use App\Form\Rcd\RcdUserAnonymousType;
  17. use App\Services\App\AppTools;
  18. use App\Services\App\FileUploader;
  19. use App\Services\Transbank\ServiceOneClickMall;
  20. use App\Services\Transbank\ServiceWebpayPlus;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  28. class IndexController extends AbstractController
  29. {
  30.     public $appTools;
  31.     public $serviceWebpayPlus;
  32.     public $serviceOneclickMall;
  33.     private $responseCode = [
  34.         => 'Transacción aprobada',
  35.         -=> 'Rechazo de transacción - Reintente (Posible error en el ingreso de datos de la transacción)',
  36.         -=> 'Rechazo de transacción (Se produjo fallo al procesar la transacción. Este mensaje de rechazo está relacionado a parámetros de la tarjeta y/o su cuenta asociada)',
  37.         -=> 'Error en transacción (Interno Transbank)',
  38.         -=> 'Rechazo emisor (Rechazada por parte del emisor)',
  39.         -=> 'Rechazo - Posible Fraude (Transacción con riesgo de posible fraude)',
  40.         -96 => 'tbk_user no existente',
  41.         -97 => 'Límites Oneclick, máximo monto diario de pago excedido.',
  42.         -98 => 'Límites Oneclick, máximo monto de pago excedido',
  43.         -99 => 'Límites Oneclick, máxima cantidad de pagos diarios excedido.',
  44.     ];
  45.     public function __construct(AppTools $appToolsServiceWebpayPlus $serviceWebpayPlusServiceOneClickMall $serviceOneClickMall)
  46.     {
  47.         $this->appTools $appTools;
  48.         $this->serviceWebpayPlus $serviceWebpayPlus;
  49.         $this->serviceOneclickMall $serviceOneClickMall;
  50.     }
  51.     private function newTransactionWebpayPlus($idUserRcdOrganization $rcdOrganization$amount$project$idRcDonation$newUser)
  52.     {
  53.         $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" "http";
  54.         $url $protocol.'://'.$this->appTools->requestStack()->getHttpHost().$this->generateUrl('index_responseWebPayPlus',[
  55.             'token' => $rcdOrganization->getToken(),
  56.             'project' => $project,
  57.             'rcDonation' => $idRcDonation,
  58.             'user' => $idUser,
  59.             'newUser' => $newUser'yes''nop',
  60.         ]);
  61.         if($rcdOrganization->getWebPayPlusType()){
  62.             $response $this->serviceWebpayPlus->newTransactionMall($amount$url$rcdOrganization$idRcDonation);
  63.         }else{
  64.             $response $this->serviceWebpayPlus->newTransaction($amount$url$rcdOrganization$idRcDonation);
  65.         }
  66.         return [
  67.             'url' => $response['url'],
  68.             'token' => $response['token']
  69.         ];
  70.     }
  71.     private function newInscriptionOneClickMall(AppUser $appUserRcdOrganization $rcdOrganization$amount$project$idRcDonation$newUser)
  72.     {
  73.         $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" "http";
  74.         $json json_encode([
  75.             'token' => $rcdOrganization->getToken(),
  76.             'project' => $project,
  77.             'rcDonation' => $idRcDonation,
  78.             'user' => $appUser->getId(),
  79.             'newUser' => $newUser'yes''nop',
  80.             'mount' => $amount,
  81.         ]);
  82.         $url $protocol.'://'.$this->appTools->requestStack()->getHttpHost().$this->generateUrl('index_responseOneClickMall',[
  83.             'token' => base64_encode($json),
  84.             #'project' => $project,
  85.             #'user' => $appUser->getId(),
  86.             #'mount' => ,
  87.         ]);
  88.         $response $this->serviceOneclickMall->newInscription($appUser->getUsername(), $appUser->getEmail(), $url$rcdOrganization);
  89.         return [
  90.             'url' => $response['url'],
  91.             'token' => $response['token']
  92.         ];
  93.     }
  94.     private function registerFirstDonation($appUser$amount$token$idProject)
  95.     {
  96.         $em $this->getDoctrine()->getManager();
  97.         $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token' => $token]);
  98.         $rcdProject null;
  99.         if ($idProject) {
  100.             $rcdProject $em->getRepository(RcdProject::class)->find($idProject);
  101.         }
  102.         $observation null;
  103.         if($appUser->getRcdPerson()){
  104.             $observation $appUser->getRcdPerson()->getObservation();
  105.         }
  106.         $rcdInvoice = new RcdInvoice();
  107.         $rcdInvoice
  108.             ->setAmount($amount)
  109.             ->setAppUser($appUser)
  110.             ->setRcdOrganization($rcdOrganization)
  111.             ->setRcdProject($rcdProject)
  112.             ->setStatusTransaction('PE')
  113.             ->setCreationDate(new \DateTime())
  114.         ;
  115.         $em->persist($rcdInvoice);
  116.         $rcdDonate = new RcdDonation();
  117.         $rcdDonate
  118.             ->setRcdInvoice($rcdInvoice)
  119.             ->setAppUser($appUser)
  120.             ->setObservation($observation)
  121.             ->setRcdProject($rcdProject)
  122.             ->setRcdOrganization($rcdOrganization)
  123.             ->setCreationDate(new \DateTime())
  124.             ->setAmount($amount)
  125.             ->setCode(null)
  126.             ->setToken(null)
  127.             ->setAuthorizationCode(null)
  128.             ->setResponseCode(null)
  129.             ->setPaymentTypeCode(null)
  130.             ->setVci(null)
  131.             ->setStatusTransaction('PENDING')
  132.         ;
  133.         if($appUser->getRcdPerson()){
  134.             if($appUser->getRcdPerson()->getNumberQuota() == ){
  135.                 $rcdDonate->setFlagRecurringDonation(true);
  136.             } else {
  137.                 $rcdDonate->setFlagRecurringDonation(false);
  138.             }
  139.         } else {
  140.             $rcdDonate->setFlagRecurringDonation(false);
  141.         }
  142.         $em->persist($rcdDonate);
  143.         $em->flush();
  144.         return $rcdDonate->getId();
  145.     }
  146.     /**
  147.      * @Route("/personNatural", name="index_personNatural", methods={"POST","GET"})
  148.      */
  149.     public function personNatural(Request $requestUserPasswordEncoderInterface $encoderFileUploader $fileUploader, \Swift_Mailer $mailer): Response
  150.     {
  151.         $em $this->getDoctrine()->getManager();
  152.         $token $request->get('rcdOrganization');
  153.         $type $request->get('type');
  154.         $idProject $request->get('project');
  155.         $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  156.         $formSubmit $request->request->get('form');
  157.         if($this->getUser()){
  158.             $appUser $em->getRepository(AppUser::class)->find($this->getUser()->getId());
  159.         }elseif(isset($formSubmit['email'])){
  160.             $appUser $em->getRepository(AppUser::class)->loadUserByUsername($formSubmit['email']);
  161.             if(!$appUser){
  162.                 $appUser = new AppUser();
  163.             }
  164.         }else{
  165.             $appUser = new AppUser();
  166.         }
  167.         $form $this->createForm(RcdUserNaturalType::class, $appUser, [
  168.             'action'    => $this->generateUrl('index_personNatural', [
  169.                 'type' => $type,
  170.                 'project' => $idProject,
  171.                 'rcdOrganization' => $token,
  172.             ]),
  173.             'attr'      => [
  174.                 'id'    => 'form',
  175.                 'method'    => 'POST',
  176.                 'autocomplete' => 'off',
  177.             ]
  178.         ]);
  179.         $form->handleRequest($request);
  180.         if ($form->isSubmitted() && $form->isValid()) {
  181.             $newUser true;
  182.             if($appUser->getId()){
  183.                 $newUser false;
  184.             } else {
  185.                 $username explode("@"$appUser->getEmail());
  186.                 $appUser_userName $em->getRepository(AppUser::class)->loadUserByUsername($username[0]);
  187.                 if($appUser_userName){
  188.                     $username $username[0].uniqid();
  189.                 } else {
  190.                     $username $username[0];
  191.                 }
  192.                 $appUser->setSalt(md5(time()));
  193.                 $password substr($appUser->getSalt(), 05);
  194.                 $encoded $encoder->encodePassword($appUser$password);
  195.                 $appUser->setPassword($encoded);
  196.                 $appProfile $em->getRepository(AppProfile::class)->findOneBy([
  197.                     'code' => 'DON'
  198.                 ]);
  199.                 $appUser
  200.                     ->setUsername($username)
  201.                     ->setCreationDate(new \DateTime())
  202.                     ->setFlagDelete(false)
  203.                     ->setFlagStatus(true)
  204.                     ->setFlagAccess(true)
  205.                     ->setAppProfile($appProfile)
  206.                 ;
  207.             }
  208.             $rcdPerson $appUser->getRcdPerson();
  209.             if($rcdPerson->getBusinessName()){
  210.                 if($type == 'dnr'){
  211.                     if($rcdPerson->getFlagRecurringDonation()){
  212.                         $rcdPerson->setFlagRecurringDonation(true);
  213.                     } else {
  214.                         $rcdPerson->setFlagRecurringDonation(true);
  215.                     }
  216.                 } else {
  217.                     if($rcdPerson->getFlagRecurringDonation()){
  218.                         $rcdPerson->setFlagRecurringDonation(true);
  219.                     } else {
  220.                         $rcdPerson->setFlagRecurringDonation(false);
  221.                     }
  222.                 }
  223.                 $rcdPerson
  224.                     ->setFlagTypePerson(true)
  225.                 ;
  226.             }else{
  227.                 if($type == 'dnr'){
  228.                     if($rcdPerson->getFlagRecurringDonation()){
  229.                         $rcdPerson->setFlagRecurringDonation(true);
  230.                     } else {
  231.                         $rcdPerson->setFlagRecurringDonation(false);
  232.                     }
  233.                 } else {
  234.                     if($rcdPerson->getFlagRecurringDonation()){
  235.                         $rcdPerson->setFlagRecurringDonation(true);
  236.                     } else {
  237.                         $rcdPerson->setFlagRecurringDonation(false);
  238.                     }
  239.                 }
  240.                 $rcdPerson
  241.                     ->setFlagTypePerson(false)
  242.                 ;
  243.             }
  244.             if($rcdPerson->getFlagPay()){
  245.                 $rcdPerson->setFlagPay(true);
  246.             } else {
  247.                 $rcdPerson->setFlagPay(false);
  248.             }
  249.             $em->persist($appUser);
  250.             $em->flush();
  251.             if($newUser){
  252.                 $this->appTools->sendMail($appUser$password);
  253.             }
  254.             $data $request->get('form');
  255.             $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  256.             #####
  257.             ## Guardar donacion
  258.             #####
  259.             if($type == 'dnu'){
  260.                 $idUser $appUser->getId();
  261.                 $idRcDonation $this->registerFirstDonation($appUser$data['amount'], $token$idProject);
  262.                 $newTransaction $this->newTransactionWebpayPlus($idUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  263.                 return $this->render('index/TransBank/payWebPayPlus.html.twig', [
  264.                     'url' => $newTransaction['url'],
  265.                     'token' => $newTransaction['token'],
  266.                     'rcdOrganization' => $rcdOrganization
  267.                 ]);
  268.             } elseif($type == 'dnp') {
  269.                 if($rcdPerson->getNumberQuota() == 1){
  270.                     $idUser $appUser->getId();
  271.                     $idRcDonation $this->registerFirstDonation($appUser$data['amount'], $token$idProject);
  272.                     $newTransaction $this->newTransactionWebpayPlus($idUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  273.                     return $this->render('index/TransBank/payWebPayPlus.html.twig', [
  274.                         'url' => $newTransaction['url'],
  275.                         'token' => $newTransaction['token'],
  276.                         'rcdOrganization' => $rcdOrganization
  277.                     ]);
  278.                 } else {
  279.                     $amount round($data['amount'] / $rcdPerson->getNumberQuota(),0);
  280.                     $idRcDonation $this->registerFirstDonation($appUser$amount$token$idProject);
  281.                     $newTransaction $this->newInscriptionOneClickMall($appUser$rcdOrganization$amount$idProject,$idRcDonation$newUser);
  282.                     return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
  283.                         'url' => $newTransaction['url'],
  284.                         'token' => $newTransaction['token'],
  285.                         'rcdOrganization' => $rcdOrganization
  286.                     ]);
  287.                 }
  288.             } else {
  289.                 $idRcDonation $this->registerFirstDonation($appUser$data['amount'], $token$idProject);
  290.                 $newTransaction $this->newInscriptionOneClickMall($appUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  291.                 return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
  292.                     'url' => $newTransaction['url'],
  293.                     'token' => $newTransaction['token'],
  294.                     'rcdOrganization' => $rcdOrganization
  295.                 ]);
  296.             }
  297.         }
  298.         return $this->render('index/_formNatural.html.twig', [
  299.             'form' => $form->createView(),
  300.             'idProject' => $idProject,
  301.             'rcdOrganization' => $rcdOrganization
  302.         ]);
  303.     }
  304.     /**
  305.      * @Route("/personLegal", name="index_personLegal", methods={"POST","GET"})
  306.      */
  307.     public function personLegal(Request $requestUserPasswordEncoderInterface $encoderFileUploader $fileUploader, \Swift_Mailer $mailer): Response
  308.     {
  309.         $em $this->getDoctrine()->getManager();
  310.         $token $request->get('rcdOrganization');
  311.         $type $request->get('type');
  312.         $idProject $request->get('project');
  313.         $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  314.         $formSubmit $request->request->get('form');
  315.         if($this->getUser()){
  316.             $appUser $em->getRepository(AppUser::class)->find($this->getUser()->getId());
  317.         }elseif(isset($formSubmit['email'])){
  318.             $appUser $em->getRepository(AppUser::class)->loadUserByUsername($formSubmit['email']);
  319.             if(!$appUser){
  320.                 $appUser = new AppUser();
  321.             }
  322.         }else{
  323.             $appUser = new AppUser();
  324.         }
  325.         $form $this->createForm(RcdUserLegalType::class, $appUser, [
  326.             'action'    => $this->generateUrl('index_personLegal', [
  327.                 'rcdOrganization' => $token,
  328.                 'type' => $type,
  329.                 'project' => $idProject
  330.             ]),
  331.             'attr'      => [
  332.                 'id'    => 'form',
  333.                 'method'    => 'POST',
  334.                 'autocomplete' => 'off',
  335.             ]
  336.         ]);
  337.         $form->handleRequest($request);
  338.         if ($form->isSubmitted() && $form->isValid()) {
  339.             $newUser true;
  340.             if($appUser->getId()){
  341.                 $newUser false;
  342.             } else {
  343.                 $username explode("@"$appUser->getEmail());
  344.                 $appUser_userName $em->getRepository(AppUser::class)->loadUserByUsername($username[0]);
  345.                 if($appUser_userName){
  346.                     $username $username[0].uniqid();
  347.                 } else {
  348.                     $username $username[0];
  349.                 }
  350.                 $appUser->setSalt(md5(time()));
  351.                 $password substr($appUser->getSalt(), 05);
  352.                 $encoded $encoder->encodePassword($appUser$password);
  353.                 $appUser->setPassword($encoded);
  354.                 $appProfile $em->getRepository(AppProfile::class)->findOneBy([
  355.                     'code' => 'DON'
  356.                 ]);
  357.                 $appUser
  358.                     ->setUsername($username)
  359.                     ->setCreationDate(new \DateTime())
  360.                     ->setFlagDelete(false)
  361.                     ->setFlagStatus(true)
  362.                     ->setFlagAccess(true)
  363.                     ->setAppProfile($appProfile)
  364.                 ;
  365.             }
  366.             $rcdPerson $appUser->getRcdPerson();
  367.             if($rcdPerson->getBusinessName()){
  368.                 if($type == 'dnr'){
  369.                     if($rcdPerson->getFlagRecurringDonation()){
  370.                         $rcdPerson->setFlagRecurringDonation(true);
  371.                     } else {
  372.                         $rcdPerson->setFlagRecurringDonation(true);
  373.                     }
  374.                 } else {
  375.                     if($rcdPerson->getFlagRecurringDonation()){
  376.                         $rcdPerson->setFlagRecurringDonation(true);
  377.                     } else {
  378.                         $rcdPerson->setFlagRecurringDonation(false);
  379.                     }
  380.                 }
  381.                 $rcdPerson
  382.                     ->setFlagTypePerson(true)
  383.                 ;
  384.             }else{
  385.                 if($type == 'dnr'){
  386.                     if($rcdPerson->getFlagRecurringDonation()){
  387.                         $rcdPerson->setFlagRecurringDonation(true);
  388.                     } else {
  389.                         $rcdPerson->setFlagRecurringDonation(false);
  390.                     }
  391.                 } else {
  392.                     if($rcdPerson->getFlagRecurringDonation()){
  393.                         $rcdPerson->setFlagRecurringDonation(true);
  394.                     } else {
  395.                         $rcdPerson->setFlagRecurringDonation(false);
  396.                     }
  397.                 }
  398.                 $rcdPerson
  399.                     ->setFlagTypePerson(false)
  400.                 ;
  401.             }
  402.             if($rcdPerson->getFlagPay()){
  403.                 $rcdPerson->setFlagPay(true);
  404.             } else {
  405.                 $rcdPerson->setFlagPay(false);
  406.             }
  407.             $em->persist($appUser);
  408.             $em->flush();
  409.             if($newUser){
  410.                 $this->appTools->sendMail($appUser$password);
  411.             }
  412.             $data $request->get('form');
  413.             $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  414.             #####
  415.             ## Guardar donacion
  416.             #####
  417.             if($type == 'dnu'){
  418.                 $idUser $appUser->getId();
  419.                 $idRcDonation $this->registerFirstDonation($appUser$data['amount'], $token$idProject);
  420.                 $newTransaction $this->newTransactionWebpayPlus($idUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  421.                 return $this->render('index/TransBank/payWebPayPlus.html.twig', [
  422.                     'url' => $newTransaction['url'],
  423.                     'token' => $newTransaction['token'],
  424.                     'rcdOrganization' => $rcdOrganization
  425.                 ]);
  426.             }  elseif($type == 'dnp') {
  427.                 if($rcdPerson->getNumberQuota() == 1){
  428.                     $idUser $appUser->getId();
  429.                     $idRcDonation $this->registerFirstDonation($appUser$data['amount'], $token$idProject);
  430.                     $newTransaction $this->newTransactionWebpayPlus($idUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  431.                     return $this->render('index/TransBank/payWebPayPlus.html.twig', [
  432.                         'url' => $newTransaction['url'],
  433.                         'token' => $newTransaction['token'],
  434.                         'rcdOrganization' => $rcdOrganization
  435.                     ]);
  436.                 }else{
  437.                     $amount round($data['amount'] / $rcdPerson->getNumberQuota(),0);
  438.                     $idRcDonation $this->registerFirstDonation($appUser$amount$token$idProject);
  439.                     $newTransaction $this->newInscriptionOneClickMall($appUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  440.                     return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
  441.                         'url' => $newTransaction['url'],
  442.                         'token' => $newTransaction['token'],
  443.                         'rcdOrganization' => $rcdOrganization
  444.                     ]);
  445.                 }
  446.             } else {
  447.                 $idRcDonation $this->registerFirstDonation($appUser$data['amount'], $token$idProject);
  448.                 $newTransaction $this->newInscriptionOneClickMall($appUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  449.                 return $this->render('index/TransBank/inscriptionOneClickMall.html.twig', [
  450.                     'url' => $newTransaction['url'],
  451.                     'token' => $newTransaction['token'],
  452.                     'rcdOrganization' => $rcdOrganization
  453.                 ]);
  454.             }
  455.         }
  456.         return $this->render('index/_formLegal.html.twig', [
  457.             'form' => $form->createView(),
  458.             'idProject' => $idProject,
  459.             'rcdOrganization' => $rcdOrganization
  460.         ]);
  461.     }
  462.     /**
  463.      * @Route("/personAnonymous", name="index_personAnonymous", methods={"POST","GET"})
  464.      */
  465.     public function personAnonymous(Request $requestUserPasswordEncoderInterface $encoderFileUploader $fileUploader, \Swift_Mailer $mailer): Response
  466.     {
  467.         $em $this->getDoctrine()->getManager();
  468.         $token $request->get('rcdOrganization');
  469.         $type $request->get('type');
  470.         $idProject $request->get('project');
  471.         $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  472.         $formSubmit $request->request->get('form');
  473.         if($this->getUser()){
  474.             $appUser $em->getRepository(AppUser::class)->find($this->getUser()->getId());
  475.             $email $appUser->getEmail();
  476.         }elseif(isset($formSubmit['email'])){
  477.             $appUser $em->getRepository(AppUser::class)->loadUserByUsername($formSubmit['email']);
  478.             $email $appUser->getEmail();
  479.             if(!$appUser){
  480.                 $appUser = new AppUser();
  481.                 $email 'anonimo@cloudbase.cl';
  482.             }
  483.         }else{
  484.             $appUser = new AppUser();
  485.             $email 'anonimo@cloudbase.cl';
  486.         }
  487.         $form $this->createForm(RcdUserAnonymousType::class, $appUser, [
  488.             'action'    => $this->generateUrl('index_personAnonymous', [
  489.                 'rcdOrganization' => $token,
  490.                 'type' => $type,
  491.                 'project' => $idProject
  492.             ]),
  493.             'attr'      => [
  494.                 'id'    => 'form',
  495.                 'method'    => 'POST',
  496.                 'autocomplete' => 'off',
  497.             ]
  498.         ]);
  499.         $form->handleRequest($request);
  500.         if ($form->isSubmitted() && $form->isValid()) {
  501.             $newUser true;
  502.             if($appUser->getId()){
  503.                 $newUser false;
  504.             } else {
  505.                 $username explode("@"$appUser->getEmail());
  506.                 $appUser_userName $em->getRepository(AppUser::class)->loadUserByUsername($username[0]);
  507.                 if($appUser_userName){
  508.                     $username $username[0].uniqid();
  509.                 } else {
  510.                     $username $username[0];
  511.                 }
  512.                 $appUser->setSalt(md5(time()));
  513.                 $password substr($appUser->getSalt(), 05);
  514.                 $encoded $encoder->encodePassword($appUser$password);
  515.                 $appUser->setPassword($encoded);
  516.                 $appProfile $em->getRepository(AppProfile::class)->findOneBy([
  517.                     'code' => 'DON'
  518.                 ]);
  519.                 $appUser
  520.                     ->setName('Anónimo')
  521.                     ->setSurname(null)
  522.                     ->setUsername($username)
  523.                     ->setCreationDate(new \DateTime())
  524.                     ->setFlagDelete(false)
  525.                     ->setFlagStatus(true)
  526.                     ->setFlagAccess(true)
  527.                     ->setAppProfile($appProfile)
  528.                 ;
  529.             }
  530.             $em->persist($appUser);
  531.             $em->flush();
  532.             if($newUser){
  533.                 $this->appTools->sendMail($appUser$password);
  534.             }
  535.             if($type != 'dnr'){
  536.                 $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  537.                 $data $request->get('form');
  538.                 $idUser $appUser->getId();
  539.                 #####
  540.                 ## Guardar donacion
  541.                 #####
  542.                 $idRcDonation $this->registerFirstDonation($appUser$data['amount'], $token$idProject);
  543.                 $newTransaction $this->newTransactionWebpayPlus($idUser$rcdOrganization$data['amount'], $idProject,$idRcDonation$newUser);
  544.                 return $this->render('index/TransBank/payWebPayPlus.html.twig', [
  545.                     'url' => $newTransaction['url'],
  546.                     'token' => $newTransaction['token'],
  547.                     'rcdOrganization' => $rcdOrganization
  548.                 ]);
  549.             }
  550.         }
  551.         return $this->render('index/_formAnonymous.html.twig', [
  552.             'form' => $form->createView(),
  553.             'rcdOrganization' => $rcdOrganization,
  554.             'email' => $email
  555.         ]);
  556.     }
  557.     /**
  558.      * @Route("/", name="index")
  559.      */
  560.     public function index(Request $request): Response
  561.     {
  562.         $token $request->get('token');
  563.         if($token){
  564.             $em $this->getDoctrine()->getManager();
  565.             $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  566.             if($rcdOrganization){
  567.                 $appUser = new AppUser();
  568.                 return $this->render('index/index.html.twig', [
  569.                     'rcdOrganization' => $rcdOrganization
  570.                 ]);
  571.             } else {
  572.                 dump('la organizacion a la que esta intentando acceder no existe');
  573.                 exit();
  574.             }
  575.         } else {
  576.             dump('es necesario usar el token para poder acceder');
  577.             exit();
  578.         }
  579.     }
  580.     private function debug($idOrganization$var_dump$exit false)
  581.     {
  582.         if($idOrganization == 4) {
  583.             dump($var_dump);
  584.             if ($exit) {
  585.                 exit();
  586.             }
  587.         }
  588.     }
  589.     /**
  590.      * @Route("/privacy", name="index_privacy")
  591.      */
  592.     public function privacy(Request $request): Response
  593.     {
  594.         $token $request->get('token');
  595.         if($token){
  596.             $em $this->getDoctrine()->getManager();
  597.             $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token'=>$token]);
  598.             if($rcdOrganization){
  599.                 $appUser = new AppUser();
  600.                 return $this->render('index/indexPrivacy.html.twig', [
  601.                     'rcdOrganization' => $rcdOrganization
  602.                 ]);
  603.             } else {
  604.                 dump('la organizacion a la que esta intentando acceder no existe');
  605.                 exit();
  606.             }
  607.         } else {
  608.             dump('es necesario usar el token para poder acceder');
  609.             exit();
  610.         }
  611.     }
  612.     /**
  613.      * @Route("/responseWebPayPlus", name="index_responseWebPayPlus")
  614.      */
  615.     public function responseWebPayPlus(Request $request, \Swift_Mailer $mailer): Response
  616.     {
  617.         $token $request->get('token');
  618.         $idProject $request->get('project');
  619.         $idRcDonation $request->get('rcDonation');
  620.         $idUser $request->get('user');
  621.         $newUser $request->get('newUser');
  622.         $token_ws $request->get('token_ws');
  623.         $em $this->getDoctrine()->getManager();
  624.         $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token' => $token]);
  625.         if($token_ws) {
  626.             $appUser $em->getRepository(AppUser::class)->find($idUser);
  627.             $rcdProject null;
  628.             if ($idProject) {
  629.                 $rcdProject $em->getRepository(RcdProject::class)->find($idProject);
  630.             }
  631.             if($rcdOrganization->getWebPayPlusType()) {
  632.                 $response $this->serviceWebpayPlus->responseTransactionMall($request->get('token_ws'), $rcdOrganization);
  633.                 $amount $response['details'][0]['amount'];
  634.                 $buyOrder $response['buyOrder'];
  635.                 $authorizationCode $response['details'][0]['authorizationCode'];
  636.                 $responseCode $response['details'][0]['responseCode'];
  637.                 $paymentTypeCode $response['details'][0]['paymentTypeCode'];
  638.                 $vci $response['vci'];
  639.                 $status $response['details'][0]['status'];
  640.                 $responseCodeText $response['details'][0]['responseCodeText'];
  641.             } else {
  642.                 $response $this->serviceWebpayPlus->responseTransaction($request->get('token_ws'), $rcdOrganization);
  643.                 $amount $response['amount'];
  644.                 $buyOrder $response['buyOrder'];
  645.                 $authorizationCode $response['authorizationCode'];
  646.                 $responseCode $response['responseCode'];
  647.                 $paymentTypeCode $response['paymentTypeCode'];
  648.                 $vci $response['vci'];
  649.                 $status $response['status'];
  650.                 $responseCodeText $response['responseCodeText'];
  651.             }
  652.             $rcdLogs = new RcdLogsDonations();
  653.             $rcdLogs
  654.                 ->setCreationDate(new \DateTime())
  655.                 ->setAppUser($appUser)
  656.                 ->setDescription(json_encode($response))
  657.             ;
  658.             $em->persist($rcdLogs);
  659.             $em->flush();
  660.             $rcdDonate $em->getRepository(RcdDonation::class)->find($idRcDonation);
  661.             $rcdDonate
  662.                 ->setCode($buyOrder)
  663.                 ->setToken($token_ws)
  664.                 ->setAuthorizationCode($authorizationCode)
  665.                 ->setResponseCode($responseCode)
  666.                 ->setPaymentTypeCode($paymentTypeCode)
  667.                 ->setVci($vci)
  668.                 ->setStatusTransaction($status)
  669.                 ->setFlagRecurringDonation(false)
  670.             ;
  671.             $rcdInvoice $rcdDonate->getRcdInvoice();
  672.             if($responseCode != 0) {
  673.                 $rcdInvoice
  674.                     ->setStatusTransaction('ER')
  675.                 ;
  676.             } else {
  677.                 $rcdInvoice
  678.                     ->setStatusTransaction('OK')
  679.                 ;
  680.             }
  681.             $rcdInvoice->setFlagFlagType(false);
  682.             $em->persist($rcdDonate);
  683.             $em->persist($rcdInvoice);
  684.             $em->flush();
  685.             if($newUser == 'yes'){
  686.                 if($responseCode == 0){
  687.                     $this->appTools->sendMailDonationNoRecurrence($appUser$rcdDonate->getAmount());
  688.                 } else {
  689.                     $this->appTools->sendMailDonationError($appUser);
  690.                 }
  691.             } else {
  692.                 if($responseCode != 0){
  693.                     $this->appTools->sendMailDonationError($appUser);
  694.                 }
  695.             }
  696.             return $this->redirectToRoute('index_responseHtml',[
  697.                 'codeResponse' => $responseCode,
  698.                 'buyOrder' => $buyOrder,
  699.                 'amount' => $amount,
  700.                 'rcdDonateDate' => $rcdDonate->getCreationDate()->format('d-m-Y'),
  701.                 'idOrganization' => $rcdOrganization->getId(),
  702.                 'idRcdProject' => $idProject
  703.             ]);
  704.         } else {
  705.             return $this->redirectToRoute('index',['token'=>$token]);
  706.         }
  707.     }
  708.     /**
  709.      * @Route("/responseOneClickMall", name="index_responseOneClickMall")
  710.      */
  711.     public function responseOneClickMall(Request $request, \Swift_Mailer $mailer): Response
  712.     {
  713.         $token $request->get('token');
  714.         $decryp json_decode(base64_decode($token), true);
  715.         $token $decryp['token'];
  716.         $idUser $decryp['user'];
  717.         $newUser $decryp['newUser'];
  718.         $idProject $decryp['project'];
  719.         $idRcDonation $decryp['rcDonation'];
  720.         $mount $decryp['mount'];
  721.         $token_tbk $request->get('TBK_TOKEN');
  722.         $em $this->getDoctrine()->getManager();
  723.         $rcdOrganization $em->getRepository(RcdOrganization::class)->findOneBy(['token' => $token]);
  724.         $response $this->serviceOneclickMall->confirmInscription($token_tbk,$rcdOrganization);
  725.         if($response['tbkUser']) {
  726.             $appUser $em->getRepository(AppUser::class)->find($idUser);
  727.             $numberQuota $appUser->getRcdPerson()->getNumberQuota();
  728.             $rcdProject null;
  729.             if ($idProject) {
  730.                 $rcdProject $em->getRepository(RcdProject::class)->find($idProject);
  731.             }
  732.             $rcdQuota false;
  733.             if($numberQuota 1){
  734.                 $numberQuota $numberQuota-1;
  735.                 $date = new \DateTime();
  736.                 $dateNewDonation $date->add(new \DateInterval("P{$numberQuota}M"));
  737.                 $rcdQuota = new RcdQuota();
  738.                 $rcdQuota
  739.                     ->setNumberQuota($appUser->getRcdPerson()->getNumberQuota())
  740.                     ->setAppUser($appUser)
  741.                     ->setAmount($mount)
  742.                     ->setTbkUser($response['tbkUser'])
  743.                     ->setDate(new \DateTime())
  744.                     ->setDateFin($dateNewDonation)
  745.                     ->setRcdOrganization($rcdOrganization)
  746.                     ->setRcdProject($rcdProject)
  747.                     ->setFlagFinish(false)
  748.                 ;
  749.                 $em->persist($rcdQuota);
  750.                 $buyOrder str_pad($idRcDonation.date('YmdHis'), 260STR_PAD_LEFT);
  751.                 $response $this->serviceOneclickMall->newTransaction($appUser->getUsername(), $rcdQuota->getTbkUser(), $buyOrder, [
  752.                     [
  753.                         "commerce_code" => $rcdOrganization->getOneClickMallCommerceCodeSell(),
  754.                         "buy_order" => $buyOrder,
  755.                         "amount" => $mount,
  756.                         "installments_number" => 0
  757.                     ],
  758.                 ],$rcdOrganization);
  759.                 $rcdLogs = new RcdLogsDonations();
  760.                 $rcdLogs
  761.                     ->setCreationDate(new \DateTime())
  762.                     ->setAppUser($appUser)
  763.                     ->setDescription(json_encode($response))
  764.                 ;
  765.                 $em->persist($rcdLogs);
  766.                 $em->flush();
  767.                 $rcdDonate $em->getRepository(RcdDonation::class)->find($idRcDonation);
  768.                 $rcdDonate
  769.                     ->setCode($response['details'][0]['buyOrder'])
  770.                     ->setToken($response['details'][0]['commerceCode'])
  771.                     ->setAuthorizationCode($response['details'][0]['authorizationCode'])
  772.                     ->setResponseCode($response['details'][0]['responseCode'])
  773.                     ->setPaymentTypeCode($response['details'][0]['paymentTypeCode'])
  774.                     ->setStatusTransaction($response['details'][0]['status'])
  775.                 ;
  776.                 if ($appUser->getRcdPerson()->getNumberQuota() == 1) {
  777.                     $rcdDonate->setFlagRecurringDonation(true);
  778.                 } else {
  779.                     $rcdDonate->setFlagRecurringDonation(false);
  780.                 }
  781.                 $rcdInvoice $rcdDonate->getRcdInvoice();
  782.                 if($response['details'][0]['responseCode'] != 0) {
  783.                     $rcdInvoice
  784.                         ->setStatusTransaction('ER')
  785.                     ;
  786.                 } else {
  787.                     $rcdInvoice
  788.                         ->setStatusTransaction('OK')
  789.                     ;
  790.                 }
  791.                 $em->persist($rcdDonate);
  792.                 if($rcdDonate->getResponseCode() == 0) {
  793.                     $rcdInvoice->setRcdQuota($rcdQuota);
  794.                 }
  795.                 $em->persist($rcdInvoice);
  796.                 $em->flush();
  797.                 if($rcdDonate->getResponseCode() != 0) {
  798.                     if($rcdQuota){
  799.                         $em->remove($rcdQuota);
  800.                         $em->flush();
  801.                     } else {
  802.                         $rcdPerson $appUser->getRcdPerson();
  803.                         $rcdPerson
  804.                             ->setFlagRecurringDonation(false)
  805.                             ->setDateRecurringDonation(null)
  806.                             ->setAmountRecurringDonation(0)
  807.                         ;
  808.                         $em->persist($rcdPerson);
  809.                         $em->flush();
  810.                     }
  811.                 }
  812.                 if($newUser == 'yes'){
  813.                     if($rcdDonate->getResponseCode() == 0){
  814.                         $this->appTools->sendMailDonationRecurrence($appUser$rcdDonate->getAmount(), $appUser->getRcdPerson()->getDateRecurringDonation()->format('d'));
  815.                     } else {
  816.                         $this->appTools->sendMailDonationError($appUser);
  817.                     }
  818.                 } else {
  819.                     if($rcdDonate->getResponseCode() != 0){
  820.                         $this->appTools->sendMailDonationError($appUser);
  821.                     }
  822.                 }
  823.                 return $this->redirectToRoute('index_responseHtml',[
  824.                     'codeResponse' => $response['details'][0]['responseCode'],
  825.                     'buyOrder' => $response['details'][0]['buyOrder'],
  826.                     'amount' => $response['details'][0]['amount'],
  827.                     'rcdDonateDate' => $rcdDonate->getCreationDate()->format('d-m-Y'),
  828.                     'idOrganization' => $rcdOrganization->getId(),
  829.                     'idRcdProject' => $idProject
  830.                 ]);
  831.             } else {
  832.                 $dayDonations $rcdOrganization->getPayDay();
  833.                 $date = new \DateTime(date('Y-m-').$dayDonations);
  834.                 if($date->format('Y-m-d') < date('Y-m-d')){
  835.                     $date $date->add(new \DateInterval("P1M"));
  836.                 }
  837.                 $appUser
  838.                     ->setRcdOrganization($rcdOrganization)
  839.                 ;
  840.                 $rcdPerson $appUser->getRcdPerson();
  841.                 $rcdPerson
  842.                     ->setTbkUser($response['tbkUser'])
  843.                     ->setTypeCard($response['cardType'])
  844.                     ->setNumberCard($response['cardNumber'])
  845.                     ->setFlagRecurringDonation(true)
  846.                     ->setDateRecurringDonation($date)
  847.                     ->setAmountRecurringDonation($mount);
  848.                 $em->persist($appUser);
  849.                 $em->persist($rcdPerson);
  850.                 $rcdUserBkToken = new RcdUserBkToken();
  851.                 $rcdUserBkToken
  852.                     ->setFlagFlagDelete(false)
  853.                     ->setAppUser($appUser)
  854.                     ->setRcdOrganization($rcdOrganization)
  855.                     ->setAmountRecurringDonation($mount)
  856.                     ->setDateRecurringDonation($date)
  857.                     ->setTbkUser($response['tbkUser'])
  858.                     ->setTypeCard($response['cardType'])
  859.                     ->setNumberCard($response['cardNumber'])
  860.                 ;
  861.                 $em->persist($rcdUserBkToken);
  862.                 $rcdDonate $em->getRepository(RcdDonation::class)->find($idRcDonation);
  863.                 $rcdInvoice $rcdDonate->getRcdInvoice();
  864.                 $em->remove($rcdDonate);
  865.                 $em->remove($rcdInvoice);
  866.                 $em->flush();
  867.                 return $this->redirectToRoute('index_responseHtml2',[
  868.                     'amount' => $mount,
  869.                     'rcdDonateDate' => $date->format('d-m-Y'),
  870.                     'idOrganization' => $rcdOrganization->getId()
  871.                 ]);
  872.             }
  873.         } else {
  874.             return $this->redirectToRoute('index',['token'=>$token]);
  875.         }
  876.     }
  877.     /**
  878.      * @Route("/responseHtml/{idOrganization}/{codeResponse}/{buyOrder}/{amount}/{rcdDonateDate}/{idRcdProject}", name="index_responseHtml")
  879.      */
  880.     public function responseHtml($idOrganization$codeResponse$buyOrder$amount$rcdDonateDate$idRcdProject): Response
  881.     {
  882.         $em $this->getDoctrine()->getManager();
  883.         $rcdOrganization $em->getRepository(RcdOrganization::class)->find($idOrganization);
  884.         $rcdProject $em->getRepository(RcdProject::class)->find($idRcdProject);
  885.         if(isset($this->responseCode[$codeResponse])){
  886.             $responseCode $this->responseCode[$codeResponse];
  887.         } else {
  888.             $responseCode 'error interno';
  889.         }
  890.         return $this->render('index/TransBank/responseHtmlWeb.html.twig', [
  891.             'responseCodeText' => $responseCode,
  892.             'codeResponse' => $codeResponse,
  893.             'buyOrder' => $buyOrder,
  894.             'amount' => $amount,
  895.             'date' => $rcdDonateDate,
  896.             'rcdOrganization' => $rcdOrganization,
  897.             'rcdProject' => $rcdProject,
  898.         ]);
  899.     }
  900.     /**
  901.      * @Route("/responseHtml2/{idOrganization}/{amount}/{rcdDonateDate}", name="index_responseHtml2")
  902.      */
  903.     public function responseHtml2($idOrganization$amount$rcdDonateDate): Response
  904.     {
  905.         $em $this->getDoctrine()->getManager();
  906.         $rcdOrganization $em->getRepository(RcdOrganization::class)->find($idOrganization);
  907.         return $this->render('index/TransBank/responseHtmlWeb2.html.twig', [
  908.             'amount' => $amount,
  909.             'date' => $rcdDonateDate,
  910.             'rcdOrganization' => $rcdOrganization
  911.         ]);
  912.     }
  913.     /**
  914.      * @Route("/test", name="index_test")
  915.      */
  916.     public function test(Request $request): Response
  917.     {
  918.         $em $this->getDoctrine()->getManager();
  919.         $appUser $em->getRepository(AppUser::class)->find(1);
  920.         $rcdInvoice $em->getRepository(RcdInvoice::class)->find(105);
  921.         $this->appTools->sendMailDonationsError($rcdInvoice$appUser);
  922.         exit();
  923.     }
  924. }