<?php declare(strict_types=1);
namespace App\Payment\Controller;
use App\Framework\Controller\APIController;
use App\Payment\Service\MercadoPagoNotificationService;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
#[Route(path: '/api/payments/mp_notification')]
class PaymentMPNotificationController extends APIController
{
#[Route(path: '', methods: ['POST'])]
public function notifyPaymentAction(
MercadoPagoNotificationService $mpNotificationService,
LoggerInterface $logger
): JsonResponse {
$body = $this->getRequestBody();
$result = false;
// MercadoPago insists on sending us IPN notifications even when disabled...
if (!array_key_exists('type', $body)) {
return new JsonResponse(null, 200);
}
if ($body['type'] === 'payment' && $body['action'] === 'payment.created') {
$result = $mpNotificationService->notifyMercadoPointPaymentCreated($body['data']['id']);
} elseif ($body['type'] === 'payment' && $body['action'] === 'payment.updated') {
$result = $mpNotificationService->notifyMercadoPagoPaymentUpdated($body['data']['id']);
}
return new JsonResponse(null, $result ? 201 : 200);
}
}