Skip to content

Commit

Permalink
[PAYSHIP-2632] Order create refactoring (#1183)
Browse files Browse the repository at this point in the history
* Created required classes

* Added order create command handler logic

* Added paypal order query handler

* Moved QueryResult to Query namespace

* Added create command and get order query to command bus factory

* CS fix

* PHPStan fixes

* Reverted to old create handler

* PHPStan fixes
  • Loading branch information
L3RAZ authored Feb 13, 2024
1 parent fb08aa1 commit 0c47470
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 262 deletions.
27 changes: 25 additions & 2 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,12 @@ services:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\PayPalOrderSummaryViewBuilder'
public: true
arguments:
- '@ps_checkout.translations.translations'
- '@ps_checkout.funding_source.translation'
- '@ps_checkout.repository.pscheckoutcart'
- '@ps_checkout.paypal.provider.order'
- '@ps_checkout.prestashop.router'
- '@ps_checkout.paypal.order.translations'
- '@ps_checkout.context.shop'


ps_checkout.paypal.builder.view_order_summary:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\PayPalOrderSummaryViewBuilder'
Expand Down Expand Up @@ -462,6 +466,7 @@ services:
PrestaShop\Module\PrestashopCheckout\Order\Command\UpdateOrderStatusCommand: "ps_checkout.command.handler.order.update_order_status"
PrestaShop\Module\PrestashopCheckout\Order\Matrice\Command\UpdateOrderMatriceCommand: "ps_checkout.command.handler.order.matrice.update_order_matrice"
PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\CapturePayPalOrderCommand: "ps_checkout.command.handler.paypal.order.capture_paypal_order"
PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\CreatePayPalOrderCommand: "ps_checkout.command.handler.paypal.order.create_paypal_order"
PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\SavePayPalOrderCommand: "ps_checkout.command.handler.paypal.order.save_paypal_order"
PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentCompletedQuery: "ps_checkout.query.handler.order.get_order_for_payment_completed"
PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentDeniedQuery: "ps_checkout.query.handler.order.get_order_for_payment_denied"
Expand All @@ -471,6 +476,7 @@ services:
PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForApprovalReversedQuery: "ps_checkout.query.handler.order.get_order_for_approval_reversed"
PrestaShop\Module\PrestashopCheckout\PayPal\Identity\Query\GetClientTokenPayPalQuery: "ps_checkout.query.handler.paypal.identity.get_client_token"
PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetCurrentPayPalOrderStatusQuery: "ps_checkout.query.handler.paypal.order.get_current_paypal_order_status"
PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderQuery: "ps_checkout.query.handler.paypal.order.get_paypal_order"
PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForCheckoutCompletedQuery: "ps_checkout.query.handler.paypal.order.get_paypal_order_for_checkout_completed"
PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderForOrderConfirmationQuery: "ps_checkout.query.handler.paypal.order.get_paypal_order_for_order_confirmation"
PrestaShop\Module\PrestashopCheckout\Checkout\Command\UpdatePaymentMethodSelectedCommand: "ps_checkout.query.handler.checkout.update_payment_method_selected"
Expand Down Expand Up @@ -505,6 +511,7 @@ services:
- "@ps_checkout.checkout.checker"
- "@ps_checkout.paypal.order.service.check_transition_paypal_order_status"
- "@ps_checkout.order.state.service.order_state_mapper"
- '@ps_checkout.paypal.configuration'

ps_checkout.event.subscriber.paypal.capture:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\EventSubscriber\PayPalCaptureEventSubscriber'
Expand Down Expand Up @@ -564,6 +571,14 @@ services:
arguments:
- "@ps_checkout.event.dispatcher"

ps_checkout.command.handler.paypal.order.create_paypal_order:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\CommandHandler\CreatePayPalOrderCommandHandler'
public: true
arguments:
- '@?'
- '@?'
- '@ps_checkout.event.dispatcher'

ps_checkout.command.handler.paypal.order.capture_paypal_order:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\CommandHandler\CapturePayPalOrderCommandHandler'
public: true
Expand All @@ -575,6 +590,14 @@ services:
public: true
arguments:
- "@ps_checkout.repository.pscheckoutcart"
- '@ps_checkout.paypal.configuration'

ps_checkout.query.handler.paypal.order.get_paypal_order:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\QueryHandler\GetPayPalOrderQueryHandler'
public: true
arguments:
- '@ps_checkout.cache.paypal.order'
- '@ps_checkout.repository.pscheckoutcart'

ps_checkout.query.handler.paypal.order.get_current_paypal_order_status:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\QueryHandler\GetCurrentPayPalOrderStatusQueryHandler'
Expand Down
142 changes: 59 additions & 83 deletions controllers/front/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException;
use PrestaShop\Module\PrestashopCheckout\Cart\ValueObject\CartId;
use PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface;
use PrestaShop\Module\PrestashopCheckout\Controller\AbstractFrontController;
use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException;
use PrestaShop\Module\PrestashopCheckout\Handler\CreatePaypalOrderHandler;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\CreatePayPalOrderCommand;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Query\GetPayPalOrderQuery;
use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration;
use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository;

/**
* This controller receive ajax call to create a PayPal Order
Expand All @@ -43,6 +46,9 @@ class Ps_CheckoutCreateModuleFrontController extends AbstractFrontController
public function postProcess()
{
try {
/** @var CommandBusInterface $commandBus */
$commandBus = $this->module->getService('ps_checkout.bus.command');

// BEGIN Express Checkout
$bodyValues = [];
$bodyContent = file_get_contents('php://input');
Expand Down Expand Up @@ -92,102 +98,72 @@ public function postProcess()
$this->context->cookie->write();
}
// END Express Checkout
$cartId = (int) $this->context->cart->id;

