Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTR: clear PPE Session and control when to clear the backup cart #876

Merged
merged 3 commits into from
Oct 23, 2024
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
23 changes: 23 additions & 0 deletions polyfill/Shopware/Core/Checkout/Cart/CartPersisterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Checkout\Cart;

use Shopware\Core\Checkout\Cart\Exception\CartTokenNotFoundException;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

/**
* @deprecated tag:v6.5.0 - Use \Shopware\Core\Checkout\Cart\AbstractCartPersister instead
*/
#[Package('checkout')]
interface CartPersisterInterface
{
/**
* @throws CartTokenNotFoundException
*/
public function load(string $token, SalesChannelContext $context): Cart;

public function save(Cart $cart, SalesChannelContext $context): void;

public function delete(string $token, SalesChannelContext $context): void;
}
9 changes: 8 additions & 1 deletion src/Components/PaypalExpress/PayPalExpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function loadSession(string $sessionId, SalesChannelContext $context): Se
$sleepTimer = self::SESSION_BASE_TIMEOUT * ($i+1);
usleep($sleepTimer);
$session = $mollie->sessions->get($sessionId);
if ($session->methodDetails !== null && $session->methodDetails->shippingAddress !== null) {
if ($session->methodDetails !== null && property_exists($session->methodDetails, 'shippingAddress') && $session->methodDetails->shippingAddress !== null) {
break;
}
}
Expand Down Expand Up @@ -202,4 +202,11 @@ public function prepareCustomer(AddressStruct $shippingAddress, SalesChannelCont
# also (always) update our payment method to use Apple Pay for our cart
return $this->cartService->updatePaymentMethod($context, $paypalExpressId);
}

