<?php declare(strict_types=1);
namespace Shopware\Core\Checkout\Payment\SalesChannel;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(defaults={"_routeScope"={"store-api"}})
*/
class PaymentMethodRoute extends AbstractPaymentMethodRoute
{
private SalesChannelRepositoryInterface $paymentMethodsRepository;
/**
* @internal
*/
public function __construct(SalesChannelRepositoryInterface $paymentMethodsRepository)
{
$this->paymentMethodsRepository = $paymentMethodsRepository;
}
public function getDecorated(): AbstractPaymentMethodRoute
{
throw new DecorationPatternException(self::class);
}
/**
* @Since("6.2.0.0")
* @Entity("payment_method")
* @Route("/store-api/payment-method", name="store-api.payment.method", methods={"GET", "POST"})
*/
public function load(Request $request, SalesChannelContext $context, Criteria $criteria): PaymentMethodRouteResponse
{
$criteria
->addFilter(new EqualsFilter('active', true))
->addSorting(new FieldSorting('position'))
->addAssociation('media');
$result = $this->paymentMethodsRepository->search($criteria, $context);
/** @var PaymentMethodCollection $paymentMethods */
$paymentMethods = $result->getEntities();
if ($request->query->getBoolean('onlyAvailable') || $request->request->getBoolean('onlyAvailable')) {
$paymentMethods = $paymentMethods->filterByActiveRules($context);
}
$result->assign(['entities' => $paymentMethods, 'elements' => $paymentMethods, 'total' => $paymentMethods->count()]);
return new PaymentMethodRouteResponse($result);
}
}