Skip to content

Commit b493bb1

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

File tree

14 files changed

+767
-439
lines changed

14 files changed

+767
-439
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use Magento\Framework\View\Element\Template;
1212
use Magento\Paypal\Model\PayLaterConfig;
13-
use Magento\Paypal\Model\SdkUrlBuilder;
13+
use Magento\Paypal\Model\SdkUrl;
1414

1515
/**
1616
* PayPal PayLater component block
@@ -23,24 +23,25 @@ class Banner extends Template
2323
private $payLaterConfig;
2424
private $position;
2525
private $placement;
26-
private SdkUrlBuilder $sdkUrl;
26+
private $sdkUrl;
2727

2828
/**
2929
* @param Template\Context $context
3030
* @param PayLaterConfig $payLaterConfig
31+
* @param SdkUrl $sdkUrl
3132
* @param array $data
3233
*/
3334
public function __construct(
3435
Template\Context $context,
3536
PayLaterConfig $payLaterConfig,
36-
/*SdkUrlBuilder $sdkUrl,*/
37+
SdkUrl $sdkUrl,
3738
array $data = []
3839
) {
3940
parent::__construct($context, $data);
4041
$this->placement = $data['placement'] ?? '';
4142
$this->position = $data['position'] ?? '';
4243
$this->payLaterConfig = $payLaterConfig;
43-
/*$this->sdkUrl = $sdkUrl;*/
44+
$this->sdkUrl = $sdkUrl;
4445
}
4546

4647
/**
@@ -63,7 +64,7 @@ public function getJsLayout()
6364
{
6465
$this->jsLayout['components']['payLater']['config']['sdkUrl'] = $this->getPayPalSdkUrl();
6566
$attributes = $this->jsLayout['components']['payLater']['config']['attributes'] ?? [];
66-
$attributes = array_replace($attributes, $this->getStyleAttributesConfig());
67+
$attributes = array_replace($this->getStyleAttributesConfig(), $attributes);
6768
$this->jsLayout['components']['payLater']['config']['attributes'] = $attributes;
6869
return parent::getJsLayout();
6970
}
@@ -75,8 +76,7 @@ public function getJsLayout()
7576
*/
7677
private function getPayPalSdkUrl()
7778
{
78-
$sandBox = "https://www.paypal.com/sdk/js?client-id=sb&components=messages,buttons";
79-
return $sandBox;
79+
return $this->sdkUrl->getUrl();
8080
}
8181

