src/Controller/HomeController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ConsumerWebService;
  4. use App\Entity\PaymentIntention;
  5. use App\Repository\ConsumerWebServiceRepository;
  6. use App\Repository\PaymentIntentionRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Nullix\CryptoJsAes\CryptoJsAesInterno;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpClient\HttpClient;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use Psr\Log\LoggerInterface;
  18. class HomeController extends AbstractController
  19. {
  20.     private $client;
  21.     private $logger;
  22.     public function __construct(HttpClientInterface $clientLoggerInterface $logger)
  23.     {
  24.         $this->client $client;
  25.         $this->logger $logger;
  26.         
  27.     }
  28.     /**
  29.      * @Route("/", name="app_index_home",methods={"GET"})
  30.      */
  31.     public function index_home(Request $request): Response
  32.     {
  33.         return $this->render('home/index.html.twig');
  34.     }
  35.     /**
  36.      * @Route("/get_link", name="app_home",methods={"POST"})
  37.      */
  38.     public function index(Request $requestPaymentIntentionRepository $paymentIntentionRepositoryEntityManagerInterface $entityManager): Response
  39.     {
  40.         $data_response json_decode(
  41.             $request->getContent(),
  42.             true
  43.         );
  44.         $datos_ad $data_response["datos_adicionales"];
  45.         $datos_adicionales "<datos_adicionales>";
  46.         $count 1;
  47.         foreach ($datos_ad as $item) {
  48.             $datos_adicionales .= '<data id="' $count '" display="true">
  49.                                   <label>' $item["label"] . '</label>
  50.                                   <value>' $item["value"] . '</value>
  51.                                 </data>';
  52.         }
  53.         $datos_adicionales .= "</datos_adicionales>";
  54.         $params = array(
  55.             'id_company' => $_ENV['ID_COMPANY_SANTANDER'],
  56.             'id_branch' => $_ENV['ID_BRANCH_SANTANDER'],
  57.             'user' => $_ENV['USER_SANTANDER'],
  58.             'passwd' => $_ENV['PASWORD_SANTANDER'],
  59.             'gen_url' => $_ENV['GEN_URL_SANTANDER'],
  60.             'key' => $_ENV['KEY_SANTANDER'],
  61.             'data0' => $_ENV['DATA0_SANTANDER']
  62.         );
  63.         $vigencia "";
  64.         $vigencia_response $data_response['fh_vigencia'] ?? null;
  65.         if ($vigencia_response !== null) {
  66.             $vigencia '<fh_vigencia>'.$data_response["fh_vigencia"].'</fh_vigencia>';
  67.         }
  68.         $originalString '<?xml version="1.0" encoding="UTF-8"?>
  69.                             <P>
  70.                             <business>
  71.                                 <id_company>'.$_ENV['ID_COMPANY_SANTANDER'].'</id_company>
  72.                                 <id_branch>'.$_ENV['ID_BRANCH_SANTANDER'].'</id_branch>
  73.                                 <user>'.$_ENV['USER_SANTANDER'].'</user>
  74.                                 <pwd>'.$_ENV['PASWORD_SANTANDER'].'</pwd>
  75.                             </business>
  76.                             <url>
  77.                                 <reference>'.$data_response["reference"].'</reference>
  78.                                 <amount>'.$data_response["amount"].'</amount>
  79.                                 <moneda>'.$data_response["moneda"].'</moneda>
  80.                                 <canal>W</canal>
  81.                                 <omitir_notif_default>0</omitir_notif_default>
  82.                                 <promociones>'.$data_response["promociones"].'</promociones>
  83.                                 <st_correo>1</st_correo>
  84.                                 ' $vigencia '
  85.                                 <version>IntegraWPP</version>
  86.                                 ' $datos_adicionales '
  87.                             </url>
  88.                             </P>';
  89.         $key $_ENV['KEY_SANTANDER'];
  90.         $encrypted \AESCrypto::encriptar($originalString$key);
  91.         $result $this->postWithFetch($encrypted$params);
  92.         $decrypted \AESCrypto::desencriptar($result$key);
  93.         $array json_decode(json_encode((array)simplexml_load_string($decrypted)), true);
  94.         // Obtén el repositorio de la entidad Producto
  95.         $webServiceRepository $entityManager->getRepository(ConsumerWebService::class);
  96.         // Utiliza findOneBy para buscar un producto por nombre
  97.         $web_service $webServiceRepository->findOneBy(['id' => $data_response["web_service"]]);
  98.         $paymentIntention = new PaymentIntention();
  99.         $paymentIntention->setConsumerWebService($web_service);
  100.         $paymentIntention->setPaymentStatus(0);
  101.         $paymentIntention->setXmlRequest($originalString);
  102.         $paymentIntention->setAmount($data_response["amount"]);
  103.         $paymentIntention->setMoneda($data_response["moneda"]);
  104.         $paymentIntention->setReference($data_response["reference"]);
  105.         $paymentIntention->setPromociones($data_response["promociones"]);
  106.         $paymentIntention->setFhVigencia(new \DateTime('now'));
  107.         $paymentIntention->setOriginalXmlRequest($decrypted);
  108.         $paymentIntention->setCreateAt(new \DateTime('now'));
  109.         $paymentIntention->setUpdateAt(new \DateTime('now'));
  110.         $paymentIntentionRepository->add($paymentIntentiontrue);
  111.         // error_log($decrypted);
  112.         $this->logger->info("decrypted" $originalString);
  113.         return new JsonResponse(
  114.             [
  115.                 'url' => $array["nb_url"],
  116.             ],
  117.             JsonResponse::HTTP_ACCEPTED
  118.         );
  119.     }
  120.     /**
  121.      * @Route("/process_payment", name="app_pago", methods={"POST"})
  122.      */
  123.     public function pago(Request $requestPaymentIntentionRepository $paymentIntentionRepositoryEntityManagerInterface $entityManagerSerializerInterface $serializer)
  124.     {
  125.         $key $_ENV['KEY_SANTANDER'];
  126.         $encryptedData $request->request->get('strResponse');
  127.         $decrypted \AESCrypto::desencriptar($encryptedData$key);
  128.         $response_XML json_decode(json_encode((array)simplexml_load_string($decrypted)), true);
  129.         // Obtén el repositorio de la entidad Producto
  130.         $paymentIntentionRepository $entityManager->getRepository(PaymentIntention::class);
  131.         // Utiliza findOneBy para buscar un producto por nombre
  132.         $paymentIntention $paymentIntentionRepository->findOneBy(['reference' => $response_XML["reference"]]);
  133.         $status 0;
  134.         if ($response_XML["response"] == "approved") {
  135.             $status 1;
  136.         } else if ($response_XML["response"] == "denied") {
  137.             $status 2;
  138.         } else if ($response_XML["response"] == "error") {
  139.             $status 3;
  140.         }
  141.         $paymentIntention->setPaymentStatus($status);
  142.         $paymentIntention->setOriginalXmlResponse($decrypted);
  143.         $paymentIntention->setResponseReceivedAt(new \DateTime('now'));
  144.         $paymentIntention->setUpdateAt(new \DateTime('now'));
  145.         $paymentIntentionRepository->add($paymentIntentiontrue);
  146.         $url $paymentIntention->getConsumerWebService()->getWebhook();
  147.         $pago = (object)[
  148.             'id' => $paymentIntention->getId(),
  149.             'referencia' => $paymentIntention->getReference(),
  150.             'moneda' => $paymentIntention->getMoneda(),
  151.             'paymentStatus' => $paymentIntention->getPaymentStatus(),
  152.             'promociones' => $paymentIntention->getPromociones(),
  153.             'xmlRequest' => $paymentIntention->getXmlRequest(),
  154.             'originalXmlRequest' => $paymentIntention->getOriginalXmlRequest(),
  155.             'originalXmlResponse' => $paymentIntention->getOriginalXmlResponse(),
  156.             'responseReceivedAt' => $paymentIntention->getResponseReceivedAt(),
  157.             'createAt' => $paymentIntention->getCreateAt(),
  158.             'updateAt' => $paymentIntention->getUpdateAt(),
  159.             'amount' => $paymentIntention->getAmount(),
  160.             'costumerService' => $paymentIntention->getConsumerWebService()->getID(),
  161.         ];
  162.         $objeto = (object) [
  163.             'referencia' => $response_XML["reference"],
  164.             'status' => $response_XML["response"],
  165.             'pago' => $pago,
  166.             'response' => (array)simplexml_load_string($decrypted)
  167.         ];
  168.         $client HttpClient::create();
  169.         $response $client->request('POST'$url, [
  170.             'headers' => [
  171.                 'Content-Type' => 'application/json',
  172.             ],
  173.             'body' => json_encode($objeto), // Convierte el array en JSON automáticamente
  174.         ]);
  175.         return new JsonResponse(
  176.             [
  177.                 'data' => "ok",
  178.             ],
  179.             JsonResponse::HTTP_ACCEPTED
  180.         );
  181.     }
  182.     function postWithFetch($encrypted$params)
  183.     {
  184.         $body http_build_query(array(
  185.             'xml' => "<pgs><data0>{$params['data0']}</data0><data>{$encrypted}</data></pgs>"
  186.         ));
  187.         $options = array(
  188.             'http' => array(
  189.                 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  190.                 'method' => 'POST',
  191.                 'content' => $body,
  192.                 'timeout' => 10// Tiempo máximo de espera
  193.             )
  194.         );
  195.         $context stream_context_create($options);
  196.         $result = @file_get_contents($params['gen_url'], false$context);
  197.         if ($result === FALSE) {
  198.             return ''// Manejar el error como lo desees
  199.         }
  200.         return $result;
  201.     }
  202. }