Skip to content

Commit ee31fd1

Browse files
Merge remote-tracking branch 'origin/MAGETWO-69340' into MPI-PR206
2 parents 48a8cda + 2379669 commit ee31fd1

File tree

11 files changed

+299
-29
lines changed

11 files changed

+299
-29
lines changed

app/code/Magento/Braintree/Block/Paypal/Button.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66
namespace Magento\Braintree\Block\Paypal;
77

8-
use Magento\Checkout\Model\Session;
8+
use Magento\Braintree\Gateway\Config\PayPal\Config;
9+
use Magento\Braintree\Model\Ui\ConfigProvider;
910
use Magento\Catalog\Block\ShortcutInterface;
10-
use Magento\Framework\View\Element\Template;
11+
use Magento\Checkout\Model\Session;
1112
use Magento\Framework\Locale\ResolverInterface;
12-
use Magento\Braintree\Model\Ui\ConfigProvider;
13+
use Magento\Framework\View\Element\Template;
1314
use Magento\Framework\View\Element\Template\Context;
14-
use Magento\Braintree\Gateway\Config\PayPal\Config;
1515
use Magento\Payment\Model\MethodInterface;
1616

1717
/**
@@ -110,7 +110,7 @@ public function getContainerId()
110110
*/
111111
public function getLocale()
112112
{
113-
return strtolower($this->localeResolver->getLocale());
113+
return $this->localeResolver->getLocale();
114114
}
115115

