<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Payum\PayumHipay\Action;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Payum;
use Payum\Core\Request\Generic;
use Payum\Core\Request\GetStatusInterface;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\HttpRequestVerifierInterface;
use Payum\Core\Security\TokenInterface;
use Sylius\Bundle\PayumBundle\Factory\GetStatusFactoryInterface;
use Sylius\Bundle\PayumBundle\Factory\ResolveNextRouteFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Order\Repository\OrderRepositoryInterface;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Request\Capture;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;
class AfterCaptureAction implements ActionInterface
{
private $container;
public function __construct(
private Payum $payum,
private OrderRepositoryInterface $orderRepository,
private MetadataInterface $orderMetadata,
private RequestConfigurationFactoryInterface $requestConfigurationFactory,
private ViewHandlerInterface $viewHandler,
private RouterInterface $router,
private GetStatusFactoryInterface $getStatusRequestFactory,
private ResolveNextRouteFactoryInterface $resolveNextRouteRequestFactory,
private LoggerInterface $logger,
ContainerInterface $container
) {
$this->container = $container;
}
private function getHttpRequestVerifier(): HttpRequestVerifierInterface
{
return $this->payum->getHttpRequestVerifier();
}
public function execute($request)
{
dump($request);
exit();
}
/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Capture &&
$request->getModel() instanceof \ArrayAccess;
}
public function afterCaptureAction(Request $request): Response {
$configuration = $this->requestConfigurationFactory->create($this->orderMetadata, $request);
$token = $this->getHttpRequestVerifier()->verify($request);
$this->logger->debug("token : ".print_r($token, true));
// $this->payLog($token);
/** @var Generic&GetStatusInterface $status */
$status = $this->getStatusRequestFactory->createNewWithModel($token);
$this->logger->debug("status : ".print_r($status, true));
$this->payum->getGateway($token->getGatewayName())->execute($status);
$this->logger->debug("status getValue : ".print_r($status->getValue(), true));
$resolveNextRoute = $this->resolveNextRouteRequestFactory->createNewWithModel($status->getFirstModel());
$this->payum->getGateway($token->getGatewayName())->execute($resolveNextRoute);
// $this->logger->debug(print_r($resolveNextRoute, true));
$this->getHttpRequestVerifier()->invalidate($token);
if (PaymentInterface::STATE_NEW !== $status->getValue()) {
/** @var FlashBagInterface $flashBag */
$flashBag = $request->getSession()->getBag('flashes');
$flashBag->add('info', sprintf('sylius.payment.%s', $status->getValue()));
}
return new RedirectResponse($this->router->generate($resolveNextRoute->getRouteName(), $resolveNextRoute->getRouteParameters()));
}
// protected function payLog($data) {
// // $log = "Cart id - ".$cartId;
// // $log .= print_r();;
// $logger = $this->container->get('monolog.logger.hipaydebug');
// // $logger = new Logger('paydebug');
// // $logger->debug($log);
// $logger->debug(print_r($data, true));
// //
// // dump($data);
// }
}