Skip to content

Commit da1aec5

Browse files
committed
Merge remote-tracking branch 'trigger/MC-41597' into MC-41701
2 parents 9d1fe17 + 8fd9c75 commit da1aec5

File tree

7 files changed

+282
-2
lines changed

7 files changed

+282
-2
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Paypal\Block\PayLater;
10+
11+
use Magento\Framework\View\Element\Template;
12+
use Magento\Paypal\Model\Config;
13+
14+
/**
15+
* PayPal PayLater component block
16+
*/
17+
class Banner extends Template
18+
{
19+
/**
20+
* @var \Magento\Paypal\Model\Config
21+
*/
22+
private $paypalConfig;
23+
24+
/**
25+
* @param Template\Context $context
26+
* @param Config $paypalConfig
27+
* @param array $data
28+
*/
29+
public function __construct(
30+
Template\Context $context,
31+
Config $paypalConfig,
32+
array $data = []
33+
)
34+
{
35+
$this->paypalConfig = $paypalConfig;
36+
parent::__construct($context, $data);
37+
}
38+
39+
/**
40+
* Disable block output
41+
*
42+
* @return string
43+
*/
44+
protected function _toHtml()
45+
{
46+
if (!$this->isEnabled()) {
47+
return '';
48+
}
49+
return parent::_toHtml();
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function getJsLayout()
56+
{
57+
$this->jsLayout['components']['payLater']['config']['sdkUrl'] = $this->getPayPalSdkUrl();
58+
$attributes = $this->jsLayout['components']['payLater']['config']['attributes'] ?? [];
59+
$attributes = array_replace($attributes, $this->getConfig());
60+
$this->jsLayout['components']['payLater']['config']['attributes'] = $attributes;
61+
return parent::getJsLayout();
62+
}
63+
64+
/**
65+
* Build\Get URL to PP SDK
66+
*
67+
* @return string
68+
*/
69+
private function getPayPalSdkUrl()
70+
{
71+
return "https://www.paypal.com/sdk/js?client-id=sb&components=messages,buttons";
72+
}
73+
74+
/**
75+
* Retrieve style configuration
76+
*
77+
* @return string[]
78+
*/
79+
private function getConfig()
80+
{
81+
return [];
82+
}
83+
84+
/**
85+
* Check if block should be displayed
86+
*
87+
* @return bool
88+
*/
89+
private function isEnabled()
90+
{
91+
return true;
92+
}
93+
}

app/code/Magento/Paypal/view/frontend/layout/catalog_product_view.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@
1414
<argument name="position" xsi:type="number">0</argument>
1515
</arguments>
1616
</block>
17+
<block class="Magento\Paypal\Block\PayLater\Banner" name="paylater.center.logo"
18+
template="Magento_Paypal::paylater/banner.phtml">
19+
<arguments>
20+
<argument name="jsLayout" xsi:type="array">
21+
<item name="components" xsi:type="array">
22+
<item name="payLater" xsi:type="array">
23+
<item name="component" xsi:type="string">Magento_Paypal/js/view/paylater</item>
24+
<item name="config" xsi:type="array">
25+
<item name="attributes" xsi:type="array">
26+
<item name="data-pp-placement" xsi:type="string">product</item>
27+
</item>
28+
</item>
29+
</item>
30+
</item>
31+
</argument>
32+
</arguments>
33+
</block>
1734
</referenceContainer>
1835
<referenceContainer name="product.info.addtocart">
1936
<block class="Magento\Paypal\Block\Bml\Banners" name="bml.right.logo" template="Magento_Paypal::bml.phtml">
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $block \Magento\Paypal\Block\PayLater\Banner */
8+
?>
9+
<div id="pay-later" data-bind="scope: 'payLater'">
10+
<!-- ko template: getTemplate() --><!-- /ko -->
11+
<script type="text/x-magento-init">
12+
{
13+
"#pay-later": {
14+
"Magento_Ui/js/core/app": <?= /* @noEscape */ $block->getJsLayout() ?>
15+
}
16+
}
17+
</script>
18+
</div>
19+
20+
21+
22+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'ko',
8+
'uiElement',
9+
'Magento_Paypal/js/in-context/paypal-sdk'
10+
], function (
11+
ko,
12+
Component,
13+
paypalSdk
14+
) {
15+
'use strict';
16+
17+
return Component.extend({
18+
19+
defaults: {
20+
template: 'Magento_Paypal/paylater',
21+
sdkUrl: '',
22+
attributes: {
23+
'data-pp-amount': '',
24+
'data-pp-style-logo-position': 'right'
25+
}
26+
},
27+
28+
/**
29+
* Initialize
30+
*
31+
* @returns {*}
32+
*/
33+
initialize: function () {
34+
this._super();
35+
this.loadPayPalSdk(this.sdkUrl);
36+
37+
return this;
38+
},
39+
40+
/**
41+
* Get attribute value from configuration
42+
*
43+
* @param {String} attributeName
44+
* @returns {*|null}
45+
*/
46+
getAttribute: function (attributeName) {
47+
return typeof this.attributes[attributeName] !== 'undefined' ?
48+
this.attributes[attributeName] : null;
49+
},
50+
51+
/**
52+
* Load PP SDK with preconfigured options
53+
*
54+
* @param {String} sdkUrl
55+
*/
56+
loadPayPalSdk: function (sdkUrl) {
57+
paypalSdk(sdkUrl);
58+
}
59+
});
60+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<div data-pp-message data-bind="attr: {
8+
'data-pp-amount': getAttribute('data-pp-amount'),
9+
'data-pp-placement': getAttribute('data-pp-placement'),
10+
'data-pp-style-layout': getAttribute('data-pp-style-layout'),
11+
'data-pp-style-logo-type': getAttribute('data-pp-style-logo-type'),
12+
'data-pp-style-logo-position': getAttribute('data-pp-style-logo-position'),
13+
'data-pp-style-text-color': getAttribute('data-pp-style-text-color'),
14+
'data-pp-style-text-size': getAttribute('data-pp-style-text-size'),
15+
'data-pp-style-color': getAttribute('data-pp-style-color'),
16+
'data-pp-style-ratio': getAttribute('data-pp-style-ratio'),
17+
}"></div>
18+

