Skip to content

Commit 0ec1dc7

Browse files
MC-41654: Read and apply configuration to PP PayLater component
1 parent db064c0 commit 0ec1dc7

File tree

5 files changed

+252
-21
lines changed

5 files changed

+252
-21
lines changed

app/code/Magento/Paypal/Block/PayLater/Banner.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,50 @@
99
namespace Magento\Paypal\Block\PayLater;
1010

1111
use Magento\Framework\View\Element\Template;
12-
use Magento\Paypal\Model\Config;
12+
use Magento\Paypal\Model\PayLaterConfig;
1313

1414
/**
1515
* PayPal PayLater component block
1616
*/
1717
class Banner extends Template
1818
{
1919
/**
20-
* @var \Magento\Paypal\Model\Config
20+
* @var PayLaterConfig
2121
*/
22-
private $paypalConfig;
22+
private $payLaterConfig;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $placement = '';
28+
29+
/**
30+
* @var string
31+
*/
32+
private $position = '';
2333

2434
/**
2535
* @param Template\Context $context
26-
* @param Config $paypalConfig
36+
* @param PayLaterConfig $payLaterConfig
2737
* @param array $data
2838
*/
2939
public function __construct(
3040
Template\Context $context,
31-
Config $paypalConfig,
41+
PayLaterConfig $payLaterConfig,
3242
array $data = []
33-
)
34-
{
35-
$this->paypalConfig = $paypalConfig;
43+
) {
3644
parent::__construct($context, $data);
45+
$this->placement = $data['placement'] ?? '';
46+
$this->position = $data['position'] ?? '';
47+
$this->payLaterConfig = $payLaterConfig;
3748
}
3849

3950
/**
4051
* Disable block output
4152
*
4253
* @return string
4354
*/
44-
protected function _toHtml()
55+
protected function _toHtml(): string
4556
{
4657
if (!$this->isEnabled()) {
4758
return '';
@@ -56,7 +67,7 @@ public function getJsLayout()
5667
{
5768
$this->jsLayout['components']['payLater']['config']['sdkUrl'] = $this->getPayPalSdkUrl();
5869
$attributes = $this->jsLayout['components']['payLater']['config']['attributes'] ?? [];
59-
$attributes = array_replace($attributes, $this->getConfig());
70+
$attributes = array_replace($attributes, $this->getStyleAttributesConfig());
6071
$this->jsLayout['components']['payLater']['config']['attributes'] = $attributes;
6172
return parent::getJsLayout();
6273
}
@@ -66,28 +77,29 @@ public function getJsLayout()
6677
*
6778
* @return string
6879
*/
69-
private function getPayPalSdkUrl()
80+
private function getPayPalSdkUrl(): string
7081
{
7182
return "https://www.paypal.com/sdk/js?client-id=sb&components=messages,buttons";
7283
}
7384

7485
/**
7586
* Retrieve style configuration
7687
*
77-
* @return string[]
88+
* @return array
7889
*/
79-
private function getConfig()
90+
private function getStyleAttributesConfig(): array
8091
{
81-
return [];
92+
return $this->payLaterConfig->getStyleConfig($this->placement);
8293
}
8394

8495
/**
8596
* Check if block should be displayed
8697
*
8798
* @return bool
8899
*/
89-
private function isEnabled()
100+
private function isEnabled(): bool
90101
{
91-
return true;
102+
$enabled = $this->payLaterConfig->isEnabled($this->placement);
103+
return $enabled && $this->payLaterConfig->getPositionConfig($this->placement) == $this->position;
92104
}
93105
}

app/code/Magento/Paypal/Model/Config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ class Config extends AbstractConfig
174174
*/
175175
const XML_PATH_PAYPAL_EXPRESS_SKIP_ORDER_REVIEW_STEP_FLAG = 'payment/paypal_express/skip_order_review_step';
176176

177+
/**
178+
* PayPal PayLater
179+
*/
180+
const PAYLATER = 'paypal_paylater';
181+
177182
/**
178183
* Instructions for generating proper BN code
179184
*
@@ -1828,4 +1833,19 @@ public function getBmlSize($section)
18281833
$this->_storeId
18291834
);
18301835
}
1836+
1837+
/**
1838+
* Get PayLater config values
1839+
*
1840+
* @param string $fieldName
1841+
* @return mixed
1842+
*/
1843+
public function getPayLaterConfigValue($fieldName)
1844+
{
1845+
return $this->_scopeConfig->getValue(
1846+
'payment/' . self::PAYLATER . '/' . $fieldName,
1847+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
1848+
$this->_storeId
1849+
);
1850+
}
18311851
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Paypal\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
12+
/**
13+
* Provides configuration values for PayPal PayLater Banners
14+
*/
15+
class PayLaterConfig
16+
{
17+
/**
18+
* @var Config
19+
*/
20+
private $config;
21+
22+
/**
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* @var array
29+
*/
30+
private $configData = [];
31+
32+
/**
33+
* @param ScopeConfigInterface $scopeConfig
34+
* @param Config $config
35+
*/
36+
public function __construct(
37+
ScopeConfigInterface $scopeConfig,
38+
Config $config
39+
) {
40+
$this->scopeConfig = $scopeConfig;
41+
$this->config = $config;
42+
}
43+
44+
/**
45+
* Get configured styles for specified page
46+
*
47+
* @param string $placement
48+
* @return array
49+
*/
50+
public function getStyleConfig(string $placement): array
51+
{
52+
return $this->getSectionConfig($placement, 'style') ?? [];
53+
}
54+
55+
/**
56+
* Get configured Banner position on specified page
57+
*
58+
* @param string $placement
59+
* @return string
60+
*/
61+
public function getPositionConfig(string $placement): string
62+
{
63+
return $this->getSectionConfig($placement, 'position') ?? '';
64+
}
65+
66+
/**
67+
* Check if Banner enabled for specified page
68+
*
69+
* @param string $placement
70+
* @return bool
71+
*/
72+
public function isEnabled(string $placement): bool
73+
{
74+
$isPayLaterEnabled = (boolean)$this->config->getPayLaterConfigValue('enabled');
75+
return $isPayLaterEnabled && $this->getSectionConfig($placement, 'display');
76+
}
77+
78+
/**
79+
* Get config for a specific section and key
80+
*
81+
* @param string $section
82+
* @param string $key
83+
* @return mixed
84+
*/
85+
private function getSectionConfig($section, $key)
86+
{
87+
if (!array_key_exists($section, $this->configData)) {
88+
$this->configData[$section] = [
89+
'display' => (boolean)$this->config->getPayLaterConfigValue("${section}page_display"),
90+
'position' => $this->config->getPayLaterConfigValue("${section}page_position"),
91+
'style' => [
92+
'data-pp-style-layout' => $this->config->getPayLaterConfigValue(
93+
"${section}page_stylelayout"
94+
),
95+
'data-pp-style-logo-type' => $this->config->getPayLaterConfigValue(
96+
"${section}page_logotype"
97+
),
98+
'data-pp-style-logo-position' => $this->config->getPayLaterConfigValue(
99+
"${section}page_logoposition"
100+
),
101+
'data-pp-style-text-color' => $this->config->getPayLaterConfigValue(
102+
"${section}page_textcolor"
103+
),
104+
'data-pp-style-text-size' => $this->config->getPayLaterConfigValue(
105+
"${section}page_textsize"
106+
),
107+
'data-pp-style-color' => $this->config->getPayLaterConfigValue(
108+
"${section}page_color"
109+
),
110+
'data-pp-style-ratio' => $this->config->getPayLaterConfigValue(
111+
"${section}page_ratio"
112+
)
113+
]
114+
];
115+
}
116+
117+
return $this->configData[$section][$key];
118+
}
119+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<block class="Magento\Paypal\Block\PayLater\Banner" name="paylater.center.logo"
1818
template="Magento_Paypal::paylater/banner.phtml">
1919
<arguments>
20+
<argument name="placement" xsi:type="string">product</argument>
21+
<argument name="position" xsi:type="string">header</argument>
2022
<argument name="jsLayout" xsi:type="array">
2123
<item name="components" xsi:type="array">
2224
<item name="payLater" xsi:type="array">
@@ -39,6 +41,25 @@
3941
<argument name="position" xsi:type="number">1</argument>
4042
</arguments>
4143
</block>
44+
<block class="Magento\Paypal\Block\PayLater\Banner" name="paylater.right.logo"
45+
template="Magento_Paypal::paylater/banner.phtml">
46+
<arguments>
47+
<argument name="placement" xsi:type="string">product</argument>
48+
<argument name="position" xsi:type="string">sidebar</argument>
49+
<argument name="jsLayout" xsi:type="array">
50+
<item name="components" xsi:type="array">
51+
<item name="payLater" xsi:type="array">
52+
<item name="component" xsi:type="string">Magento_Paypal/js/view/paylater</item>
53+
<item name="config" xsi:type="array">
54+
<item name="attributes" xsi:type="array">
55+
<item name="data-pp-placement" xsi:type="string">product</item>
56+
</item>
57+
</item>
58+
</item>
59+
</item>
60+
</argument>
61+
</arguments>
62+
</block>
4263
</referenceContainer>
4364
</body>
4465
</page>

dev/tests/integration/testsuite/Magento/Paypal/Block/PayLater/BannerTest.php

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ class BannerTest extends \PHPUnit\Framework\TestCase
1616
/**
1717
* @magentoAppArea frontend
1818
* @dataProvider getJsLayoutDataProvider
19+
* @magentoConfigFixture current_store payment/paypal_paylater/test1page_stylelayout flex
20+
* @magentoConfigFixture current_store payment/paypal_paylater/test1page_ratio 20x1
21+
* @magentoConfigFixture current_store payment/paypal_paylater/test1page_color blue
22+
* @magentoConfigFixture current_store payment/paypal_paylater/test2page_stylelayout text
23+
* @magentoConfigFixture current_store payment/paypal_paylater/test2page_logotype primary
24+
* @magentoConfigFixture current_store payment/paypal_paylater/test2page_logoposition left
25+
* @magentoConfigFixture current_store payment/paypal_paylater/test2page_textcolor white
26+
* @magentoConfigFixture current_store payment/paypal_paylater/test2page_textsize 10
1927
* @covers \Magento\Paypal\Block\PayLater\Banner::getJsLayout()
20-
* @covers \Magento\Paypal\Block\PayLater\Banner::getConfig()
28+
* @covers \Magento\Paypal\Block\PayLater\Banner::getStyleAttributesConfig()
2129
*/
22-
public function testGetJsLayout($layoutConfig, $expectedConfig)
30+
public function testGetJsLayout($blockConfig, $expectedConfig)
2331
{
2432
/** @var LayoutInterface $layout */
2533
$layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
26-
$blockConfig['jsLayout']['components']['payLater']['config']['attributes'] = $layoutConfig;
2734
$block = $layout->createBlock(
2835
\Magento\Paypal\Block\PayLater\Banner::class,
2936
'',
@@ -43,8 +50,60 @@ public function getJsLayoutDataProvider()
4350
{
4451
return [
4552
[
46-
['data-pp-placement' => 'test-page'],
47-
['attributes' => ['data-pp-placement' => 'test-page']]
53+
[
54+
'placement' => 'test1',
55+
'position' => 'header',
56+
'jslayout' => [
57+
'components' => [
58+
'payLater' => [
59+
'config' => [
60+
'attributes' => [
61+
'data-pp-placement' => 'test1'
62+
]
63+
]
64+
]
65+
]
66+
]
67+
],
68+
[
69+
'attributes' => [
70+
'data-pp-style-layout' => 'flex',
71+
'data-pp-style-logo-type' => null,
72+
'data-pp-style-logo-position' => null,
73+
'data-pp-style-text-color' => null,
74+
'data-pp-style-text-size' => null,
75+
'data-pp-style-color' => 'blue',
76+
'data-pp-style-ratio' => '20x1',
77+
]
78+
]
79+
],
80+
[
81+
[
82+
'placement' => 'test2',
83+
'position' => 'sidebar',
84+
'jslayout' => [
85+
'components' => [
86+
'payLater' => [
87+
'config' => [
88+
'attributes' => [
89+
'data-pp-placement' => 'test2'
90+
]
91+
]
92+
]
93+
]
94+
]
95+
],
96+
[
97+
'attributes' => [
98+
'data-pp-style-layout' => 'text',
99+
'data-pp-style-logo-type' => 'primary',
100+
'data-pp-style-logo-position' => 'left',
101+
'data-pp-style-text-color' => 'white',
102+
'data-pp-style-text-size' => '10',
103+
'data-pp-style-color' => null,
104+
'data-pp-style-ratio' => null,
105+
]
106+
]
48107
],
49108
];
50109
}

0 commit comments

Comments
 (0)