File tree Expand file tree Collapse file tree 5 files changed +122
-5
lines changed
app/code/Magento/Braintree Expand file tree Collapse file tree 5 files changed +122
-5
lines changed Original file line number Diff line number Diff line change 1515class 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 }
Original file line number Diff line number Diff line change 1919 */
2020class 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 ())
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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>
Original file line number Diff line number Diff line change 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+ ) ;
You can’t perform that action at this time.
0 commit comments