dev/tests/integration/testsuite/Magento/Paypal/Block/Bml/BannersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BannersTest extends \PHPUnit\Framework\TestCase
1717
* @param bool $isEmptyHtml
1818
* @param string $methodWppBml
1919
* @param string $methodWppPeBml
20-
* @dataProvider testToHtmlDataProvider
20+
* @dataProvider toHtmlDataProvider
2121
* @magentoAppIsolation enabled
2222
* @magentoAppArea frontend
2323
*/
@@ -75,7 +75,7 @@ public function testToHtml(
7575
/**
7676
* @return array
7777
*/
78-
public function testToHtmlDataProvider()
78+
public function toHtmlDataProvider()
7979
{
8080
return [
8181
[
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Paypal\Block\Paylater;
10+
11+
use Magento\Framework\View\LayoutInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
class BannerTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* @magentoAppArea frontend
18+
* @dataProvider getJsLayoutDataProvider
19+
* @covers \Magento\Paypal\Block\PayLater\Banner::getJsLayout()
20+
* @covers \Magento\Paypal\Block\PayLater\Banner::getConfig()
21+
*/
22+
public function testGetJsLayout($layoutConfig, $expectedConfig)
23+
{
24+
/** @var LayoutInterface $layout */
25+
$layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
26+
$blockConfig['jsLayout']['components']['payLater']['config']['attributes'] = $layoutConfig;
27+
$block = $layout->createBlock(
28+
\Magento\Paypal\Block\PayLater\Banner::class,
29+
'',
30+
['data' => $blockConfig]
31+
);
32+
33+
$jsConfig = json_decode($block->getJsLayout(), true);
34+
$this->assertArrayHasKey('config', $jsConfig['components']['payLater']);
35+
$optionsConfig = $jsConfig['components']['payLater']['config'];
36+
$this->assertEquals($expectedConfig, array_intersect_key($optionsConfig, $expectedConfig));
37+
}
38+
39+
/**
40+
* @return array
41+
*/
42+
public function getJsLayoutDataProvider()
43+
{
44+
return [
45+
[
46+
['data-pp-placement' => 'test-page'],
47+
['attributes' => ['data-pp-placement' => 'test-page']]
48+
],
49+
];
50+
}
51+
52+
/**
53+
* @magentoAppArea frontend
54+
* @covers \Magento\Paypal\Block\PayLater\Banner::getJsLayout()
55+
* @covers \Magento\Paypal\Block\PayLater\Banner::getPayPalSdkUrl()
56+
*/
57+
public function testSdkUrl()
58+
{
59+
$layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
60+
$block = $layout->createBlock(
61+
\Magento\Paypal\Block\PayLater\Banner::class,
62+
'',
63+
[]
64+
);
65+
66+
$jsConfig = json_decode($block->getJsLayout(), true);
67+
$this->assertArrayHasKey('config', $jsConfig['components']['payLater']);
68+
$this->assertArrayHasKey('sdkUrl', $jsConfig['components']['payLater']['config']);
69+
}
70+
}

0 commit comments

Comments
 (0)