custom/plugins/SwagPayPal/src/Storefront/Data/Service/FundingEligibilityDataService.php line 79

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Storefront\Data\Service;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Swag\PayPal\Checkout\SalesChannel\MethodEligibilityRoute;
  11. use Swag\PayPal\Setting\Service\CredentialsUtilInterface;
  12. use Swag\PayPal\Setting\Settings;
  13. use Swag\PayPal\Storefront\Data\Struct\FundingEligibilityData;
  14. use Swag\PayPal\Util\LocaleCodeProvider;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Routing\RouterInterface;
  17. class FundingEligibilityDataService
  18. {
  19.     private CredentialsUtilInterface $credentialsUtil;
  20.     private SystemConfigService $systemConfigService;
  21.     private LocaleCodeProvider $localeCodeProvider;
  22.     private RouterInterface $router;
  23.     private RequestStack $requestStack;
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(
  28.         CredentialsUtilInterface $credentialsUtil,
  29.         SystemConfigService $systemConfigService,
  30.         LocaleCodeProvider $localeCodeProvider,
  31.         RouterInterface $router,
  32.         RequestStack $requestStack
  33.     ) {
  34.         $this->credentialsUtil $credentialsUtil;
  35.         $this->systemConfigService $systemConfigService;
  36.         $this->localeCodeProvider $localeCodeProvider;
  37.         $this->router $router;
  38.         $this->requestStack $requestStack;
  39.     }
  40.     public function buildData(SalesChannelContext $context): ?FundingEligibilityData
  41.     {
  42.         return (new FundingEligibilityData())->assign(
  43.             [
  44.                 'clientId' => $this->credentialsUtil->getClientId($context->getSalesChannelId()),
  45.                 'merchantPayerId' => $this->credentialsUtil->getMerchantPayerId($context->getSalesChannelId()),
  46.                 'languageIso' => $this->getButtonLanguage($context),
  47.                 'currency' => $context->getCurrency()->getIsoCode(),
  48.                 'intent' => \mb_strtolower($this->systemConfigService->getString(Settings::INTENT$context->getSalesChannelId())),
  49.                 'methodEligibilityUrl' => $this->router->generate('frontend.paypal.payment-method-eligibility'),
  50.                 'filteredPaymentMethods' => $this->getFilteredPaymentMethods(),
  51.             ]
  52.         );
  53.     }
  54.     private function getButtonLanguage(SalesChannelContext $context): string
  55.     {
  56.         if ($settingsLocale $this->systemConfigService->getString(Settings::SPB_BUTTON_LANGUAGE_ISO$context->getSalesChannelId())) {
  57.             return $settingsLocale;
  58.         }
  59.         return \str_replace(
  60.             '-',
  61.             '_',
  62.             $this->localeCodeProvider->getLocaleCodeFromContext($context->getContext())
  63.         );
  64.     }
  65.     private function getFilteredPaymentMethods(): array
  66.     {
  67.         $handlers $this->requestStack->getSession()->get(MethodEligibilityRoute::SESSION_KEY, []);
  68.         if (!$handlers) {
  69.             return [];
  70.         }
  71.         $methods = [];
  72.         foreach ($handlers as $handler) {
  73.             $methods[] = \array_search($handlerMethodEligibilityRoute::REMOVABLE_PAYMENT_HANDLERStrue);
  74.         }
  75.         return \array_filter($methods);
  76.     }
  77. }