Skip to content

Commit e1de8b4

Browse files
committed
MAGETWO-59196: Refactor Braintree Vault js on backend
- Refactored Braintree Vault js component
1 parent 152309b commit e1de8b4

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

app/code/Magento/Braintree/Model/Ui/Adminhtml/TokenUiComponentProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function getComponentForToken(PaymentTokenInterface $paymentToken)
4949
$component = $this->componentFactory->create(
5050
[
5151
'config' => [
52+
'code' => ConfigProvider::CC_VAULT_CODE,
5253
'nonceUrl' => $this->getNonceRetrieveUrl(),
5354
TokenUiComponentProviderInterface::COMPONENT_DETAILS => $data,
5455
TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH => $paymentToken->getPublicHash(),

app/code/Magento/Braintree/Test/Unit/Model/Ui/Adminhtml/TokenUiComponentProviderTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
use Magento\Braintree\Model\Ui\Adminhtml\TokenUiComponentProvider;
99
use Magento\Framework\UrlInterface;
10-
use Magento\Framework\View\Element\Template;
1110
use Magento\Vault\Api\Data\PaymentTokenInterface;
1211
use Magento\Vault\Model\Ui\TokenUiComponentInterface;
1312
use Magento\Vault\Model\Ui\TokenUiComponentInterfaceFactory;
13+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1414

1515
/**
1616
* Class TokenUiComponentProviderTest
@@ -19,12 +19,12 @@ class TokenUiComponentProviderTest extends \PHPUnit_Framework_TestCase
1919
{
2020

2121
/**
22-
* @var TokenUiComponentInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
22+
* @var TokenUiComponentInterfaceFactory|MockObject
2323
*/
2424
private $componentFactory;
2525

2626
/**
27-
* @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
27+
* @var UrlInterface|MockObject
2828
*/
2929
private $urlBuilder;
3030

@@ -59,6 +59,7 @@ public function testGetComponentForToken()
5959
$expirationDate = '12/2015';
6060

6161
$expected = [
62+
'code' => 'vault',
6263
'nonceUrl' => $nonceUrl,
6364
'details' => [
6465
'type' => $type,

app/code/Magento/Braintree/view/adminhtml/templates/form/vault.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface;
77
// @codingStandardsIgnoreFile
88

99
/** @var \Magento\Framework\View\Element\Template $block */
10-
$details = $block->getData('details');
10+
$details = $block->getData(TokenUiComponentProviderInterface::COMPONENT_DETAILS);
1111
$icon = $block->getData('icons')[$details['type']];
1212
$id = $block->escapeHtml($block->getData('id'));
1313
?>
1414
<div data-mage-init='{
1515
"Magento_Braintree/js/vault": {
1616
"container": "payment_<?php /* @noEscape */ echo $id; ?>",
1717
"publicHash": "<?php echo $block->escapeHtml($block->getData(TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH)); ?>",
18+
"code": "<?php echo $block->escapeHtml($block->getData('code')); ?>",
1819
"nonceUrl": "<?php echo $block->escapeUrl($block->getData('nonceUrl')); ?>"
1920
}
2021
}' id="payment_<?php /* @noEscape */ echo $id;?>" class="admin__field">

app/code/Magento/Braintree/view/adminhtml/web/js/vault.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ define([
1414
return Class.extend({
1515
defaults: {
1616
$selector: null,
17-
selector: 'edit_form'
17+
selector: 'edit_form',
18+
$container: null
1819
},
1920

2021
/**
@@ -25,32 +26,33 @@ define([
2526
var self = this;
2627

2728
self.$selector = $('#' + self.selector);
29+
self.$container = $('#' + self.container);
2830
self.$selector.on(
2931
'setVaultNotActive',
3032
function () {
31-
self.$selector.off('submitOrder.braintree_vault');
33+
self.$selector.off('submitOrder.' + self.getCode());
3234
}
3335
);
34-
this._super();
36+
self._super();
3537

36-
this.initEventHandlers();
38+
self.initEventHandlers();
3739

38-
return this;
40+
return self;
3941
},
4042

4143
/**
4244
* Get payment code
4345
* @returns {String}
4446
*/
4547
getCode: function () {
46-
return 'braintree';
48+
return this.code;
4749
},
4850

4951
/**
5052
* Init event handlers
5153
*/
5254
initEventHandlers: function () {
53-
$('#' + this.container).find('[name="payment[token_switcher]"]')
55+
$(this.$container).find('[name="payment[token_switcher]"]')
5456
.on('click', this.selectPaymentMethod.bind(this));
5557
},
5658

@@ -66,7 +68,7 @@ define([
6668
* Enable form event listeners
6769
*/
6870
enableEventListeners: function () {
69-
this.$selector.on('submitOrder.braintree_vault', this.submitOrder.bind(this));
71+
this.$selector.on('submitOrder.' + this.getCode(), this.submitOrder.bind(this));
7072
},
7173

7274
/**
@@ -129,7 +131,7 @@ define([
129131
this.createPublicHashSelector();
130132

131133
this.$selector.find('[name="payment[public_hash]"]').val(this.publicHash);
132-
this.$selector.find('#braintree_nonce').val(nonce);
134+
this.$container.find('#' + this.getNonceSelectorName()).val(nonce);
133135
},
134136

135137
/**
@@ -138,16 +140,16 @@ define([
138140
createPublicHashSelector: function () {
139141
var $input;
140142

141-
if (this.$selector.find('#braintree_nonce').size() === 0) {
143+
if (this.$container.find('#' + this.getNonceSelectorName()).size() === 0) {
142144
$input = $('<input>').attr(
143145
{
144146
type: 'hidden',
145-
id: 'braintree_nonce',
147+
id: this.getNonceSelectorName(),
146148
name: 'payment[payment_method_nonce]'
147149
}
148150
);
149151

150-
$input.appendTo(this.$selector);
152+
$input.appendTo(this.$container);
151153
$input.prop('disabled', false);
152154
}
153155
},
@@ -160,6 +162,14 @@ define([
160162
alert({
161163
content: message
162164
});
165+
},
166+
167+
/**
168+
* Get selector name for nonce input
169+
* @returns {String}
170+
*/
171+
getNonceSelectorName: function () {
172+
return 'nonce_' + this.getCode();
163173
}
164174
});
165175
});

0 commit comments

Comments
 (0)