Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hookActionOrderSlipAdd #1206

Draft
wants to merge 4 commits into
base: prestashop/8.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ services:
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"
PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Command\RefundPayPalCaptureCommand: 'PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\CommandHandler\RefundPayPalCaptureCommandHandler'
PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Command\RefundPayPalCaptureCommand: "ps_checkout.command.handler.paypal.payment.refund.refund_paypal_capture"
PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Query\GetPayPalRefundQuery: "ps_checkout.query.handler.paypal.payment.refund.get_paypal_refund"
PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Query\GetPayPalRefundForPDFOrderSlipQuery: "ps_checkout.query.handler.paypal.payment.refund.get_paypal_refund_order_slip"

ps_checkout.event.dispatcher.factory:
class: 'PrestaShop\Module\PrestashopCheckout\Event\SymfonyEventDispatcherFactory'
Expand Down Expand Up @@ -627,6 +629,7 @@ services:
public: true
arguments:
- "@ps_checkout.repository.pscheckoutcart"
- "@ps_checkout.order.state.service.order_state_mapper"

ps_checkout.query.handler.order.get_order_for_payment_reversed:
class: 'PrestaShop\Module\PrestashopCheckout\Order\QueryHandler\GetOrderForPaymentReversedQueryHandler'
Expand Down Expand Up @@ -683,7 +686,7 @@ services:
arguments:
- "@ps_checkout.repository.pscheckoutcart"

PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\CommandHandler\RefundPayPalCaptureCommandHandler:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change back the dependency name from full class name to ps_checkout.command.handler.paypal.payment.refund.refund_paypal_capture ?
Going forward we should create services/dependencies with full class name, as it's more convenient to call getService(Service::class) than having to remember what the service is named in config

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a problem with my dependencies, as it was the only one wrote with this syntax i thought that it may be the problem (it was obviously not the problem). Forgot to change it back.
As it's more convenient for the getService calls, I will add the new Query in this new style, as well as going back on this change

ps_checkout.command.handler.paypal.payment.refund.refund_paypal_capture:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\CommandHandler\RefundPayPalCaptureCommandHandler'
public: true
arguments:
Expand Down Expand Up @@ -735,3 +738,15 @@ services:
public: true
arguments:
- "@ps_checkout.http.client"

ps_checkout.query.handler.paypal.payment.refund.get_paypal_refund:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\QueryHandler\GetPayPalRefundQueryHandler'
public: true
arguments:
- "@ps_checkout.paypal.provider.order"

ps_checkout.query.handler.paypal.payment.refund.get_paypal_refund_order_slip:
class: 'PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\QueryHandler\GetPayPalRefundForPDFOrderSlipQueryHandler'
public: true
arguments:
- "@ps_checkout.paypal.provider.order"
80 changes: 80 additions & 0 deletions ps_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Ps_checkout extends PaymentModule
'actionObjectOrderPaymentUpdateAfter',
'displayPaymentReturn',
'displayOrderDetail',
'actionOrderSlipAdd',
'displayPDFOrderSlip',
];

/**
Expand Down Expand Up @@ -1671,4 +1673,82 @@ public function hookDisplayOrderDetail(array $params)

return $this->display(__FILE__, 'views/templates/hook/displayOrderDetail.tpl');
}

/**
* Refund based on OrderSlip
*
* @param array{cookie: Cookie, cart: Cart, altern: int, order: Order} $params
*
* @return void
*/
public function hookActionOrderSlipAdd(array $params)
{
try {
/** @var Order $order */
$order = $params['order'];
if (!Validate::isLoadedObject($order)) {
throw new \PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException('Unable to get the Order from params');
}

/** @var \PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface $commandBus */
$commandBus = $this->getService('ps_checkout.bus.command');

/** @var \PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Query\GetPayPalRefundQueryResult $getPayPalRefundQueryResult */
$getPayPalRefundQueryResult = $commandBus->handle(new \PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Query\GetPayPalRefundQuery(
$order
));

$commandBus->handle(new \PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Command\RefundPayPalCaptureCommand(
$getPayPalRefundQueryResult->getPayPalOrderId(),
$getPayPalRefundQueryResult->getPayPalCaptureId(),
$getPayPalRefundQueryResult->getCurrencyIsoCode(),
sprintf('%01.2F', $getPayPalRefundQueryResult->getAmount())
));
} catch (Exception $exception) {
// Do not break the Admin process if an exception is thrown
$this->getLogger()->error(__FUNCTION__, [
'exception' => $exception,
]);
}
}

/**
* Add content to the PDF OrderSlip.
*
* @param array{cookie: Cookie, cart: Cart, altern: int, object: OrderSlip} $params
*
* @return string
*/
public function hookDisplayPDFOrderSlip(array $params)
{
try {
/** @var \PrestaShop\Module\PrestashopCheckout\CommandBus\CommandBusInterface $commandBus */
$commandBus = $this->getService('ps_checkout.bus.command');

/** @var \PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Query\GetPayPalRefundForPDFOrderSlipQueryResult $getPayPalRefundForPDFOrderSlipQueryResult */
$getPayPalRefundForPDFOrderSlipQueryResult = $commandBus->handle(new \PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Query\GetPayPalRefundForPDFOrderSlipQuery(
$params['object']
));

$this->context->smarty->assign([
'refund_id' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundId(),
'refund_amount' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundAmount(),
'refund_currency' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundCurrency(),
'refund_currency_id' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundCurrencyId(),
'refund_status' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundStatus(),
'refund_note_to_payer' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundNote(),
'refund_create_time' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundCreateTime(),
'refund_update_time' => $getPayPalRefundForPDFOrderSlipQueryResult->getPaypalRefundUpdateTime(),
]);

return $this->display(__FILE__, 'views/templates/hook/displayPDFOrderSlip.tpl');
} catch (Exception $exception) {
// Do not break the Admin process if an exception is thrown
$this->getLogger()->error(__FUNCTION__, [
'exception' => $exception,
]);

return '';
}
}
}
18 changes: 17 additions & 1 deletion src/Order/Query/GetOrderForPaymentRefundedQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class GetOrderForPaymentRefundedQueryResult
*/
private $currencyId;

