Skip to content

Commit be426f6

Browse files
committed
[6.6.x] fix: filter cookie provider by active sales channel payment method
1 parent 426f654 commit be426f6

File tree

7 files changed

+112
-11
lines changed

7 files changed

+112
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# REPLACE_GLOBALLY_WITH_NEXT_VERSION
2+
- Fixes an issue, where cookies are added even though associated payment methods are not active (shopware/SwagPayPal#457)
3+
14
# 9.9.0
25
- Fixes an issue, where Paypal Express Checkout does not recalculate cart price after changing shipping country (shopware/SwagPayPal#342)
36

CHANGELOG_de-DE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# REPLACE_GLOBALLY_WITH_NEXT_VERSION
2+
- Behebt ein Problem, bei dem Cookies geladen wurden, obwohl die zugehörigen Zahlungsarten nicht aktiv sind (shopware/SwagPayPal#457)
3+
14
# 9.9.0
25
- Behebt ein Problem, bei dem der Warenkorbpreis beim Verändern des Versandlandes im Express Checkout nicht angepasst wurde (shopware/SwagPayPal#342)
36

src/Resources/config/services/storefront.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
<service id="Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider"
2525
decorates="Shopware\Storefront\Framework\Cookie\CookieProviderInterface">
2626
<argument type="service" id="Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider.inner"/>
27+
<argument type="service" id="payment_method.repository"/>
28+
<argument type="service" id="request_stack"/>
2729
</service>
2830

2931
<service id="Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider"
3032
decorates="Shopware\Storefront\Framework\Cookie\CookieProviderInterface">
3133
<argument type="service" id="Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider.inner"/>
3234
<argument type="service" id="payment_method.repository"/>
35+
<argument type="service" id="request_stack"/>
3336
</service>
3437

3538
<service id="Swag\PayPal\Storefront\RequestSubscriber">

src/Storefront/Framework/Cookie/GooglePayCookieProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
1313
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
1414
use Shopware\Core\Framework\Log\Package;
15+
use Shopware\Core\Framework\Uuid\Uuid;
1516
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
1617
use Swag\PayPal\Checkout\Payment\Method\GooglePayHandler;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1719

1820
#[Package('checkout')]
1921
class GooglePayCookieProvider implements CookieProviderInterface
@@ -26,6 +28,7 @@ class GooglePayCookieProvider implements CookieProviderInterface
2628
public function __construct(
2729
CookieProviderInterface $cookieProvider,
2830
private readonly EntityRepository $paymentMethodRepository,
31+
private readonly RequestStack $requestStack,
2932
) {
3033
$this->original = $cookieProvider;
3134
}
@@ -67,9 +70,18 @@ private function isRequiredCookieGroup(array $cookie): bool
6770
private function isGooglePayActive(): bool
6871
{
6972
$criteria = new Criteria();
73+
$criteria->setLimit(1);
7074
$criteria->addFilter(new EqualsFilter('active', true));
7175
$criteria->addFilter(new EqualsFilter('handlerIdentifier', GooglePayHandler::class));
7276

73-
return $this->paymentMethodRepository->searchIds($criteria, Context::createDefaultContext())->firstId() !== null;
77+
$mainRequestAttributes = $this->requestStack->getMainRequest()?->attributes;
78+
$context = $mainRequestAttributes?->get('sw-context') ?? Context::createDefaultContext();
79+
$salesChannelId = $mainRequestAttributes?->getString('sw-sales-channel-id') ?? '';
80+
81+
if (Uuid::isValid($salesChannelId)) {
82+
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelId));
83+
}
84+
85+
return $this->paymentMethodRepository->searchIds($criteria, $context)->firstId() !== null;
7486
}
7587
}

src/Storefront/Framework/Cookie/PayPalCookieProvider.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77

88
namespace Swag\PayPal\Storefront\Framework\Cookie;
99

10+
use Shopware\Core\Framework\Context;
11+
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
12+
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
13+
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
14+
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\PrefixFilter;
1015
use Shopware\Core\Framework\Log\Package;
16+
use Shopware\Core\Framework\Uuid\Uuid;
1117
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1219

1320
#[Package('checkout')]
1421
class PayPalCookieProvider implements CookieProviderInterface
@@ -18,8 +25,11 @@ class PayPalCookieProvider implements CookieProviderInterface
1825
/**
1926
* @internal
2027
*/
21-
public function __construct(CookieProviderInterface $cookieProvider)
22-
{
28+
public function __construct(
29+
CookieProviderInterface $cookieProvider,
30+
private readonly EntityRepository $paymentMethodRepository,
31+
private readonly RequestStack $requestStack,
32+
) {
2333
$this->original = $cookieProvider;
2434
}
2535

@@ -40,6 +50,10 @@ public function getCookieGroups(): array
4050
continue;
4151
}
4252

53+
if (!$this->isPayPalPaymentActive()) {
54+
continue;
55+
}
56+
4357
$cookie['entries'][] = [
4458
'snippet_name' => 'paypal.cookie.name',
4559
'cookie' => 'paypal-cookie-key',
@@ -54,4 +68,22 @@ private function isRequiredCookieGroup(array $cookie): bool
5468
return (\array_key_exists('isRequired', $cookie) && $cookie['isRequired'] === true)
5569
&& (\array_key_exists('snippet_name', $cookie) && $cookie['snippet_name'] === 'cookie.groupRequired');
5670
}
71+
72+
private function isPayPalPaymentActive(): bool
73+
{
74+
$criteria = new Criteria();
75+
$criteria->setLimit(1);
76+
$criteria->addFilter(new EqualsFilter('active', true));
77+
$criteria->addFilter(new PrefixFilter('technicalName', 'swag_paypal_'));
78+
79+
$mainRequestAttributes = $this->requestStack->getMainRequest()?->attributes;
80+
$context = $mainRequestAttributes?->get('sw-context') ?? Context::createDefaultContext();
81+
$salesChannelId = $mainRequestAttributes?->getString('sw-sales-channel-id') ?? '';
82+
83+
if (Uuid::isValid($salesChannelId)) {
84+
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelId));
85+
}
86+
87+
return $this->paymentMethodRepository->searchIds($criteria, $context)->firstId() !== null;
88+
}
5789
}

