<?php
namespace App\Controller;
use App\Entity\PaymentIntention;
use App\Form\PaymentIntentionType;
use App\Repository\PaymentIntentionRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/admin/payment_intention")
*/
class PaymentIntentionController extends AbstractController
{
/**
* @Route("/", name="app_payment_intention_index", methods={"GET"})
*/
public function index(PaymentIntentionRepository $paymentIntentionRepository): Response
{
return $this->render('payment_intention/index.html.twig', [
'payment_intentions' => $paymentIntentionRepository->findAll(),
]);
}
/**
* @Route("/paymen_json", name="app_payment_intention_index_json", methods={"GET"})
*/
public function indexJson(PaymentIntentionRepository $paymentIntentionRepository): Response
{
$paymentIntention=$paymentIntentionRepository->findAll();
return $this->json(["data"=>$paymentIntention]);
}
/**
* @Route("/{id}/process_payment_ajax", name="app_pago_ajax", methods={"POST"})
*/
public function pago_resend(Request $request, PaymentIntention $paymentIntention)
{
$pago=(object)[
'id' => $paymentIntention->getId(),
'referencia' => $paymentIntention->getReference(),
'moneda' => $paymentIntention->getMoneda(),
'paymentStatus' => $paymentIntention->getPaymentStatus(),
'promociones' => $paymentIntention->getPromociones(),
'xmlRequest' => $paymentIntention->getXmlRequest(),
'originalXmlRequest' => $paymentIntention->getOriginalXmlRequest(),
'originalXmlResponse' => $paymentIntention->getOriginalXmlResponse(),
'responseReceivedAt' => $paymentIntention->getResponseReceivedAt(),
'createAt' => $paymentIntention->getCreateAt(),
'updateAt' => $paymentIntention->getUpdateAt(),
'amount' => $paymentIntention->getAmount(),
'costumerService' => $paymentIntention->getConsumerWebService()->getID(),
];
$objeto = (object) [
'referencia' => $paymentIntention->getReference(),
'status' => $paymentIntention->getPaymentStatus(),
'pago' => $pago,
'response' => (array)simplexml_load_string($paymentIntention->getOriginalXmlResponse())
];
$url=$paymentIntention->getConsumerWebService()->getWebhook();
$client = HttpClient::create();
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode($objeto), // Convierte el array en JSON automáticamente
]);
return new JsonResponse(
[
'data' => $objeto,
],
JsonResponse::HTTP_ACCEPTED
);
}
/**
* @Route("/new", name="app_payment_intention_new", methods={"GET", "POST"})
*/
public function new(Request $request, PaymentIntentionRepository $paymentIntentionRepository): Response
{
$paymentIntention = new PaymentIntention();
$form = $this->createForm(PaymentIntentionType::class, $paymentIntention);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$paymentIntentionRepository->add($paymentIntention, true);
return $this->redirectToRoute('app_payment_intention_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('payment_intention/new.html.twig', [
'payment_intention' => $paymentIntention,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_payment_intention_show", methods={"GET"})
*/
public function show(PaymentIntention $paymentIntention): Response
{
return $this->render('payment_intention/show.html.twig', [
'payment_intention' => $paymentIntention,
]);
}
/**
* @Route("/{id}/edit", name="app_payment_intention_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, PaymentIntention $paymentIntention, PaymentIntentionRepository $paymentIntentionRepository): Response
{
$form = $this->createForm(PaymentIntentionType::class, $paymentIntention);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$paymentIntentionRepository->add($paymentIntention, true);
return $this->redirectToRoute('app_payment_intention_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('payment_intention/edit.html.twig', [
'payment_intention' => $paymentIntention,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_payment_intention_delete", methods={"POST"})
*/
public function delete(Request $request, PaymentIntention $paymentIntention, PaymentIntentionRepository $paymentIntentionRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$paymentIntention->getId(), $request->request->get('_token'))) {
$paymentIntentionRepository->remove($paymentIntention, true);
}
return $this->redirectToRoute('app_payment_intention_index', [], Response::HTTP_SEE_OTHER);
}
}