if (false === Validate::isLoadedObject($this->context->cart)) {
$this->exitWithResponse([
'httpCode' => 400,
'body' => 'No cart found.',
]);
}

/** @var PsCheckoutCartRepository $psCheckoutCartRepository */
$psCheckoutCartRepository = $this->module->getService('ps_checkout.repository.pscheckoutcart');

/** @var PsCheckoutCart|false $psCheckoutCart */
$psCheckoutCart = $psCheckoutCartRepository->findOneByCartId((int) $this->context->cart->id);

$fundingSource = isset($bodyValues['fundingSource']) ? $bodyValues['fundingSource'] : 'paypal';
$isHostedFields = isset($bodyValues['isHostedFields']) && $bodyValues['isHostedFields'];
$isExpressCheckout = (isset($bodyValues['isExpressCheckout']) && $bodyValues['isExpressCheckout']) || empty($this->context->cart->id_address_delivery);

if (false !== $psCheckoutCart && $psCheckoutCart->isExpressCheckout() && $psCheckoutCart->isOrderAvailable()) {
$this->exitWithResponse([
'status' => true,
'httpCode' => 200,
'body' => [
'orderID' => $psCheckoutCart->paypal_order,
],
'exceptionCode' => null,
'exceptionMessage' => null,
]);
}
$commandBus->handle(new CreatePayPalOrderCommand(new CartId($cartId), $fundingSource, $isHostedFields, $isExpressCheckout));

$order = $commandBus->handle(new GetPayPalOrderQuery(null, new CartId($cartId)));
$orderId = $order['id'];

// If we have a PayPal Order Id with a status CREATED or APPROVED or PAYER_ACTION_REQUIRED we mark it as CANCELED and create new one
// This is needed because cart gets updated so we need to update paypal order too
if (
false !== $psCheckoutCart && $psCheckoutCart->getPaypalOrderId()
) {
$psCheckoutCart->paypal_status = PsCheckoutCart::STATUS_CANCELED;
$psCheckoutCartRepository->save($psCheckoutCart);
$psCheckoutCart = false;
}

$paypalOrder = new CreatePaypalOrderHandler($this->context);
$response = $paypalOrder->handle($isExpressCheckout);

if (false === $response['status']) {
throw new PsCheckoutException($response['exceptionMessage'], (int) $response['exceptionCode']);
}

if (empty($response['body']['id'])) {
throw new PsCheckoutException('Paypal order id is missing.', PsCheckoutException::PAYPAL_ORDER_IDENTIFIER_MISSING);
}

$paymentSource = isset($response['body']['payment_source']) ? key($response['body']['payment_source']) : 'paypal';
$fundingSource = isset($bodyValues['fundingSource']) ? $bodyValues['fundingSource'] : $paymentSource;
$orderId = isset($bodyValues['orderID']) ? $bodyValues['orderID'] : null;
$isExpressCheckout = isset($bodyValues['isExpressCheckout']) && $bodyValues['isExpressCheckout'];
$isHostedFields = isset($bodyValues['isHostedFields']) && $bodyValues['isHostedFields'];
/** @var PayPalConfiguration $configuration */
$configuration = $this->module->getService('ps_checkout.paypal.configuration');