tests/Storefront/Framework/Cookie/GooglePayCookieProviderTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Swag\PayPal\Test\Storefront\Framework\Cookie;
99

10+
use PHPUnit\Framework\Attributes\CoversClass;
1011
use PHPUnit\Framework\Attributes\DataProvider;
1112
use PHPUnit\Framework\MockObject\MockObject;
1213
use PHPUnit\Framework\TestCase;
@@ -15,20 +16,30 @@
1516
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
1617
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
1718
use Shopware\Core\Framework\Log\Package;
19+
use Shopware\Core\Framework\Uuid\Uuid;
1820
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
1921
use Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider;
22+
use Symfony\Component\HttpFoundation\Request;
23+
use Symfony\Component\HttpFoundation\RequestStack;
2024

2125
/**
2226
* @internal
2327
*/
2428
#[Package('checkout')]
29+
#[CoversClass(GooglePayCookieProvider::class)]
2530
class GooglePayCookieProviderTest extends TestCase
2631
{
2732
private EntityRepository&MockObject $paymentMethodRepository;
2833

34+
private RequestStack $requestStack;
35+
2936
protected function setUp(): void
3037
{
38+
$request = new Request();
39+
$request->attributes->set('sw-sales-channel-id', Uuid::randomHex());
40+
3141
$this->paymentMethodRepository = $this->createMock(EntityRepository::class);
42+
$this->requestStack = new RequestStack([$request]);
3243
}
3344

3445
public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookies(): void
@@ -39,7 +50,7 @@ public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookie
3950
->method('getCookieGroups')
4051
->willReturn($cookies);
4152

42-
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
53+
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
4354
static::assertSame($cookies, $result);
4455
}
4556

@@ -54,7 +65,7 @@ public function testGetCookieGroupsWithOriginalCookiesNotInSubArraysReturnsOrigi
5465
->method('getCookieGroups')
5566
->willReturn($cookies);
5667

57-
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
68+
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
5869
static::assertSame($cookies, $result);
5970
}
6071