116116
/**
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Braintree\Model;
7+
8+
use Magento\Framework\Locale\ResolverInterface;
9+
use Magento\Braintree\Gateway\Config\PayPal\Config;
10+
11+
class LocaleResolver implements ResolverInterface
12+
{
13+
/**
14+
* @var ResolverInterface
15+
*/
16+
private $resolver;
17+
18+
/**
19+
* @var Config
20+
*/
21+
private $config;
22+
23+
/**
24+
* @param ResolverInterface $resolver
25+
* @param Config $config
26+
*/
27+
public function __construct(ResolverInterface $resolver, Config $config)
28+
{
29+
$this->resolver = $resolver;
30+
$this->config = $config;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function getDefaultLocalePath()
37+
{
38+
return $this->resolver->getDefaultLocalePath();
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function setDefaultLocale($locale)
45+
{
46+
return $this->resolver->setDefaultLocale($locale);
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function getDefaultLocale()
53+
{
54+
return $this->resolver->getDefaultLocale();
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function setLocale($locale = null)
61+
{
62+
return $this->resolver->setLocale($locale);
63+
}
64+
65+
/**
66+
* Gets store's locale or the `en_US` locale if store's locale does not supported by PayPal.
67+
*
68+
* @return string
69+
*/
70+
public function getLocale()
71+
{
72+
$locale = $this->resolver->getLocale();
73+
$allowedLocales = $this->config->getValue('supported_locales');
74+
75+
return strpos($allowedLocales, $locale) !== false ? $locale : 'en_US';
76+
}
77+
78+
/**
79+
* @inheritdoc
80+
*/
81+
public function emulate($scopeId)
82+
{
83+
return $this->resolver->emulate($scopeId);
84+
}
85+
86+
/**
87+
* @inheritdoc
88+
*/
89+
public function revert()
90+
{
91+
return $this->resolver->revert();
92+
}
93+
}

app/code/Magento/Braintree/Model/Ui/PayPal/ConfigProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getConfig()
5454
'title' => $this->config->getTitle(),
5555
'isAllowShippingAddressOverride' => $this->config->isAllowToEditShippingAddress(),
5656
'merchantName' => $this->config->getMerchantName(),
57-
'locale' => strtolower($this->resolver->getLocale()),
57+
'locale' => $this->resolver->getLocale(),
5858
'paymentAcceptanceMarkSrc' =>
5959
'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png',
6060
'vaultCode' => self::PAYPAL_VAULT_CODE,

app/code/Magento/Braintree/Test/Unit/Model/Ui/PayPal/ConfigProviderTest.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,42 +49,35 @@ protected function setUp()
4949
/**
5050
* Run test getConfig method
5151
*
52-
* @param array $config
52+
* @param array $expected
5353
* @dataProvider getConfigDataProvider
5454
*/
5555
public function testGetConfig($expected)
5656
{
57-
$this->config->expects(static::once())
58-
->method('isActive')
57+
$this->config->method('isActive')
5958
->willReturn(true);
6059

61-
$this->config->expects(static::once())
62-
->method('isAllowToEditShippingAddress')
60+
$this->config->method('isAllowToEditShippingAddress')
6361
->willReturn(true);
6462

65-
$this->config->expects(static::once())
66-
->method('getMerchantName')
63+
$this->config->method('getMerchantName')
6764
->willReturn('Test');
6865

69-
$this->config->expects(static::once())
70-
->method('getTitle')
66+
$this->config->method('getTitle')
7167
->willReturn('Payment Title');
7268

73-
$this->localeResolver->expects(static::once())
74-
->method('getLocale')
69+
$this->localeResolver->method('getLocale')
7570
->willReturn('en_US');
7671

77-
$this->config->expects(static::once())
78-
->method('isSkipOrderReview')
72+
$this->config->method('isSkipOrderReview')
7973
->willReturn(false);
8074

81-
$this->config->expects(static::once())
82-
->method('getPayPalIcon')
75+
$this->config->method('getPayPalIcon')
8376
->willReturn([
8477
'width' => 30, 'height' => 26, 'url' => 'https://icon.test.url'
8578
]);
8679

87-
static::assertEquals($expected, $this->configProvider->getConfig());
80+
self::assertEquals($expected, $this->configProvider->getConfig());
8881
}
8982

9083
/**
@@ -101,7 +94,7 @@ public function getConfigDataProvider()
10194
'title' => 'Payment Title',
10295
'isAllowShippingAddressOverride' => true,
10396
'merchantName' => 'Test',
104-
'locale' => 'en_us',
97+
'locale' => 'en_US',
10598
'paymentAcceptanceMarkSrc' =>
10699
'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png',
107100
'vaultCode' => ConfigProvider::PAYPAL_VAULT_CODE,

app/code/Magento/Braintree/etc/config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<order_status>processing</order_status>
3535
<environment>sandbox</environment>
3636
<allowspecific>0</allowspecific>
37-
<sdk_url><![CDATA[https://js.braintreegateway.com/js/braintree-2.25.0.min.js]]></sdk_url>
37+
<sdk_url><![CDATA[https://js.braintreegateway.com/js/braintree-2.32.0.min.js]]></sdk_url>
3838
<public_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
3939
<private_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
4040
<masked_fields>cvv,number</masked_fields>
@@ -66,6 +66,7 @@
6666
<can_capture_vault>1</can_capture_vault>
6767
<privateInfoKeys>processorResponseCode,processorResponseText,paymentId</privateInfoKeys>
6868
<paymentInfoKeys>processorResponseCode,processorResponseText,paymentId,payerEmail</paymentInfoKeys>
69+
<supported_locales>en_US,en_GB,en_AU,da_DK,fr_FR,fr_CA,de_DE,zh_HK,it_IT,nl_NL,no_NO,pl_PL,es_ES,sv_SE,tr_TR,pt_BR,ja_JP,id_ID,ko_KR,pt_PT,ru_RU,th_TH,zh_CN,zh_TW</supported_locales>
6970
</braintree_paypal>
7071
<braintree_cc_vault>
7172
<model>BraintreeCreditCardVaultFacade</model>

app/code/Magento/Braintree/etc/frontend/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
<type name="Magento\Braintree\Block\Paypal\Button">
4848
<arguments>
49+
<argument name="localeResolver" xsi:type="object">Magento\Braintree\Model\LocaleResolver</argument>
4950
<argument name="data" xsi:type="array">
5051
<item name="template" xsi:type="string">Magento_Braintree::paypal/button.phtml</item>
5152
<item name="alias" xsi:type="string">braintree.paypal.mini-cart</item>
@@ -54,4 +55,10 @@
5455
<argument name="payment" xsi:type="object">BraintreePayPalFacade</argument>
5556
</arguments>
5657
</type>
58+
59+
<type name="Magento\Braintree\Model\Ui\PayPal\ConfigProvider">
60+
<arguments>
61+
<argument name="resolver" xsi:type="object">Magento\Braintree\Model\LocaleResolver</argument>
62+
</arguments>
63+
</type>
5764
</config>

app/code/Magento/Braintree/view/frontend/requirejs-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
var config = {
77
map: {
88
'*': {
9-
braintree: 'https://js.braintreegateway.com/js/braintree-2.25.0.min.js'
9+
braintree: 'https://js.braintreegateway.com/js/braintree-2.32.0.min.js'
1010
}
1111
}
1212
};

app/code/Magento/Braintree/view/frontend/web/js/paypal/button.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ define(
105105
event.preventDefault();
106106

107107
registry.get(self.integrationName, function (integration) {
108-
integration.paypal.initAuthFlow();
108+
try {
109+
integration.paypal.initAuthFlow();
110+
} catch (e) {
111+
$this.attr('disabled', 'disabled');
112+
}
109113
});
110114
});
111115
}.bind(this);

app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/paypal.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ define([
1313
'Magento_Checkout/js/model/full-screen-loader',
1414
'Magento_Checkout/js/model/payment/additional-validators',
1515
'Magento_Vault/js/view/payment/vault-enabler',
16-
'Magento_Checkout/js/action/create-billing-address'
16+
'Magento_Checkout/js/action/create-billing-address',
17+
'mage/translate'
1718
], function (
1819
$,
1920
_,
@@ -23,7 +24,8 @@ define([
2324
fullScreenLoader,
2425
additionalValidators,
2526
VaultEnabler,
26-
createBillingAddress
27+
createBillingAddress,
28+
$t
2729
) {
2830
'use strict';
2931

@@ -403,7 +405,13 @@ define([
403405
*/
404406
payWithPayPal: function () {
405407
if (additionalValidators.validate()) {
406-
Braintree.checkout.paypal.initAuthFlow();
408+
try {
409+
Braintree.checkout.paypal.initAuthFlow();
410+
} catch (e) {
411+
this.messageContainer.addErrorMessage({
412+
message: $t('Payment ' + this.getTitle() + ' can\'t be initialized.')
413+
});
414+
}
407415
}
408416
},
409417

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint-disable max-nested-callbacks */
7+
define([
8+
'squire',
9+
'jquery'
10+
], function (Squire) {
11+
'use strict';
12+
13+
describe('Magento_Braintree/js/paypal/button', function () {
14+
var injector,
15+
mocks,
16+
braintree,
17+
component,
18+
registry,
19+
btnId = 'braintree_paypal_btn',
20+
tplElement = jQuery('<button id="' + btnId + '"></button>')[0];
21+
22+
require.config({
23+
map: {
24+
'*': {
25+
'braintree': 'braintree'
26+
}
27+
}
28+
});
29+
30+
injector = new Squire();
31+
mocks = {
32+
'braintree': {
33+
paypal: {
34+
/** Stub */
35+
initAuthFlow: function () {}
36+
},
37+
38+
/** Stub */
39+
setup: function () {}
40+
}
41+
};
42+
43+
beforeEach(function (done) {
44+
injector.mock(mocks);
45+
46+
injector.require([
47+
'braintree',
48+
'uiRegistry',
49+
'Magento_Braintree/js/paypal/button'
50+
], function (adapter, reg, Constr) {
51+
braintree = adapter;
52+
registry = reg;
53+
jQuery(document.body).append(tplElement);
54+
55+
spyOn(braintree, 'setup').and.callFake(function () {
56+
registry.set('braintreePaypal.currentIntegration', braintree);
57+
jQuery('#' + btnId).removeAttr('disabled');
58+
});
59+
60+
component = new Constr({
61+
id: btnId
62+
});
63+
done();
64+
});
65+
});
66+
67+
afterAll(function (done) {
68+
tplElement.remove();
69+
registry.remove(component.integrationName);
70+
done();
71+
});
72+
73+
it('The PayPal::initAuthFlow throws an exception.', function () {
74+
var $selector = jQuery('#' + component.id);
75+
76+
spyOn(braintree.paypal, 'initAuthFlow').and.callFake(function () {
77+
throw new TypeError('Cannot read property of undefined');
78+
});
79+
80+
$selector.trigger('click');
81+
82+
expect($selector.prop('disabled')).toEqual(true);
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)