Skip to content

Commit 797e05e

Browse files
committed
MC-19538: Error Sorry we cannot connect to PayPal. Please try again in a few minutes
- Fixed sending shipping amount to Braintree PayPal from shopping cart page.
1 parent 39cfaa7 commit 797e05e

File tree

5 files changed

+122
-5
lines changed

5 files changed

+122
-5
lines changed

app/code/Magento/Braintree/Observer/AddPaypalShortcuts.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,27 @@
1515
class AddPaypalShortcuts implements ObserverInterface
1616
{
1717
/**
18-
* Block class
18+
* Alias for mini-cart block.
1919
*/
20-
const PAYPAL_SHORTCUT_BLOCK = \Magento\Braintree\Block\Paypal\Button::class;
20+
private const PAYPAL_MINICART_ALIAS = 'mini_cart';
21+
22+
/**
23+
* Alias for shopping cart page.
24+
*/
25+
private const PAYPAL_SHOPPINGCART_ALIAS = 'shopping_cart';
26+
27+
/**
28+
* @var string[]
29+
*/
30+
private $buttonBlocks;
31+
32+
/**
33+
* @param string[] $buttonBlocks
34+
*/
35+
public function __construct(array $buttonBlocks = [])
36+
{
37+
$this->buttonBlocks = $buttonBlocks;
38+
}
2139

2240
/**
2341
* Add Braintree PayPal shortcut buttons
@@ -35,7 +53,13 @@ public function execute(Observer $observer)
3553
/** @var ShortcutButtons $shortcutButtons */
3654
$shortcutButtons = $observer->getEvent()->getContainer();
3755

38-
$shortcut = $shortcutButtons->getLayout()->createBlock(self::PAYPAL_SHORTCUT_BLOCK);
56+
if ($observer->getData('is_shopping_cart')) {
57+
$shortcut = $shortcutButtons->getLayout()
58+
->createBlock($this->buttonBlocks[self::PAYPAL_SHOPPINGCART_ALIAS]);
59+
} else {
60+
$shortcut = $shortcutButtons->getLayout()
61+
->createBlock($this->buttonBlocks[self::PAYPAL_MINICART_ALIAS]);
62+
}
3963

4064
$shortcutButtons->addShortcut($shortcut);
4165
}

app/code/Magento/Braintree/Test/Unit/Observer/AddPaypalShortcutsTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919
*/
2020
class AddPaypalShortcutsTest extends \PHPUnit\Framework\TestCase
2121
{
22+
/**
23+
* Tests PayPal shortcuts observer.
24+
*/
2225
public function testExecute()
2326
{
24-
$addPaypalShortcuts = new AddPaypalShortcuts();
27+
$addPaypalShortcuts = new AddPaypalShortcuts(
28+
[
29+
'mini_cart' => 'Minicart-block',
30+
'shopping_cart' => 'Shoppingcart-block'
31+
]
32+
);
2533

2634
/** @var Observer|\PHPUnit_Framework_MockObject_MockObject $observerMock */
2735
$observerMock = $this->getMockBuilder(Observer::class)
@@ -60,7 +68,7 @@ public function testExecute()
6068

6169
$layoutMock->expects(self::once())
6270
->method('createBlock')
63-
->with(AddPaypalShortcuts::PAYPAL_SHORTCUT_BLOCK)
71+
->with('Minicart-block')
6472
->willReturn($blockMock);
6573

6674
$shortcutButtonsMock->expects(self::once())

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@
5555
<argument name="payment" xsi:type="object">BraintreePayPalFacade</argument>
5656
</arguments>
5757
</type>
58+
<virtualType name="Magento\Braintree\Block\Paypal\ButtonShoppingCartVirtual" type="Magento\Braintree\Block\Paypal\Button">
59+
<arguments>
60+
<argument name="data" xsi:type="array">
61+
<item name="template" xsi:type="string">Magento_Braintree::paypal/button_shopping_cart.phtml</item>
62+
<item name="alias" xsi:type="string">braintree.paypal.mini-cart</item>
63+
<item name="button_id" xsi:type="string">braintree-paypal-mini-cart</item>
64+
</argument>
65+
</arguments>
66+
</virtualType>
67+
<type name="Magento\Braintree\Observer\AddPaypalShortcuts">
68+
<arguments>
69+
<argument name="buttonBlocks" xsi:type="array">
70+
<item name="mini_cart" xsi:type="string">Magento\Braintree\Block\Paypal\Button</item>
71+
<item name="shopping_cart" xsi:type="string">Magento\Braintree\Block\Paypal\ButtonShoppingCartVirtual</item>
72+
</argument>
73+
</arguments>
74+
</type>
5875

5976
<type name="Magento\Braintree\Model\Ui\PayPal\ConfigProvider">
6077
<arguments>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* @var \Magento\Braintree\Block\Paypal\Button $block
9+
*/
10+
11+
$id = $block->getContainerId() . random_int(0, PHP_INT_MAX);
12+
13+
$config = [
14+
'Magento_Braintree/js/paypal/button_shopping_cart' => [
15+
'id' => $id,
16+
'clientToken' => $block->getClientToken(),
17+
'displayName' => $block->getMerchantName(),
18+
'actionSuccess' => $block->getActionSuccess(),
19+
'environment' => $block->getEnvironment()
20+
]
21+
];
22+
23+
?>
24+
<div data-mage-init='<?= /* @noEscape */ json_encode($config); ?>'
25+
class="paypal checkout paypal-logo braintree-paypal-logo<?= /* @noEscape */ $block->getContainerId(); ?>-container">
26+
<div data-currency="<?= /* @noEscape */ $block->getCurrency(); ?>"
27+
data-locale="<?= /* @noEscape */ $block->getLocale(); ?>"
28+
data-amount="<?= /* @noEscape */ $block->getAmount(); ?>"
29+
id="<?= /* @noEscape */ $id; ?>"
30+
class="action-braintree-paypal-logo">
31+
</div>
32+
</div>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define(
6+
[
7+
'Magento_Braintree/js/paypal/button',
8+
'Magento_Checkout/js/model/quote',
9+
'domReady!'
10+
],
11+
function (
12+
Component,
13+
quote
14+
) {
15+
'use strict';
16+
17+
return Component.extend({
18+
19+
/**
20+
* Overrides amount with a value from quote.
21+
*
22+
* @returns {Object}
23+
* @private
24+
*/
25+
getClientConfig: function (data) {
26+
var config = this._super(data);
27+
28+
if (config.amount !== quote.totals()['base_grand_total']) {
29+
config.amount = quote.totals()['base_grand_total'];
30+
}
31+
32+
return config;
33+
}
34+
});
35+
}
36+
);

0 commit comments

Comments
 (0)