Skip to content

Commit

Permalink
NTR: PISHPS-338: Add compatiblity with custom products
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Sep 2, 2024
1 parent 5436200 commit 6338971
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 299 deletions.
66 changes: 66 additions & 0 deletions src/Checkout/Cart/ExpressCartItemAddRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Checkout\Cart;

use Kiener\MolliePayments\Service\Cart\CartBackupService;
use Kiener\MolliePayments\Service\CartServiceInterface;
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 CartBackupService
*/

private $cartBackupService;
/**
* @var CartServiceInterface
*/
private $cartService;

public function __construct(AbstractCartItemAddRoute $cartItemAddRoute, CartBackupService $cartBackupService, CartServiceInterface $cartService)
{
$this->cartItemAddRoute = $cartItemAddRoute;
$this->cartBackupService = $cartBackupService;
$this->cartService = $cartService;
}

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

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);
}

if (!$this->cartBackupService->isBackupExisting($context)) {
$this->cartBackupService->backupCart($context);
}

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

# clear existing cart and also update it to save it
$cart->setLineItems(new LineItemCollection());
$this->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 @@ -95,12 +95,11 @@ class ApplePayDirectControllerBase extends AbstractStoreFrontController
* @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, CartBackupService $cartBackup, 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 @@ -332,7 +331,7 @@ public function startPayment(SalesChannelContext $context, Request $request): Re
# 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);
// $this->cartBackupService->clearBackup($context);


$email = (string)$request->get('email', '');
Expand Down Expand Up @@ -375,10 +374,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 +417,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 +458,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 6338971

Please sign in to comment.