src/Controller/HomeController.php line 33

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