diff --git a/config.xml b/config.xml index 04c4a7e81..21dc64603 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ ps_checkout - + diff --git a/config/common.yml b/config/common.yml index f94a67ac5..2871a1fad 100644 --- a/config/common.yml +++ b/config/common.yml @@ -519,8 +519,6 @@ services: class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\EventSubscriber\PayPalRefundEventSubscriber' arguments: - '@ps_checkout.module' - - '@ps_checkout.order.service.check_order_amount' - - '@ps_checkout.cache.paypal.capture' - '@ps_checkout.cache.paypal.order' - '@ps_checkout.order.state.service.order_state_mapper' - '@ps_checkout.paypal.provider.order' diff --git a/controllers/front/cancel.php b/controllers/front/cancel.php index d0ced054e..6581a3a0b 100644 --- a/controllers/front/cancel.php +++ b/controllers/front/cancel.php @@ -70,7 +70,9 @@ public function postProcess() $isExpressCheckout = isset($bodyValues['isExpressCheckout']) && $bodyValues['isExpressCheckout']; $isHostedFields = isset($bodyValues['isHostedFields']) && $bodyValues['isHostedFields']; $reason = isset($bodyValues['reason']) ? Tools::safeOutput($bodyValues['reason']) : null; - $error = isset($bodyValues['error']) ? Tools::safeOutput($bodyValues['error']) : null; + $error = isset($bodyValues['error']) + ? Tools::safeOutput(is_string($bodyValues['error']) ? $bodyValues['error'] : json_encode($bodyValues['error'])) + : null; if ($orderId) { /** @var CommandBusInterface $commandBus */ diff --git a/ps_checkout.php b/ps_checkout.php index 64b240d68..6e8f7107c 100755 --- a/ps_checkout.php +++ b/ps_checkout.php @@ -105,6 +105,7 @@ class Ps_checkout extends PaymentModule 'PS_CHECKOUT_LIABILITY_SHIFT_REQ' => '1', 'PS_CHECKOUT_DISPLAY_LOGO_PRODUCT' => '1', 'PS_CHECKOUT_DISPLAY_LOGO_CART' => '1', + 'PS_CHECKOUT_HOSTED_FIELDS_CONTINGENCIES' => 'SCA_WHEN_REQUIRED', ]; public $confirmUninstall; @@ -112,7 +113,7 @@ class Ps_checkout extends PaymentModule // Needed in order to retrieve the module version easier (in api call headers) than instanciate // the module each time to get the version - const VERSION = '7.3.6.3'; + const VERSION = '7.3.6.4'; const INTEGRATION_DATE = '2022-14-06'; @@ -133,7 +134,7 @@ public function __construct() // We cannot use the const VERSION because the const is not computed by addons marketplace // when the zip is uploaded - $this->version = '7.3.6.3'; + $this->version = '7.3.6.4'; $this->author = 'PrestaShop'; $this->currencies = true; $this->currencies_mode = 'checkbox'; @@ -171,7 +172,7 @@ public function install() $this->installConfiguration() && $this->installHooks() && (new PrestaShop\Module\PrestashopCheckout\Database\TableManager())->createTable() && - (new PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceInstaller())->createFundingSources() && + (new PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceInstaller())->createFundingSourcesOnAllShops() && $this->installTabs() && $this->disableIncompatibleCountries() && $this->disableIncompatibleCurrencies(); @@ -1291,6 +1292,7 @@ public function hookActionObjectShopAddAfter(array $params) Configuration::set($name, $value, (int) $shop->id_shop_group, (int) $shop->id); } + (new PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceInstaller())->createFundingSources((int) $shop->id); $this->addCheckboxCarrierRestrictionsForModule([(int) $shop->id]); $this->addCheckboxCountryRestrictionsForModule([(int) $shop->id]); diff --git a/src/Event/EventDispatcherInterface.php b/src/Event/EventDispatcherInterface.php index d30df28af..815f6495f 100644 --- a/src/Event/EventDispatcherInterface.php +++ b/src/Event/EventDispatcherInterface.php @@ -20,25 +20,17 @@ namespace PrestaShop\Module\PrestashopCheckout\Event; -use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface; - -if (interface_exists(PsrEventDispatcherInterface::class)) { - interface EventDispatcherInterface extends PsrEventDispatcherInterface - { - } -} else { +/** + * Defines a dispatcher for events. + */ +interface EventDispatcherInterface +{ /** - * Defines a dispatcher for events. + * Provide all relevant listeners with an event to process. + * + * @param object $event the object to process + * + * @return object the Event that was passed, now modified by listeners */ - interface EventDispatcherInterface - { - /** - * Provide all relevant listeners with an event to process. - * - * @param object $event the object to process - * - * @return object the Event that was passed, now modified by listeners - */ - public function dispatch($event); - } + public function dispatch($event); } diff --git a/src/FundingSource/FundingSourceConfigurationRepository.php b/src/FundingSource/FundingSourceConfigurationRepository.php index c4936eaa3..ac7d67165 100644 --- a/src/FundingSource/FundingSourceConfigurationRepository.php +++ b/src/FundingSource/FundingSourceConfigurationRepository.php @@ -50,12 +50,13 @@ public function __construct(PrestaShopContext $context) /** * @param string $name + * @param int|null $shopId * * @return array|null */ - public function get($name) + public function get($name, $shopId = null) { - $fundingSources = $this->getAll(); + $fundingSources = $this->getAll($shopId); if (null === $fundingSources) { return null; @@ -71,42 +72,53 @@ public function get($name) } /** + * @param int|null $shopId + * * @return array|null */ - public function getAll() + public function getAll($shopId = null) { - if (null !== $this->fundingSources) { - return $this->fundingSources; + $shopId = (int) ($shopId === null ? $this->context->getShopId() : $shopId); + + if (isset($this->fundingSources[$shopId]) && null !== $this->fundingSources[$shopId]) { + return $this->fundingSources[$shopId]; } $data = $this->db->executeS(' SELECT `name`, `active`, `position` FROM `' . _DB_PREFIX_ . 'pscheckout_funding_source` - WHERE `id_shop` = ' . (int) $this->context->getShopId() + WHERE `id_shop` = ' . (int) $shopId ); if (!empty($data)) { - $this->fundingSources = $data; + $this->fundingSources[$shopId] = $data; } - return $this->fundingSources; + return isset($this->fundingSources[$shopId]) ? $this->fundingSources[$shopId] : null; } /** * @param array $data + * @param int|null $shopId * * @return bool + * + * @throws \PrestaShopDatabaseException */ - public function save($data) + public function save($data, $shopId = null) { - if ($this->get($data['name'])) { + $shopId = (int) ($shopId === null ? $this->context->getShopId() : $shopId); + + $this->fundingSources[$shopId] = null; + + if ($this->get($data['name'], $shopId)) { return (bool) $this->db->update( 'pscheckout_funding_source', [ 'position' => (int) $data['position'], 'active' => (int) $data['isEnabled'], ], - '`name` = "' . pSQL($data['name']) . '" AND `id_shop` = ' . (int) $this->context->getShopId() + '`name` = "' . pSQL($data['name']) . '" AND `id_shop` = ' . (int) $shopId ); } @@ -116,7 +128,7 @@ public function save($data) 'name' => pSQL($data['name']), 'position' => (int) $data['position'], 'active' => (int) $data['isEnabled'], - 'id_shop' => (int) $this->context->getShopId(), + 'id_shop' => (int) $shopId, ] ); } diff --git a/src/FundingSource/FundingSourceInstaller.php b/src/FundingSource/FundingSourceInstaller.php index e764707c1..ddc811491 100644 --- a/src/FundingSource/FundingSourceInstaller.php +++ b/src/FundingSource/FundingSourceInstaller.php @@ -27,9 +27,11 @@ class FundingSourceInstaller /** * Saves Funding Sources for the first time into the database * + * @param int|null $shopId + * * @return bool */ - public function createFundingSources() + public function createFundingSources($shopId = null) { $fundingSourceConfigurationRepository = new FundingSourceConfigurationRepository(new PrestaShopContext()); $fundingSourceCollectionBuilder = new FundingSourceCollectionBuilder( @@ -43,9 +45,20 @@ public function createFundingSources() 'name' => $fundingSourceEntity->getName(), 'position' => $fundingSourceEntity->getPosition(), 'isEnabled' => $fundingSourceEntity->getIsEnabled() ? 1 : 0, - ]); + ], $shopId); } return true; } + + public function createFundingSourcesOnAllShops() + { + $result = true; + + foreach (\Shop::getShops(false, null, true) as $shopId) { + $result &= $this->createFundingSources((int) $shopId); + } + + return $result; + } } diff --git a/src/Order/Query/GetOrderForApprovalReversedQueryResult.php b/src/Order/Query/GetOrderForApprovalReversedQueryResult.php index 96cc0b578..668e862bb 100644 --- a/src/Order/Query/GetOrderForApprovalReversedQueryResult.php +++ b/src/Order/Query/GetOrderForApprovalReversedQueryResult.php @@ -22,7 +22,6 @@ use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderException; use PrestaShop\Module\PrestashopCheckout\Order\State\Exception\OrderStateException; -use PrestaShop\Module\PrestashopCheckout\Order\State\ValueObject\OrderStateId; use PrestaShop\Module\PrestashopCheckout\Order\ValueObject\OrderId; class GetOrderForApprovalReversedQueryResult @@ -32,11 +31,6 @@ class GetOrderForApprovalReversedQueryResult */ private $orderId; - /** - * @var OrderStateId - */ - private $currentStateId; - /** * @var bool */ @@ -49,7 +43,6 @@ class GetOrderForApprovalReversedQueryResult /** * @param int $orderId - * @param int $currentStateId * @param bool $hasBeenPaid * @param bool $hasBeenCanceled * @@ -58,12 +51,10 @@ class GetOrderForApprovalReversedQueryResult */ public function __construct( $orderId, - $currentStateId, $hasBeenPaid, $hasBeenCanceled ) { $this->orderId = new OrderId($orderId); - $this->currentStateId = new OrderStateId($currentStateId); $this->hasBeenPaid = $hasBeenPaid; $this->hasBeenCanceled = $hasBeenCanceled; } @@ -76,14 +67,6 @@ public function getOrderId() return $this->orderId; } - /** - * @return OrderStateId - */ - public function getCurrentStateId() - { - return $this->currentStateId; - } - /** * @return bool */ diff --git a/src/Order/Query/GetOrderForPaymentCompletedQueryResult.php b/src/Order/Query/GetOrderForPaymentCompletedQueryResult.php index 166a7889a..ff49f9bef 100644 --- a/src/Order/Query/GetOrderForPaymentCompletedQueryResult.php +++ b/src/Order/Query/GetOrderForPaymentCompletedQueryResult.php @@ -23,8 +23,6 @@ use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartException; use PrestaShop\Module\PrestashopCheckout\Cart\ValueObject\CartId; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderException; -use PrestaShop\Module\PrestashopCheckout\Order\State\Exception\OrderStateException; -use PrestaShop\Module\PrestashopCheckout\Order\State\ValueObject\OrderStateId; use PrestaShop\Module\PrestashopCheckout\Order\ValueObject\OrderId; class GetOrderForPaymentCompletedQueryResult @@ -39,11 +37,6 @@ class GetOrderForPaymentCompletedQueryResult */ private $cartId; - /** - * @var OrderStateId - */ - private $currentStateId; - /** * @var bool */ @@ -54,11 +47,6 @@ class GetOrderForPaymentCompletedQueryResult */ private $totalAmount; - /** - * @var string - */ - private $totalAmountPaid; - /** * @var int */ @@ -77,35 +65,28 @@ class GetOrderForPaymentCompletedQueryResult /** * @param int $orderId * @param int $cartId - * @param int $currentStateId * @param bool $hasBeenPaid * @param string $totalAmount - * @param string $totalAmountPaid * @param int $currencyId * @param string $paymentMethod * @param int|null $orderPaymentId * * @throws OrderException * @throws CartException - * @throws OrderStateException */ public function __construct( $orderId, $cartId, - $currentStateId, $hasBeenPaid, $totalAmount, - $totalAmountPaid, $currencyId, $paymentMethod, $orderPaymentId = null ) { $this->orderId = new OrderId($orderId); $this->cartId = new CartId($cartId); - $this->currentStateId = new OrderStateId($currentStateId); $this->hasBeenPaid = $hasBeenPaid; $this->totalAmount = $totalAmount; - $this->totalAmountPaid = $totalAmountPaid; $this->currencyId = $currencyId; $this->paymentMethod = $paymentMethod; $this->orderPaymentId = $orderPaymentId; @@ -127,14 +108,6 @@ public function getCartId() return $this->cartId; } - /** - * @return OrderStateId - */ - public function getCurrentStateId() - { - return $this->currentStateId; - } - /** * @return bool */ @@ -151,14 +124,6 @@ public function getTotalAmount() return $this->totalAmount; } - /** - * @return string - */ - public function getTotalAmountPaid() - { - return $this->totalAmountPaid; - } - /** * @return int */ diff --git a/src/Order/Query/GetOrderForPaymentDeniedQueryResult.php b/src/Order/Query/GetOrderForPaymentDeniedQueryResult.php index 76a343cc3..4f9e2b3ee 100644 --- a/src/Order/Query/GetOrderForPaymentDeniedQueryResult.php +++ b/src/Order/Query/GetOrderForPaymentDeniedQueryResult.php @@ -21,8 +21,6 @@ namespace PrestaShop\Module\PrestashopCheckout\Order\Query; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderException; -use PrestaShop\Module\PrestashopCheckout\Order\State\Exception\OrderStateException; -use PrestaShop\Module\PrestashopCheckout\Order\State\ValueObject\OrderStateId; use PrestaShop\Module\PrestashopCheckout\Order\ValueObject\OrderId; class GetOrderForPaymentDeniedQueryResult @@ -32,11 +30,6 @@ class GetOrderForPaymentDeniedQueryResult */ private $orderId; - /** - * @var OrderStateId - */ - private $currentStateId; - /** * @var bool */ @@ -44,19 +37,15 @@ class GetOrderForPaymentDeniedQueryResult /** * @param int $orderId - * @param int $currentState * @param bool $hasBeenError * * @throws OrderException - * @throws OrderStateException */ public function __construct( $orderId, - $currentState, $hasBeenError ) { $this->orderId = new OrderId($orderId); - $this->currentStateId = new OrderStateId($currentState); $this->hasBeenError = $hasBeenError; } @@ -68,14 +57,6 @@ public function getOrderId() return $this->orderId; } - /** - * @return OrderStateId - */ - public function getCurrentStateId() - { - return $this->currentStateId; - } - /** * @return bool */ diff --git a/src/Order/Query/GetOrderForPaymentPendingQueryResult.php b/src/Order/Query/GetOrderForPaymentPendingQueryResult.php index f50c016a9..174e40856 100644 --- a/src/Order/Query/GetOrderForPaymentPendingQueryResult.php +++ b/src/Order/Query/GetOrderForPaymentPendingQueryResult.php @@ -22,8 +22,6 @@ namespace PrestaShop\Module\PrestashopCheckout\Order\Query; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderException; -use PrestaShop\Module\PrestashopCheckout\Order\State\Exception\OrderStateException; -use PrestaShop\Module\PrestashopCheckout\Order\State\ValueObject\OrderStateId; use PrestaShop\Module\PrestashopCheckout\Order\ValueObject\OrderId; class GetOrderForPaymentPendingQueryResult @@ -33,40 +31,23 @@ class GetOrderForPaymentPendingQueryResult */ private $orderId; - /** - * @var OrderStateId - */ - private $currentStateId; - /** * @var bool */ private $isInPending; - /** - * @var string - */ - private $paymentMethod; - /** * @param int $orderId - * @param int $currentStateId * @param bool $isInPending - * @param string $paymentMethod * * @throws OrderException - * @throws OrderStateException */ public function __construct( $orderId, - $currentStateId, - $isInPending, - $paymentMethod + $isInPending ) { $this->orderId = new OrderId($orderId); - $this->currentStateId = new OrderStateId($currentStateId); $this->isInPending = $isInPending; - $this->paymentMethod = $paymentMethod; } /** @@ -77,14 +58,6 @@ public function getOrderId() return $this->orderId; } - /** - * @return OrderStateId - */ - public function getCurrentStateId() - { - return $this->currentStateId; - } - /** * @return bool */ @@ -92,12 +65,4 @@ public function isInPending() { return $this->isInPending; } - - /** - * @return string - */ - public function getPaymentMethod() - { - return $this->paymentMethod; - } } diff --git a/src/Order/Query/GetOrderForPaymentRefundedQueryResult.php b/src/Order/Query/GetOrderForPaymentRefundedQueryResult.php index b3cff68f1..faac91cda 100644 --- a/src/Order/Query/GetOrderForPaymentRefundedQueryResult.php +++ b/src/Order/Query/GetOrderForPaymentRefundedQueryResult.php @@ -45,17 +45,17 @@ class GetOrderForPaymentRefundedQueryResult /** * @var bool */ - private $hasBeenTotallyRefund; + private $hasBeenPartiallyRefund; /** - * @var string + * @var bool */ - private $totalAmount; + private $hasBeenTotallyRefund; /** * @var string */ - private $totalRefund; + private $totalAmount; /** * @var int @@ -66,9 +66,9 @@ class GetOrderForPaymentRefundedQueryResult * @param int $orderId * @param int $currentStateId * @param bool $hasBeenPaid + * @param bool $hasBeenPartiallyRefund * @param bool $hasBeenTotallyRefund * @param string $totalAmount - * @param string $totalRefund * @param int $currencyId * * @throws OrderException @@ -78,17 +78,17 @@ public function __construct( $orderId, $currentStateId, $hasBeenPaid, + $hasBeenPartiallyRefund, $hasBeenTotallyRefund, $totalAmount, - $totalRefund, $currencyId ) { $this->orderId = new OrderId($orderId); $this->currentStateId = new OrderStateId($currentStateId); $this->hasBeenPaid = $hasBeenPaid; + $this->hasBeenPartiallyRefund = $hasBeenPartiallyRefund; $this->hasBeenTotallyRefund = $hasBeenTotallyRefund; $this->totalAmount = $totalAmount; - $this->totalRefund = $totalRefund; $this->currencyId = $currencyId; } @@ -116,6 +116,11 @@ public function hasBeenPaid() return $this->hasBeenPaid; } + public function hasBeenPartiallyRefund() + { + return $this->hasBeenPartiallyRefund; + } + /** * @return bool */ @@ -132,14 +137,6 @@ public function getTotalAmount() return $this->totalAmount; } - /** - * @return string - */ - public function getTotalRefund() - { - return $this->totalRefund; - } - /** * @return int */ diff --git a/src/Order/Query/GetOrderForPaymentReversedQuery.php b/src/Order/Query/GetOrderForPaymentReversedQuery.php index 2e52e4374..bdb6d4488 100644 --- a/src/Order/Query/GetOrderForPaymentReversedQuery.php +++ b/src/Order/Query/GetOrderForPaymentReversedQuery.php @@ -22,8 +22,6 @@ use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Exception\PayPalOrderException; use PrestaShop\Module\PrestashopCheckout\PayPal\Order\ValueObject\PayPalOrderId; -use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\Exception\PayPalCaptureException; -use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\ValueObject\PayPalCaptureId; class GetOrderForPaymentReversedQuery { @@ -32,22 +30,14 @@ class GetOrderForPaymentReversedQuery */ private $orderPayPalId; - /** - * @var PayPalCaptureId - */ - private $capturePayPalId; - /** * @param string $orderPayPalId - * @param string $capturePayPalId * * @throws PayPalOrderException - * @throws PayPalCaptureException */ - public function __construct($orderPayPalId, $capturePayPalId) + public function __construct($orderPayPalId) { $this->orderPayPalId = new PayPalOrderId($orderPayPalId); - $this->capturePayPalId = new PayPalCaptureId($capturePayPalId); } /** @@ -57,9 +47,4 @@ public function getOrderPayPalId() { return $this->orderPayPalId; } - - public function getCapturePayPalId() - { - return $this->capturePayPalId; - } } diff --git a/src/Order/Query/GetOrderForPaymentReversedQueryResult.php b/src/Order/Query/GetOrderForPaymentReversedQueryResult.php index 874edfdf1..ffdba476d 100644 --- a/src/Order/Query/GetOrderForPaymentReversedQueryResult.php +++ b/src/Order/Query/GetOrderForPaymentReversedQueryResult.php @@ -21,8 +21,6 @@ namespace PrestaShop\Module\PrestashopCheckout\Order\Query; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderException; -use PrestaShop\Module\PrestashopCheckout\Order\State\Exception\OrderStateException; -use PrestaShop\Module\PrestashopCheckout\Order\State\ValueObject\OrderStateId; use PrestaShop\Module\PrestashopCheckout\Order\ValueObject\OrderId; class GetOrderForPaymentReversedQueryResult @@ -32,11 +30,6 @@ class GetOrderForPaymentReversedQueryResult */ private $orderId; - /** - * @var OrderStateId - */ - private $currentStateId; - /** * @var bool */ @@ -49,21 +42,17 @@ class GetOrderForPaymentReversedQueryResult /** * @param int $orderId - * @param int $currentStateId * @param bool $hasBeenPaid * @param bool $hasBeenTotallyRefund * * @throws OrderException - * @throws OrderStateException */ public function __construct( $orderId, - $currentStateId, $hasBeenPaid, $hasBeenTotallyRefund ) { $this->orderId = new OrderId($orderId); - $this->currentStateId = new OrderStateId($currentStateId); $this->hasBeenPaid = $hasBeenPaid; $this->hasBeenTotallyRefund = $hasBeenTotallyRefund; } @@ -76,14 +65,6 @@ public function getOrderId() return $this->orderId; } - /** - * @return OrderStateId - */ - public function getCurrentStateId() - { - return $this->currentStateId; - } - /** * @return bool */ diff --git a/src/Order/QueryHandler/GetOrderForApprovalReversedQueryHandler.php b/src/Order/QueryHandler/GetOrderForApprovalReversedQueryHandler.php index 461fdd10c..b188f5673 100644 --- a/src/Order/QueryHandler/GetOrderForApprovalReversedQueryHandler.php +++ b/src/Order/QueryHandler/GetOrderForApprovalReversedQueryHandler.php @@ -81,11 +81,13 @@ public function handle(GetOrderForApprovalReversedQuery $query) } $hasBeenCanceled = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_CANCELED))); + $hasBeenPaid = $order->hasBeenPaid(); + $hasBeenCompleted = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED))); + $hasBeenPartiallyPaid = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_PAID))); return new GetOrderForApprovalReversedQueryResult( (int) $order->id, - (int) $order->getCurrentState(), - (bool) $order->hasBeenPaid(), + $hasBeenPaid || $hasBeenCompleted || $hasBeenPartiallyPaid, (bool) $hasBeenCanceled ); } diff --git a/src/Order/QueryHandler/GetOrderForPaymentCompletedQueryHandler.php b/src/Order/QueryHandler/GetOrderForPaymentCompletedQueryHandler.php index 5cb2def84..b94423127 100644 --- a/src/Order/QueryHandler/GetOrderForPaymentCompletedQueryHandler.php +++ b/src/Order/QueryHandler/GetOrderForPaymentCompletedQueryHandler.php @@ -21,6 +21,7 @@ namespace PrestaShop\Module\PrestashopCheckout\Order\QueryHandler; +use Configuration; use Order; use OrderPayment; use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException; @@ -28,6 +29,7 @@ use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderNotFoundException; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentCompletedQuery; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentCompletedQueryResult; +use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys; use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository; use PrestaShopCollection; use PrestaShopDatabaseException; @@ -89,13 +91,15 @@ public function handle(GetOrderForPaymentCompletedQuery $query) } } + $hasBeenPaid = $order->hasBeenPaid(); + $hasBeenCompleted = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED))); + $hasBeenPartiallyPaid = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_PAID))); + return new GetOrderForPaymentCompletedQueryResult( (int) $order->id, (int) $order->id_cart, - (int) $order->getCurrentState(), - (bool) $order->hasBeenPaid(), + $hasBeenPaid || $hasBeenCompleted || $hasBeenPartiallyPaid, (string) $order->total_paid, - (string) $order->total_paid_real, (int) $order->id_currency, $psCheckoutCart->getPaypalFundingSource(), $orderPaymentId diff --git a/src/Order/QueryHandler/GetOrderForPaymentDeniedQueryHandler.php b/src/Order/QueryHandler/GetOrderForPaymentDeniedQueryHandler.php index 8e8046718..8b8347e07 100644 --- a/src/Order/QueryHandler/GetOrderForPaymentDeniedQueryHandler.php +++ b/src/Order/QueryHandler/GetOrderForPaymentDeniedQueryHandler.php @@ -82,7 +82,6 @@ public function handle(GetOrderForPaymentDeniedQuery $query) return new GetOrderForPaymentDeniedQueryResult( (int) $order->id, - (int) $order->getCurrentState(), $this->hasBeenError($order) ); } diff --git a/src/Order/QueryHandler/GetOrderForPaymentPendingQueryHandler.php b/src/Order/QueryHandler/GetOrderForPaymentPendingQueryHandler.php index 5f87d43ef..a978cebe2 100644 --- a/src/Order/QueryHandler/GetOrderForPaymentPendingQueryHandler.php +++ b/src/Order/QueryHandler/GetOrderForPaymentPendingQueryHandler.php @@ -82,9 +82,7 @@ public function handle(GetOrderForPaymentPendingQuery $query) return new GetOrderForPaymentPendingQueryResult( (int) $order->id, - (int) $order->getCurrentState(), - $this->isInPending($order), - $psCheckoutCart->getPaypalFundingSource() + $this->isInPending($order) ); } diff --git a/src/Order/QueryHandler/GetOrderForPaymentRefundedQueryHandler.php b/src/Order/QueryHandler/GetOrderForPaymentRefundedQueryHandler.php index 3ba5b28b5..7a967eae3 100644 --- a/src/Order/QueryHandler/GetOrderForPaymentRefundedQueryHandler.php +++ b/src/Order/QueryHandler/GetOrderForPaymentRefundedQueryHandler.php @@ -21,13 +21,14 @@ namespace PrestaShop\Module\PrestashopCheckout\Order\QueryHandler; +use Configuration; use Order; -use OrderSlip; use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException; use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderNotFoundException; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentRefundedQuery; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentRefundedQueryResult; +use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys; use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository; use PrestaShopCollection; use PrestaShopDatabaseException; @@ -79,34 +80,18 @@ public function handle(GetOrderForPaymentRefundedQuery $query) throw new OrderNotFoundException('No PrestaShop Order associated to this PayPal Order at this time.'); } - $totalRefund = $this->getTotalRefund($order); + $hasBeenPaid = $order->hasBeenPaid(); + $hasBeenCompleted = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED))); + $hasBeenPartiallyPaid = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_PAID))); return new GetOrderForPaymentRefundedQueryResult( (int) $order->id, (int) $order->getCurrentState(), - (bool) $order->hasBeenPaid(), - $this->hasBeenTotallyRefunded($totalRefund, $order), + $hasBeenPaid || $hasBeenCompleted || $hasBeenPartiallyPaid, + (bool) count($order->getHistory((int) $order->id_lang, (int) Configuration::get('PS_CHECKOUT_STATE_PARTIALLY_REFUNDED'))), + (bool) count($order->getHistory((int) $order->id_lang, (int) Configuration::get('PS_CHECKOUT_STATE_REFUNDED'))), (string) $order->getTotalPaid(), - (string) $totalRefund, (int) $order->id_currency ); } - - private function hasBeenTotallyRefunded($refundAmount, $order) - { - return $refundAmount >= $order->total_paid; - } - - private function getTotalRefund(Order $order) - { - /** @var OrderSlip[] $orderSlips */ - $orderSlips = $order->getOrderSlipsCollection()->getResults(); - $refundAmount = 0; - - foreach ($orderSlips as $orderSlip) { - $refundAmount += $orderSlip->amount + $orderSlip->shipping_cost_amount; - } - - return $refundAmount; - } } diff --git a/src/Order/QueryHandler/GetOrderForPaymentReversedQueryHandler.php b/src/Order/QueryHandler/GetOrderForPaymentReversedQueryHandler.php index 9c6dc432d..06676d5b7 100644 --- a/src/Order/QueryHandler/GetOrderForPaymentReversedQueryHandler.php +++ b/src/Order/QueryHandler/GetOrderForPaymentReversedQueryHandler.php @@ -21,13 +21,14 @@ namespace PrestaShop\Module\PrestashopCheckout\Order\QueryHandler; +use Configuration; use Order; -use OrderSlip; use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException; use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderNotFoundException; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentReversedQuery; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentReversedQueryResult; +use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys; use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository; use PrestaShopCollection; use PrestaShopDatabaseException; @@ -79,26 +80,15 @@ public function handle(GetOrderForPaymentReversedQuery $query) throw new OrderNotFoundException('No PrestaShop Order associated to this PayPal Order at this time.'); } + $hasBeenPaid = $order->hasBeenPaid(); + $hasBeenCompleted = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_COMPLETED))); + $hasBeenPartiallyPaid = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_PAID))); + $hasBeenTotallyRefunded = count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_REFUNDED))); + return new GetOrderForPaymentReversedQueryResult( (int) $order->id, - (int) $order->getCurrentState(), - (bool) $order->hasBeenPaid(), - $this->hasBeenTotallyRefunded($order) + $hasBeenPaid || $hasBeenCompleted || $hasBeenPartiallyPaid, + (bool) $hasBeenTotallyRefunded ); } - - private function hasBeenTotallyRefunded(Order $order) - { - /** @var OrderSlip[] $orderSlips */ - $orderSlips = $order->getOrderSlipsCollection()->getResults(); - $refundAmount = 0; - - if (!empty($orderSlips)) { - foreach ($orderSlips as $orderSlip) { - $refundAmount += $orderSlip->amount + $orderSlip->shipping_cost_amount; - } - } - - return $refundAmount >= $order->total_paid; - } } diff --git a/src/PayPal/Order/CommandHandler/UpdatePayPalOrderCommandHandler.php b/src/PayPal/Order/CommandHandler/UpdatePayPalOrderCommandHandler.php index e59ad0d5c..37e9e86b6 100644 --- a/src/PayPal/Order/CommandHandler/UpdatePayPalOrderCommandHandler.php +++ b/src/PayPal/Order/CommandHandler/UpdatePayPalOrderCommandHandler.php @@ -89,12 +89,16 @@ public function handle(UpdatePayPalOrderCommand $command) $builder->buildMinimalPayload(); } - $response = $this->httpClient->updateOrder($builder->presentPayload()->getArray()); - $order = json_decode($response->getBody(), true); + $payload = $builder->presentPayload()->getArray(); + $response = $this->httpClient->updateOrder($payload); + + if ($response->getStatusCode() !== 204) { + throw new PayPalOrderException('Failed to update PayPal Order', PayPalOrderException::PAYPAL_ORDER_UPDATE_FAILED); + } $this->eventDispatcher->dispatch(new PayPalOrderUpdatedEvent( - $order['id'], - $order, + $command->getPayPalOrderId()->getValue(), + $payload, $command->getCartId()->getValue(), $command->isHostedFields(), $command->isExpressCheckout(), diff --git a/src/PayPal/Order/Exception/PayPalOrderException.php b/src/PayPal/Order/Exception/PayPalOrderException.php index b4de3faf4..949e62cb1 100644 --- a/src/PayPal/Order/Exception/PayPalOrderException.php +++ b/src/PayPal/Order/Exception/PayPalOrderException.php @@ -27,7 +27,5 @@ class PayPalOrderException extends PsCheckoutException const INVALID_ID = 1; const CANNOT_RETRIEVE_ORDER = 2; const EMPTY_ORDER_DATA = 3; - const CANNOT_CAPTURE_ORDER = 4; - const SESSION_EXCEPTION = 5; - const CACHE_EXCEPTION = 6; + const PAYPAL_ORDER_UPDATE_FAILED = 4; } diff --git a/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php b/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php index eefa235de..87e0b5756 100644 --- a/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php +++ b/src/PayPal/Payment/Capture/EventSubscriber/PayPalCaptureEventSubscriber.php @@ -213,7 +213,7 @@ public function setPaymentReversedOrderStatus(PayPalCaptureReversedEvent $event) { try { /** @var GetOrderForPaymentReversedQueryResult $order */ - $order = $this->commandBus->handle(new GetOrderForPaymentReversedQuery($event->getPayPalOrderId()->getValue(), $event->getPayPalCaptureId()->getValue())); + $order = $this->commandBus->handle(new GetOrderForPaymentReversedQuery($event->getPayPalOrderId()->getValue())); } catch (OrderNotFoundException $exception) { return; } diff --git a/src/PayPal/Payment/Refund/CommandHandler/RefundPayPalCaptureCommandHandler.php b/src/PayPal/Payment/Refund/CommandHandler/RefundPayPalCaptureCommandHandler.php index 82a7c9ee6..f04a9ad9a 100644 --- a/src/PayPal/Payment/Refund/CommandHandler/RefundPayPalCaptureCommandHandler.php +++ b/src/PayPal/Payment/Refund/CommandHandler/RefundPayPalCaptureCommandHandler.php @@ -94,7 +94,7 @@ public function handle(RefundPayPalCaptureCommand $command) ), ]); - $refund = json_decode($response->getBody()->getContents(), true); + $refund = json_decode($response->getBody(), true); $this->eventDispatcher->dispatch( new PayPalCaptureRefundedEvent( $refund['id'], diff --git a/src/PayPal/Payment/Refund/EventSubscriber/PayPalRefundEventSubscriber.php b/src/PayPal/Payment/Refund/EventSubscriber/PayPalRefundEventSubscriber.php index 19699c7d7..cfe2df3f2 100644 --- a/src/PayPal/Payment/Refund/EventSubscriber/PayPalRefundEventSubscriber.php +++ b/src/PayPal/Payment/Refund/EventSubscriber/PayPalRefundEventSubscriber.php @@ -25,7 +25,6 @@ use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderNotFoundException; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentRefundedQuery; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentRefundedQueryResult; -use PrestaShop\Module\PrestashopCheckout\Order\Service\CheckOrderAmount; use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys; use PrestaShop\Module\PrestashopCheckout\Order\State\Service\OrderStateMapper; use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Event\PayPalCaptureRefundedEvent; @@ -42,21 +41,11 @@ class PayPalRefundEventSubscriber implements EventSubscriberInterface */ private $module; - /** - * @var CheckOrderAmount - */ - private $checkOrderAmount; - /** * @var CommandBusInterface */ private $commandBus; - /** - * @var CacheInterface - */ - private $capturePayPalCache; - /** * @var CacheInterface */ @@ -73,16 +62,12 @@ class PayPalRefundEventSubscriber implements EventSubscriberInterface public function __construct( Ps_checkout $module, - CheckOrderAmount $checkOrderAmount, - CacheInterface $capturePayPalCache, CacheInterface $orderPayPalCache, OrderStateMapper $orderStateMapper, PayPalOrderProvider $orderProvider ) { $this->module = $module; - $this->checkOrderAmount = $checkOrderAmount; $this->commandBus = $this->module->getService('ps_checkout.bus.command'); - $this->capturePayPalCache = $capturePayPalCache; $this->orderPayPalCache = $orderPayPalCache; $this->orderStateMapper = $orderStateMapper; $this->orderProvider = $orderProvider; @@ -129,11 +114,22 @@ public function setPaymentRefundedOrderStatus(PayPalCaptureRefundedEvent $event) }); $orderFullyRefunded = (float) $order->getTotalAmount() <= (float) $totalRefunded; + $orderStateRefunded = $this->orderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_REFUNDED); + $orderStatePartiallyRefunded = $this->orderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_REFUNDED); + $newOrderState = $orderFullyRefunded ? $orderStateRefunded : $orderStatePartiallyRefunded; + + if ($order->hasBeenPartiallyRefund() && $newOrderState === $orderStatePartiallyRefunded) { + return; + } + + if ($order->getCurrentStateId()->getValue() === $newOrderState) { + return; + } $this->commandBus->handle( new UpdateOrderStatusCommand( $order->getOrderId()->getValue(), - $this->orderStateMapper->getIdByKey($orderFullyRefunded ? OrderStateConfigurationKeys::PS_CHECKOUT_STATE_REFUNDED : OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_REFUNDED) + $newOrderState ) ); } diff --git a/upgrade/upgrade-7.3.3.1.php b/upgrade/upgrade-7.3.3.1.php index 12d0a0aa1..c48b05a76 100644 --- a/upgrade/upgrade-7.3.3.1.php +++ b/upgrade/upgrade-7.3.3.1.php @@ -38,7 +38,6 @@ function upgrade_module_7_3_3_1($module) $module->registerHook('displayPaymentReturn'); $module->registerHook('displayOrderDetail'); - $module->registerHook('displayHeader'); try { $db = Db::getInstance();