Skip to content

Commit 7661e81

Browse files
committed
MC-41710: Build URL to paypal SDK
- add configuration
1 parent 0462574 commit 7661e81

File tree

3 files changed

+110
-13
lines changed

3 files changed

+110
-13
lines changed

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,38 @@
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;
13+
use Magento\Paypal\Model\SdkUrlBuilder;
1314

1415
/**
1516
* PayPal PayLater component block
1617
*/
1718
class Banner extends Template
1819
{
1920
/**
20-
* @var \Magento\Paypal\Model\Config
21+
* @var PayLaterConfig
2122
*/
22-
private $paypalConfig;
23+
private $payLaterConfig;
24+
private $position;
25+
private $placement;
26+
private SdkUrlBuilder $sdkUrl;
2327

2428
/**
2529
* @param Template\Context $context
26-
* @param Config $paypalConfig
30+
* @param PayLaterConfig $payLaterConfig
2731
* @param array $data
2832
*/
2933
public function __construct(
3034
Template\Context $context,
31-
Config $paypalConfig,
35+
PayLaterConfig $payLaterConfig,
36+
/*SdkUrlBuilder $sdkUrl,*/
3237
array $data = []
33-
)
34-
{
35-
$this->paypalConfig = $paypalConfig;
38+
) {
3639
parent::__construct($context, $data);
40+
$this->placement = $data['placement'] ?? '';
41+
$this->position = $data['position'] ?? '';
42+
$this->payLaterConfig = $payLaterConfig;
43+
/*$this->sdkUrl = $sdkUrl;*/
3744
}
3845

3946
/**
@@ -56,7 +63,7 @@ public function getJsLayout()
5663
{
5764
$this->jsLayout['components']['payLater']['config']['sdkUrl'] = $this->getPayPalSdkUrl();
5865
$attributes = $this->jsLayout['components']['payLater']['config']['attributes'] ?? [];
59-
$attributes = array_replace($attributes, $this->getConfig());
66+
$attributes = array_replace($attributes, $this->getStyleAttributesConfig());
6067
$this->jsLayout['components']['payLater']['config']['attributes'] = $attributes;
6168
return parent::getJsLayout();
6269
}
@@ -68,17 +75,21 @@ public function getJsLayout()
6875
*/
6976
private function getPayPalSdkUrl()
7077
{
71-
return "https://www.paypal.com/sdk/js?client-id=sb&components=messages,buttons";
78+
$sandBox = "https://www.paypal.com/sdk/js?client-id=sb&components=messages,buttons";
79+
return $sandBox;
7280
}
7381

7482
/**
7583
* Retrieve style configuration
7684
*
7785
* @return string[]
7886
*/
79-
private function getConfig()
87+
private function getStyleAttributesConfig()
8088
{
81-
return ['data-pp-style-logo-position' => 'right'];
89+
return array_replace(
90+
['data-pp-style-logo-position' => 'center'],
91+
$this->payLaterConfig->getStyleConfig($this->placement)
92+
);
8293
}
8394

8495
/**
@@ -88,6 +99,7 @@ private function getConfig()
8899
*/
89100
private function isEnabled()
90101
{
91-
return true;
102+
$enabled = $this->payLaterConfig->isEnabled($this->placement);
103+
return $enabled && $this->payLaterConfig->getPositionConfig($this->placement) == $this->position;
92104
}
93105
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Checkout\Helper\Data;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Locale\ResolverInterface;
13+
use Magento\Store\Model\ScopeInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
16+
/**
17+
* Provides configuration values for PayPal PayLater Banners
18+
*/
19+
class PayLaterConfig
20+
{
21+
const PLACEMENT_PRODUCT = 'product';
22+
23+
/**
24+
* @var ScopeConfigInterface
25+
*/
26+
private ScopeConfigInterface $scopeConfig;
27+
28+
/**
29+
* @param ScopeConfigInterface $scopeConfig
30+
*/
31+
public function __construct(
32+
ScopeConfigInterface $scopeConfig
33+
) {
34+
$this->scopeConfig = $scopeConfig;
35+
}
36+
37+
/**
38+
* Get configured styles for specified page
39+
*
40+
* @return array
41+
*/
42+
public function getStyleConfig(string $placement): array
43+
{
44+
return $this->getSectionConfig($placement, 'style') ?? [];
45+
}
46+
47+
/**
48+
* Get configured Banner position on specified page
49+
*
50+
* @param string $placement
51+
* @return mixed|string
52+
*/
53+
public function getPositionConfig(string $placement)
54+
{
55+
return $this->getSectionConfig($placement, 'position') ?? '';
56+
}
57+
58+
/**
59+
* Check if Banner enabled for specified page
60+
*
61+
* @param string $placement
62+
* @return bool
63+
*/
64+
public function isEnabled(string $placement)
65+
{
66+
$isPayLaterEnabled = true; //read from config
67+
return $isPayLaterEnabled && $this->getSectionConfig($placement, 'display');
68+
}
69+
70+
private function getSectionConfig($section, $key)
71+
{
72+
$configMock = [
73+
self::PLACEMENT_PRODUCT => [
74+
'display' => 1,
75+
'position' => 'header', // 'sidebar'
76+
'style' => [
77+
'data-pp-style-logo-position' => 'right'
78+
]
79+
],
80+
];
81+
return $configMock[$section][$key];
82+
}
83+
}

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

Lines changed: 2 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">

0 commit comments

Comments
 (0)