@@ -66,13 +77,17 @@ public function testGetCookieGroupsWithRequiredCookieGroup(array $cookies, bool
6677
->method('getCookieGroups')
6778
->willReturn($cookies);
6879

69-
$searchResult = new IdSearchResult(0, [['primaryKey' => 'test-id', 'data' => []]], new Criteria(), Context::createDefaultContext());
80+
$searchResult = new IdSearchResult(0, ['test-id' => ['primaryKey' => 'test-id', 'data' => []]], new Criteria(), Context::createDefaultContext());
7081

7182
$this->paymentMethodRepository->expects($cookieAdded ? static::once() : static::never())
7283
->method('searchIds')
73-
->willReturn($searchResult);
84+
->willReturnCallback(static function (Criteria $criteria) use ($searchResult) {
85+
static::assertCount(3, $criteria->getFilters());
86+
87+
return $searchResult;
88+
});
7489

75-
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
90+
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
7691

7792
if (!$cookieAdded) {
7893
static::assertSame($cookies, $result);

tests/Storefront/Framework/Cookie/PayPalCookieProviderTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,41 @@
77

88
namespace Swag\PayPal\Test\Storefront\Framework\Cookie;
99

10+
use PHPUnit\Framework\Attributes\CoversClass;
1011
use PHPUnit\Framework\Attributes\DataProvider;
12+
use PHPUnit\Framework\MockObject\MockObject;
1113
use PHPUnit\Framework\TestCase;
14+
use Shopware\Core\Framework\Context;
15+
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
16+
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
17+
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
1218
use Shopware\Core\Framework\Log\Package;
19+
use Shopware\Core\Framework\Uuid\Uuid;
1320
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
1421
use Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider;
22+
use Symfony\Component\HttpFoundation\Request;
23+
use Symfony\Component\HttpFoundation\RequestStack;
1524

1625
/**
1726
* @internal
1827
*/
28+
#[CoversClass(PayPalCookieProvider::class)]
1929
#[Package('checkout')]
2030
class PayPalCookieProviderTest extends TestCase
2131
{
32+
private EntityRepository&MockObject $paymentMethodRepository;
33+
34+
private RequestStack $requestStack;
35+
36+
protected function setUp(): void
37+
{
38+
$request = new Request();
39+
$request->attributes->set('sw-sales-channel-id', Uuid::randomHex());
40+
41+
$this->paymentMethodRepository = $this->createMock(EntityRepository::class);
42+
$this->requestStack = new RequestStack([$request]);
43+
}
44+
2245
public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookies(): void
2346
{
2447
$cookieProviderMock = $this->getMockBuilder(CookieProviderInterface::class)->getMock();
@@ -27,7 +50,7 @@ public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookie
2750
->method('getCookieGroups')
2851
->willReturn($cookies);
2952

30-
$result = (new PayPalCookieProvider($cookieProviderMock))->getCookieGroups();
53+
$result = (new PayPalCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
3154
static::assertSame($cookies, $result);
3255
}
3356

@@ -42,7 +65,7 @@ public function testGetCookieGroupsWithOriginalCookiesNotInSubArraysReturnsOrigi
4265
->method('getCookieGroups')
4366
->willReturn($cookies);
4467

45-
$result = (new PayPalCookieProvider($cookieProviderMock))->getCookieGroups();
68+
$result = (new PayPalCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
4669
static::assertSame($cookies, $result);
4770
}
4871

@@ -54,7 +77,17 @@ public function testGetCookieGroupsWithRequiredCookieGroup(array $cookies, bool
5477
->method('getCookieGroups')
5578
->willReturn($cookies);
5679

57-
$result = (new PayPalCookieProvider($cookieProviderMock))->getCookieGroups();
80+
$searchResult = new IdSearchResult(0, [Uuid::randomHex() => ['primaryKey' => 'test-id', 'data' => []]], new Criteria(), Context::createDefaultContext());
81+
82+
$this->paymentMethodRepository->expects($payPalCookieAdded ? static::once() : static::never())
83+
->method('searchIds')
84+
->willReturnCallback(static function (Criteria $criteria) use ($searchResult) {
85+
static::assertCount(3, $criteria->getFilters());
86+
87+
return $searchResult;
88+
});
89+
90+
$result = (new PayPalCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
5891
if (!$payPalCookieAdded) {
5992
static::assertSame($cookies, $result);
6093

0 commit comments

Comments
 (0)