Skip to content

Commit 5c4f467

Browse files
authored
[6.5.x] fix: filter cookie provider by active sales channel payment method (#475)
1 parent c43046e commit 5c4f467

File tree

8 files changed

+121
-14
lines changed

8 files changed

+121
-14
lines changed

.github/actions/setup-paypal/action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ runs:
4343
scope: shopware
4444
identity: ${{ github.event.repository.name }}
4545

46+
- name: Ignore audit
47+
if: ${{ inputs.shopware-version != 'trunk' }}
48+
shell: bash
49+
run: |
50+
mkdir -p ~/.config/composer
51+
yq e -n -o=json '.config.audit.block-insecure=false' > ~/.config/composer/config.json
52+
4653
- name: Prepare dependency list
4754
id: dependency-list
4855
shell: bash

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
# 8.8.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
# 8.8.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: 14 additions & 2 deletions
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
@@ -25,7 +27,8 @@ class GooglePayCookieProvider implements CookieProviderInterface
2527
*/
2628
public function __construct(
2729
CookieProviderInterface $cookieProvider,
28-
private readonly EntityRepository $paymentMethodRepository
30+
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
1616
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
1717
use Shopware\Core\Framework\Log\Package;
18+
use Shopware\Core\Framework\Uuid\Uuid;
1819
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
1920
use Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider;
21+
use Symfony\Component\HttpFoundation\Request;
22+
use Symfony\Component\HttpFoundation\RequestStack;
2023

2124
/**
2225
* @internal
@@ -26,9 +29,16 @@ class GooglePayCookieProviderTest extends TestCase
2629
{
2730
private EntityRepository&MockObject $paymentMethodRepository;
2831

32+
private RequestStack $requestStack;
33+
2934
protected function setUp(): void
3035
{
36+
$request = new Request();
37+
$request->attributes->set('sw-sales-channel-id', Uuid::randomHex());
38+
3139
$this->paymentMethodRepository = $this->createMock(EntityRepository::class);
40+
$this->requestStack = new RequestStack();
41+
$this->requestStack->push($request);
3242
}
3343

3444
public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookies(): void
@@ -39,7 +49,7 @@ public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookie
3949
->method('getCookieGroups')
4050
->willReturn($cookies);
4151

42-
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
52+
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
4353
static::assertSame($cookies, $result);
4454
}
4555

@@ -54,7 +64,7 @@ public function testGetCookieGroupsWithOriginalCookiesNotInSubArraysReturnsOrigi
5464
->method('getCookieGroups')
5565
->willReturn($cookies);
5666

57-
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
67+
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
5868
static::assertSame($cookies, $result);
5969
}
6070

@@ -68,13 +78,17 @@ public function testGetCookieGroupsWithRequiredCookieGroup(array $cookies, bool
6878
->method('getCookieGroups')
6979
->willReturn($cookies);
7080

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

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

77-
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
91+
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
7892

7993
if (!$cookieAdded) {
8094
static::assertSame($cookies, $result);

tests/Storefront/Framework/Cookie/PayPalCookieProviderTest.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,40 @@
77

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

10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use PHPUnit\Framework\MockObject\MockObject;
1012
use PHPUnit\Framework\TestCase;
13+
use Shopware\Core\Framework\Context;
14+
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
15+
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
16+
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
1117
use Shopware\Core\Framework\Log\Package;
18+
use Shopware\Core\Framework\Uuid\Uuid;
1219
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
1320
use Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider;
21+
use Symfony\Component\HttpFoundation\Request;
22+
use Symfony\Component\HttpFoundation\RequestStack;
1423

1524
/**
1625
* @internal
1726
*/
1827
#[Package('checkout')]
1928
class PayPalCookieProviderTest extends TestCase
2029
{
30+
private EntityRepository&MockObject $paymentMethodRepository;
31+
32+
private RequestStack $requestStack;
33+
34+
protected function setUp(): void
35+
{
36+
$request = new Request();
37+
$request->attributes->set('sw-sales-channel-id', Uuid::randomHex());
38+
39+
$this->paymentMethodRepository = $this->createMock(EntityRepository::class);
40+
$this->requestStack = new RequestStack();
41+
$this->requestStack->push($request);
42+
}
43+
2144
public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookies(): void
2245
{
2346
$cookieProviderMock = $this->getMockBuilder(CookieProviderInterface::class)->getMock();
@@ -26,7 +49,7 @@ public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookie
2649
->method('getCookieGroups')
2750
->willReturn($cookies);
2851

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

@@ -41,7 +64,7 @@ public function testGetCookieGroupsWithOriginalCookiesNotInSubArraysReturnsOrigi
4164
->method('getCookieGroups')
4265
->willReturn($cookies);
4366

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

@@ -55,7 +78,17 @@ public function testGetCookieGroupsWithRequiredCookieGroup(array $cookies, bool
5578
->method('getCookieGroups')
5679
->willReturn($cookies);
5780

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

@@ -74,7 +107,7 @@ public function testGetCookieGroupsWithRequiredCookieGroup(array $cookies, bool
74107
static::assertSame('paypal-cookie-key', $payPalCookie['cookie']);
75108
}
76109

77-
public function dataTestGetCookieGroupsWithRequiredCookieGroup(): array
110+
public static function dataTestGetCookieGroupsWithRequiredCookieGroup(): array
78111
{
79112
return [
80113
// Matching snippet name, missing is required flag

0 commit comments

Comments
 (0)