/**
* @var int[]
*/
private $orderStateIdHistory;

/**
* @param int $orderId
* @param int $currentStateId
Expand All @@ -70,6 +75,7 @@ class GetOrderForPaymentRefundedQueryResult
* @param string $totalAmount
* @param string $totalRefund
* @param int $currencyId
* @param int[] $orderStateIdHistory
*
* @throws OrderException
* @throws OrderStateException
Expand All @@ -81,7 +87,8 @@ public function __construct(
$hasBeenTotallyRefund,
$totalAmount,
$totalRefund,
$currencyId
$currencyId,
array $orderStateIdHistory = []
) {
$this->orderId = new OrderId($orderId);
$this->currentStateId = new OrderStateId($currentStateId);
Expand All @@ -90,6 +97,7 @@ public function __construct(
$this->totalAmount = $totalAmount;
$this->totalRefund = $totalRefund;
$this->currencyId = $currencyId;
$this->orderStateIdHistory = $orderStateIdHistory;
}

/**
Expand Down Expand Up @@ -147,4 +155,12 @@ public function getCurrencyId()
{
return $this->currencyId;
}

/**
* @return int[]
*/
public function getOrderStateIdHistory()
{
return $this->orderStateIdHistory;
}
}
49 changes: 47 additions & 2 deletions src/Order/QueryHandler/GetOrderForPaymentRefundedQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
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\Exception\OrderStateException;
use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys;
use PrestaShop\Module\PrestashopCheckout\Order\State\Service\OrderStateMapper;
use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository;
use PrestaShopCollection;
use PrestaShopDatabaseException;
Expand All @@ -42,9 +45,15 @@ class GetOrderForPaymentRefundedQueryHandler
*/
private $psCheckoutCartRepository;

public function __construct(PsCheckoutCartRepository $psCheckoutCartRepository)
/**
* @var OrderStateMapper
*/
private $orderStateMapper;

public function __construct(PsCheckoutCartRepository $psCheckoutCartRepository, OrderStateMapper $orderStateMapper)
{
$this->psCheckoutCartRepository = $psCheckoutCartRepository;
$this->orderStateMapper = $orderStateMapper;
}

/**
Expand Down Expand Up @@ -88,7 +97,8 @@ public function handle(GetOrderForPaymentRefundedQuery $query)
$this->hasBeenTotallyRefunded($totalRefund, $order),
(string) $order->getTotalPaid(),
(string) $totalRefund,
(int) $order->id_currency
(int) $order->id_currency,
$this->getOrderStateHistory($order)
);
}

Expand All @@ -109,4 +119,39 @@ private function getTotalRefund(Order $order)

return $refundAmount;
}

/**
* @param Order $order
*
* @return int[]
*/
private function getOrderStateHistory(Order $order)
{
$orderHistory = $order->getHistory($order->id_lang);

if (!$orderHistory) {
return [];
}

try {
$orderStateRefundedId = $this->orderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_REFUNDED);
$orderStatePartiallyRefundedId = $this->orderStateMapper->getIdByKey(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_REFUNDED);
} catch (OrderStateException $exception) {
return [];
}

$orderStateIdHistory = [];

foreach ($orderHistory as $historyItem) {
$orderStateId = (int) $historyItem['id_order_state'];

if ($orderStateId !== $orderStateRefundedId && $orderStateId !== $orderStatePartiallyRefundedId) {
continue;
}

$orderStateIdHistory[] = $orderStateId;
}

return $orderStateIdHistory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ public function setPaymentRefundedOrderStatus(PayPalCaptureRefundedEvent $event)
$this->orderPayPalCache->delete($event->getPayPalOrderId()->getValue());
}

if (!$order->hasBeenPaid() || $order->hasBeenTotallyRefund()) {
return;
}

$orderPayPal = $this->orderProvider->getById($event->getPayPalOrderId()->getValue());

if (empty($orderPayPal['purchase_units'][0]['payments']['refunds'])) {
Expand All @@ -129,11 +125,16 @@ public function setPaymentRefundedOrderStatus(PayPalCaptureRefundedEvent $event)
});

$orderFullyRefunded = (float) $order->getTotalAmount() <= (float) $totalRefunded;
$newOrderStateId = $this->orderStateMapper->getIdByKey($orderFullyRefunded ? OrderStateConfigurationKeys::PS_CHECKOUT_STATE_REFUNDED : OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_REFUNDED);

if (in_array($newOrderStateId, $order->getOrderStateIdHistory())) {
return;
}

$this->commandBus->handle(
new UpdateOrderStatusCommand(
$order->getOrderId()->getValue(),
$this->orderStateMapper->getIdByKey($orderFullyRefunded ? OrderStateConfigurationKeys::PS_CHECKOUT_STATE_REFUNDED : OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PARTIALLY_REFUNDED)
$newOrderStateId
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Refund\Query;

use OrderSlip;

class GetPayPalRefundForPDFOrderSlipQuery
{
/** @var OrderSlip */
private $orderSlip;

/**
* @param OrderSlip $orderSlip
*/
public function __construct($orderSlip)
{
$this->orderSlip = $orderSlip;
}

/**
* @return OrderSlip
*/
public function getOrderSlip()
{
return $this->orderSlip;
}
}
Loading
Loading