DisabledFundingOptions.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Paypal\Block\Adminhtml\System\Config\MultiSelect;
  8. use Magento\Paypal\Block\Adminhtml\System\Config\Field\Enable\AbstractEnable;
  9. use Magento\Paypal\Model\Config\StructurePlugin;
  10. use Magento\Backend\Block\Template\Context;
  11. use Magento\Paypal\Model\Config;
  12. use Magento\Framework\Data\Form\Element\AbstractElement;
  13. /**
  14. * Class DisabledFundingOptions
  15. */
  16. class DisabledFundingOptions extends AbstractEnable
  17. {
  18. /**
  19. * @var Config
  20. */
  21. private $config;
  22. /**
  23. * DisabledFundingOptions constructor.
  24. * @param Context $context
  25. * @param Config $config
  26. * @param array $data
  27. */
  28. public function __construct(
  29. Context $context,
  30. Config $config,
  31. $data = []
  32. ) {
  33. $this->config = $config;
  34. parent::__construct($context, $data);
  35. }
  36. /**
  37. * Render country field considering request parameter
  38. *
  39. * @param AbstractElement $element
  40. * @return string
  41. */
  42. public function render(AbstractElement $element)
  43. {
  44. if (!$this->isSelectedMerchantCountry('US')) {
  45. $fundingOptions = $element->getValues();
  46. $element->setValues($this->filterValuesForPaypalCredit($fundingOptions));
  47. }
  48. return parent::render($element);
  49. }
  50. /**
  51. * Getting the name of a UI attribute
  52. *
  53. * @return string
  54. */
  55. protected function getDataAttributeName(): string
  56. {
  57. return 'disable-funding-options';
  58. }
  59. /**
  60. * Filters array for CREDIT
  61. *
  62. * @param array $options
  63. * @return array
  64. */
  65. private function filterValuesForPaypalCredit($options): array
  66. {
  67. return array_filter($options, function ($opt) {
  68. return ($opt['value'] !== 'CREDIT');
  69. });
  70. }
  71. /**
  72. * Checks for chosen Merchant country from the config/url
  73. *
  74. * @param string $country
  75. * @return bool
  76. */
  77. private function isSelectedMerchantCountry(string $country): bool
  78. {
  79. $merchantCountry = $this->getRequest()->getParam(StructurePlugin::REQUEST_PARAM_COUNTRY)
  80. ?: $this->config->getMerchantCountry();
  81. return $merchantCountry === $country;
  82. }
  83. }