Skip to content

Commit

Permalink
PISHPS-338: custom product compatibility (#820)
Browse files Browse the repository at this point in the history
* NTR: PISHPS-338: Add compatiblity with custom products

* NTR: stan fix

* NTR: fix recursion in sw6.4

* NTR: fix container

* NTR: fix container

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Sep 2, 2024
1 parent 5436200 commit 719ca14
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 318 deletions.
70 changes: 70 additions & 0 deletions src/Checkout/Cart/ExpressCartItemAddRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Checkout\Cart;

use Kiener\MolliePayments\Service\Cart\CartBackupService;
use Kiener\MolliePayments\Service\CartService;
use Psr\Container\ContainerInterface;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Core\Checkout\Cart\SalesChannel\AbstractCartItemAddRoute;
use Shopware\Core\Checkout\Cart\SalesChannel\CartResponse;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;

class ExpressCartItemAddRoute extends AbstractCartItemAddRoute
{
/**
* @var AbstractCartItemAddRoute
*/
private $cartItemAddRoute;

/**
* @var ContainerInterface
*/
private $container;

public function __construct(AbstractCartItemAddRoute $cartItemAddRoute, ContainerInterface $container)
{
$this->cartItemAddRoute = $cartItemAddRoute;
$this->container = $container;
}

public function getDecorated(): AbstractCartItemAddRoute
{
return $this->cartItemAddRoute;
}

/**
* @param Request $request
* @param Cart $cart
* @param SalesChannelContext $context
* @param ?array<mixed> $items
* @return CartResponse
*/
public function add(Request $request, Cart $cart, SalesChannelContext $context, ?array $items): CartResponse
{
//we have to create a new request from global variables, because the request is not set here in the route
$request = Request::createFromGlobals();

$isExpressCheckout = (bool)$request->get('isExpressCheckout', false);
if ($isExpressCheckout === false) {
return $this->getDecorated()->add($request, $cart, $context, $items);
}
$cartBackupService = $this->container->get(CartBackupService::class); //Shopware 6.4 have circular injection, we have to use contaier
if (!$cartBackupService->isBackupExisting($context)) {
$cartBackupService->backupCart($context);
}

$cartService = $this->container->get(CartService::class);

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

# clear existing cart and also update it to save it
$cart->setLineItems(new LineItemCollection());
$cartService->updateCart($cart);

return $this->getDecorated()->add($request, $cart, $context, $items);
}
}
6 changes: 0 additions & 6 deletions src/Components/ApplePayDirect/ApplePayDirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,6 @@ public function prepareCustomer(string $firstname, string $lastname, string $ema
}


# we clear our cart backup now
# we are in the user redirection process where a restoring wouldn't make sense
# because from now on we would end on the cart page where we could even switch payment method.
$this->cartBackupService->clearBackup($context);


$applePayID = $this->getActiveApplePayID($context);

# if we are not logged in,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,29 @@ public function pay(RequestDataBag $data, SalesChannelContext $context): StoreAp
if (empty($paymentToken)) {
throw new \Exception('PaymentToken not found!');
}
try {
# make sure to create a customer if necessary
# then update to our apple pay payment method
# and return the new context
$newContext = $this->applePay->prepareCustomer(
$firstname,
$lastname,
$email,
$street,
$zipcode,
$city,
$countryCode,
$phone,
$paymentToken,
$context
);

# we only start our TRY/CATCH here!
# we always need to throw exceptions on an API level
# but if something BELOW breaks, we want to navigate to the error page.
# customers are ready, data is ready, but the handling has a problem.

# make sure to create a customer if necessary
# then update to our apple pay payment method
# and return the new context
$newContext = $this->applePay->prepareCustomer(
$firstname,
$lastname,
$email,
$street,
$zipcode,
$city,
$countryCode,
$phone,
$paymentToken,
$context
);

# we only start our TRY/CATCH here!
# we always need to throw exceptions on an API level
# but if something BELOW breaks, we want to navigate to the error page.
# customers are ready, data is ready, but the handling has a problem.

try {
# create our new Shopware Order
$order = $this->applePay->createOrder($newContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Kiener\MolliePayments\Components\ApplePayDirect\ApplePayDirect;
use Kiener\MolliePayments\Controller\Storefront\AbstractStoreFrontController;
use Kiener\MolliePayments\Repository\Customer\CustomerRepositoryInterface;
use Kiener\MolliePayments\Service\Cart\CartBackupService;
use Kiener\MolliePayments\Service\OrderService;
use Kiener\MolliePayments\Traits\Storefront\RedirectTrait;
use Psr\Log\LoggerInterface;
Expand All @@ -21,7 +20,6 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\Routing\RouterInterface;
use Throwable;

Expand All @@ -42,20 +40,12 @@ class ApplePayDirectControllerBase extends AbstractStoreFrontController
*/
private $applePay;

/**
* @var CartBackupService
*/
private $cartBackupService;

/**
* @var RouterInterface
*/
private $router;

/**
* @var ?FlashBag
*/
private $flashBag;

/**
* @var FlowBuilderDispatcherAdapterInterface
Expand Down Expand Up @@ -87,21 +77,18 @@ class ApplePayDirectControllerBase extends AbstractStoreFrontController
* @param ApplePayDirect $applePay
* @param RouterInterface $router
* @param LoggerInterface $logger
* @param CartBackupService $cartBackup
* @param null|FlashBag $sessionFlashBag
* @param FlowBuilderFactory $flowBuilderFactory
* @param FlowBuilderEventFactory $flowBuilderEventFactory
* @param CustomerRepositoryInterface $repoCustomers
* @param OrderService $orderService
* @throws \Exception
*/
public function __construct(ApplePayDirect $applePay, RouterInterface $router, LoggerInterface $logger, CartBackupService $cartBackup, ?FlashBag $sessionFlashBag, FlowBuilderFactory $flowBuilderFactory, FlowBuilderEventFactory $flowBuilderEventFactory, CustomerRepositoryInterface $repoCustomers, OrderService $orderService)
public function __construct(ApplePayDirect $applePay, RouterInterface $router, LoggerInterface $logger, FlowBuilderFactory $flowBuilderFactory, FlowBuilderEventFactory $flowBuilderEventFactory, CustomerRepositoryInterface $repoCustomers, OrderService $orderService)
{
$this->applePay = $applePay;
$this->router = $router;
$this->logger = $logger;
$this->flashBag = $sessionFlashBag;
$this->cartBackupService = $cartBackup;

$this->repoCustomers = $repoCustomers;
$this->orderService = $orderService;

Expand Down Expand Up @@ -329,12 +316,6 @@ public function setShippingMethod(SalesChannelContext $context, Request $request
public function startPayment(SalesChannelContext $context, Request $request): Response
{
try {
# we clear our cart backup now
# we are in the user redirection process where a restoring wouldnt make sense
# because from now on we would end on the cart page where we could even switch payment method.
$this->cartBackupService->clearBackup($context);


$email = (string)$request->get('email', '');
$firstname = (string)$request->get('firstname', '');
$lastname = (string)$request->get('lastname', '');
Expand Down Expand Up @@ -375,10 +356,8 @@ public function startPayment(SalesChannelContext $context, Request $request): Re
# if we have an error here, we have to redirect to the confirm page
$returnUrl = $this->getCheckoutConfirmPage($this->router);
# also add an error for our target page
if ($this->flashBag !== null) {
$this->flashBag->add('danger', $this->trans(self::SNIPPET_ERROR));
}

$this->addFlash('danger', $this->trans(self::SNIPPET_ERROR));
return new RedirectResponse($returnUrl);
}
}
Expand Down Expand Up @@ -420,10 +399,8 @@ public function finishPayment(SalesChannelContext $context, Request $request): R
# if we have an error here, we have to redirect to the confirm page
$returnUrl = $this->getCheckoutConfirmPage($this->router);
# also add an error for our target page
if ($this->flashBag !== null) {
$this->flashBag->add('danger', $this->trans(self::SNIPPET_ERROR));
}

$this->addFlash('danger', $this->trans(self::SNIPPET_ERROR));
return new RedirectResponse($returnUrl);
}

Expand Down Expand Up @@ -463,9 +440,7 @@ public function finishPayment(SalesChannelContext $context, Request $request): R
$returnUrl = $this->getEditOrderPage($order->getId(), $this->router);

# also add an error for our target page
if ($this->flashBag !== null) {
$this->flashBag->add('danger', $this->trans(self::SNIPPET_ERROR));
}
$this->addFlash('danger', $this->trans(self::SNIPPET_ERROR));

# fire our custom storefront event
$this->fireFlowBuilderStorefrontEvent(self::FLOWBUILDER_FAILED, $order, $context->getContext());
Expand Down
Loading

0 comments on commit 719ca14

Please sign in to comment.