Skip to content

Commit ae70cca

Browse files
committed
Merge branch 'MAGETWO-44658' of github.corp.magento.com:magento-mpi/magento2ce into MAGETWO-44603
2 parents 9a735e4 + b47d9ca commit ae70cca

File tree

4 files changed

+60
-39
lines changed

4 files changed

+60
-39
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class ConfigProvider extends CcGenericConfigProvider
1414
{
15+
1516
/**
1617
* @var string[]
1718
*/
@@ -97,8 +98,7 @@ public function getStoredCards()
9798
*/
9899
protected function getCcAvailableCcTypes()
99100
{
100-
$country = $this->checkoutSession->getQuote()->getBillingAddress()->getCountryId();
101-
return $this->dataHelper->getCcAvailableCardTypes($country);
101+
return $this->dataHelper->getCcAvailableCardTypes();
102102
}
103103

104104
/**
@@ -185,7 +185,7 @@ public function getConfig()
185185
'isCcDetectionEnabled' => $this->config->isCcDetectionEnabled(),
186186
'availableCardTypes' => $this->getCcAvailableCcTypes(),
187187
'braintreeDataJs'=> $this->config->getBraintreeDataJs(),
188-
'ajaxGenerateNonceUrl' => $this->getAjaxGenerateNonceUrl(),
188+
'ajaxGenerateNonceUrl' => $this->getAjaxGenerateNonceUrl()
189189
],
190190
],
191191
]);

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Braintree\Test\Unit\Model;
88

9+
use Magento\Braintree\Model\ConfigProvider;
910
use Magento\Braintree\Model\PaymentMethod;
1011
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1112

@@ -193,16 +194,6 @@ public function testGetConfig(
193194
->method('generatePaymentMethodToken')
194195
->willReturnMap($tokenNonceMap);
195196

196-
$quoteMock = $this->getMockBuilder('\Magento\Quote\Model\Quote')
197-
->disableOriginalConstructor()
198-
->getMock();
199-
$quoteMock->expects($this->once())
200-
->method('getBillingAddress')
201-
->willReturn(new \Magento\Framework\DataObject(['country_id' => 'US']));
202-
$this->checkoutSessionMock->expects($this->once())
203-
->method('getQuote')
204-
->willReturn($quoteMock);
205-
206197
$cardTypeMap = [
207198
['Visa', 'VI'],
208199
['Master Card', 'MA'],
@@ -272,7 +263,7 @@ public function getConfigDataProvider()
272263
'isCcDetectionEnabled' => true,
273264
'availableCardTypes' => $this->availableCardTypes,
274265
'braintreeDataJs'=> 'https://js.braintreegateway.com/v1/braintree-data.js',
275-
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL,
266+
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL
276267
],
277268
],
278269
]
@@ -347,7 +338,7 @@ public function getConfigDataProvider()
347338
'isCcDetectionEnabled' => true,
348339
'availableCardTypes' => $this->availableCardTypes,
349340
'braintreeDataJs'=> 'https://js.braintreegateway.com/v1/braintree-data.js',
350-
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL,
341+
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL
351342
],
352343
],
353344
]
@@ -422,7 +413,7 @@ public function getConfigDataProvider()
422413
'isCcDetectionEnabled' => true,
423414
'availableCardTypes' => $this->availableCardTypes,
424415
'braintreeDataJs'=> 'https://js.braintreegateway.com/v1/braintree-data.js',
425-
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL,
416+
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL
426417
],
427418
],
428419
]
@@ -466,7 +457,7 @@ public function getConfigDataProvider()
466457
'isCcDetectionEnabled' => true,
467458
'availableCardTypes' => $this->availableCardTypes,
468459
'braintreeDataJs'=> 'https://js.braintreegateway.com/v1/braintree-data.js',
469-
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL,
460+
'ajaxGenerateNonceUrl' => self::PAYMENT_NONCE_GENERATION_URL
470461
],
471462
],
472463
]

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

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ define(
1212
'underscore',
1313
'jquery',
1414
'Magento_Ui/js/model/messageList',
15-
'mage/translate'
15+
'mage/translate',
16+
'uiRegistry',
17+
'mage/utils/wrapper'
1618
],
17-
function (ko, Component, setPaymentInformationAction, quote, braintreeClientSDK, _, $, messageList, $t) {
19+
function (
20+
ko,
21+
Component,
22+
setPaymentInformationAction,
23+
quote,
24+
braintreeClientSDK,
25+
_,
26+
$,
27+
messageList,
28+
$t
29+
) {
1830
'use strict';
1931
var configBraintree= window.checkoutConfig.payment.braintree;
2032
return Component.extend({
21-
2233
placeOrderHandler: null,
2334
validateHandler: null,
2435
setPlaceOrderHandler: function(handler) {
@@ -52,7 +63,8 @@ define(
5263
storedCards: configBraintree ? configBraintree.storedCards : {},
5364
availableCardTypes: configBraintree ? configBraintree.availableCardTypes : {},
5465
creditCardExpMonth: null,
55-
creditCardExpYear: null
66+
creditCardExpYear: null,
67+
lastBillingAddress: null
5668
},
5769
initVars: function() {
5870
this.ajaxGenerateNonceUrl = configBraintree ? configBraintree.ajaxGenerateNonceUrl : '';
@@ -77,8 +89,10 @@ define(
7789
* @override
7890
*/
7991
initObservable: function () {
92+
var self = this;
8093
this.initVars();
8194
this._super()
95+
.track('availableCcValues')
8296
.observe([
8397
'selectedCardToken',
8498
'storeInVault',
@@ -106,6 +120,11 @@ define(
106120
this.messageContainer.addErrorMessage({'message': $t('Can not initialize PayPal (Braintree)')});
107121
}
108122

123+
// subscribe on billing address update
124+
quote.billingAddress.subscribe(function () {
125+
self.updateAvailableTypeValues();
126+
});
127+
109128
return this;
110129
},
111130
/**
@@ -293,26 +312,30 @@ define(
293312
}
294313
return filteredCards;
295314
},
296-
getCcAvailableTypes: function() {
297-
var billingAddress = quote.billingAddress;
298-
var billingCountryId = billingAddress.countryId;
299-
if (typeof billingCountryId == 'undefined') {
300-
billingCountryId = billingAddress.country_id;
301-
}
315+
316+
/**
317+
* Get list of available CC types
318+
*/
319+
getCcAvailableTypes: function () {
302320
var availableTypes = configBraintree.availableCardTypes;
303-
var countrySpecificCardTypeConfig = configBraintree.countrySpecificCardTypes;
304-
if (billingCountryId && typeof countrySpecificCardTypeConfig.billingCountryId != 'undefined') {
305-
var countrySpecificCardTypes = countrySpecificCardTypeConfig[billingCountryId];
306-
if (typeof countrySpecificCardTypes != 'undefined') {
307-
var filteredTypes = {};
321+
var billingAddress = quote.billingAddress();
322+
this.lastBillingAddress = quote.shippingAddress();
323+
if (!billingAddress) {
324+
billingAddress = this.lastBillingAddress;
325+
}
326+
var billingCountryId = billingAddress.countryId;
327+
if (billingCountryId &&
328+
typeof configBraintree.countrySpecificCardTypes[billingCountryId] !== 'undefined'
329+
) {
330+
var countrySpecificCardTypes = configBraintree.countrySpecificCardTypes[billingCountryId];
331+
var filteredTypes = {};
308332

309-
for (var key in availableTypes) {
310-
if (_.indexOf(countrySpecificCardTypes, key) != -1) {
311-
filteredTypes[key] = availableTypes[key];
312-
}
333+
for (var key in availableTypes) {
334+
if (_.indexOf(countrySpecificCardTypes, key) != -1) {
335+
filteredTypes[key] = availableTypes[key];
313336
}
314-
return filteredTypes;
315337
}
338+
return filteredTypes;
316339
}
317340
return availableTypes;
318341
},
@@ -353,6 +376,13 @@ define(
353376

354377
getCssClass: function () {
355378
return (this.isCcDetectionEnabled()) ? 'field type detection' : 'field type required';
379+
},
380+
381+
/**
382+
* Update list of available CC types values
383+
*/
384+
updateAvailableTypeValues: function () {
385+
this.availableCcValues = this.getCcAvailableTypesValues();
356386
}
357387
});
358388
}

app/code/Magento/Braintree/view/frontend/web/template/payment/cc-form.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
data-bind="attr: {id: getCode() + '_cc_type', 'data-container': getCode() + '-cc-type', 'data-validate': JSON.stringify({required:true, 'validate-cc-type-select':'#' + getCode() + '_cc_number'})},
7171
mageInit: {creditCardType:{creditCardTypeContainer:'#' + getCode() + '_cc_type_ss_div'}},
7272
enable: true,
73-
options: getCcAvailableTypesValues(),
73+
options: availableCcValues,
7474
optionsValue: 'value',
7575
optionsText: 'type',
7676
optionsCaption: $t('--Please Select--'),
@@ -79,7 +79,7 @@
7979
<!-- /ko -->
8080
<!-- ko if: (isCcDetectionEnabled())-->
8181
<ul class="credit-card-types">
82-
<!-- ko foreach: {data: getCcAvailableTypesValues(), as: 'item'} -->
82+
<!-- ko foreach: {data: availableCcValues, as: 'item'} -->
8383
<li class="item" data-bind="css: {
8484
_active: $parent.selectedCardType() == item.value,
8585
_inactive: $parent.selectedCardType() != null && $parent.selectedCardType() != item.value

0 commit comments

Comments
 (0)