Skip to content

Commit

Permalink
Merge pull request #1189 from magento-mpi/MPI-PR206
Browse files Browse the repository at this point in the history
MPI-PR206-2
  • Loading branch information
viktym authored Jun 13, 2017
2 parents 8545fd8 + 2312b77 commit 2c89e48
Show file tree
Hide file tree
Showing 16 changed files with 505 additions and 67 deletions.
10 changes: 5 additions & 5 deletions app/code/Magento/Braintree/Block/Paypal/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/
namespace Magento\Braintree\Block\Paypal;

use Magento\Checkout\Model\Session;
use Magento\Braintree\Gateway\Config\PayPal\Config;
use Magento\Braintree\Model\Ui\ConfigProvider;
use Magento\Catalog\Block\ShortcutInterface;
use Magento\Framework\View\Element\Template;
use Magento\Checkout\Model\Session;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Braintree\Model\Ui\ConfigProvider;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Braintree\Gateway\Config\PayPal\Config;
use Magento\Payment\Model\MethodInterface;

/**
Expand Down Expand Up @@ -110,7 +110,7 @@ public function getContainerId()
*/
public function getLocale()
{
return strtolower($this->localeResolver->getLocale());
return $this->localeResolver->getLocale();
}

/**
Expand Down
93 changes: 93 additions & 0 deletions app/code/Magento/Braintree/Model/LocaleResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Braintree\Model;

use Magento\Framework\Locale\ResolverInterface;
use Magento\Braintree\Gateway\Config\PayPal\Config;

class LocaleResolver implements ResolverInterface
{
/**
* @var ResolverInterface
*/
private $resolver;

/**
* @var Config
*/
private $config;

/**
* @param ResolverInterface $resolver
* @param Config $config
*/
public function __construct(ResolverInterface $resolver, Config $config)
{
$this->resolver = $resolver;
$this->config = $config;
}

/**
* @inheritdoc
*/
public function getDefaultLocalePath()
{
return $this->resolver->getDefaultLocalePath();
}

/**
* @inheritdoc
*/
public function setDefaultLocale($locale)
{
return $this->resolver->setDefaultLocale($locale);
}

/**
* @inheritdoc
*/
public function getDefaultLocale()
{
return $this->resolver->getDefaultLocale();
}

/**
* @inheritdoc
*/
public function setLocale($locale = null)
{
return $this->resolver->setLocale($locale);
}

/**
* Gets store's locale or the `en_US` locale if store's locale does not supported by PayPal.
*
* @return string
*/
public function getLocale()
{
$locale = $this->resolver->getLocale();
$allowedLocales = $this->config->getValue('supported_locales');

return strpos($allowedLocales, $locale) !== false ? $locale : 'en_US';
}

/**
* @inheritdoc
*/
public function emulate($scopeId)
{
return $this->resolver->emulate($scopeId);
}

