Skip to content
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

Fix - Skip excluded payment methods from activation toggling #2503

Merged
merged 17 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ configure: n98-magerun2.phar
bin/magento config:set payment/adyen_abstract/demo_mode 1
bin/magento adyen:enablepaymentmethods:run
bin/magento config:set payment/adyen_abstract/has_holder_name 1
bin/magento config:set payment/adyen_pay_by_link/active 1
bin/magento config:set payment/adyen_pay_by_link/days_to_expire 5
bin/magento config:set payment/adyen_giving/active 1
bin/magento config:set payment/adyen_giving/charity_description 'test'
Expand Down
16 changes: 16 additions & 0 deletions Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Adyen\Client;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Notification;
use Adyen\Payment\Model\Ui\Adminhtml\AdyenMotoConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Adyen\Util\ManualCapture;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
Expand Down Expand Up @@ -54,6 +57,15 @@ class PaymentMethods extends AbstractHelper

const ADYEN_GROUP_ALTERNATIVE_PAYMENT_METHODS = 'adyen-alternative-payment-method';

/*
* Following payment methods should be enabled with their own configuration path.
*/
const EXCLUDED_PAYMENT_METHODS = [
AdyenPayByLinkConfigProvider::CODE,
AdyenPosCloudConfigProvider::CODE,
AdyenMotoConfigProvider::CODE
];

protected CartRepositoryInterface $quoteRepository;
protected ScopeConfigInterface $config;
protected Data $adyenHelper;
Expand Down Expand Up @@ -153,6 +165,10 @@ public function togglePaymentMethodsActivation(?bool $isActive =null): array
}

foreach ($this->getAdyenPaymentMethods() as $paymentMethod) {
if (in_array($paymentMethod, self::EXCLUDED_PAYMENT_METHODS)) {
continue;
}

$value = $isActive ? '1': '0';
$field = 'active';
$this->configHelper->setConfigData($value, $field, $paymentMethod);
Expand Down
31 changes: 1 addition & 30 deletions Model/Config/Backend/PaymentMethodsStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
namespace Adyen\Payment\Model\Config\Backend;

use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Model\Ui\Adminhtml\AdyenMotoConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Config\Value;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\Context;
Expand All @@ -27,50 +23,25 @@
class PaymentMethodsStatus extends Value
{
protected PaymentMethods $paymentMethodsHelper;
private WriterInterface $configWriter;

/*
* Following payment methods should be enabled with their own configuration path.
*/
const EXCLUDED_PAYMENT_METHODS = [
AdyenPayByLinkConfigProvider::CODE,
AdyenPosCloudConfigProvider::CODE,
AdyenMotoConfigProvider::CODE
];

public function __construct(
Context $context,
Registry $registry,
ScopeConfigInterface $config,
TypeListInterface $cacheTypeList,
WriterInterface $configWriter,
PaymentMethods $paymentMethodsHelper,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
$this->configWriter = $configWriter;
$this->paymentMethodsHelper = $paymentMethodsHelper;

parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}

public function afterSave(): PaymentMethodsStatus
{
$value = $this->getValue();
$adyenPaymentMethods = $this->paymentMethodsHelper->getAdyenPaymentMethods();

foreach ($adyenPaymentMethods as $adyenPaymentMethod) {
// Exclude following payment methods. They need to use their own configuration path.
if (in_array($adyenPaymentMethod, self::EXCLUDED_PAYMENT_METHODS)) {
continue;
}

$this->configWriter->save(
'payment/' . $adyenPaymentMethod . '/active',
$value,
);
}
$this->paymentMethodsHelper->togglePaymentMethodsActivation((bool) $this->getValue());

return $this;
}
Expand Down
5 changes: 4 additions & 1 deletion Test/Unit/Helper/PaymentMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ public function testTogglePaymentMethodsActivation()
[
'adyen_cc' => [],
'adyen_oneclick' => [],
'adyen_cc_vault' => []
'adyen_cc_vault' => [],
'adyen_pos_cloud' => [],
'adyen_moto' => [],
'adyen_pay_by_link' => [],
]);

$this->configHelperMock
Expand Down
41 changes: 41 additions & 0 deletions Test/Unit/Model/Config/Backend/PaymentMethodsStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2024 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Test\Unit\Model\Config\Backend;

use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Model\Config\Backend\PaymentMethodsStatus;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class PaymentMethodsStatusTest extends AbstractAdyenTestCase
{
const ENABLED_METHODS = [
'adyen_cc',
'adyen_ideal'
];

public function testAfterSave()
{
$paymentMethodsHelperMock = $this->createMock(PaymentMethods::class);
$paymentMethodsHelperMock->method('togglePaymentMethodsActivation')
->willReturn(self::ENABLED_METHODS);

$objectManager = new ObjectManager($this);
$paymentMethodsStatus = $objectManager->getObject(PaymentMethodsStatus::class, [
'paymentMethodsHelper' => $paymentMethodsHelperMock
]);

$result = $paymentMethodsStatus->afterSave();

$this->assertInstanceOf(PaymentMethodsStatus::class, $result);
}
}
Loading