public function cancelSession(string $sessionId, SalesChannelContext $context): Session
{
$mollie = $this->mollieApiFactory->getLiveClient($context->getSalesChannelId());

return $mollie->sessions->cancel($sessionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kiener\MolliePayments\Controller\Storefront\PaypalExpress;

use Kiener\MolliePayments\Components\PaypalExpress\PayPalExpress;
use Kiener\MolliePayments\Service\Cart\CartBackupService;
use Kiener\MolliePayments\Service\CartService;
use Kiener\MolliePayments\Service\CustomFieldsInterface;
use Kiener\MolliePayments\Service\SettingsService;
Expand Down Expand Up @@ -42,6 +43,7 @@ class PaypalExpressControllerBase extends StorefrontController
*/
private $logger;
private SettingsService $settingsService;
private CartBackupService $cartBackupService;


/**
Expand All @@ -50,13 +52,14 @@ class PaypalExpressControllerBase extends StorefrontController
* @param RouterInterface $router
* @param LoggerInterface $logger
*/
public function __construct(PayPalExpress $paypalExpress, CartService $cartService, RouterInterface $router, SettingsService $settingsService, LoggerInterface $logger)
public function __construct(PayPalExpress $paypalExpress, CartService $cartService, RouterInterface $router, SettingsService $settingsService, CartBackupService $cartBackupService, LoggerInterface $logger)
{
$this->paypalExpress = $paypalExpress;
$this->cartService = $cartService;
$this->router = $router;
$this->logger = $logger;
$this->settingsService = $settingsService;
$this->cartBackupService = $cartBackupService;
}

/**
Expand Down Expand Up @@ -112,6 +115,49 @@ public function startCheckout(Request $request, SalesChannelContext $context): R
return new RedirectResponse($redirectUrl);
}

public function cancelCheckout(SalesChannelContext $context): Response
{
$redirectUrl = $this->getCheckoutCartPage($this->router);

$settings = $this->settingsService->getSettings($context->getSalesChannelId());

if ($settings->isPaypalExpressEnabled() === false) {
$this->logger->error('Paypal Express is disabled');
return new RedirectResponse($redirectUrl);
}



$cart = $this->cartService->getCalculatedMainCart($context);


$cartExtension = $cart->getExtension(CustomFieldsInterface::MOLLIE_KEY);
$sessionId = null;

if ($cartExtension instanceof ArrayStruct) {
$sessionId = $cartExtension[CustomFieldsInterface::PAYPAL_EXPRESS_SESSION_ID_KEY] ?? null;
}

$this->cartBackupService->restoreCart($context);
$this->cartBackupService->clearBackup($context);

if ($sessionId === null) {
$this->logger->error('Paypal Express session id is null');
return new RedirectResponse($redirectUrl);
}
try {
$session = $this->paypalExpress->cancelSession($sessionId, $context);
} catch (\Throwable $e) {
$this->logger->error('Failed to cancel Session at mollie', [
'message' => $e->getMessage(),
'sessionId' => $sessionId,
]);
}


return new RedirectResponse($redirectUrl);
}

/**
* @param SalesChannelContext $context
* @return Response
Expand Down Expand Up @@ -141,9 +187,6 @@ public function finishCheckout(SalesChannelContext $context): Response
}





if ($payPalExpressSessionId === null) {
$this->logger->error('Failed to finish checkout, session not exists');

Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/routes/storefront/paypal_express.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
<option key="seo">false</option>
</route>

<route id="frontend.mollie.paypal-express.cancel" path="/mollie/paypal-express/cancel" methods="GET">
<default key="_controller">Kiener\MolliePayments\Controller\Storefront\PaypalExpress\PaypalExpressControllerBase::cancelCheckout</default>
<default key="_routeScope"><list><string>storefront</string></list></default>
<default key="csrf_protected"><bool>true</bool></default>
<option key="seo">false</option>
</route>
</routes>
1 change: 1 addition & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
<argument type="service" id="Kiener\MolliePayments\Service\CartService"/>
<argument type="service" id="router"/>
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument type="service" id="Kiener\MolliePayments\Service\Cart\CartBackupService"/>
<argument type="service" id="mollie_payments.logger"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/services/subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<tag name="kernel.event_subscriber" />
</service>

<service id="Kiener\MolliePayments\Subscriber\SalesChannelContextTokenChangedSubscriber">
<argument type="service" id="Kiener\MolliePayments\Service\Cart\CartBackupService"/>
<tag name="kernel.event_subscriber"/>
</service>


</services>
</container>
17 changes: 11 additions & 6 deletions src/Service/Cart/CartBackupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Shopware\Core\Checkout\Cart\AbstractCartPersister;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\CartPersisterInterface;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
Expand All @@ -21,7 +22,7 @@ class CartBackupService
private $cartService;

/**
* @var AbstractCartPersister
* @var AbstractCartPersister|CartPersisterInterface
*/
private $cartPersister;
/**
Expand All @@ -30,12 +31,13 @@ class CartBackupService
private array $backupExistingCache;

/**
*
* @param CartService $cartService
* @param AbstractCartPersister|CartPersisterInterface $cartPersister
*/
public function __construct(CartService $cartService, AbstractCartPersister $cartPersister)
public function __construct(CartService $cartService, $cartPersister)
{
$this->cartService = $cartService;

$this->cartPersister = $cartPersister;
}

Expand Down Expand Up @@ -126,8 +128,11 @@ public function clearBackup(SalesChannelContext $context): void

public function replaceToken(string $oldToken, string $currentToken, SalesChannelContext $context): void
{
$oldToken = $this->getToken($oldToken);
$currentToken = $this->getToken($currentToken);
$this->cartPersister->replace($oldToken, $currentToken, $context);
#only cart persister has replace method, so it wont work in shopware 6.4.1.0
if ($this->cartPersister instanceof AbstractCartPersister) {
$oldToken = $this->getToken($oldToken);
$currentToken = $this->getToken($currentToken);
$this->cartPersister->replace($oldToken, $currentToken, $context);
}
}
}
2 changes: 1 addition & 1 deletion src/Service/Router/RoutingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function buildPaypalExpressRedirectUrl(): string
*/
public function buildPaypalExpressCancelUrl(): string
{
$confirmPage = $this->router->generate('frontend.checkout.confirm.page', [], $this->router::ABSOLUTE_URL);
$confirmPage = $this->router->generate('frontend.mollie.paypal-express.cancel', [], $this->router::ABSOLUTE_URL);

return $confirmPage;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Subscriber/ExpressCartRestoreSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
namespace Kiener\MolliePayments\Subscriber;

use Kiener\MolliePayments\Service\Cart\CartBackupService;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand All @@ -26,8 +24,6 @@ public static function getSubscribedEvents(): array
{
return [
CheckoutFinishPageLoadedEvent::class => 'onRestoreBackup',
CheckoutConfirmPageLoadedEvent::class => 'onRestoreBackup',
CheckoutRegisterPageLoadedEvent::class => 'onRestoreBackup'
];
}

Expand Down
33 changes: 33 additions & 0 deletions src/Subscriber/SalesChannelContextTokenChangedSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Subscriber;

use Kiener\MolliePayments\Service\Cart\CartBackupService;
use Shopware\Core\System\SalesChannel\Event\SalesChannelContextTokenChangeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SalesChannelContextTokenChangedSubscriber implements EventSubscriberInterface
{
/**
* @var CartBackupService
*/
private $cartBackupService;

public function __construct(CartBackupService $cartBackupService)
{
$this->cartBackupService = $cartBackupService;
}

public static function getSubscribedEvents()
{
return [
SalesChannelContextTokenChangeEvent::class => 'onTokenChange',
];
}

public function onTokenChange(SalesChannelContextTokenChangeEvent $event): void
{
$this->cartBackupService->replaceToken($event->getPreviousToken(), $event->getCurrentToken(), $event->getSalesChannelContext());
}
}
Loading