/**
* @inheritdoc
*/
public function revert()
{
return $this->resolver->revert();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getConfig()
'title' => $this->config->getTitle(),
'isAllowShippingAddressOverride' => $this->config->isAllowToEditShippingAddress(),
'merchantName' => $this->config->getMerchantName(),
'locale' => strtolower($this->resolver->getLocale()),
'locale' => $this->resolver->getLocale(),
'paymentAcceptanceMarkSrc' =>
'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png',
'vaultCode' => self::PAYPAL_VAULT_CODE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,35 @@ protected function setUp()
/**
* Run test getConfig method
*
* @param array $config
* @param array $expected
* @dataProvider getConfigDataProvider
*/
public function testGetConfig($expected)
{
$this->config->expects(static::once())
->method('isActive')
$this->config->method('isActive')
->willReturn(true);

$this->config->expects(static::once())
->method('isAllowToEditShippingAddress')
$this->config->method('isAllowToEditShippingAddress')
->willReturn(true);

$this->config->expects(static::once())
->method('getMerchantName')
$this->config->method('getMerchantName')
->willReturn('Test');

$this->config->expects(static::once())
->method('getTitle')
$this->config->method('getTitle')
->willReturn('Payment Title');

$this->localeResolver->expects(static::once())
->method('getLocale')
$this->localeResolver->method('getLocale')
->willReturn('en_US');

$this->config->expects(static::once())
->method('isSkipOrderReview')
$this->config->method('isSkipOrderReview')
->willReturn(false);

$this->config->expects(static::once())
->method('getPayPalIcon')
$this->config->method('getPayPalIcon')
->willReturn([
'width' => 30, 'height' => 26, 'url' => 'https://icon.test.url'
]);

static::assertEquals($expected, $this->configProvider->getConfig());
self::assertEquals($expected, $this->configProvider->getConfig());
}

/**
Expand All @@ -101,7 +94,7 @@ public function getConfigDataProvider()
'title' => 'Payment Title',
'isAllowShippingAddressOverride' => true,
'merchantName' => 'Test',
'locale' => 'en_us',
'locale' => 'en_US',
'paymentAcceptanceMarkSrc' =>
'https://www.paypalobjects.com/webstatic/en_US/i/buttons/pp-acceptance-medium.png',
'vaultCode' => ConfigProvider::PAYPAL_VAULT_CODE,
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Braintree/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<order_status>processing</order_status>
<environment>sandbox</environment>
<allowspecific>0</allowspecific>
<sdk_url><![CDATA[https://js.braintreegateway.com/js/braintree-2.25.0.min.js]]></sdk_url>
<sdk_url><![CDATA[https://js.braintreegateway.com/js/braintree-2.32.0.min.js]]></sdk_url>
<public_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<private_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<masked_fields>cvv,number</masked_fields>
Expand Down Expand Up @@ -66,6 +66,7 @@
<can_capture_vault>1</can_capture_vault>
<privateInfoKeys>processorResponseCode,processorResponseText,paymentId</privateInfoKeys>
<paymentInfoKeys>processorResponseCode,processorResponseText,paymentId,payerEmail</paymentInfoKeys>
<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>
</braintree_paypal>
<braintree_cc_vault>
<model>BraintreeCreditCardVaultFacade</model>
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/Braintree/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

<type name="Magento\Braintree\Block\Paypal\Button">
<arguments>
<argument name="localeResolver" xsi:type="object">Magento\Braintree\Model\LocaleResolver</argument>
<argument name="data" xsi:type="array">
<item name="template" xsi:type="string">Magento_Braintree::paypal/button.phtml</item>
<item name="alias" xsi:type="string">braintree.paypal.mini-cart</item>
Expand All @@ -54,4 +55,10 @@
<argument name="payment" xsi:type="object">BraintreePayPalFacade</argument>
</arguments>
</type>

<type name="Magento\Braintree\Model\Ui\PayPal\ConfigProvider">
<arguments>
<argument name="resolver" xsi:type="object">Magento\Braintree\Model\LocaleResolver</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var config = {
map: {
'*': {
braintree: 'https://js.braintreegateway.com/js/braintree-2.25.0.min.js'
braintree: 'https://js.braintreegateway.com/js/braintree-2.32.0.min.js'
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ define(
event.preventDefault();

registry.get(self.integrationName, function (integration) {
integration.paypal.initAuthFlow();
try {
integration.paypal.initAuthFlow();
} catch (e) {
$this.attr('disabled', 'disabled');
}
});
});
}.bind(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ define([
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/model/payment/additional-validators',
'Magento_Vault/js/view/payment/vault-enabler',
'Magento_Checkout/js/action/create-billing-address'
'Magento_Checkout/js/action/create-billing-address',
'mage/translate'
], function (
$,
_,
Expand All @@ -23,7 +24,8 @@ define([
fullScreenLoader,
additionalValidators,
VaultEnabler,
createBillingAddress
createBillingAddress,
$t
) {
'use strict';

Expand Down Expand Up @@ -403,7 +405,13 @@ define([
*/
payWithPayPal: function () {
if (additionalValidators.validate()) {
Braintree.checkout.paypal.initAuthFlow();
try {
Braintree.checkout.paypal.initAuthFlow();
} catch (e) {
this.messageContainer.addErrorMessage({
message: $t('Payment ' + this.getTitle() + ' can\'t be initialized.')
});
}
}
},

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"ext-openssl": "*",
"ext-zip": "*",
"ext-pdo_mysql": "*",
"ext-soap": "*",
"sjparkinson/static-review": "~4.1",
"ramsey/uuid": "3.6.1"
},
Expand Down
7 changes: 4 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/* eslint-disable max-nested-callbacks */
define([
'squire',
'jquery'
], function (Squire) {
'use strict';

describe('Magento_Braintree/js/paypal/button', function () {
var injector,
mocks,
braintree,
component,
registry,
btnId = 'braintree_paypal_btn',
tplElement = jQuery('<button id="' + btnId + '"></button>')[0];

require.config({
map: {
'*': {
'braintree': 'braintree'
}
}
});

injector = new Squire();
mocks = {
'braintree': {
paypal: {
/** Stub */
initAuthFlow: function () {}
},

/** Stub */
setup: function () {}
}
};

beforeEach(function (done) {
injector.mock(mocks);

injector.require([
'braintree',
'uiRegistry',
'Magento_Braintree/js/paypal/button'
], function (adapter, reg, Constr) {
braintree = adapter;
registry = reg;
jQuery(document.body).append(tplElement);

spyOn(braintree, 'setup').and.callFake(function () {
registry.set('braintreePaypal.currentIntegration', braintree);
jQuery('#' + btnId).removeAttr('disabled');
});

component = new Constr({
id: btnId
});
done();
});
});

afterAll(function (done) {
tplElement.remove();
registry.remove(component.integrationName);
done();
});

it('The PayPal::initAuthFlow throws an exception.', function () {
var $selector = jQuery('#' + component.id);

spyOn(braintree.paypal, 'initAuthFlow').and.callFake(function () {
throw new TypeError('Cannot read property of undefined');
});

$selector.trigger('click');

expect($selector.prop('disabled')).toEqual(true);
});
});
});
Loading

0 comments on commit 2c89e48

Please sign in to comment.