Skip to content

Commit

Permalink
Fix order state change after refund
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt75 committed Aug 7, 2024
1 parent 8fc6169 commit 9ddb3da
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
2 changes: 0 additions & 2 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 13 additions & 0 deletions src/Order/Query/GetOrderForPaymentRefundedQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class GetOrderForPaymentRefundedQueryResult
*/
private $hasBeenPaid;

/**
* @var bool
*/
private $hasBeenPartiallyRefund;

/**
* @var bool
*/
Expand All @@ -66,6 +71,7 @@ class GetOrderForPaymentRefundedQueryResult
* @param int $orderId
* @param int $currentStateId
* @param bool $hasBeenPaid
* @param bool $hasBeenPartiallyRefund
* @param bool $hasBeenTotallyRefund
* @param string $totalAmount
* @param string $totalRefund
Expand All @@ -78,6 +84,7 @@ public function __construct(
$orderId,
$currentStateId,
$hasBeenPaid,
$hasBeenPartiallyRefund,
$hasBeenTotallyRefund,
$totalAmount,
$totalRefund,
Expand All @@ -86,6 +93,7 @@ public function __construct(
$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;
Expand Down Expand Up @@ -116,6 +124,11 @@ public function hasBeenPaid()
return $this->hasBeenPaid;
}

public function hasBeenPartiallyRefund()
{
return $this->hasBeenPartiallyRefund;
}

/**
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,14 @@ public function handle(GetOrderForPaymentRefundedQuery $query)
(int) $order->id,
(int) $order->getCurrentState(),
(bool) $order->hasBeenPaid(),
$this->hasBeenTotallyRefunded($totalRefund, $order),
(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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,21 +41,11 @@ class PayPalRefundEventSubscriber implements EventSubscriberInterface
*/
private $module;

/**
* @var CheckOrderAmount
*/
private $checkOrderAmount;

/**
* @var CommandBusInterface
*/
private $commandBus;

/**
* @var CacheInterface
*/
private $capturePayPalCache;

/**
* @var CacheInterface
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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
)
);
}
Expand Down

0 comments on commit 9ddb3da

Please sign in to comment.