Skip to content

Commit 66a1283

Browse files
committed
load SDK only if it is necessary
1 parent 6db0437 commit 66a1283

File tree

5 files changed

+70
-88
lines changed

5 files changed

+70
-88
lines changed

src/Core/ViewConfig.php

+56-63
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@ class ViewConfig extends ViewConfig_parent
2525

2626
/**
2727
* is this a "Flow"-Theme Compatible Theme?
28-
* @param boolean
28+
* @var boolean
2929
*/
3030
protected $isFlowCompatibleTheme = null;
3131

3232
/**
3333
* is this a "Wave"-Theme Compatible Theme?
34-
* @param boolean
34+
* @var boolean
3535
*/
3636
protected $isWaveCompatibleTheme = null;
3737

38+
/**
39+
* is this SDK necessary?
40+
* @var boolean
41+
*/
42+
protected $isSDKNecessary = false;
43+
3844
/**
3945
* @return bool
4046
*/
@@ -123,6 +129,22 @@ public function getCheckoutOrderId()
123129
return PayPalSession::getCheckoutOrderId();
124130
}
125131

132+
/**
133+
* @return void
134+
*/
135+
public function setSDKIsNecessary()
136+
{
137+
$this->isSDKNecessary = true;
138+
}
139+
140+
/**
141+
* @return bool
142+
*/
143+
public function isSDKNecessary()
144+
{
145+
return $this->isSDKNecessary;
146+
}
147+
126148
/**
127149
* @return string
128150
*/
@@ -158,12 +180,30 @@ public function getPayPalJsSdkUrl(): string
158180
{
159181
$config = Registry::getConfig();
160182
$lang = Registry::getLang();
183+
$params = [];
184+
$enableFunding = [];
185+
$disableFunding = [
186+
'bancontact',
187+
'blik',
188+
'eps',
189+
'giropay',
190+
'ideal',
191+
'mercadopago',
192+
'p24',
193+
'venmo',
194+
];
195+
$components = [
196+
'buttons',
197+
];
198+
199+
if ($this->getTopActiveClassName() !== 'payment') {
200+
$disableFunding[] = 'sepa';
201+
}
161202

162203
$localeCode = $this->getServiceFromContainer(LanguageLocaleMapper::class)
163204
->mapLanguageToLocale($lang->getLanguageAbbr());
164205

165206
$moduleSettings = $this->getServiceFromContainer(ModuleSettings::class);
166-
$params = [];
167207

168208
$params['client-id'] = $this->getPayPalClientId();
169209
$params['integration-date'] = Constants::PAYPAL_INTEGRATION_DATE;
@@ -174,79 +214,32 @@ public function getPayPalJsSdkUrl(): string
174214
$params['currency'] = strtoupper($currency->name);
175215
}
176216

177-
$params['components'] = 'buttons';
178-
// Available components: enable messages+buttons for PDP
217+
$params['merchant-id'] = $moduleSettings->getMerchantId();
218+
179219
if ($this->isPayPalBannerActive()) {
180-
$params['components'] .= ',messages';
220+
$components[] = 'messages';
181221
}
182222

183223
if ($moduleSettings->showPayPalPayLaterButton()) {
184-
$params['enable-funding'] = 'paylater';
224+
$enableFunding[] = 'paylater';
185225
}
186226

187-
$params['disable-funding'] = 'sepa,bancontact,blik,eps,giropay,ideal,mercadopago,p24,venmo';
188-
189227
if ($moduleSettings->isAcdcEligibility()) {
190-
$params['disable-funding'] .= ',card';
228+
$components[] = 'hosted-fields';
191229
} else {
192-
if (isset($params['enable-funding'])) {
193-
$params['enable-funding'] .= ',card';
194-
} else {
195-
$params['enable-funding'] = 'card';
196-
}
230+
$enableFunding[] = 'card';
197231
}
198-
$params['locale'] = $localeCode;
199-
200-
return Constants::PAYPAL_JS_SDK_URL . '?' . http_build_query($params);
201-
}
202232

