Skip to content

Commit 8cef0bb

Browse files
MAGETWO-59600: Errors for invalid addresses from payment gateway are not displayed on Checkout Flow
1 parent 71db48a commit 8cef0bb

File tree

2 files changed

+114
-90
lines changed

2 files changed

+114
-90
lines changed

app/code/Magento/Paypal/view/frontend/web/js/in-context/express-checkout.js

Lines changed: 80 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,88 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5-
define(
6-
[
7-
'underscore',
8-
'jquery',
9-
'uiComponent',
10-
'paypalInContextExpressCheckout',
11-
'Magento_Customer/js/customer-data',
12-
'domReady!'
13-
],
14-
function (
15-
_,
16-
$,
17-
Component,
18-
paypalExpressCheckout,
19-
customerData
20-
) {
21-
'use strict';
22-
23-
return Component.extend({
24-
25-
defaults: {
26-
clientConfig: {
27-
28-
checkoutInited: false,
29-
30-
/**
31-
* @param {Object} event
32-
*/
33-
click: function (event) {
34-
$('body').trigger('processStart');
35-
36-
event.preventDefault();
37-
38-
if (!this.clientConfig.checkoutInited) {
39-
paypalExpressCheckout.checkout.initXO();
40-
this.clientConfig.checkoutInited = true;
41-
} else {
42-
paypalExpressCheckout.checkout.closeFlow();
43-
}
5+
define([
6+
'underscore',
7+
'jquery',
8+
'uiComponent',
9+
'paypalInContextExpressCheckout',
10+
'Magento_Customer/js/customer-data',
11+
'domReady!'
12+
], function (_, $, Component, paypalExpressCheckout, customerData) {
13+
'use strict';
4414

45-
$.get(
46-
this.path,
47-
{
48-
button: 1
49-
}
50-
).done(
51-
function (response) {
52-
if (response && response.url) {
53-
paypalExpressCheckout.checkout.startFlow(response.url);
54-
55-
return;
56-
}
57-
58-
paypalExpressCheckout.checkout.closeFlow();
59-
}
60-
).fail(
61-
function () {
62-
paypalExpressCheckout.checkout.closeFlow();
63-
}
64-
).always(
65-
function () {
66-
$('body').trigger('processStop');
67-
customerData.invalidate(['cart']);
68-
}
69-
);
70-
}
71-
}
72-
},
73-
74-
/**
75-
* @returns {Object}
76-
*/
77-
initialize: function () {
78-
this._super();
79-
80-
return this.initClient();
81-
},
82-
83-
/**
84-
* @returns {Object}
85-
*/
86-
initClient: function () {
87-
_.each(this.clientConfig, function (fn, name) {
88-
if (typeof fn === 'function') {
89-
this.clientConfig[name] = fn.bind(this);
15+
return Component.extend({
16+
17+
defaults: {
18+
clientConfig: {
19+
20+
checkoutInited: false,
21+
22+
/**
23+
* @param {Object} event
24+
*/
25+
click: function (event) {
26+
$('body').trigger('processStart');
27+
28+
event.preventDefault();
29+
30+
if (!this.clientConfig.checkoutInited) {
31+
paypalExpressCheckout.checkout.initXO();
32+
this.clientConfig.checkoutInited = true;
33+
} else {
34+
paypalExpressCheckout.checkout.closeFlow();
9035
}
91-
}, this);
9236

93-
paypalExpressCheckout.checkout.setup(this.merchantId, this.clientConfig);
37+
$.get(this.path, {
38+
button: 1
39+
}).done(function (response) {
40+
var message = response && response.message;
41+
42+
if (message) {
43+
customerData.set('messages', {
44+
messages: [message]
45+
});
46+
}
47+
48+
if (response && response.url) {
49+
paypalExpressCheckout.checkout.startFlow(response.url);
50+
51+
return;
52+
}
9453

95-
return this;
54+
paypalExpressCheckout.checkout.closeFlow();
55+
}).fail(function () {
56+
paypalExpressCheckout.checkout.closeFlow();
57+
}).always(function () {
58+
$('body').trigger('processStop');
59+
customerData.invalidate(['cart']);
60+
});
61+
}
9662
}
97-
});
98-
}
99-
);
63+
},
64+
65+
/**
66+
* @returns {Object}
67+
*/
68+
initialize: function () {
69+
this._super();
70+
71+
return this.initClient();
72+
},
73+
74+
/**
75+
* @returns {Object}
76+
*/
77+
initClient: function () {
78+
_.each(this.clientConfig, function (fn, name) {
79+
if (typeof fn === 'function') {
80+
this.clientConfig[name] = fn.bind(this);
81+
}
82+
}, this);
83+
84+
paypalExpressCheckout.checkout.setup(this.merchantId, this.clientConfig);
85+
86+
return this;
87+
}
88+
});
89+
});

dev/tests/js/jasmine/tests/app/code/Magento/Paypal/frontend/js/in-context/express-checkout.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ define([
2121
checkout: jasmine.createSpyObj('checkout',
2222
['setup', 'initXO', 'startFlow', 'closeFlow']
2323
)
24+
},
25+
'Magento_Customer/js/customer-data': {
26+
set: jasmine.createSpy(),
27+
invalidate: jasmine.createSpy()
2428
}
2529
};
2630

@@ -87,6 +91,36 @@ define([
8791
jasmine.arrayContaining(['processStart'], ['processStop'])
8892
);
8993
});
94+
95+
it('Check call "click" method', function () {
96+
var message = {
97+
text: 'text',
98+
type: 'error'
99+
};
100+
101+
spyOn(jQuery.fn, 'trigger');
102+
spyOn(jQuery, 'get').and.callFake(function () {
103+
var d = $.Deferred();
104+
105+
d.resolve({
106+
message: message
107+
});
108+
109+
return d.promise();
110+
});
111+
112+
model.clientConfig.click(event);
113+
expect(mocks['Magento_Customer/js/customer-data'].set).toHaveBeenCalledWith('messages', {
114+
messages: [message]
115+
});
116+
expect(event.preventDefault).toHaveBeenCalled();
117+
expect(paypalExpressCheckout.checkout.initXO).toHaveBeenCalled();
118+
expect(model.clientConfig.checkoutInited).toEqual(true);
119+
expect(jQuery.get).toHaveBeenCalled();
120+
expect(jQuery('body').trigger).toHaveBeenCalledWith(
121+
jasmine.arrayContaining(['processStart'], ['processStop'])
122+
);
123+
});
90124
});
91125
});
92126
});

0 commit comments

Comments
 (0)