<?php
/*
* This file is part of the HipaySyliusPlugin
*
* (c) Smile <dirtech@smile.fr>
*
* 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\Event;
use App\Payum\PayumHipay\Context\PaymentContext;
use App\Payum\PayumHipay\Factory\HipayCardGatewayFactory;
use App\Payum\PayumHipay\Factory\HipayAlmaGatewayFactory;
use App\Payum\PayumHipay\Factory\HipayPaypalGatewayFactory;
use App\Payum\PayumHipay\Factory\HipayAppleGatewayFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
class HipayStoreTokenPaymentEventSubscriber implements EventSubscriberInterface
{
private const AUTHORIZED_ROUTE = [
'sylius_shop_checkout_select_payment',
'bitbag_sylius_one_page_checkout_shop_checkout_complete',
'sylius_shop_order_pay',
'sylius_shop_order_show',
'app_order_payment_link_complete'
];
private PaymentContext $paymentContext;
public function __construct(PaymentContext $paymentContext)
{
$this->paymentContext = $paymentContext;
}
public static function getSubscribedEvents()
{
return [
'kernel.controller' => 'handle',
];
}
public function handle(KernelEvent $event): void
{
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if (!in_array($route, self::AUTHORIZED_ROUTE)) {
return;
}
// dump( $route);
$hipayTokenCard = $request->request->get(
sprintf('%s_%s', PaymentContext::HIPAY_TOKEN, HipayCardGatewayFactory::FACTORY_NAME)
);
$hipayPaymentProductCard = $request->request->get(
sprintf('%s_%s', PaymentContext::HIPAY_PAYMENT_PRODUCT, HipayCardGatewayFactory::FACTORY_NAME)
);
if ($hipayTokenCard !== '') {
$token = $hipayTokenCard;
}
// dump($route);
// dump($request->request);
// dump($hipayPaymentProductCard);
if ($hipayPaymentProductCard !== '' && $hipayPaymentProductCard !== null) {
$paymentProduct = $hipayPaymentProductCard;
} else {
// exit();
$paymentProduct = $request->request->get(
sprintf('%s_%s', PaymentContext::HIPAY_PAYMENT_PRODUCT, HipayAlmaGatewayFactory::FACTORY_NAME)
) ?? null;
if ($paymentProduct === null) {
$paymentProduct = $request->request->get(
sprintf('%s_%s', PaymentContext::HIPAY_PAYMENT_PRODUCT, HipayPaypalGatewayFactory::FACTORY_NAME)
) ?? null;
}
if ($paymentProduct === null) {
$paymentProduct = $request->request->get(
sprintf('%s_%s', PaymentContext::HIPAY_PAYMENT_PRODUCT, HipayAppleGatewayFactory::FACTORY_NAME)
) ?? null;
}
}
if ($token !== null) {
$this->paymentContext->set(PaymentContext::HIPAY_TOKEN, $token);
}
if ($paymentProduct !== null) {
$this->paymentContext->set(PaymentContext::HIPAY_PAYMENT_PRODUCT, $paymentProduct);
}
}
}