Skip to content

Commit

Permalink
added order creation
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Oct 14, 2024
1 parent e4ad9a9 commit 70232f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
23 changes: 6 additions & 17 deletions controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException;
use PrestaShop\Module\PrestashopCheckout\Order\Command\CreateOrderCommand;
use PrestaShop\Module\PrestashopCheckout\Order\Command\UpdateOrderStatusCommand;
use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys;
use PrestaShop\Module\PrestashopCheckout\Order\State\Service\OrderStateMapper;
use PrestaShop\Module\PrestashopCheckout\PayPal\Card3DSecure;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\CapturePayPalOrderCommand;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Entity\PayPalOrder;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Exception\PayPalOrderException;
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;
Expand Down Expand Up @@ -90,35 +89,25 @@ public function postProcess()
$commandBus = $this->module->getService('ps_checkout.bus.command');
/** @var Psr\SimpleCache\CacheInterface $payPalOrderCache */
$payPalOrderCache = $this->module->getService('ps_checkout.cache.paypal.order');
/** @var OrderStateMapper $orderStateMapper */
$orderStateMapper = $this->module->getService(OrderStateMapper::class);

$payPalOrder = $payPalOrderRepository->getPayPalOrderById($this->paypalOrderId);

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

$payPalOrderCache->delete($this->paypalOrderId->getValue());

$payPalOrderFromCache = $payPalOrderProvider->getById($payPalOrder->getId()->getValue());
/** @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', '=', pSQL($payPalOrder->getIdCart()));
$orders->where('id_cart', '=', $payPalOrder->getIdCart());

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

/** @var Order $order */
$order = $orders->getFirst();
$paidOrderStateId = (int) $orderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED);

if ($order->getCurrentOrderState()->id !== $paidOrderStateId) {
$commandBus->handle(new UpdateOrderStatusCommand($order->id, $paidOrderStateId));
}

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

Expand Down
2 changes: 1 addition & 1 deletion controllers/front/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function postProcess()
} catch (HttpTimeoutException $exception) {
$this->exitWithResponse([
'status' => false,
'httpCode' => 408,
'httpCode' => 504,
'body' => [
'error' => [
'message' => $exception->getMessage(),
Expand Down
5 changes: 3 additions & 2 deletions src/Checkout/EventSubscriber/CheckoutEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public function proceedToPayment(CheckoutCompletedEvent $event)
$this->checkoutChecker->continueWithAuthorization($event->getCartId()->getValue(), $getPayPalOrderForCheckoutCompletedQueryResult->getPayPalOrder());
} catch (PsCheckoutException $exception) {
if ($exception->getCode() === PsCheckoutException::PAYPAL_ORDER_ALREADY_CAPTURED) {
// $this->commandBus->handle(new CreateOrderCommand($event->getPayPalOrderId()->getValue()));
// return;
$this->commandBus->handle(new CreateOrderCommand($event->getPayPalOrderId()->getValue()));
return;
} else {
throw $exception;
}
Expand Down Expand Up @@ -160,6 +160,7 @@ public function proceedToPayment(CheckoutCompletedEvent $event)

throw $exception;
} elseif ($exception->getCode() === PayPalException::ORDER_ALREADY_CAPTURED) {
$this->commandBus->handle(new CreateOrderCommand($event->getPayPalOrderId()->getValue()));
return;
} else {
throw $exception;
Expand Down

0 comments on commit 70232f4

Please sign in to comment.