8282
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ private function getSectionConfig($section, $key)
7878
]
7979
],
8080
];
81-
return $configMock[$section][$key];
81+
return $configMock[$section][$key] ?? [];
8282
}
8383
}
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
use Magento\Framework\Locale\ResolverInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Provides URL to PP SDK based on configuration
16+
*/
17+
class SdkUrl
18+
{
19+
/**
20+
* Base url for Paypal SDK
21+
*/
22+
private const BASE_URL = 'https://www.paypal.com/sdk/js?';
23+
24+
/**
25+
* @var ScopeConfigInterface
26+
*/
27+
private $scopeConfig;
28+
29+
/**
30+
* @var Config
31+
*/
32+
private $config;
33+
34+
/**
35+
* @var StoreManagerInterface
36+
*/
37+
private $storeManager;
38+
39+
/**
40+
* @var array
41+
*/
42+
private $queryParams = [];
43+
44+
/**
45+
* Maps the old checkout SDK configuration values to the current ones
46+
*
47+
* @var array
48+
*/
49+
private $disallowedFundingMap;
50+
51+
/**
52+
* These payment methods will be added as parameters to the SDK url to disable them.
53+
*
54+
* @var array
55+
*/
56+
private $unsupportedPaymentMethods;
57+
58+
/**
59+
* @var ResolverInterface
60+
*/
61+
private $localeResolver;
62+
63+
/**
64+
* Generated Url to PayPAl SDK
65+
*
66+
* @var string
67+
*/
68+
private $url;
69+
70+
/**
71+
* @param ResolverInterface $localeResolver
72+
* @param ConfigFactory $configFactory
73+
* @param ScopeConfigInterface $scopeConfig
74+
* @param StoreManagerInterface $storeManager
75+
* @param array $disallowedFundingMap
76+
* @param array $unsupportedPaymentMethods
77+
*/
78+
public function __construct(
79+
ResolverInterface $localeResolver,
80+
ConfigFactory $configFactory,
81+
ScopeConfigInterface $scopeConfig,
82+
StoreManagerInterface $storeManager,
83+
$disallowedFundingMap = [],
84+
$unsupportedPaymentMethods = []
85+
) {
86+
$this->localeResolver = $localeResolver;
87+
$this->config = $configFactory->create();
88+
$this->config->setMethod(Config::METHOD_EXPRESS);
89+
$this->scopeConfig = $scopeConfig;
90+
$this->storeManager = $storeManager;
91+
$this->disallowedFundingMap = $disallowedFundingMap;
92+
$this->unsupportedPaymentMethods = $unsupportedPaymentMethods;
93+
}
94+
95+
/**
96+
* Generate the url to download the Paypal SDK
97+
*
98+
* @return string
99+
*/
100+
public function getUrl(): string
101+
{
102+
if (empty($this->url)) {
103+
$components = [];
104+
$params = [
105+
'client-id' => $this->getClientId(),
106+
'locale' => $this->localeResolver->getLocale(),
107+
'currency' => $this->storeManager->getStore()->getBaseCurrencyCode(),
108+
];
109+
110+
if ($this->areMessagesEnabled()) {
111+
$components[] = 'messages';
112+
}
113+
if ($this->areButtonsEnabled()) {
114+
$components[] = 'buttons';
115+
$params['commit'] = 'false';
116+
$params['intent'] = $this->getIntent();
117+
$params['merchant-id'] = $this->config->getValue('merchant_id');
118+
$params['disable-funding'] = $this->getDisallowedFunding();
119+
$params = array_replace($params, $this->queryParams);
120+
}
121+
$params['components'] = implode(',', $components);
122+
$this->url = self::BASE_URL . http_build_query(array_filter($params));
123+
}
124+
return $this->url;
125+
}
126+
127+
/**
128+
* Set query params in PayPal SDK Url
129+
*
130+
* @param string $key
131+
* @param string $value
132+
*/
133+
public function setQueryParam(string $key, string $value)
134+
{
135+
$allowedParams = ['commit'];
136+
if (in_array($key, $allowedParams)) {
137+
$this->queryParams[$key] = $value;
138+
}
139+
}
140+
141+
/**
142+
* Check if PP PayLater Messages enabled
143+
*
144+
* @return bool
145+
*/
146+
private function areMessagesEnabled()
147+
{
148+
//ToDo read from config
149+
return true;
150+
}
151+
152+
/**
153+
* Check if SmartButtons enabled
154+
*
155+
* @return bool
156+
*/
157+
private function areButtonsEnabled()
158+
{
159+
return (bool)(int) $this->config->getValue('in_context');
160+
}
161+
162+
/**
163+
* Get configured value for PayPal client id
164+
*
165+
* @return mixed
166+
*/
167+
private function getClientId()
168+
{
169+
return (int)$this->config->getValue('sandbox_flag') ?
170+
$this->config->getValue('sandbox_client_id') :
171+
$this->config->getValue('client_id');
172+
}
173+
174+
/**
175+
* Returns disallowed funding from configuration after updating values
176+
*
177+
* @return string
178+
*/
179+
private function getDisallowedFunding()
180+
{
181+
$disallowedFunding = $this->config->getValue('disable_funding_options');
182+
$result = $disallowedFunding ? explode(',', $disallowedFunding) : [];
183+
184+
// PayPal Guest Checkout Credit Card Icons only available when Guest Checkout option is enabled
185+
if ($this->isPaypalGuestCheckoutAllowed() === false && !in_array('CARD', $result)) {
186+
array_push($result, 'CARD');
187+
}
188+
189+
// Map old configuration values to current ones
190+
$result = array_map(function ($oldValue) {
191+
return $this->disallowedFundingMap[$oldValue] ?? $oldValue;
192+
}, $result);
193+
194+
//disable unsupported payment methods
195+
$result = array_combine($result, $result);
196+
$result = array_merge($result, $this->unsupportedPaymentMethods);
197+
198+
return implode(',', $result);
199+
}
200+
201+
/**
202+
* Returns if is allowed PayPal Guest Checkout.
203+
*
204+
* @return bool
205+
*/
206+
private function isPaypalGuestCheckoutAllowed(): bool
207+
{
208+
return $this->config->getValue('solution_type') === Config::EC_SOLUTION_TYPE_SOLE;
209+
}
210+
211+
/**
212+
* Return intent value from the configuration payment_action value
213+
*
214+
* @return string
215+
*/
216+
private function getIntent(): string
217+
{
218+
$paymentAction = $this->config->getValue('paymentAction');
219+
$mappedIntentValues = [
220+
Config::PAYMENT_ACTION_AUTH => 'authorize',
221+
Config::PAYMENT_ACTION_SALE => 'capture',
222+
Config::PAYMENT_ACTION_ORDER => 'order'
223+
];
224+
return $mappedIntentValues[$paymentAction];
225+
}
226+
}

0 commit comments

Comments
 (0)