Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/actions/setup-paypal/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ runs:
scope: shopware
identity: ${{ github.event.repository.name }}

- name: Ignore audit
if: ${{ inputs.shopware-version != 'trunk' }}
shell: bash
run: |
mkdir -p ~/.config/composer
yq e -n -o=json '.config.audit.block-insecure=false' > ~/.config/composer/config.json

- name: Prepare dependency list
id: dependency-list
shell: bash
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# REPLACE_GLOBALLY_WITH_NEXT_VERSION
- Fixes an issue, where cookies are added even though associated payment methods are not active (shopware/SwagPayPal#457)

# 9.9.0
- Fixes an issue, where Paypal Express Checkout does not recalculate cart price after changing shipping country (shopware/SwagPayPal#342)

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG_de-DE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# REPLACE_GLOBALLY_WITH_NEXT_VERSION
- Behebt ein Problem, bei dem Cookies geladen wurden, obwohl die zugehörigen Zahlungsarten nicht aktiv sind (shopware/SwagPayPal#457)

# 9.9.0
- Behebt ein Problem, bei dem der Warenkorbpreis beim Verändern des Versandlandes im Express Checkout nicht angepasst wurde (shopware/SwagPayPal#342)

Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/services/storefront.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
<service id="Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider"
decorates="Shopware\Storefront\Framework\Cookie\CookieProviderInterface">
<argument type="service" id="Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider.inner"/>
<argument type="service" id="payment_method.repository"/>
<argument type="service" id="request_stack"/>
</service>

<service id="Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider"
decorates="Shopware\Storefront\Framework\Cookie\CookieProviderInterface">
<argument type="service" id="Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider.inner"/>
<argument type="service" id="payment_method.repository"/>
<argument type="service" id="request_stack"/>
</service>

<service id="Swag\PayPal\Storefront\RequestSubscriber">
Expand Down
14 changes: 13 additions & 1 deletion src/Storefront/Framework/Cookie/GooglePayCookieProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
use Swag\PayPal\Checkout\Payment\Method\GooglePayHandler;
use Symfony\Component\HttpFoundation\RequestStack;

#[Package('checkout')]
class GooglePayCookieProvider implements CookieProviderInterface
Expand All @@ -26,6 +28,7 @@ class GooglePayCookieProvider implements CookieProviderInterface
public function __construct(
CookieProviderInterface $cookieProvider,
private readonly EntityRepository $paymentMethodRepository,
private readonly RequestStack $requestStack,
) {
$this->original = $cookieProvider;
}
Expand Down Expand Up @@ -67,9 +70,18 @@ private function isRequiredCookieGroup(array $cookie): bool
private function isGooglePayActive(): bool
{
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new EqualsFilter('handlerIdentifier', GooglePayHandler::class));

return $this->paymentMethodRepository->searchIds($criteria, Context::createDefaultContext())->firstId() !== null;
$mainRequestAttributes = $this->requestStack->getMainRequest()?->attributes;
$context = $mainRequestAttributes?->get('sw-context') ?? Context::createDefaultContext();
$salesChannelId = $mainRequestAttributes?->getString('sw-sales-channel-id') ?? '';

if (Uuid::isValid($salesChannelId)) {
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelId));
}

return $this->paymentMethodRepository->searchIds($criteria, $context)->firstId() !== null;
}
}
36 changes: 34 additions & 2 deletions src/Storefront/Framework/Cookie/PayPalCookieProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@

namespace Swag\PayPal\Storefront\Framework\Cookie;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\PrefixFilter;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
use Symfony\Component\HttpFoundation\RequestStack;

#[Package('checkout')]
class PayPalCookieProvider implements CookieProviderInterface
Expand All @@ -18,8 +25,11 @@ class PayPalCookieProvider implements CookieProviderInterface
/**
* @internal
*/
public function __construct(CookieProviderInterface $cookieProvider)
{
public function __construct(
CookieProviderInterface $cookieProvider,
private readonly EntityRepository $paymentMethodRepository,
private readonly RequestStack $requestStack,
) {
$this->original = $cookieProvider;
}

Expand All @@ -40,6 +50,10 @@ public function getCookieGroups(): array
continue;
}

if (!$this->isPayPalPaymentActive()) {
continue;
}

$cookie['entries'][] = [
'snippet_name' => 'paypal.cookie.name',
'cookie' => 'paypal-cookie-key',
Expand All @@ -54,4 +68,22 @@ private function isRequiredCookieGroup(array $cookie): bool
return (\array_key_exists('isRequired', $cookie) && $cookie['isRequired'] === true)
&& (\array_key_exists('snippet_name', $cookie) && $cookie['snippet_name'] === 'cookie.groupRequired');
}

private function isPayPalPaymentActive(): bool
{
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new PrefixFilter('technicalName', 'swag_paypal_'));

