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

Backport of MAGETWO-60351 for Magento 2.1 - Unnecessary disabled paym… #9365

Merged
merged 1 commit into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
73 changes: 73 additions & 0 deletions app/code/Magento/Payment/Plugin/PaymentConfigurationProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Payment\Plugin;

/**
* Class PaymentConfigurationProcess
*
* Removes inactive payment methods and group from checkout configuration.
*/
class PaymentConfigurationProcess
{
/**
* @var \Magento\Payment\Api\PaymentMethodListInterface
*/
private $paymentMethodList;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->paymentMethodList = $paymentMethodList;
$this->storeManager = $storeManager;
}

/**
* Checkout LayoutProcessor before process plugin.
*
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $processor
* @param array $jsLayout
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout)
{
$configuration = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['renders']['children'];

if (!isset($configuration)) {
return [$jsLayout];
}

$storeId = $this->storeManager->getStore()->getId();
$activePaymentMethodList = $this->paymentMethodList->getActiveList($storeId);
$getCodeFunc = function ($method) {
return $method->getCode();
};
$activePaymentMethodCodes = array_map($getCodeFunc, $activePaymentMethodList);

foreach ($configuration as $paymentGroup => $groupConfig) {
$notActivePaymentMethodCodes = array_diff(array_keys($groupConfig['methods']), $activePaymentMethodCodes);
foreach ($notActivePaymentMethodCodes as $notActivePaymentMethodCode) {
unset($configuration[$paymentGroup]['methods'][$notActivePaymentMethodCode]);
}
if (empty($configuration[$paymentGroup]['methods'])) {
unset($configuration[$paymentGroup]);
}
}

return [$jsLayout];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Payment\Test\Unit\Plugin;

/**
* Class PaymentConfigurationProcessTest.
*/
class PaymentConfigurationProcessTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManager;

/**
* @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $store;

/**
* @var \Magento\Payment\Api\PaymentMethodListInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $paymentMethodList;

/**
* @var \Magento\Checkout\Block\Checkout\LayoutProcessor|\PHPUnit_Framework_MockObject_MockObject
*/
private $layoutProcessor;

/**
* @var \Magento\Payment\Plugin\PaymentConfigurationProcess
*/
private $plugin;

/**
* Set up
*/
protected function setUp()
{
$this->storeManager = $this
->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
->disableOriginalConstructor()
->setMethods(['getStore'])
->getMockForAbstractClass();
$this->store = $this
->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMockForAbstractClass();
$this->paymentMethodList = $this
->getMockBuilder(\Magento\Payment\Api\PaymentMethodListInterface::class)
->disableOriginalConstructor()
->setMethods(['getActiveList'])
->getMockForAbstractClass();
$this->layoutProcessor = $this
->getMockBuilder(\Magento\Checkout\Block\Checkout\LayoutProcessor::class)
->disableOriginalConstructor()
->setMethods(['process'])
->getMockForAbstractClass();

$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->plugin = $objectManagerHelper->getObject(
\Magento\Payment\Plugin\PaymentConfigurationProcess::class,
[
'paymentMethodList' => $this->paymentMethodList,
'storeManager' => $this->storeManager
]
);
}

/**
* @param array $jsLayout
* @param array $activePaymentList
* @param array $expectedResult
* @dataProvider beforeProcessDataProvider
*/
public function testBeforeProcess($jsLayout, $activePaymentList, $expectedResult)
{
$this->store->expects($this->once())->method('getId')->willReturn(1);
$this->storeManager->expects($this->once())->method('getStore')->willReturn($this->store);
$this->paymentMethodList->expects($this->once())
->method('getActiveList')
->with(1)
->willReturn($activePaymentList);

$result = $this->plugin->beforeProcess($this->layoutProcessor, $jsLayout);
$this->assertEquals($result[0], $expectedResult);
}

