From e1de8b45544331e13508fa0a17563f7c4eb5449e Mon Sep 17 00:00:00 2001 From: Ievgen Sentiabov Date: Tue, 4 Oct 2016 13:30:13 +0300 Subject: [PATCH 01/13] MAGETWO-59196: Refactor Braintree Vault js on backend - Refactored Braintree Vault js component --- .../Ui/Adminhtml/TokenUiComponentProvider.php | 1 + .../TokenUiComponentProviderTest.php | 7 ++-- .../view/adminhtml/templates/form/vault.phtml | 3 +- .../Braintree/view/adminhtml/web/js/vault.js | 34 ++++++++++++------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Braintree/Model/Ui/Adminhtml/TokenUiComponentProvider.php b/app/code/Magento/Braintree/Model/Ui/Adminhtml/TokenUiComponentProvider.php index 6cfc96ea23d0d..420b8365b3ea4 100644 --- a/app/code/Magento/Braintree/Model/Ui/Adminhtml/TokenUiComponentProvider.php +++ b/app/code/Magento/Braintree/Model/Ui/Adminhtml/TokenUiComponentProvider.php @@ -49,6 +49,7 @@ public function getComponentForToken(PaymentTokenInterface $paymentToken) $component = $this->componentFactory->create( [ 'config' => [ + 'code' => ConfigProvider::CC_VAULT_CODE, 'nonceUrl' => $this->getNonceRetrieveUrl(), TokenUiComponentProviderInterface::COMPONENT_DETAILS => $data, TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH => $paymentToken->getPublicHash(), diff --git a/app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/TokenUiComponentProviderTest.php b/app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/TokenUiComponentProviderTest.php index d1665c71804cf..f159136cf4c46 100644 --- a/app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/TokenUiComponentProviderTest.php +++ b/app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/TokenUiComponentProviderTest.php @@ -7,10 +7,10 @@ use Magento\Braintree\Model\Ui\Adminhtml\TokenUiComponentProvider; use Magento\Framework\UrlInterface; -use Magento\Framework\View\Element\Template; use Magento\Vault\Api\Data\PaymentTokenInterface; use Magento\Vault\Model\Ui\TokenUiComponentInterface; use Magento\Vault\Model\Ui\TokenUiComponentInterfaceFactory; +use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Class TokenUiComponentProviderTest @@ -19,12 +19,12 @@ class TokenUiComponentProviderTest extends \PHPUnit_Framework_TestCase { /** - * @var TokenUiComponentInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject + * @var TokenUiComponentInterfaceFactory|MockObject */ private $componentFactory; /** - * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject + * @var UrlInterface|MockObject */ private $urlBuilder; @@ -59,6 +59,7 @@ public function testGetComponentForToken() $expirationDate = '12/2015'; $expected = [ + 'code' => 'vault', 'nonceUrl' => $nonceUrl, 'details' => [ 'type' => $type, diff --git a/app/code/Magento/Braintree/view/adminhtml/templates/form/vault.phtml b/app/code/Magento/Braintree/view/adminhtml/templates/form/vault.phtml index 3811461884725..001422d4bf911 100644 --- a/app/code/Magento/Braintree/view/adminhtml/templates/form/vault.phtml +++ b/app/code/Magento/Braintree/view/adminhtml/templates/form/vault.phtml @@ -7,7 +7,7 @@ use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface; // @codingStandardsIgnoreFile /** @var \Magento\Framework\View\Element\Template $block */ -$details = $block->getData('details'); +$details = $block->getData(TokenUiComponentProviderInterface::COMPONENT_DETAILS); $icon = $block->getData('icons')[$details['type']]; $id = $block->escapeHtml($block->getData('id')); ?> @@ -15,6 +15,7 @@ $id = $block->escapeHtml($block->getData('id')); "Magento_Braintree/js/vault": { "container": "payment_", "publicHash": "escapeHtml($block->getData(TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH)); ?>", + "code": "escapeHtml($block->getData('code')); ?>", "nonceUrl": "escapeUrl($block->getData('nonceUrl')); ?>" } }' id="payment_" class="admin__field"> diff --git a/app/code/Magento/Braintree/view/adminhtml/web/js/vault.js b/app/code/Magento/Braintree/view/adminhtml/web/js/vault.js index fcff173e7fcd4..ea832acb537e0 100644 --- a/app/code/Magento/Braintree/view/adminhtml/web/js/vault.js +++ b/app/code/Magento/Braintree/view/adminhtml/web/js/vault.js @@ -14,7 +14,8 @@ define([ return Class.extend({ defaults: { $selector: null, - selector: 'edit_form' + selector: 'edit_form', + $container: null }, /** @@ -25,17 +26,18 @@ define([ var self = this; self.$selector = $('#' + self.selector); + self.$container = $('#' + self.container); self.$selector.on( 'setVaultNotActive', function () { - self.$selector.off('submitOrder.braintree_vault'); + self.$selector.off('submitOrder.' + self.getCode()); } ); - this._super(); + self._super(); - this.initEventHandlers(); + self.initEventHandlers(); - return this; + return self; }, /** @@ -43,14 +45,14 @@ define([ * @returns {String} */ getCode: function () { - return 'braintree'; + return this.code; }, /** * Init event handlers */ initEventHandlers: function () { - $('#' + this.container).find('[name="payment[token_switcher]"]') + $(this.$container).find('[name="payment[token_switcher]"]') .on('click', this.selectPaymentMethod.bind(this)); }, @@ -66,7 +68,7 @@ define([ * Enable form event listeners */ enableEventListeners: function () { - this.$selector.on('submitOrder.braintree_vault', this.submitOrder.bind(this)); + this.$selector.on('submitOrder.' + this.getCode(), this.submitOrder.bind(this)); }, /** @@ -129,7 +131,7 @@ define([ this.createPublicHashSelector(); this.$selector.find('[name="payment[public_hash]"]').val(this.publicHash); - this.$selector.find('#braintree_nonce').val(nonce); + this.$container.find('#' + this.getNonceSelectorName()).val(nonce); }, /** @@ -138,16 +140,16 @@ define([ createPublicHashSelector: function () { var $input; - if (this.$selector.find('#braintree_nonce').size() === 0) { + if (this.$container.find('#' + this.getNonceSelectorName()).size() === 0) { $input = $('').attr( { type: 'hidden', - id: 'braintree_nonce', + id: this.getNonceSelectorName(), name: 'payment[payment_method_nonce]' } ); - $input.appendTo(this.$selector); + $input.appendTo(this.$container); $input.prop('disabled', false); } }, @@ -160,6 +162,14 @@ define([ alert({ content: message }); + }, + + /** + * Get selector name for nonce input + * @returns {String} + */ + getNonceSelectorName: function () { + return 'nonce_' + this.getCode(); } }); }); From 217f0f119da69643b41f1f378188142379014385 Mon Sep 17 00:00:00 2001 From: Ievgen Sentiabov Date: Wed, 5 Oct 2016 11:56:47 +0300 Subject: [PATCH 02/13] MAGETWO-59197: Create UI component and server structure for Vault - Added possibility to use Vault payments without main payment on the Backend - Added component provider for Braintree PayPal Vault for admin area --- .../PayPal/TokenUiComponentProvider.php | 84 +++++++++++++ .../PayPal/TokenUiComponentProviderTest.php | 114 ++++++++++++++++++ .../Magento/Braintree/etc/adminhtml/di.xml | 1 + app/code/Magento/Braintree/etc/config.xml | 3 +- .../layout/sales_order_create_index.xml | 4 + ...order_create_load_block_billing_method.xml | 4 + .../templates/form/paypal/vault.phtml | 30 +++++ app/code/Magento/Vault/Model/Method/Vault.php | 3 +- .../Test/Unit/Model/Method/VaultTest.php | 70 +++++++++-- .../PayPal/TokenUiComponentProviderTest.php | 62 ++++++++++ 10 files changed, 362 insertions(+), 13 deletions(-) create mode 100644 app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php create mode 100644 app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php create mode 100644 app/code/Magento/Braintree/view/adminhtml/templates/form/paypal/vault.phtml create mode 100644 dev/tests/integration/testsuite/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php diff --git a/app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php b/app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php new file mode 100644 index 0000000000000..9247b466009e5 --- /dev/null +++ b/app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php @@ -0,0 +1,84 @@ +componentFactory = $componentFactory; + $this->urlBuilder = $urlBuilder; + $this->config = $config; + } + + /** + * @inheritdoc + */ + public function getComponentForToken(PaymentTokenInterface $paymentToken) + { + $data = json_decode($paymentToken->getTokenDetails() ?: '{}', true); + $data['icon'] = $this->config->getPayPalIcon(); + $component = $this->componentFactory->create( + [ + 'config' => [ + 'code' => PayPalConfigProvider::PAYPAL_VAULT_CODE, + 'nonceUrl' => $this->getNonceRetrieveUrl(), + TokenUiComponentProviderInterface::COMPONENT_DETAILS => $data, + TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH => $paymentToken->getPublicHash(), + 'template' => 'Magento_Braintree::form/paypal/vault.phtml' + ], + 'name' => Template::class + ] + ); + + return $component; + } + + /** + * Get url to retrieve payment method nonce + * @return string + */ + private function getNonceRetrieveUrl() + { + return $this->urlBuilder->getUrl(ConfigProvider::CODE . '/payment/getnonce', ['_secure' => true]); + } +} diff --git a/app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php b/app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php new file mode 100644 index 0000000000000..bdc39cbc5b868 --- /dev/null +++ b/app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php @@ -0,0 +1,114 @@ +componentFactory = $this->getMockBuilder(TokenUiComponentInterfaceFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + + $this->urlBuilder = $this->getMock(UrlInterface::class); + + $this->config = $this->getMockBuilder(Config::class) + ->disableOriginalConstructor() + ->setMethods(['getPayPalIcon']) + ->getMock(); + + $this->tokenUiComponentProvider = new TokenUiComponentProvider( + $this->componentFactory, + $this->urlBuilder, + $this->config + ); + } + + /** + * @covers \Magento\Braintree\Model\Ui\Adminhtml\PayPal\TokenUiComponentProvider::getComponentForToken + */ + public function testGetComponentForToken() + { + $nonceUrl = 'https://payment/adminhtml/nonce/url'; + $payerEmail = 'john.doe@test.com'; + $icon = [ + 'url' => 'https://payment/adminhtml/icon.png', + 'width' => 48, + 'height' => 32 + ]; + + $expected = [ + 'code' => 'vault', + 'nonceUrl' => $nonceUrl, + 'details' => [ + 'payerEmail' => $payerEmail, + 'icon' => $icon + ], + 'template' => 'vault.phtml' + ]; + + $this->config->expects(static::once()) + ->method('getPayPalIcon') + ->willReturn($icon); + + $paymentToken = $this->getMock(PaymentTokenInterface::class); + $paymentToken->expects(static::once()) + ->method('getTokenDetails') + ->willReturn('{"payerEmail":" ' . $payerEmail . '"}'); + $paymentToken->expects(static::once()) + ->method('getPublicHash') + ->willReturn('cmk32dl21l'); + + $this->urlBuilder->expects(static::once()) + ->method('getUrl') + ->willReturn($nonceUrl); + + $tokenComponent = $this->getMock(TokenUiComponentInterface::class); + $tokenComponent->expects(static::once()) + ->method('getConfig') + ->willReturn($expected); + + $this->componentFactory->expects(static::once()) + ->method('create') + ->willReturn($tokenComponent); + + $component = $this->tokenUiComponentProvider->getComponentForToken($paymentToken); + static::assertEquals($tokenComponent, $component); + static::assertEquals($expected, $component->getConfig()); + } +} diff --git a/app/code/Magento/Braintree/etc/adminhtml/di.xml b/app/code/Magento/Braintree/etc/adminhtml/di.xml index f252b977f20bd..d154aabbb01b5 100644 --- a/app/code/Magento/Braintree/etc/adminhtml/di.xml +++ b/app/code/Magento/Braintree/etc/adminhtml/di.xml @@ -47,6 +47,7 @@ Magento\Braintree\Model\Ui\Adminhtml\TokenUiComponentProvider + Magento\Braintree\Model\Ui\Adminhtml\PayPal\TokenUiComponentProvider diff --git a/app/code/Magento/Braintree/etc/config.xml b/app/code/Magento/Braintree/etc/config.xml index 095a8419c8529..bf19324ae7a02 100644 --- a/app/code/Magento/Braintree/etc/config.xml +++ b/app/code/Magento/Braintree/etc/config.xml @@ -71,7 +71,8 @@ BraintreePayPalVaultFacade - Vault Token (Braintree PayPal) + Stored Accounts (Braintree PayPal) + 1 diff --git a/app/code/Magento/Braintree/view/adminhtml/layout/sales_order_create_index.xml b/app/code/Magento/Braintree/view/adminhtml/layout/sales_order_create_index.xml index 571c5ededeb99..5e4f36e1c1fb4 100644 --- a/app/code/Magento/Braintree/view/adminhtml/layout/sales_order_create_index.xml +++ b/app/code/Magento/Braintree/view/adminhtml/layout/sales_order_create_index.xml @@ -18,6 +18,10 @@ braintree_cc_vault Magento_Vault::form/vault.phtml + + braintree_paypal_vault + Magento_Vault::form/vault.phtml + braintree_cc_vault Magento_Vault::form/vault.phtml + + braintree_paypal_vault + Magento_Vault::form/vault.phtml + \ No newline at end of file diff --git a/app/code/Magento/Braintree/view/adminhtml/templates/form/paypal/vault.phtml b/app/code/Magento/Braintree/view/adminhtml/templates/form/paypal/vault.phtml new file mode 100644 index 0000000000000..22930bbc65666 --- /dev/null +++ b/app/code/Magento/Braintree/view/adminhtml/templates/form/paypal/vault.phtml @@ -0,0 +1,30 @@ +getData(TokenUiComponentProviderInterface::COMPONENT_DETAILS); +$icon = $details['icon']; +$id = $block->escapeHtml($block->getData('id')); +?> +
", + "nonceUrl": "escapeUrl($block->getData('nonceUrl')); ?>" + } + }' id="payment_" class="admin__field"> +
+ + + escapeHtml($details['payerEmail']); ?> +
+
diff --git a/app/code/Magento/Vault/Model/Method/Vault.php b/app/code/Magento/Vault/Model/Method/Vault.php index 41a2d6b0e26ef..b4f08b92960ec 100644 --- a/app/code/Magento/Vault/Model/Method/Vault.php +++ b/app/code/Magento/Vault/Model/Method/Vault.php @@ -275,7 +275,8 @@ public function canVoid() */ public function canUseInternal() { - return $this->getVaultProvider()->canUseInternal(); + return $this->config->getValue('can_use_internal', $this->getStore()) + || $this->getVaultProvider()->canUseInternal(); } /** diff --git a/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php b/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php index 8bded498dab76..c26842a2612da 100644 --- a/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php +++ b/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php @@ -19,6 +19,7 @@ use Magento\Vault\Api\PaymentTokenManagementInterface; use Magento\Vault\Model\Method\Vault; use Magento\Vault\Model\VaultPaymentInterface; +use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Class VaultTest @@ -31,9 +32,15 @@ class VaultTest extends \PHPUnit_Framework_TestCase */ private $objectManager; + /** + * @var MethodInterface|MockObject + */ + private $vaultProvider; + public function setUp() { $this->objectManager = new ObjectManager($this); + $this->vaultProvider = $this->getMock(MethodInterface::class); } /** @@ -137,8 +144,6 @@ public function testAuthorize() $commandManagerPool = $this->getMock(CommandManagerPoolInterface::class); $commandManager = $this->getMock(CommandManagerInterface::class); - $vaultProvider = $this->getMock(MethodInterface::class); - $tokenManagement = $this->getMock(PaymentTokenManagementInterface::class); $token = $this->getMock(PaymentTokenInterface::class); @@ -161,7 +166,7 @@ public function testAuthorize() ->method('setVaultPaymentToken') ->with($token); - $vaultProvider->expects(static::atLeastOnce()) + $this->vaultProvider->expects(static::atLeastOnce()) ->method('getCode') ->willReturn($vaultProviderCode); $commandManagerPool->expects(static::once()) @@ -188,7 +193,7 @@ public function testAuthorize() [ 'tokenManagement' => $tokenManagement, 'commandManagerPool' => $commandManagerPool, - 'vaultProvider' => $vaultProvider + 'vaultProvider' => $this->vaultProvider ] ); $model->authorize($paymentModel, $amount); @@ -235,10 +240,9 @@ public function testIsAvailable($isAvailableProvider, $isActive, $expected) { $storeId = 1; $quote = $this->getMockForAbstractClass(CartInterface::class); - $vaultProvider = $this->getMockForAbstractClass(MethodInterface::class); $config = $this->getMockForAbstractClass(ConfigInterface::class); - $vaultProvider->expects(static::once()) + $this->vaultProvider->expects(static::once()) ->method('isAvailable') ->with($quote) ->willReturn($isAvailableProvider); @@ -255,7 +259,7 @@ public function testIsAvailable($isAvailableProvider, $isActive, $expected) /** @var Vault $model */ $model = $this->objectManager->getObject(Vault::class, [ 'config' => $config, - 'vaultProvider' => $vaultProvider + 'vaultProvider' => $this->vaultProvider ]); $actual = $model->isAvailable($quote); static::assertEquals($expected, $actual); @@ -281,11 +285,9 @@ public function isAvailableDataProvider() public function testIsAvailableWithoutQuote() { $quote = null; - - $vaultProvider = $this->getMockForAbstractClass(MethodInterface::class); $config = $this->getMockForAbstractClass(ConfigInterface::class); - $vaultProvider->expects(static::once()) + $this->vaultProvider->expects(static::once()) ->method('isAvailable') ->with($quote) ->willReturn(true); @@ -298,8 +300,54 @@ public function testIsAvailableWithoutQuote() /** @var Vault $model */ $model = $this->objectManager->getObject(Vault::class, [ 'config' => $config, - 'vaultProvider' => $vaultProvider + 'vaultProvider' => $this->vaultProvider ]); static::assertFalse($model->isAvailable($quote)); } + + /** + * @covers \Magento\Vault\Model\Method\Vault::canUseInternal + * @param bool|null $configValue + * @param bool|null $paymentValue + * @param bool $expected + * @dataProvider internalUsageDataProvider + */ + public function testCanUseInternal($configValue, $paymentValue, $expected) + { + $config = $this->getMock(ConfigInterface::class); + + $config->expects(static::once()) + ->method('getValue') + ->with('can_use_internal', null) + ->willReturn($configValue); + + $this->vaultProvider->expects(static::any()) + ->method('canUseInternal') + ->willReturn($paymentValue); + + /** @var Vault $model */ + $model = $this->objectManager->getObject(Vault::class, [ + 'config' => $config, + 'vaultProvider' => $this->vaultProvider + ]); + static::assertEquals($expected, $model->canUseInternal()); + } + + /** + * Get list of variations for testing canUseInternal method + * @return array + */ + public function internalUsageDataProvider() + { + return [ + ['configValue' => true, 'paymentValue' => true, 'expected' => true], + ['configValue' => true, 'paymentValue' => null, 'expected' => true], + ['configValue' => true, 'paymentValue' => false, 'expected' => true], + ['configValue' => false, 'paymentValue' => true, 'expected' => true], + ['configValue' => false, 'paymentValue' => false, 'expected' => false], + ['configValue' => null, 'paymentValue' => true, 'expected' => true], + ['configValue' => null, 'paymentValue' => false, 'expected' => false], + ['configValue' => null, 'paymentValue' => null, 'expected' => false], + ]; + } } diff --git a/dev/tests/integration/testsuite/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php b/dev/tests/integration/testsuite/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php new file mode 100644 index 0000000000000..e79f4ad361e36 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProviderTest.php @@ -0,0 +1,62 @@ +objectManager = Bootstrap::getObjectManager(); + $this->tokenComponentProvider = $this->objectManager->get(TokenUiComponentProvider::class); + } + + /** + * @covers \Magento\Braintree\Model\Ui\Adminhtml\PayPal\TokenUiComponentProvider::getComponentForToken + * @magentoDataFixture Magento/Braintree/_files/paypal_vault_token.php + * @magentoAppArea adminhtml + */ + public function testGetComponentForToken() + { + $customerId = 1; + $token = 'mx29vk'; + $payerEmail = 'john.doe@example.com'; + + /** @var PaymentTokenManagement $tokenManagement */ + $tokenManagement = $this->objectManager->get(PaymentTokenManagement::class); + $paymentToken = $tokenManagement->getByGatewayToken($token, ConfigProvider::PAYPAL_CODE, $customerId); + + $component = $this->tokenComponentProvider->getComponentForToken($paymentToken); + $config = $component->getConfig(); + + static::assertNotEmpty($config[TokenUiComponentProviderInterface::COMPONENT_DETAILS]); + static::assertNotEmpty($config[TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH]); + static::assertEquals(ConfigProvider::PAYPAL_VAULT_CODE, $config['code']); + + $details = $config[TokenUiComponentProviderInterface::COMPONENT_DETAILS]; + static::assertEquals($payerEmail, $details['payerEmail']); + static::assertNotEmpty($details['icon']); + } +} From b6cc0078572b79dc02e9804947734334347a284d Mon Sep 17 00:00:00 2001 From: Ievgen Sentiabov Date: Wed, 5 Oct 2016 18:11:08 +0300 Subject: [PATCH 03/13] MAGETWO-59197: Create UI component and server structure for Vault - Added exception throwing for non exists tokens --- .../PayPal/TokenUiComponentProvider.php | 2 +- .../Ui/Adminhtml/TokensConfigProvider.php | 8 +- .../Ui/Adminhtml/TokensConfigProviderTest.php | 208 +++++++++--------- 3 files changed, 115 insertions(+), 103 deletions(-) diff --git a/app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php b/app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php index 9247b466009e5..fd94e18e2cd94 100644 --- a/app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php +++ b/app/code/Magento/Braintree/Model/Ui/Adminhtml/PayPal/TokenUiComponentProvider.php @@ -15,7 +15,7 @@ use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface; /** - * Gets Ui component configuration for + * Gets Ui component configuration for Braintree PayPal Vault */ class TokenUiComponentProvider implements TokenUiComponentProviderInterface { diff --git a/app/code/Magento/Vault/Model/Ui/Adminhtml/TokensConfigProvider.php b/app/code/Magento/Vault/Model/Ui/Adminhtml/TokensConfigProvider.php index 98e7728f732fe..f3a2cbfa78739 100644 --- a/app/code/Magento/Vault/Model/Ui/Adminhtml/TokensConfigProvider.php +++ b/app/code/Magento/Vault/Model/Ui/Adminhtml/TokensConfigProvider.php @@ -221,9 +221,11 @@ private function getVaultPayment($vaultPaymentCode) */ private function getPaymentTokenEntityId() { - return $this->getPaymentTokenManagement() - ->getByPaymentId($this->getOrderPaymentEntityId()) - ->getEntityId(); + $paymentToken = $this->getPaymentTokenManagement()->getByPaymentId($this->getOrderPaymentEntityId()); + if ($paymentToken === null) { + throw new NoSuchEntityException(__('No available payment tokens for specified order payment.')); + } + return $paymentToken->getEntityId(); } /** diff --git a/app/code/Magento/Vault/Test/Unit/Model/Ui/Adminhtml/TokensConfigProviderTest.php b/app/code/Magento/Vault/Test/Unit/Model/Ui/Adminhtml/TokensConfigProviderTest.php index 38948b004eebc..bbac491386c44 100644 --- a/app/code/Magento/Vault/Test/Unit/Model/Ui/Adminhtml/TokensConfigProviderTest.php +++ b/app/code/Magento/Vault/Test/Unit/Model/Ui/Adminhtml/TokensConfigProviderTest.php @@ -13,6 +13,7 @@ use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Intl\DateTimeFactory; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex; use Magento\Payment\Helper\Data; use Magento\Sales\Api\Data\OrderInterface; @@ -29,7 +30,6 @@ use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface; use Magento\Vault\Model\VaultPaymentInterface; use PHPUnit_Framework_MockObject_MockObject as MockObject; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; /** * Class TokensConfigProviderTest @@ -104,11 +104,21 @@ class TokensConfigProviderTest extends \PHPUnit_Framework_TestCase */ private $orderRepository; + /** + * @var TokenUiComponentProviderInterface|MockObject + */ + private $tokenComponentProvider; + /** * @var ObjectManager */ private $objectManager; + /** + * @var TokensConfigProvider + */ + private $configProvider; + protected function setUp() { $this->paymentTokenRepository = $this->getMockBuilder(PaymentTokenRepositoryInterface::class) @@ -138,6 +148,38 @@ protected function setUp() $this->vaultPayment = $this->getMockForAbstractClass(VaultPaymentInterface::class); $this->objectManager = new ObjectManager($this); + + $this->initStoreMock(); + + $this->tokenComponentProvider = $this->getMock(TokenUiComponentProviderInterface::class); + + $this->configProvider = new TokensConfigProvider( + $this->session, + $this->paymentTokenRepository, + $this->filterBuilder, + $this->searchCriteriaBuilder, + $this->storeManager, + $this->dateTimeFactory, + [ + self::VAULT_PROVIDER_CODE => $this->tokenComponentProvider + ] + ); + + $this->objectManager->setBackwardCompatibleProperty( + $this->configProvider, + 'paymentDataHelper', + $this->paymentDataHelper + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->configProvider, + 'paymentTokenManagement', + $this->paymentTokenManagement + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->configProvider, + 'orderRepository', + $this->orderRepository + ); } /** @@ -147,8 +189,6 @@ public function testGetTokensComponentsRegisteredCustomer() { $customerId = 1; - $this->initStoreMock(); - $this->session->expects(static::once()) ->method('getCustomerId') ->willReturn($customerId); @@ -170,7 +210,7 @@ public function testGetTokensComponentsRegisteredCustomer() $token = $this->getMockBuilder(PaymentTokenInterface::class) ->getMockForAbstractClass(); - list($tokenUiComponent, $tokenUiComponentProvider) = $this->getTokenUiComponentProvider($token); + $tokenUiComponent = $this->getTokenUiComponentProvider($token); $searchCriteria = $this->getSearchCriteria($customerId, self::ENTITY_ID, self::VAULT_PROVIDER_CODE); @@ -197,25 +237,7 @@ public function testGetTokensComponentsRegisteredCustomer() ->method('getItems') ->willReturn([$token]); - $configProvider = new TokensConfigProvider( - $this->session, - $this->paymentTokenRepository, - $this->filterBuilder, - $this->searchCriteriaBuilder, - $this->storeManager, - $this->dateTimeFactory, - [ - self::VAULT_PROVIDER_CODE => $tokenUiComponentProvider - ] - ); - - $this->objectManager->setBackwardCompatibleProperty( - $configProvider, - 'paymentDataHelper', - $this->paymentDataHelper - ); - - static::assertEquals([$tokenUiComponent], $configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); + static::assertEquals([$tokenUiComponent], $this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); } /** @@ -263,7 +285,7 @@ public function testGetTokensComponentsGuestCustomer() ->method('getEntityId') ->willReturn(self::ENTITY_ID); - list($tokenUiComponent, $tokenUiComponentProvider) = $this->getTokenUiComponentProvider($token); + $tokenUiComponent = $this->getTokenUiComponentProvider($token); $searchCriteria = $this->getSearchCriteria($customerId, self::ENTITY_ID, self::VAULT_PROVIDER_CODE); @@ -290,35 +312,7 @@ public function testGetTokensComponentsGuestCustomer() ->method('getItems') ->willReturn([$token]); - $configProvider = new TokensConfigProvider( - $this->session, - $this->paymentTokenRepository, - $this->filterBuilder, - $this->searchCriteriaBuilder, - $this->storeManager, - $this->dateTimeFactory, - [ - self::VAULT_PROVIDER_CODE => $tokenUiComponentProvider - ] - ); - - $this->objectManager->setBackwardCompatibleProperty( - $configProvider, - 'paymentDataHelper', - $this->paymentDataHelper - ); - $this->objectManager->setBackwardCompatibleProperty( - $configProvider, - 'paymentTokenManagement', - $this->paymentTokenManagement - ); - $this->objectManager->setBackwardCompatibleProperty( - $configProvider, - 'orderRepository', - $this->orderRepository - ); - - static::assertEquals([$tokenUiComponent], $configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); + static::assertEquals([$tokenUiComponent], $this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); } /** @@ -330,8 +324,6 @@ public function testGetTokensComponentsGuestCustomerOrderNotFound($exception) { $customerId = null; - $this->initStoreMock(); - $this->session->expects(static::once()) ->method('getCustomerId') ->willReturn($customerId); @@ -366,35 +358,7 @@ public function testGetTokensComponentsGuestCustomerOrderNotFound($exception) $this->searchCriteriaBuilder->expects(self::never()) ->method('addFilters'); - $configProvider = new TokensConfigProvider( - $this->session, - $this->paymentTokenRepository, - $this->filterBuilder, - $this->searchCriteriaBuilder, - $this->storeManager, - $this->dateTimeFactory, - [ - self::VAULT_PROVIDER_CODE => $this->getMock(TokenUiComponentProviderInterface::class) - ] - ); - - $this->objectManager->setBackwardCompatibleProperty( - $configProvider, - 'paymentDataHelper', - $this->paymentDataHelper - ); - $this->objectManager->setBackwardCompatibleProperty( - $configProvider, - 'paymentTokenManagement', - $this->paymentTokenManagement - ); - $this->objectManager->setBackwardCompatibleProperty( - $configProvider, - 'orderRepository', - $this->orderRepository - ); - - static::assertEmpty($configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); + static::assertEmpty($this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); } /** @@ -453,18 +417,66 @@ public function testGetTokensComponentsEmptyComponentProvider() static::assertEmpty($configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); } + /** + * @covers \Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider::getTokensComponents + */ + public function testGetTokensComponentsForGuestCustomerWithoutStoredTokens() + { + $this->session->expects(static::once()) + ->method('getCustomerId') + ->willReturn(null); + + $this->paymentDataHelper->expects(static::once()) + ->method('getMethodInstance') + ->with(self::VAULT_PAYMENT_CODE) + ->willReturn($this->vaultPayment); + + $this->vaultPayment->expects(static::once()) + ->method('isActive') + ->with(self::STORE_ID) + ->willReturn(true); + $this->vaultPayment->expects(static::once()) + ->method('getProviderCode') + ->willReturn(self::VAULT_PROVIDER_CODE); + + $this->session->expects(static::once()) + ->method('getReordered') + ->willReturn(self::ORDER_ID); + $this->orderRepository->expects(static::once()) + ->method('get') + ->with(self::ORDER_ID) + ->willReturn($this->getOrderMock()); + + $this->paymentTokenManagement->expects(static::once()) + ->method('getByPaymentId') + ->with(self::ORDER_PAYMENT_ENTITY_ID) + ->willReturn(null); + + $this->filterBuilder->expects(static::once()) + ->method('setField') + ->with(PaymentTokenInterface::ENTITY_ID) + ->willReturnSelf(); + $this->filterBuilder->expects(static::never()) + ->method('setValue'); + + $this->searchCriteriaBuilder->expects(static::never()) + ->method('addFilters'); + + static::assertEmpty($this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE)); + } + /** * Create mock object for store */ private function initStoreMock() { $this->store = $this->getMock(StoreInterface::class); - $this->store->expects(static::once()) + $this->store->expects(static::any()) ->method('getId') ->willReturn(self::STORE_ID); $this->storeManager = $this->getMock(StoreManagerInterface::class); - $this->storeManager->expects(static::once()) + $this->storeManager->expects(static::any()) ->method('getStore') ->with(null) ->willReturn($this->store); @@ -476,39 +488,37 @@ private function initStoreMock() */ private function getOrderMock() { - /** @var OrderInterface|MockObject $orderMock */ - $orderMock = $this->getMockBuilder(OrderInterface::class) + /** @var OrderInterface|MockObject $order */ + $order = $this->getMockBuilder(OrderInterface::class) ->getMockForAbstractClass(); - /** @var OrderPaymentInterface|MockObject $orderPaymentMock */ - $orderPaymentMock = $this->getMockBuilder(OrderPaymentInterface::class) + /** @var OrderPaymentInterface|MockObject $orderPayment */ + $orderPayment = $this->getMockBuilder(OrderPaymentInterface::class) ->getMockForAbstractClass(); - $orderMock->expects(static::once()) + $order->expects(static::once()) ->method('getPayment') - ->willReturn($orderPaymentMock); - $orderPaymentMock->expects(static::once()) + ->willReturn($orderPayment); + $orderPayment->expects(static::once()) ->method('getEntityId') ->willReturn(self::ORDER_PAYMENT_ENTITY_ID); - return $orderMock; + return $order; } /** * Get mock for token ui component provider * @param PaymentTokenInterface $token - * @return array + * @return TokenUiComponentInterface|MockObject */ private function getTokenUiComponentProvider($token) { $tokenUiComponent = $this->getMock(TokenUiComponentInterface::class); - - $tokenUiComponentProvider = $this->getMock(TokenUiComponentProviderInterface::class); - $tokenUiComponentProvider->expects(static::once()) + $this->tokenComponentProvider->expects(static::once()) ->method('getComponentForToken') ->with($token) ->willReturn($tokenUiComponent); - return [$tokenUiComponent, $tokenUiComponentProvider]; + return $tokenUiComponent; } /** From e5f266d097932706cb6638185d13d9b90c75f775 Mon Sep 17 00:00:00 2001 From: Ievgen Sentiabov Date: Thu, 6 Oct 2016 13:17:14 +0300 Subject: [PATCH 04/13] MAGETWO-59197: Create UI component and server structure for Vault - Added more flexible configuration for Vault internal usage --- app/code/Magento/Vault/Model/Method/Vault.php | 11 ++++++---- .../Test/Unit/Model/Method/VaultTest.php | 22 +++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Vault/Model/Method/Vault.php b/app/code/Magento/Vault/Model/Method/Vault.php index b4f08b92960ec..ca6fe06f91eef 100644 --- a/app/code/Magento/Vault/Model/Method/Vault.php +++ b/app/code/Magento/Vault/Model/Method/Vault.php @@ -5,17 +5,16 @@ */ namespace Magento\Vault\Model\Method; -use Magento\Framework\DataObject; use Magento\Framework\Event\ManagerInterface; use Magento\Framework\ObjectManagerInterface; use Magento\Payment\Gateway\Command; -use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory; use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface; use Magento\Payment\Gateway\ConfigFactoryInterface; use Magento\Payment\Gateway\ConfigInterface; use Magento\Payment\Model\InfoInterface; use Magento\Payment\Model\MethodInterface; use Magento\Payment\Observer\AbstractDataAssignObserver; +use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory; use Magento\Sales\Api\Data\OrderPaymentInterface; use Magento\Sales\Model\Order\Payment; use Magento\Vault\Api\Data\PaymentTokenInterface; @@ -275,8 +274,12 @@ public function canVoid() */ public function canUseInternal() { - return $this->config->getValue('can_use_internal', $this->getStore()) - || $this->getVaultProvider()->canUseInternal(); + $isInternalAllowed = $this->getConfiguredValue('can_use_internal'); + // if config has't been specified for Vault, need to check payment provider option + if ($isInternalAllowed === null) { + return $this->getVaultProvider()->canUseInternal(); + } + return (bool) $isInternalAllowed; } /** diff --git a/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php b/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php index c26842a2612da..ade93c9367858 100644 --- a/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php +++ b/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php @@ -8,6 +8,8 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Payment\Gateway\Command\CommandManagerInterface; use Magento\Payment\Gateway\Command\CommandManagerPoolInterface; +use Magento\Payment\Gateway\Config\ValueHandlerInterface; +use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface; use Magento\Payment\Gateway\ConfigInterface; use Magento\Payment\Model\InfoInterface; use Magento\Payment\Model\MethodInterface; @@ -314,11 +316,17 @@ public function testIsAvailableWithoutQuote() */ public function testCanUseInternal($configValue, $paymentValue, $expected) { - $config = $this->getMock(ConfigInterface::class); + $handlerPool = $this->getMock(ValueHandlerPoolInterface::class); + $handler = $this->getMock(ValueHandlerInterface::class); - $config->expects(static::once()) - ->method('getValue') - ->with('can_use_internal', null) + $handlerPool->expects(static::once()) + ->method('get') + ->with('can_use_internal') + ->willReturn($handler); + + $handler->expects(static::once()) + ->method('handle') + ->with(['field' => 'can_use_internal'], null) ->willReturn($configValue); $this->vaultProvider->expects(static::any()) @@ -327,8 +335,8 @@ public function testCanUseInternal($configValue, $paymentValue, $expected) /** @var Vault $model */ $model = $this->objectManager->getObject(Vault::class, [ - 'config' => $config, - 'vaultProvider' => $this->vaultProvider + 'vaultProvider' => $this->vaultProvider, + 'valueHandlerPool' => $handlerPool, ]); static::assertEquals($expected, $model->canUseInternal()); } @@ -343,7 +351,7 @@ public function internalUsageDataProvider() ['configValue' => true, 'paymentValue' => true, 'expected' => true], ['configValue' => true, 'paymentValue' => null, 'expected' => true], ['configValue' => true, 'paymentValue' => false, 'expected' => true], - ['configValue' => false, 'paymentValue' => true, 'expected' => true], + ['configValue' => false, 'paymentValue' => true, 'expected' => false], ['configValue' => false, 'paymentValue' => false, 'expected' => false], ['configValue' => null, 'paymentValue' => true, 'expected' => true], ['configValue' => null, 'paymentValue' => false, 'expected' => false], From d323e1e60dee828431cb4f4de93850a6337f1569 Mon Sep 17 00:00:00 2001 From: Ievgen Sentiabov Date: Thu, 6 Oct 2016 13:52:57 +0300 Subject: [PATCH 05/13] MAGETWO-59199: Create Functional test for PayPal Braintree Vault - Covered a place order case from Admin by PayPal Braintree Vault token --- ...derWithPayPalBraintreeVaultBackendTest.php | 56 +++++++++++++++++++ ...derWithPayPalBraintreeVaultBackendTest.xml | 31 ++++++++++ .../Test/TestStep/UsePayPalVaultTokenStep.php | 47 ++++++++++++++++ .../Magento/Braintree/Test/etc/testcase.xml | 17 ++++++ 4 files changed, 151 insertions(+) create mode 100644 dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.php create mode 100644 dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.xml create mode 100644 dev/tests/functional/tests/app/Magento/Braintree/Test/TestStep/UsePayPalVaultTokenStep.php diff --git a/dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.php b/dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.php new file mode 100644 index 0000000000000..fe29437bb3d03 --- /dev/null +++ b/dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.php @@ -0,0 +1,56 @@ +executeScenario(); + } +} diff --git a/dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.xml b/dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.xml new file mode 100644 index 0000000000000..553d69994d08f --- /dev/null +++ b/dev/tests/functional/tests/app/Magento/Braintree/Test/TestCase/CreateOrderWithPayPalBraintreeVaultBackendTest.xml @@ -0,0 +1,31 @@ + + + + + + est_type:3rd_party_test,severity:S0 + catalogProductSimple::product_10_dollar + default + US_address_1_without_email + login + Flat Rate + Fixed + braintree_paypal + Processing + braintree, braintree_paypal, braintree_paypal_skip_order_review + + 15.00 + + + + + + + + + diff --git a/dev/tests/functional/tests/app/Magento/Braintree/Test/TestStep/UsePayPalVaultTokenStep.php b/dev/tests/functional/tests/app/Magento/Braintree/Test/TestStep/UsePayPalVaultTokenStep.php new file mode 100644 index 0000000000000..2834e881b5c8a --- /dev/null +++ b/dev/tests/functional/tests/app/Magento/Braintree/Test/TestStep/UsePayPalVaultTokenStep.php @@ -0,0 +1,47 @@ +orderCreateIndex = $orderCreateIndex; + $this->payment = $payment; + } + + /** + * @inheritdoc + */ + public function run() + { + $block = $this->orderCreateIndex->getCreateBlock(); + $this->payment['method'] = 'braintree_paypal_vault'; + $block->selectPaymentMethod($this->payment); + $block->selectVaultToken('token_switcher_' . $this->payment['method']); + } +} diff --git a/dev/tests/functional/tests/app/Magento/Braintree/Test/etc/testcase.xml b/dev/tests/functional/tests/app/Magento/Braintree/Test/etc/testcase.xml index 34f4be6493838..d77377965df50 100644 --- a/dev/tests/functional/tests/app/Magento/Braintree/Test/etc/testcase.xml +++ b/dev/tests/functional/tests/app/Magento/Braintree/Test/etc/testcase.xml @@ -158,4 +158,21 @@ + + + + + + + + + + + + + + + + + From d396f0bf4b2930848c640acc97798ba080e587ad Mon Sep 17 00:00:00 2001 From: Iurii Ivashchenko Date: Thu, 6 Oct 2016 15:53:51 +0300 Subject: [PATCH 06/13] MAGETWO-58251: PayPal Best Practice to Separate Saved Tokens --- .../web/js/model/payment/method-group.js | 31 ++++ .../view/frontend/web/js/view/payment/list.js | 137 ++++++++++++++---- .../web/template/payment-methods/list.html | 19 ++- .../view/frontend/web/template/payment.html | 1 - .../frontend/web/js/view/payment/vault.js | 43 +++--- 5 files changed, 182 insertions(+), 49 deletions(-) create mode 100644 app/code/Magento/Checkout/view/frontend/web/js/model/payment/method-group.js diff --git a/app/code/Magento/Checkout/view/frontend/web/js/model/payment/method-group.js b/app/code/Magento/Checkout/view/frontend/web/js/model/payment/method-group.js new file mode 100644 index 0000000000000..4236a215d7359 --- /dev/null +++ b/app/code/Magento/Checkout/view/frontend/web/js/model/payment/method-group.js @@ -0,0 +1,31 @@ +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +define([ + 'uiElement', + 'mage/translate' +], function (Element, $t) { + 'use strict'; + + var DEFAULT_GROUP_ALIAS = 'default'; + + return Element.extend({ + defaults: { + alias: DEFAULT_GROUP_ALIAS, + title: $t('Payment Method'), + sortOrder: 100, + displayArea: 'payment-methods-items-${ $.alias }' + }, + + /** + * Checks if group instance is default + * + * @returns {Boolean} + */ + isDefault: function () { + return this.alias === DEFAULT_GROUP_ALIAS; + } + }); +}); diff --git a/app/code/Magento/Checkout/view/frontend/web/js/view/payment/list.js b/app/code/Magento/Checkout/view/frontend/web/js/view/payment/list.js index 18f6b8479c229..b6b3c5fd7495a 100644 --- a/app/code/Magento/Checkout/view/frontend/web/js/view/payment/list.js +++ b/app/code/Magento/Checkout/view/frontend/web/js/view/payment/list.js @@ -10,14 +10,24 @@ define([ 'Magento_Checkout/js/model/payment/method-list', 'Magento_Checkout/js/model/payment/renderer-list', 'uiLayout', - 'Magento_Checkout/js/model/checkout-data-resolver' -], function (_, ko, utils, Component, paymentMethods, rendererList, layout, checkoutDataResolver) { + 'Magento_Checkout/js/model/checkout-data-resolver', + 'mage/translate' +], function (_, ko, utils, Component, paymentMethods, rendererList, layout, checkoutDataResolver, $t) { 'use strict'; return Component.extend({ defaults: { template: 'Magento_Checkout/payment-methods/list', - visible: paymentMethods().length > 0 + visible: paymentMethods().length > 0, + configDefaultGroup: { + name: 'methodGroup', + component: 'Magento_Checkout/js/model/payment/method-group' + }, + modules: { + defaultGroup: '${ $.configDefaultGroup.name }' + }, + paymentGroupsList: [], + defaultGroupTitle: $t('Select a new payment method') }, /** @@ -26,7 +36,7 @@ define([ * @returns {Component} Chainable. */ initialize: function () { - this._super().initChildren(); + this._super().initDefaulGroup().initChildren(); paymentMethods.subscribe( function (changes) { checkoutDataResolver.resolvePaymentMethod(); @@ -47,6 +57,27 @@ define([ return this; }, + /** @inheritdoc */ + initObservable: function () { + this._super(). + observe(['paymentGroupsList']); + + return this; + }, + + /** + * Creates default group + * + * @returns {Component} Chainable. + */ + initDefaulGroup: function() { + layout([ + this.configDefaultGroup + ]); + + return this; + }, + /** * Create renders for child payment methods. * @@ -77,7 +108,7 @@ define([ rendererTemplate = { parent: '${ $.$data.parentName }', name: '${ $.$data.name }', - displayArea: 'payment-method-items', + displayArea: payment.displayArea, component: payment.component }; rendererComponent = utils.template(rendererTemplate, templateData); @@ -95,7 +126,8 @@ define([ * @param {Object} paymentMethodData */ createRenderer: function (paymentMethodData) { - var isRendererForMethod = false; + var isRendererForMethod = false, + currentGroup; _.find(rendererList(), function (renderer) { @@ -108,36 +140,91 @@ define([ } if (isRendererForMethod) { - layout( - [ - this.createComponent( - { - config: renderer.config, - component: renderer.component, - name: renderer.type, - method: paymentMethodData.method, - item: paymentMethodData - } - ) - ] - ); + currentGroup = renderer.group ? renderer.group : this.defaultGroup(); + + this.collectPaymentGroups(currentGroup); + + layout([ + this.createComponent( + { + config: renderer.config, + component: renderer.component, + name: renderer.type, + method: paymentMethodData.method, + item: paymentMethodData, + displayArea: currentGroup.displayArea + } + )]); + + return true; } }.bind(this)); }, + /** + * Collects unique groups of available payment methods + * + * @param {Object} group + */ + collectPaymentGroups: function (group) { + var groupsList = this.paymentGroupsList(), + isGroupExists = _.some(groupsList, function (existsGroup) { + return existsGroup.alias === group.alias; + }); + + if (!isGroupExists) { + groupsList.push(group); + groupsList = _.sortBy(groupsList, function (existsGroup) { + return existsGroup.sortOrder; + }); + this.paymentGroupsList(groupsList); + } + }, + + /** + * Returns payment group title + * + * @param {Object} group + * @returns {String} + */ + getGroupTitle: function (group) { + var title = group().title; + + if (group().isDefault() && this.paymentGroupsList().length > 1) { + title = this.defaultGroupTitle; + } + + return title + ':'; + }, + + /** + * Checks if at least one payment method available + * + * @returns {String} + */ + isPaymentMethodsAvailable: function () { + return _.some(this.paymentGroupsList(), function (group) { + return this.getRegion(group.displayArea)().length; + }, this); + }, + /** * Remove view renderer. * * @param {String} paymentMethodCode */ removeRenderer: function (paymentMethodCode) { - var items = this.getRegion('payment-method-items'); + var items; - _.find(items(), function (value) { - if (value.item.method.indexOf(paymentMethodCode) === 0) { - value.disposeSubscriptions(); - value.destroy(); - } + _.each(this.paymentGroupsList(), function (group) { + items = this.getRegion(group.displayArea); + + _.find(items(), function (value) { + if (value.item.method.indexOf(paymentMethodCode) === 0) { + value.disposeSubscriptions(); + value.destroy(); + } + }); }, this); } }); diff --git a/app/code/Magento/Checkout/view/frontend/web/template/payment-methods/list.html b/app/code/Magento/Checkout/view/frontend/web/template/payment-methods/list.html index 66769f37386d6..4b39f2fd983a0 100644 --- a/app/code/Magento/Checkout/view/frontend/web/template/payment-methods/list.html +++ b/app/code/Magento/Checkout/view/frontend/web/template/payment-methods/list.html @@ -4,9 +4,18 @@ * See COPYING.txt for license details. */ --> -
- - - +
+
+
+
+ +
+
+
-
diff --git a/app/code/Magento/Checkout/view/frontend/web/template/payment.html b/app/code/Magento/Checkout/view/frontend/web/template/payment.html index 292ece611e6b6..46467839da3fb 100644 --- a/app/code/Magento/Checkout/view/frontend/web/template/payment.html +++ b/app/code/Magento/Checkout/view/frontend/web/template/payment.html @@ -5,7 +5,6 @@ */ -->