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

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.     public function __construct(
  25.         CredentialsUtilInterface $credentialsUtil,
  26.         SystemConfigService $systemConfigService,
  27.         LocaleCodeProvider $localeCodeProvider,
  28.         RouterInterface $router,
  29.         RequestStack $requestStack
  30.     ) {
  31.         $this->credentialsUtil $credentialsUtil;
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->localeCodeProvider $localeCodeProvider;
  34.         $this->router $router;
  35.         $this->requestStack $requestStack;
  36.     }
  37.     public function buildData(SalesChannelContext $context): ?FundingEligibilityData
  38.     {
  39.         return (new FundingEligibilityData())->assign(
  40.             [
  41.                 'clientId' => $this->credentialsUtil->getClientId($context->getSalesChannelId()),
  42.                 'merchantPayerId' => $this->credentialsUtil->getMerchantPayerId($context->getSalesChannelId()),
  43.                 'languageIso' => $this->getButtonLanguage($context),
  44.                 'currency' => $context->getCurrency()->getIsoCode(),
  45.                 'intent' => \mb_strtolower($this->systemConfigService->getString(Settings::INTENT$context->getSalesChannelId())),
  46.                 'methodEligibilityUrl' => $this->router->generate('store-api.paypal.payment-method-eligibility'),
  47.                 'filteredPaymentMethods' => $this->getFilteredPaymentMethods(),
  48.             ]
  49.         );
  50.     }
  51.     private function getButtonLanguage(SalesChannelContext $context): string
  52.     {
  53.         if ($settingsLocale $this->systemConfigService->getString(Settings::SPB_BUTTON_LANGUAGE_ISO$context->getSalesChannelId())) {
  54.             return $settingsLocale;
  55.         }
  56.         return \str_replace(
  57.             '-',
  58.             '_',
  59.             $this->localeCodeProvider->getLocaleCodeFromContext($context->getContext())
  60.         );
  61.     }
  62.     private function getFilteredPaymentMethods(): array
  63.     {
  64.         $handlers $this->requestStack->getSession()->get(MethodEligibilityRoute::SESSION_KEY, []);
  65.         if (!$handlers) {
  66.             return [];
  67.         }
  68.         $methods = [];
  69.         foreach ($handlers as $handler) {
  70.             $methods[] = \array_search($handlerMethodEligibilityRoute::REMOVABLE_PAYMENT_HANDLERStrue);
  71.         }
  72.         return \array_filter($methods);
  73.     }
  74. }