/**
* Data provider for BeforeProcess.
*
* @return array
*/
public function beforeProcessDataProvider()
{
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['renders']['children'] = [
'braintree' => [
'methods' => [
'braintree_paypal' => [],
'braintree' => []
]
],
'paypal-payments' => [
'methods' => [
'payflowpro' => [],
'payflow_link' => []
]
]
];
$result1['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['renders']['children'] = [];
$result2['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['renders']['children'] = [
'braintree' => [
'methods' => [
'braintree' => [],
'braintree_paypal' => []
]
]
];

$braintreePaymentMethod = $this
->getMockBuilder(\Magento\Payment\Api\Data\PaymentMethodInterface::class)
->disableOriginalConstructor()
->setMethods(['getCode'])
->getMockForAbstractClass();
$braintreePaypalPaymentMethod = $this
->getMockBuilder(\Magento\Payment\Api\Data\PaymentMethodInterface::class)
->disableOriginalConstructor()
->setMethods(['getCode'])
->getMockForAbstractClass();

$braintreePaymentMethod->expects($this->any())->method('getCode')->willReturn('braintree');
$braintreePaypalPaymentMethod->expects($this->any())->method('getCode')->willReturn('braintree_paypal');

return [
[$jsLayout, [], $result1],
[$jsLayout, [$braintreePaymentMethod, $braintreePaypalPaymentMethod], $result2]
];
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Payment/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
</argument>
</arguments>
</type>
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="ProcessPaymentConfiguration" type="Magento\Payment\Plugin\PaymentConfigurationProcess"/>
</type>
</config>
93 changes: 93 additions & 0 deletions app/code/Magento/Vault/Plugin/PaymentVaultConfigurationProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Vault\Plugin;

/**
* Class PaymentVaultConfigurationProcess
*
* Checks if vault group have active vaults.
*/
class PaymentVaultConfigurationProcess
{
/**
* @var \Magento\Vault\Api\PaymentMethodListInterface
*/
private $vaultPaymentList;

/**
* @var \Magento\Vault\Api\PaymentMethodListInterface
*/
private $paymentMethodList;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Vault\Api\PaymentMethodListInterface $vaultPaymentList
* @param \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Vault\Api\PaymentMethodListInterface $vaultPaymentList,
\Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->vaultPaymentList = $vaultPaymentList;
$this->paymentMethodList = $paymentMethodList;
$this->storeManager = $storeManager;
}

/**
* Checkout LayoutProcessor before process plugin.
*
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $processor
* @param array $jsLayout
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout)
{
$configuration = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['renders']['children'];

if (!isset($configuration)) {
return [$jsLayout];
}

$storeId = $this->storeManager->getStore()->getId();
$activePaymentMethodList = $this->paymentMethodList->getActiveList($storeId);
$activeVaultList = $this->vaultPaymentList->getActiveList($storeId);
$getCodeFunc = function ($method) {
return $method->getCode();
};
$getProviderCodeFunc = function ($method) {
return $method->getProviderCode();
};
$activePaymentMethodCodes = array_map($getCodeFunc, $activePaymentMethodList);
$activeVaultProviderCodes = array_map($getProviderCodeFunc, $activeVaultList);
$activePaymentMethodCodes = array_merge(
$activePaymentMethodCodes,
$activeVaultProviderCodes
);

foreach ($configuration as $paymentGroup => $groupConfig) {
$notActivePaymentMethodCodes = array_diff(array_keys($groupConfig['methods']), $activePaymentMethodCodes);
foreach ($notActivePaymentMethodCodes as $notActivePaymentMethodCode) {
unset($configuration[$paymentGroup]['methods'][$notActivePaymentMethodCode]);
}
if ($paymentGroup === 'vault' && !empty($activeVaultProviderCodes)) {
continue;
}
if (empty($configuration[$paymentGroup]['methods'])) {
unset($configuration[$paymentGroup]);
}
}

return [$jsLayout];
}
}
Loading