Skip to content

Commit

Permalink
Added redirects from payment controller
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Oct 30, 2024
1 parent 5e0341f commit a7b2026
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForOrderConfirmationQuery;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForOrderConfirmationQueryResult;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\ValueObject\PayPalOrderId;
use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalOrderProvider;
use PrestaShop\Module\PrestashopCheckout\Repository\PaymentTokenRepository;
use PrestaShop\Module\PrestashopCheckout\Repository\PayPalOrderRepository;

Expand Down Expand Up @@ -83,8 +82,6 @@ public function postProcess()

/** @var PayPalOrderRepository $payPalOrderRepository */
$payPalOrderRepository = $this->module->getService(PayPalOrderRepository::class);
/** @var PayPalOrderProvider $payPalOrderProvider */
$payPalOrderProvider = $this->module->getService(PayPalOrderProvider::class);
/** @var CommandBusInterface $commandBus */
$commandBus = $this->module->getService('ps_checkout.bus.command');
/** @var Psr\SimpleCache\CacheInterface $payPalOrderCache */
Expand All @@ -93,30 +90,26 @@ public function postProcess()
$payPalOrder = $payPalOrderRepository->getPayPalOrderById($this->paypalOrderId);

if ($payPalOrder->getIdCart() !== $this->context->cart->id) {
throw new Exception('PayPal order does not belong to this customer');
$this->redirectToOrderPage();
}

$orders = new PrestaShopCollection(Order::class);
$orders->where('id_cart', '=', $payPalOrder->getIdCart());

if ($orders->count()) {
$this->redirectToOrderHistoryPage();
}

/** @var GetPayPalOrderForOrderConfirmationQueryResult $payPalOrderQueryResult */
$payPalOrderQueryResult = $commandBus->handle(new GetPayPalOrderForOrderConfirmationQuery($this->paypalOrderId->getValue()));
$payPalOrderFromCache = $payPalOrderQueryResult->getOrderPayPal();

if ($payPalOrderFromCache['status'] === 'COMPLETED') {
$orders = new PrestaShopCollection(Order::class);
$orders->where('id_cart', '=', $payPalOrder->getIdCart());

if (!$orders->count()) {
$this->createOrder($payPalOrderFromCache, $payPalOrder);
}

$this->createOrder($payPalOrderFromCache, $payPalOrder);
$this->redirectToOrderConfirmationPage($payPalOrder->getIdCart(), $payPalOrderFromCache['purchase_units'][0]['payments']['captures'][0]['id'], $payPalOrderFromCache['status']);
}

if ($payPalOrderFromCache['status'] === 'PAYER_ACTION_REQUIRED') {
// Delete from cache so when user is redirected from 3DS authentication page the order is fetched from PayPal
if ($payPalOrderCache->has($this->paypalOrderId->getValue())) {
$payPalOrderCache->delete($this->paypalOrderId->getValue());
}

$this->redirectTo3DSVerification($payPalOrderFromCache);
}

Expand Down Expand Up @@ -235,4 +228,9 @@ private function redirectToOrderConfirmationPage($cartId, $captureId, $payPalOrd
));
}
}

private function redirectToOrderHistoryPage()
{
Tools::redirect($this->context->link->getPageLink('history'));
}
}

0 comments on commit a7b2026

Please sign in to comment.