Skip to content

Commit 65f3db1

Browse files
authored
[6.6.x] fix: fallback to supported locale (#503)
1 parent faa777f commit 65f3db1

File tree

11 files changed

+1292
-21
lines changed

11 files changed

+1292
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 9.10.1
2+
- Fixes an issue, where languages not supported by PayPal did not fall back to a supported language (shopware/shopware#13950)
3+
14
# 9.10.0
25
- Fixes an issue, where the express checkout could choose a customer country that was not assigned to the correct sales channel (shopware/SwagPayPal#479)
36
- Fixes an issue, where cookies are added even though associated payment methods are not active (shopware/SwagPayPal#457)

CHANGELOG_de-DE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 9.10.1
2+
- Behebt ein Problem, bei dem nicht von PayPal unterstützte Sprachen nicht auf eine unterstützte Sprache korrigiert wurden (shopware/shopware#13950)
3+
14
# 9.10.0
25
- Behebt ein Problem, bei dem beim Express-Checkout ein Kundenland ausgewählt werden konnte, das nicht dem richtigen Vertriebskanal zugeordnet war (shopware/SwagPayPal#479)
36
- Behebt ein Problem, bei dem Cookies geladen wurden, obwohl die zugehörigen Zahlungsarten nicht aktiv sind (shopware/SwagPayPal#457)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "swag/paypal",
33
"description": "PayPal integration for Shopware 6",
4-
"version": "9.10.0",
4+
"version": "9.10.1",
55
"type": "shopware-platform-plugin",
66
"license": "MIT",
77
"authors": [

src/Checkout/ExpressCheckout/Service/PayPalExpressCheckoutDataService.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,9 @@ public function buildExpressCheckoutButtonData(
118118
]);
119119
}
120120

121-
protected function getButtonLanguage(SalesChannelContext $context): string
121+
protected function getButtonLanguageSetting(): string
122122
{
123-
if ($settingsLocale = $this->systemConfigService->getString(Settings::ECS_BUTTON_LANGUAGE_ISO, $context->getSalesChannelId())) {
124-
return $this->localeCodeProvider->getFormattedLocaleCode($settingsLocale);
125-
}
126-
127-
return $this->localeCodeProvider->getFormattedLocaleCode(
128-
$this->localeCodeProvider->getLocaleCodeFromContext($context->getContext())
129-
);
123+
return Settings::ECS_BUTTON_LANGUAGE_ISO;
130124
}
131125

132126
private function showPayLater(string $salesChannelId, ?AvailabilityContext $availabilityContext): bool
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* (c) shopware AG <info@shopware.com>
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
namespace Swag\PayPal\DevOps\Command;
9+
10+
use Shopware\Core\Framework\Log\Package;
11+
use Symfony\Component\Console\Attribute\AsCommand;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\DomCrawler\Crawler;
16+
use Symfony\Component\HttpClient\HttpClient;
17+
18+
#[AsCommand(
19+
name: 'swag:paypal:scrape:locales',
20+
description: 'Scrapes the PayPal developer website for locales and updates "Swag\PayPal\Util\PaypalLocales"',
21+
)]
22+
#[Package('checkout')]
23+
class LocaleScraperCommand extends Command
24+
{
25+
public const REGION_KEY = 'region';
26+
public const LOCALE_CODE_KEY = 'locale_code';
27+
public const PRIORITY = 'priority';
28+
private const PAYPAL_LOCALES_PAGE = 'https://developer.paypal.com/reference/locale-codes/';
29+
30+
protected function execute(InputInterface $input, OutputInterface $output): int
31+
{
32+
$client = HttpClient::create();
33+
$response = $client->request('GET', self::PAYPAL_LOCALES_PAGE);
34+
$html = $response->getContent();
35+
36+
$crawler = new Crawler($html);
37+
$locales = [];
38+
39+
$crawler->filter('table tbody tr')->each(function (Crawler $row) use (&$locales): void {
40+
$columns = $row->filter('td');
41+
if ($columns->count() < 3) {
42+
return;
43+
}
44+
45+
$countryCode = trim($columns->eq(1)->text());
46+
47+
$locales[$countryCode][] = [
48+
self::PRIORITY => trim($columns->eq(2)->text()),
49+
self::LOCALE_CODE_KEY => trim($columns->eq(3)->text()),
50+
];
51+
});
52+
53+
$localesClassContent = '';
54+
foreach ($locales as $countryCode => $localeOptions) {
55+
$localeString = '';
56+
foreach ($localeOptions as $locale) {
57+
$localeString .= $this->getLocaleString($locale);
58+
}
59+
$localesClassContent .= \sprintf(
60+
" '%s' => [\n%s\n ],\n",
61+
$countryCode,
62+
trim($localeString, "\n")
63+
);
64+
}
65+
66+
$localesClass = \sprintf($this->getClassTemplate(), self::PAYPAL_LOCALES_PAGE, \trim($localesClassContent, "\n"));
67+
68+
$localesClassPath = __DIR__ . '/../../Util/SupportedLocales.php';
69+
$result = \file_put_contents($localesClassPath, $localesClass, \LOCK_EX);
70+
if ($result === false) {
71+
throw new \RuntimeException(\sprintf('File "%s" could not be written', $localesClassPath));
72+
}
73+
74+
return 0;
75+
}
76+
77+
private function getLocaleString(array $locale): string
78+
{
79+
return <<<EOD
80+
{$locale[self::PRIORITY]} => '{$locale[self::LOCALE_CODE_KEY]}',
81+
82+
EOD;
83+
}
84+
85+
private function getClassTemplate(): string
86+
{
87+
return <<<EOD
88+
<?php declare(strict_types=1);
89+
/*
90+
* (c) shopware AG <info@shopware.com>
91+
* For the full copyright and license information, please view the LICENSE
92+
* file that was distributed with this source code.
93+
*/
94+
95+
namespace Swag\PayPal\Util;
96+
97+
use Shopware\Core\Framework\Log\Package;
98+
99+
/**
100+
* @url %s
101+
*/
102+
#[Package('checkout')]
103+
final class SupportedLocales
104+
{
105+
public const LOCALES = [
106+
%s
107+
];
108+
}
109+
110+
EOD;
111+
}
112+
}

src/Resources/config/services/dev_ops.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
<service id="Swag\PayPal\DevOps\Command\GenerateOpenApi">
99
<tag name="console.command"/>
1010
</service>
11+
12+
<service id="Swag\PayPal\DevOps\Command\LocaleScraperCommand">
13+
<tag name="console.command"/>
14+
</service>
1115
</services>
1216
</container>

src/Storefront/Data/Service/AbstractScriptDataService.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ protected function getBaseData(SalesChannelContext $context, ?OrderEntity $order
4646

4747
protected function getButtonLanguage(SalesChannelContext $context): string
4848
{
49-
if ($settingsLocale = $this->systemConfigService->getString(Settings::SPB_BUTTON_LANGUAGE_ISO, $context->getSalesChannelId())) {
49+
if ($settingsLocale = $this->systemConfigService->getString($this->getButtonLanguageSetting(), $context->getSalesChannelId())) {
5050
return $this->localeCodeProvider->getFormattedLocaleCode($settingsLocale);
5151
}
5252

5353
return $this->localeCodeProvider->getFormattedLocaleCode(
5454
$this->localeCodeProvider->getLocaleCodeFromContext($context->getContext())
5555
);
5656
}
57+
58+
protected function getButtonLanguageSetting(): string
59+
{
60+
return Settings::SPB_BUTTON_LANGUAGE_ISO;
61+
}
5762
}

src/Util/LocaleCodeProvider.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#[Package('checkout')]
2020
class LocaleCodeProvider implements ResetInterface
2121
{
22-
private const SUPPORTED_LOCALE_CODE_LENGTH = 5;
23-
2422
private const DEFAULT_LOCALE_CODE = 'en_GB';
2523

2624
private EntityRepository $languageRepository;
@@ -67,16 +65,31 @@ public function getFormattedLocaleCode(string $localeCode): string
6765
{
6866
$canonicalizedCode = (string) \Locale::canonicalize($localeCode);
6967

70-
if (\mb_strlen($canonicalizedCode) !== self::SUPPORTED_LOCALE_CODE_LENGTH) {
68+
$locales = SupportedLocales::LOCALES;
69+
70+
if (!\in_array($canonicalizedCode, \array_merge(...\array_values($locales)), true)) {
71+
$matched = $this->findMatchingSupportedLocale($canonicalizedCode, $locales);
72+
if (!$matched) {
73+
$this->logger->notice(
74+
\sprintf(
75+
'PayPal does not support locale code %s. Switched to default %s.',
76+
$localeCode,
77+
self::DEFAULT_LOCALE_CODE
78+
)
79+
);
80+
81+
return self::DEFAULT_LOCALE_CODE;
82+
}
83+
7184
$this->logger->notice(
7285
\sprintf(
73-
'PayPal does not support locale code %s. Switch to default %s',
86+
'PayPal does not support locale code %s. Switched to %s.',
7487
$localeCode,
75-
self::DEFAULT_LOCALE_CODE
88+
$matched
7689
)
7790
);
7891

79-
return self::DEFAULT_LOCALE_CODE;
92+
return $matched;
8093
}
8194

8295
return $canonicalizedCode;
@@ -86,4 +99,11 @@ public function reset(): void
8699
{
87100
$this->cache = [];
88101
}
102+
103+
private function findMatchingSupportedLocale(string $localeCode, array $locales): ?string
104+
{
105+
$localeCode = \Locale::getRegion($localeCode);
106+
107+
return $locales[$localeCode][0] ?? null;
108+
}
89109
}

0 commit comments

Comments
 (0)