<?php
declare(strict_types=1);
namespace Abap\PaymentBundle\Controller;
use Abap\PaymentBundle\Handler\NotifyRequest\CompositeNotifyRequestHandler;
use Abap\PaymentBundle\Handler\NotifyRequest\NotifyRequestHandlerInterface;
use Abap\PaymentBundle\Handler\NotifyRequest\NotifyRequestHandlersRegistry;
use Payum\Core\Payum;
use Payum\Core\Request\Notify;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
final class NotifyController
{
private Payum $payum;
private CompositeNotifyRequestHandler $notifyRequestHandler;
public function __construct(
Payum $payum,
CompositeNotifyRequestHandler $notifyRequestHandler,
) {
$this->payum = $payum;
$this->notifyRequestHandler = $notifyRequestHandler;
}
public function notifyAction(Request $request): Response
{
$gatewayName = $request->get('gateway');
$gateway = $this->payum->getGateway($gatewayName);
try {
// if there is token in request, then use default behavior
$token = $this->payum->getHttpRequestVerifier()->verify($request);
$gateway->execute(new Notify($token));
} catch (NotFoundHttpException $e) {
// if not then use custom notify request handler
$this->notifyRequestHandler->handle($gatewayName);
}
return new Response('', 204);
}
}