Skip to content

Commit

Permalink
bug #152 Process locales to match paypal requirements (SirDomin, Zale…
Browse files Browse the repository at this point in the history
…s0123)

This PR was merged into the 1.0-dev branch.

Discussion
----------

fixes #131

Commits
-------

1dfe4ad process locales to match paypal requirements
4cf8151 use locale from symfony
ebc657f Support more than 5-signs locales properly
  • Loading branch information
Zales0123 authored Oct 29, 2020
2 parents b67bc93 + ebc657f commit d51d2c4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"vimeo/psalm": "3.11.4"
},
"conflict": {
"symplify/package-builder": "^8.3.24"
"symplify/package-builder": "^8.3.24",
"symfony/doctrine-bridge": "4.4.16"
},
"autoload": {
"psr-4": {
Expand Down
24 changes: 24 additions & 0 deletions spec/Processor/LocaleProcessorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\PayPalPlugin\Processor;

use PhpSpec\ObjectBehavior;

final class LocaleProcessorSpec extends ObjectBehavior
{
function it_always_processes_locale_to_version_with_region(): void
{
$this->process('et')->shouldReturn('et_EE');
$this->process('pl')->shouldReturn('pl_PL');
$this->process('ja')->shouldReturn('ja_JP');
}

function it_returns_same_locale_if_it_is_valid(): void
{
$this->process('it_IT')->shouldReturn('it_IT');
$this->process('ja_JP_TRADITIONAL')->shouldReturn('ja_JP_TRADITIONAL');
$this->process('sd_Arab_PK')->shouldReturn('sd_Arab_PK');
}
}
14 changes: 10 additions & 4 deletions src/Controller/PayPalButtonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\PayPalPlugin\Processor\LocaleProcessorInterface;
use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
use Sylius\PayPalPlugin\Provider\PayPalConfigurationProviderInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -39,14 +40,18 @@ final class PayPalButtonsController
/** @var AvailableCountriesProviderInterface */
private $availableCountriesProvider;

/** @var LocaleProcessorInterface */
private $localeProcessor;

public function __construct(
Environment $twig,
UrlGeneratorInterface $router,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
PayPalConfigurationProviderInterface $payPalConfigurationProvider,
OrderRepositoryInterface $orderRepository,
AvailableCountriesProviderInterface $availableCountriesProvider
AvailableCountriesProviderInterface $availableCountriesProvider,
LocaleProcessorInterface $localeProcessor
) {
$this->twig = $twig;
$this->router = $router;
Expand All @@ -55,6 +60,7 @@ public function __construct(
$this->payPalConfigurationProvider = $payPalConfigurationProvider;
$this->orderRepository = $orderRepository;
$this->availableCountriesProvider = $availableCountriesProvider;
$this->localeProcessor = $localeProcessor;
}

public function renderProductPageButtonsAction(Request $request): Response
Expand All @@ -70,7 +76,7 @@ public function renderProductPageButtonsAction(Request $request): Response
'completeUrl' => $this->router->generate('sylius_shop_checkout_complete'),
'createPayPalOrderFromProductUrl' => $this->router->generate('sylius_paypal_plugin_create_paypal_order_from_product', ['productId' => $productId]),
'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error'),
'locale' => $this->localeContext->getLocaleCode(),
'locale' => $this->localeProcessor->process($this->localeContext->getLocaleCode()),
'processPayPalOrderUrl' => $this->router->generate('sylius_paypal_plugin_process_paypal_order'),
]));
} catch (\InvalidArgumentException $exception) {
Expand All @@ -94,7 +100,7 @@ public function renderCartPageButtonsAction(Request $request): Response
'createPayPalOrderFromCartUrl' => $this->router->generate('sylius_paypal_plugin_create_paypal_order_from_cart', ['id' => $orderId]),
'currency' => $order->getCurrencyCode(),
'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error'),
'locale' => $order->getLocaleCode(),
'locale' => $this->localeProcessor->process((string) $order->getLocaleCode()),
'orderId' => $orderId,
'partnerAttributionId' => $this->payPalConfigurationProvider->getPartnerAttributionId($channel),
'processPayPalOrderUrl' => $this->router->generate('sylius_paypal_plugin_process_paypal_order'),
Expand All @@ -121,7 +127,7 @@ public function renderPaymentPageButtonsAction(Request $request): Response
'completePayPalOrderFromPaymentPageUrl' => $this->router->generate('sylius_paypal_plugin_complete_paypal_order_from_payment_page', ['id' => $orderId]),
'createPayPalOrderFromPaymentPageUrl' => $this->router->generate('sylius_paypal_plugin_create_paypal_order_from_payment_page', ['id' => $orderId]),
'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error'),
'locale' => $order->getLocaleCode(),
'locale' => $this->localeProcessor->process((string) $order->getLocaleCode()),
'orderId' => $orderId,
'partnerAttributionId' => $this->payPalConfigurationProvider->getPartnerAttributionId($channel),
]));
Expand Down
31 changes: 31 additions & 0 deletions src/Processor/LocaleProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Processor;

use Symfony\Component\Intl\Locales;

final class LocaleProcessor implements LocaleProcessorInterface
{
public function process(string $locale): string
{
if ($this->isValidLocale($locale)) {
return $locale;
}

$locales = array_filter(Locales::getLocales(), function (string $targetLocale) use ($locale): bool {
return
strpos($targetLocale, $locale) === 0 &&
strpos($targetLocale, '_') !== false
;
});

return $locales[array_key_first($locales)];
}

private function isValidLocale(string $locale): bool
{
return strpos($locale, '_') !== false;
}
}
10 changes: 10 additions & 0 deletions src/Processor/LocaleProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Processor;

interface LocaleProcessorInterface
{
public function process(string $locale): string;
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
<argument type="service" id="payum" />
</service>

<service
id="Sylius\PayPalPlugin\Processor\LocaleProcessorInterface"
class="Sylius\PayPalPlugin\Processor\LocaleProcessor"
/>

<service
id="Sylius\PayPalPlugin\Processor\PaymentRefundProcessorInterface"
class="Sylius\PayPalPlugin\Processor\PayPalPaymentRefundProcessor"
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<argument type="service" id="Sylius\PayPalPlugin\Provider\PayPalConfigurationProviderInterface" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Processor\LocaleProcessorInterface" />
</service>

<service id="Sylius\PayPalPlugin\Controller\PayWithPayPalFormAction">
Expand Down

0 comments on commit d51d2c4

Please sign in to comment.