-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-67724: Backport of MAGETWO-60351 for Magento 2.1 - Unnecessar…
…y disabled paym… #9365
- Loading branch information
Showing
6 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
app/code/Magento/Payment/Plugin/PaymentConfigurationProcess.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
app/code/Magento/Payment/Test/Unit/Plugin/PaymentConfigurationProcessTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
app/code/Magento/Vault/Plugin/PaymentVaultConfigurationProcess.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
Oops, something went wrong.