203-
/**
204-
* Gets PayPal JS SDK url for ACDC
205-
*
206-
* @return string
207-
*/
208-
public function getPayPalJsSdkUrlForACDC(): string
209-
{
210-
return $this->getBasePayPalJsSdkUrl('hosted-fields');
211-
}
212-
213-
/**
214-
* Gets PayPal JS SDK url for Button Payments like SEPA and CreditCardFallback
215-
*
216-
* @return string
217-
*/
218-
public function getPayPalJsSdkUrlForButtonPayments(): string
219-
{
220-
return $this->getBasePayPalJsSdkUrl('funding-eligibility', true);
221-
}
222-
223-
protected function getBasePayPalJsSdkUrl($type = '', $continueFlow = false): string
224-
{
225-
$config = Registry::getConfig();
226-
$lang = Registry::getLang();
227-
228-
$localeCode = $this->getServiceFromContainer(LanguageLocaleMapper::class)
229-
->mapLanguageToLocale($lang->getLanguageAbbr());
230-
231-
$params = [];
232-
233-
$params['client-id'] = $this->getPayPalClientId();
234-
$params['integration-date'] = Constants::PAYPAL_INTEGRATION_DATE;
235-
236-
if ($currency = $config->getActShopCurrencyObject()) {
237-
$params['currency'] = strtoupper($currency->name);
233+
if ($components) {
234+
$params['components'] = implode(',', $components);
238235
}
239-
240-
if ($continueFlow) {
241-
$params['intent'] = strtolower(Constants::PAYPAL_ORDER_INTENT_CAPTURE);
242-
$params['commit'] = 'false';
236+
if ($enableFunding) {
237+
$params['enable-funding'] = implode(',', $enableFunding);
243238
}
244-
245-
$params['components'] = 'buttons,' . $type;
246-
247-
if ($this->isPayPalBannerActive()) {
248-
$params['components'] .= ',messages';
239+
if ($disableFunding) {
240+
$params['disable-funding'] = implode(',', $disableFunding);
249241
}
242+
250243
$params['locale'] = $localeCode;
251244

252245
return Constants::PAYPAL_JS_SDK_URL . '?' . http_build_query($params);

views/tpl/shared/installment_banners.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[{if $oViewConf->enablePayPalBanners() && $oViewConf->getPayPalClientId()}]
2+
[{$oViewConf->setSDKIsNecessary()}]
23
[{if !isset($size) }]
34
[{assign var="size" value="20x1"}]
45
[{/if}]

views/tpl/shared/layout/base_js.tpl

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
[{if $oViewConf->isPayPalCheckoutActive()}]
2-
[{assign var="className" value=$oViewConf->getTopActiveClassName()}]
3-
[{if $oViewConf->isPayPalExpressPaymentEnabled() &&
4-
(($className == 'order' && !$oViewConf->isPayPalACDCSessionActive()) || ($className !== 'order' && $className !== 'payment')) &&
5-
(
6-
($oxcmp_basket->getProductsCount() && $oViewConf->showPayPalMiniBasketButton()) ||
7-
($className == 'details' && $oViewConf->showPayPalProductDetailsButton()) ||
8-
($className == 'basket' && $oViewConf->showPayPalBasketButton())
9-
)
10-
}]
11-
<script src="[{$oViewConf->getPayPalJsSdkUrl()}]" data-partner-attribution-id="[{$oViewConf->getPayPalPartnerAttributionIdForBanner()}]"></script>
12-
[{assign var="sCountryRestriction" value=$oViewConf->getCountryRestrictionForPayPalExpress()}]
13-
[{if $sCountryRestriction}]
14-
<script>
15-
const countryRestriction = [[{$sCountryRestriction}]];
16-
</script>
17-
[{/if}]
18-
[{elseif $className == 'order' && $oViewConf->isPayPalACDCSessionActive()}]
19-
<script src="[{$oViewConf->getPayPalJsSdkUrlForACDC()}]" data-client-token="[{$oViewConf->getDataClientToken()}]"></script>
20-
[{elseif $className == 'payment'}]
21-
<script src="[{$oViewConf->getPayPalJsSdkUrlForButtonPayments()}]" data-partner-attribution-id="[{$oViewConf->getPayPalPartnerAttributionIdForBanner()}]}]"></script>
22-
[{elseif $oViewConf->isPayPalBannerActive() && ($className == 'start' || $className == 'search' || $className == 'details' || $className == 'alist' || $className == 'basket')}]
23-
<script src="[{$oViewConf->getPayPalApiBannerUrl()}]" data-partner-attribution-id="[{$oViewConf->getPayPalPartnerAttributionIdForBanner()}]"></script>
1+
[{if $oViewConf->isPayPalCheckoutActive() && $oViewConf->isSDKNecessary()}]
2+
<script src="[{$oViewConf->getPayPalJsSdkUrl()}]"
3+
data-partner-attribution-id="[{$oViewConf->getPayPalPartnerAttributionIdForBanner()}]"
4+
data-client-token="[{$oViewConf->getDataClientToken()}]"
5+
></script>
6+
[{assign var="sCountryRestriction" value=$oViewConf->getCountryRestrictionForPayPalExpress()}]
7+
[{if $sCountryRestriction}]
8+
<script>
9+
const countryRestriction = [[{$sCountryRestriction}]];
10+
</script>
2411
[{/if}]
2512
[{if $submitCart}]
2613
<script>
2714
document.getElementById('orderConfirmAgbBottom').submit();
2815
</script>
2916
[{/if}]
30-
[{/if}]
17+
[{/if}]
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[{if $oViewConf->isPayPalCheckoutActive()}]
1+
[{if $oViewConf->isPayPalCheckoutActive() && $oViewConf->isSDKNecessary()}]
22
[{oxstyle include=$oViewConf->getModuleUrl('osc_paypal', 'out/src/css/paypal.min.css')}]
33
[{/if}]

views/tpl/shared/paymentbuttons.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[{block name="oscpaypal_paymentbuttons"}]
2+
[{$oViewConf->setSDKIsNecessary()}]
23
<div id="[{$buttonId}]" class="paypal-button-container [{$buttonClass}]"></div>
34
[{if $phpStorm}]<script>[{/if}]
45
[{capture assign="paypal_init"}]

0 commit comments

Comments
 (0)