$mainRequestAttributes = $this->requestStack->getMainRequest()?->attributes;
$context = $mainRequestAttributes?->get('sw-context') ?? Context::createDefaultContext();
$salesChannelId = $mainRequestAttributes?->getString('sw-sales-channel-id') ?? '';

if (Uuid::isValid($salesChannelId)) {
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelId));
}

return $this->paymentMethodRepository->searchIds($criteria, $context)->firstId() !== null;
}
}
26 changes: 21 additions & 5 deletions tests/Storefront/Framework/Cookie/GooglePayCookieProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -15,20 +16,31 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
use Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @internal
*/
#[Package('checkout')]
#[CoversClass(GooglePayCookieProvider::class)]
class GooglePayCookieProviderTest extends TestCase
{
private EntityRepository&MockObject $paymentMethodRepository;

private RequestStack $requestStack;

protected function setUp(): void
{
$request = new Request();
$request->attributes->set('sw-sales-channel-id', Uuid::randomHex());

$this->paymentMethodRepository = $this->createMock(EntityRepository::class);
$this->requestStack = new RequestStack();
$this->requestStack->push($request);
}

public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookies(): void
Expand All @@ -39,7 +51,7 @@ public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookie
->method('getCookieGroups')
->willReturn($cookies);

$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
static::assertSame($cookies, $result);
}

Expand All @@ -54,7 +66,7 @@ public function testGetCookieGroupsWithOriginalCookiesNotInSubArraysReturnsOrigi
->method('getCookieGroups')
->willReturn($cookies);

$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository))->getCookieGroups();
$result = (new GooglePayCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
static::assertSame($cookies, $result);
}

Expand All @@ -66,13 +78,17 @@ public function testGetCookieGroupsWithRequiredCookieGroup(array $cookies, bool
->method('getCookieGroups')
->willReturn($cookies);

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

$this->paymentMethodRepository->expects($cookieAdded ? static::once() : static::never())
->method('searchIds')
->willReturn($searchResult);
->willReturnCallback(static function (Criteria $criteria) use ($searchResult) {
static::assertCount(3, $criteria->getFilters());

return $searchResult;
});

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

if (!$cookieAdded) {
static::assertSame($cookies, $result);
Expand Down
40 changes: 37 additions & 3 deletions tests/Storefront/Framework/Cookie/PayPalCookieProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,42 @@

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

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
use Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @internal
*/
#[CoversClass(PayPalCookieProvider::class)]
#[Package('checkout')]
class PayPalCookieProviderTest extends TestCase
{
private EntityRepository&MockObject $paymentMethodRepository;

private RequestStack $requestStack;

protected function setUp(): void
{
$request = new Request();
$request->attributes->set('sw-sales-channel-id', Uuid::randomHex());

$this->paymentMethodRepository = $this->createMock(EntityRepository::class);
$this->requestStack = new RequestStack();
$this->requestStack->push($request);
}

public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookies(): void
{
$cookieProviderMock = $this->getMockBuilder(CookieProviderInterface::class)->getMock();
Expand All @@ -27,7 +51,7 @@ public function testGetCookieGroupsWithEmptyOriginalCookiesReturnsOriginalCookie
->method('getCookieGroups')
->willReturn($cookies);

$result = (new PayPalCookieProvider($cookieProviderMock))->getCookieGroups();
$result = (new PayPalCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
static::assertSame($cookies, $result);
}

Expand All @@ -42,7 +66,7 @@ public function testGetCookieGroupsWithOriginalCookiesNotInSubArraysReturnsOrigi
->method('getCookieGroups')
->willReturn($cookies);

$result = (new PayPalCookieProvider($cookieProviderMock))->getCookieGroups();
$result = (new PayPalCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
static::assertSame($cookies, $result);
}

Expand All @@ -54,7 +78,17 @@ public function testGetCookieGroupsWithRequiredCookieGroup(array $cookies, bool
->method('getCookieGroups')
->willReturn($cookies);

$result = (new PayPalCookieProvider($cookieProviderMock))->getCookieGroups();
$searchResult = new IdSearchResult(0, [Uuid::randomHex() => ['primaryKey' => 'test-id', 'data' => []]], new Criteria(), Context::createDefaultContext());

$this->paymentMethodRepository->expects($payPalCookieAdded ? static::once() : static::never())
->method('searchIds')
->willReturnCallback(static function (Criteria $criteria) use ($searchResult) {
static::assertCount(3, $criteria->getFilters());

return $searchResult;
});

$result = (new PayPalCookieProvider($cookieProviderMock, $this->paymentMethodRepository, $this->requestStack))->getCookieGroups();
if (!$payPalCookieAdded) {
static::assertSame($cookies, $result);

Expand Down
Loading