Skip to content

Commit 58135bc

Browse files
MAGETWO-59600: Errors for invalid addresses from payment gateway are not displayed on Checkout Flow
1 parent ae53ec1 commit 58135bc

File tree

3 files changed

+93
-46
lines changed

3 files changed

+93
-46
lines changed

app/code/Magento/Paypal/Controller/Express/GetToken.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Exception\LocalizedException;
1414
use Magento\Framework\Webapi\Exception;
1515
use Magento\Paypal\Model\Express\Checkout;
16+
use Magento\Framework\App\ObjectManager;
1617

1718
/**
1819
* Class GetToken
@@ -40,6 +41,48 @@ class GetToken extends AbstractExpress
4041
*/
4142
protected $_checkoutType = \Magento\Paypal\Model\Express\Checkout::class;
4243

44+
/**
45+
* @var \Psr\Log\LoggerInterface
46+
*/
47+
private $_logger;
48+
49+
/**
50+
* @param \Magento\Framework\App\Action\Context $context
51+
* @param \Magento\Customer\Model\Session $customerSession
52+
* @param \Magento\Checkout\Model\Session $checkoutSession
53+
* @param \Magento\Sales\Model\OrderFactory $orderFactory
54+
* @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
55+
* @param \Magento\Framework\Session\Generic $paypalSession
56+
* @param \Magento\Framework\Url\Helper\Data $urlHelper
57+
* @param \Magento\Customer\Model\Url $customerUrl
58+
* @param \Psr\Log\LoggerInterface|null $logger
59+
*/
60+
public function __construct(
61+
\Magento\Framework\App\Action\Context $context,
62+
\Magento\Customer\Model\Session $customerSession,
63+
\Magento\Checkout\Model\Session $checkoutSession,
64+
\Magento\Sales\Model\OrderFactory $orderFactory,
65+
\Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
66+
\Magento\Framework\Session\Generic $paypalSession,
67+
\Magento\Framework\Url\Helper\Data $urlHelper,
68+
\Magento\Customer\Model\Url $customerUrl,
69+
\Psr\Log\LoggerInterface $logger = null
70+
) {
71+
$this->_logger = $logger ?: ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
72+
parent::__construct(
73+
$context,
74+
$customerSession,
75+
$checkoutSession,
76+
$orderFactory,
77+
$checkoutFactory,
78+
$paypalSession,
79+
$urlHelper,
80+
$customerUrl
81+
);
82+
$parameters = ['params' => [$this->_configMethod]];
83+
$this->_config = $this->_objectManager->create($this->_configType, $parameters);
84+
}
85+
4386
/**
4487
* @inheritdoc
4588
*/
@@ -56,10 +99,13 @@ public function execute()
5699
$this->_initToken($token);
57100
$controllerResult->setData(['url' => $url]);
58101
} catch (LocalizedException $exception) {
59-
$this->messageManager->addExceptionMessage(
60-
$exception,
61-
$exception->getMessage()
62-
);
102+
$this->_logger->critical($exception);
103+
$controllerResult->setData([
104+
'message' => [
105+
'text' => $exception->getMessage(),
106+
'type' => 'error'
107+
]
108+
]);
63109
} catch (\Exception $exception) {
64110
$this->messageManager->addExceptionMessage(
65111
$exception,

app/code/Magento/Paypal/view/frontend/web/js/action/set-payment-method.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ define([
4444

4545
return storage[method](
4646
serviceUrl, JSON.stringify(payload)
47-
).fail(
48-
function (response) {
49-
errorProcessor.process(response, messageContainer);
50-
fullScreenLoader.stopLoader();
51-
}
52-
);
47+
).fail(function (response) {
48+
errorProcessor.process(response, messageContainer);
49+
}).always(function () {
50+
fullScreenLoader.stopLoader();
51+
});
5352
};
5453
});

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

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ define(
1111
'Magento_Checkout/js/model/payment/additional-validators',
1212
'Magento_Ui/js/lib/view/utils/dom-observer',
1313
'paypalInContextExpressCheckout',
14-
'Magento_Customer/js/customer-data'
14+
'Magento_Customer/js/customer-data',
15+
'Magento_Ui/js/model/messageList'
1516
],
1617
function (
1718
_,
@@ -21,7 +22,8 @@ define(
2122
additionalValidators,
2223
domObserver,
2324
paypalExpressCheckout,
24-
customerData
25+
customerData,
26+
messageList
2527
) {
2628
'use strict';
2729

@@ -33,7 +35,6 @@ define(
3335
defaults: {
3436
template: 'Magento_Paypal/payment/paypal-express-in-context',
3537
clientConfig: {
36-
3738
/**
3839
* @param {Object} event
3940
*/
@@ -42,41 +43,42 @@ define(
4243

4344
if (additionalValidators.validate()) {
4445
paypalExpressCheckout.checkout.initXO();
45-
this.selectPaymentMethod();
46-
setPaymentMethodAction(this.messageContainer).done(
47-
function () {
48-
$('body').trigger('processStart');
49-
50-
$.get(
51-
this.path,
52-
{
53-
button: 0
54-
}
55-
).done(
56-
function (response) {
57-
if (response && response.url) {
58-
paypalExpressCheckout.checkout.startFlow(response.url);
59-
60-
return;
61-
}
6246

63-
paypalExpressCheckout.checkout.closeFlow();
64-
window.location.reload();
65-
}
66-
).fail(
67-
function () {
68-
paypalExpressCheckout.checkout.closeFlow();
69-
window.location.reload();
70-
}
71-
).always(
72-
function () {
73-
$('body').trigger('processStop');
74-
customerData.invalidate(['cart']);
47+
this.selectPaymentMethod();
48+
setPaymentMethodAction(this.messageContainer).done(function () {
49+
$('body').trigger('processStart');
50+
51+
$.get(this.path, {
52+
button: 0
53+
}).done(function (response) {
54+
var message = response && response.message;
55+
56+
if (message) {
57+
if (message.type === 'error') {
58+
messageList.addErrorMessage({
59+
message: message.text
60+
});
61+
} else {
62+
messageList.addSuccessMessage({
63+
message: message.text
64+
});
7565
}
76-
);
77-
78-
}.bind(this)
79-
);
66+
}
67+
68+
if (response && response.url) {
69+
paypalExpressCheckout.checkout.startFlow(response.url);
70+
71+
return;
72+
}
73+
74+
paypalExpressCheckout.checkout.closeFlow();
75+
}).fail(function () {
76+
paypalExpressCheckout.checkout.closeFlow();
77+
}).always(function () {
78+
$('body').trigger('processStop');
79+
customerData.invalidate(['cart']);
80+
});
81+
}.bind(this));
8082
}
8183
}
8284
}

0 commit comments

Comments
 (0)