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 15
15
class AddPaypalShortcuts implements ObserverInterface
16
16
{
17
17
/**
18
- * Block class
18
+ * Alias for mini-cart block.
19
19
*/
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
+ }
21
39
22
40
/**
23
41
* Add Braintree PayPal shortcut buttons
@@ -35,7 +53,13 @@ public function execute(Observer $observer)
35
53
/** @var ShortcutButtons $shortcutButtons */
36
54
$ shortcutButtons = $ observer ->getEvent ()->getContainer ();
37
55
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
+ }
39
63
40
64
$ shortcutButtons ->addShortcut ($ shortcut );
41
65
}
Original file line number Diff line number Diff line change 19
19
*/
20
20
class AddPaypalShortcutsTest extends \PHPUnit \Framework \TestCase
21
21
{
22
+ /**
23
+ * Tests PayPal shortcuts observer.
24
+ */
22
25
public function testExecute ()
23
26
{
24
- $ addPaypalShortcuts = new AddPaypalShortcuts ();
27
+ $ addPaypalShortcuts = new AddPaypalShortcuts (
28
+ [
29
+ 'mini_cart ' => 'Minicart-block ' ,
30
+ 'shopping_cart ' => 'Shoppingcart-block '
31
+ ]
32
+ );
25
33
26
34
/** @var Observer|\PHPUnit_Framework_MockObject_MockObject $observerMock */
27
35
$ observerMock = $ this ->getMockBuilder (Observer::class)
@@ -60,7 +68,7 @@ public function testExecute()
60
68
61
69
$ layoutMock ->expects (self ::once ())
62
70
->method ('createBlock ' )
63
- ->with (AddPaypalShortcuts:: PAYPAL_SHORTCUT_BLOCK )
71
+ ->with (' Minicart-block ' )
64
72
->willReturn ($ blockMock );
65
73
66
74
$ shortcutButtonsMock ->expects (self ::once ())
Original file line number Diff line number Diff line change 55
55
<argument name =" payment" xsi : type =" object" >BraintreePayPalFacade</argument >
56
56
</arguments >
57
57
</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 >
58
75
59
76
<type name =" Magento\Braintree\Model\Ui\PayPal\ConfigProvider" >
60
77
<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