-
Notifications
You must be signed in to change notification settings - Fork 220
[ECP-9693] Add payment method title override configuration #3285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
| /** | ||
| * | ||
| * Adyen Payment module (https://www.adyen.com/) | ||
| * | ||
| * Copyright (c) 2026 Adyen N.V. (https://www.adyen.com/) | ||
| * See LICENSE.txt for license details. | ||
| * | ||
| * Author: Adyen <magento@adyen.com> | ||
| */ | ||
|
|
||
| namespace Adyen\Payment\Block\Adminhtml\System\Config\Field; | ||
|
|
||
| use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray; | ||
| use Magento\Framework\DataObject; | ||
| use Magento\Framework\Exception\LocalizedException; | ||
|
|
||
| class PaymentMethodTitles extends AbstractFieldArray | ||
| { | ||
| private ?PaymentMethodType $_paymentMethodTypeRenderer = null; | ||
|
Check warning on line 20 in Block/Adminhtml/System/Config/Field/PaymentMethodTitles.php
|
||
|
|
||
| /** | ||
| * @return PaymentMethodType | ||
| * @throws LocalizedException | ||
| */ | ||
| protected function getPaymentMethodTypeRenderer(): PaymentMethodType | ||
| { | ||
| if (!$this->_paymentMethodTypeRenderer) { | ||
| $this->_paymentMethodTypeRenderer = $this->getLayout()->createBlock( | ||
| PaymentMethodType::class, | ||
| '', | ||
| ['data' => ['is_render_to_js_template' => true]] | ||
| ); | ||
| } | ||
|
|
||
| return $this->_paymentMethodTypeRenderer; | ||
| } | ||
|
|
||
| /** | ||
| * @return void | ||
| * @throws LocalizedException | ||
| */ | ||
| protected function _prepareToRender(): void | ||
| { | ||
| $this->addColumn( | ||
| 'payment_method_type', | ||
| [ | ||
| 'label' => __('Payment Method'), | ||
| 'renderer' => $this->getPaymentMethodTypeRenderer(), | ||
| ] | ||
| ); | ||
| $this->addColumn( | ||
| 'title', | ||
| [ | ||
| 'label' => __('Custom Title'), | ||
| 'renderer' => false, | ||
| ] | ||
| ); | ||
|
|
||
| $this->_addAfter = false; | ||
| $this->_addButtonLabel = __('Add Override'); | ||
| } | ||
|
|
||
| /** | ||
| * @param DataObject $row | ||
| * @return void | ||
| * @throws LocalizedException | ||
| */ | ||
| protected function _prepareArrayRow(DataObject $row): void | ||
| { | ||
| $options = []; | ||
| $paymentMethodType = $row->getPaymentMethodType(); | ||
|
|
||
| if ($paymentMethodType) { | ||
| $options['option_' . $this->getPaymentMethodTypeRenderer()->calcOptionHash($paymentMethodType)] | ||
| = 'selected="selected"'; | ||
| } | ||
|
|
||
| $row->setData('option_extra_attrs', $options); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
| /** | ||
| * | ||
| * Adyen Payment module (https://www.adyen.com/) | ||
| * | ||
| * Copyright (c) 2026 Adyen N.V. (https://www.adyen.com/) | ||
| * See LICENSE.txt for license details. | ||
| * | ||
| * Author: Adyen <magento@adyen.com> | ||
| */ | ||
|
|
||
| namespace Adyen\Payment\Block\Adminhtml\System\Config\Field; | ||
|
|
||
| use Adyen\Payment\Model\Config\Source\PaymentMethodType as PaymentMethodTypeSource; | ||
| use Magento\Framework\View\Element\Context; | ||
| use Magento\Framework\View\Element\Html\Select; | ||
|
|
||
| class PaymentMethodType extends Select | ||
| { | ||
| /** | ||
| * @param Context $context | ||
| * @param PaymentMethodTypeSource $source | ||
| * @param array $data | ||
| */ | ||
| public function __construct( | ||
| Context $context, | ||
| private readonly PaymentMethodTypeSource $source, | ||
| array $data = [] | ||
| ) { | ||
| parent::__construct($context, $data); | ||
| } | ||
|
|
||
| /** | ||
| * @return string | ||
| */ | ||
| public function _toHtml(): string | ||
| { | ||
| if (!$this->getOptions()) { | ||
| foreach ($this->source->toOptionArray() as $option) { | ||
| $this->addOption($option['value'], addslashes((string) $option['label'])); | ||
|
shubhamk67 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| return parent::_toHtml(); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $value | ||
| * @return $this | ||
| */ | ||
| public function setInputName(string $value): self | ||
| { | ||
| return $this->setName($value); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $value | ||
| * @return $this | ||
| */ | ||
| public function setInputId(string $value): self | ||
| { | ||
| return $this->setId($value); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <?php | ||
| /** | ||
| * | ||
| * Adyen Payment module (https://www.adyen.com/) | ||
| * | ||
| * Copyright (c) 2026 Adyen N.V. (https://www.adyen.com/) | ||
| * See LICENSE.txt for license details. | ||
| * | ||
| * Author: Adyen <magento@adyen.com> | ||
| */ | ||
|
|
||
| namespace Adyen\Payment\Model\Config\Backend; | ||
|
|
||
| use Magento\Framework\App\Cache\TypeListInterface; | ||
| use Magento\Framework\App\Config\ScopeConfigInterface; | ||
| use Magento\Framework\App\Config\Value; | ||
| use Magento\Framework\Data\Collection\AbstractDb; | ||
| use Magento\Framework\Math\Random; | ||
| use Magento\Framework\Model\Context; | ||
| use Magento\Framework\Model\ResourceModel\AbstractResource; | ||
| use Magento\Framework\Registry; | ||
| use Magento\Framework\Serialize\SerializerInterface; | ||
|
|
||
| class PaymentMethodTitles extends Value | ||
| { | ||
| /** | ||
| * @param Context $context | ||
| * @param Registry $registry | ||
| * @param ScopeConfigInterface $config | ||
| * @param TypeListInterface $cacheTypeList | ||
| * @param Random $mathRandom | ||
| * @param SerializerInterface $serializer | ||
| * @param AbstractResource|null $resource | ||
| * @param AbstractDb|null $resourceCollection | ||
| * @param array $data | ||
| */ | ||
| public function __construct( | ||
| Context $context, | ||
| Registry $registry, | ||
| ScopeConfigInterface $config, | ||
| TypeListInterface $cacheTypeList, | ||
| protected readonly Random $mathRandom, | ||
| private readonly SerializerInterface $serializer, | ||
| ?AbstractResource $resource = null, | ||
| ?AbstractDb $resourceCollection = null, | ||
| array $data = [] | ||
| ) { | ||
| parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); | ||
| } | ||
|
|
||
| /** | ||
| * Serialize the dynamic table rows before saving to the database. | ||
| * Duplicate payment method types are deduplicated (last row wins). | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function beforeSave(): static | ||
| { | ||
| $value = $this->getValue(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would you think about adding a validator to check the uniqueness of the values? So that, each payment method can only have one override per store. |
||
|
|
||
| if (!is_array($value)) { | ||
| return $this; | ||
| } | ||
|
|
||
| $result = []; | ||
|
|
||
| foreach ($value as $rowData) { | ||
| if (!is_array($rowData)) { | ||
| continue; | ||
| } | ||
|
|
||
| $type = trim((string) ($rowData['payment_method_type'] ?? '')); | ||
| $title = trim((string) ($rowData['title'] ?? '')); | ||
|
|
||
| if ($type === '' || $title === '') { | ||
| continue; | ||
| } | ||
|
|
||
| $result[$type] = $title; | ||
| } | ||
|
|
||
| $this->setValue($this->serializer->serialize($result)); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Deserialize the stored value after loading from the database so it can | ||
| * be rendered by the AbstractFieldArray dynamic table. | ||
| * | ||
| * @return $this | ||
| */ | ||
| protected function _afterLoad(): static | ||
| { | ||
| $value = $this->getValue(); | ||
|
|
||
| if (empty($value)) { | ||
| return $this; | ||
| } | ||
|
|
||
| $decoded = $this->serializer->unserialize($value); | ||
|
|
||
| if (!is_array($decoded)) { | ||
| return $this; | ||
| } | ||
|
|
||
| $this->setValue($this->encodeArrayFieldValue($decoded)); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Convert the stored `{type: title}` map into the row-keyed format that | ||
| * AbstractFieldArray expects for pre-population. | ||
| * | ||
| * @param array $value | ||
| * @return array | ||
| */ | ||
| protected function encodeArrayFieldValue(array $value): array | ||
| { | ||
| $result = []; | ||
|
|
||
| foreach ($value as $type => $title) { | ||
| $id = $this->mathRandom->getUniqueHash('_'); | ||
| $result[$id] = [ | ||
| 'payment_method_type' => $type, | ||
| 'title' => $title, | ||
| ]; | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change the property name to conform with Sonar rules?