$this->module->getLogger()->info(
'PayPal Order created',
[
'PayPalOrderId' => $orderId,
'FundingSource' => $fundingSource,
'isExpressCheckout' => $isExpressCheckout,
'isHostedFields' => $isHostedFields,
'id_cart' => (int) $this->context->cart->id,
'amount' => $this->context->cart->getOrderTotal(true, Cart::BOTH),
'environment' => $configuration->getPaymentMode(),
'intent' => $configuration->getIntent(),
]
);

if (false === $psCheckoutCart) {
$psCheckoutCart = new PsCheckoutCart();
$psCheckoutCart->id_cart = (int) $this->context->cart->id;
}

$psCheckoutCart->paypal_funding = $fundingSource;
$psCheckoutCart->paypal_order = $response['body']['id'];
$psCheckoutCart->paypal_status = $response['body']['status'];
$psCheckoutCart->paypal_intent = $configuration->getIntent();
$psCheckoutCart->paypal_token = $response['body']['client_token'];
$psCheckoutCart->paypal_token_expire = (new DateTime())->modify('+3550 seconds')->format('Y-m-d H:i:s');
$psCheckoutCart->environment = $configuration->getPaymentMode();
$psCheckoutCart->isExpressCheckout = isset($bodyValues['isExpressCheckout']) && $bodyValues['isExpressCheckout'];
$psCheckoutCart->isHostedFields = isset($bodyValues['isHostedFields']) && $bodyValues['isHostedFields'];
$psCheckoutCartRepository->save($psCheckoutCart);
// if ($psCheckoutCart && $psCheckoutCart->getPaypalOrderId()) {
// $psCheckoutCart->paypal_status = PsCheckoutCart::STATUS_CANCELED;
// $psCheckoutCartRepository->save($psCheckoutCart);
// $psCheckoutCart = false;
// }

// $paypalOrder = $this->module->getService('ps_checkout.handler.order.create_order');
// $response = $paypalOrder->handle($isExpressCheckout);

// if (false === $response['status']) {
// throw new PsCheckoutException($response['exceptionMessage'], (int) $response['exceptionCode']);
// }
//
// if (empty($response['body']['id'])) {
// throw new PsCheckoutException('Paypal order id is missing.', PsCheckoutException::PAYPAL_ORDER_IDENTIFIER_MISSING);
// }

// $paymentSource = isset($response['body']['payment_source']) ? key($response['body']['payment_source']) : 'paypal';
// $fundingSource = isset($bodyValues['fundingSource']) ? $bodyValues['fundingSource'] : $paymentSource;
// $orderId = isset($bodyValues['orderID']) ? $bodyValues['orderID'] : null;
// $isExpressCheckout = isset($bodyValues['isExpressCheckout']) && $bodyValues['isExpressCheckout'];
// $isHostedFields = isset($bodyValues['isHostedFields']) && $bodyValues['isHostedFields'];
// /** @var PayPalConfiguration $configuration */
// $configuration = $this->module->getService('ps_checkout.paypal.configuration');
//
// $this->module->getLogger()->info(
// 'PayPal Order created',
// [
// 'PayPalOrderId' => $orderId,
// 'FundingSource' => $fundingSource,
// 'isExpressCheckout' => $isExpressCheckout,
// 'isHostedFields' => $isHostedFields,
// 'id_cart' => (int) $this->context->cart->id,
// 'amount' => $this->context->cart->getOrderTotal(true, Cart::BOTH),
// 'environment' => $configuration->getPaymentMode(),
// 'intent' => $configuration->getIntent(),
// ]
// );

$this->exitWithResponse([
'status' => true,
'httpCode' => 200,
'body' => [
'orderID' => $psCheckoutCart->paypal_order,
'orderID' => $orderId,
],
'exceptionCode' => null,
'exceptionMessage' => null,
]);
} catch (CartNotFoundException $exception) {
$this->exitWithResponse([
'httpCode' => 400,
'body' => 'No cart found.',
]);
} catch (Exception $exception) {
$this->module->getLogger()->error(
'CreateController - Exception ' . $exception->getCode(),
Expand Down
88 changes: 0 additions & 88 deletions src/DTO/Orders/CreatePayPalOrderRequest.php

This file was deleted.

6 changes: 1 addition & 5 deletions src/Handler/CreatePaypalOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ class CreatePaypalOrderHandler
*/
private $context;

public function __construct(Context $context = null)
public function __construct(Context $context)
{
if (null === $context) {
$context = Context::getContext();
}

$this->context = $context;
}

Expand Down
Loading

0 comments on commit 0c47470

Please sign in to comment.