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 1 commit
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
18 changes: 16 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 @@ -684,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 @@ -736,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"
183 changes: 25 additions & 158 deletions ps_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -1686,115 +1686,23 @@ public function hookActionOrderSlipAdd(array $params)
try {
/** @var Order $order */
$order = $params['order'];

if (!Validate::isLoadedObject($order)) {
return;
}

/** @var OrderSlip[]|bool $orderSlipCollection */
$orderSlipCollection = $order->getOrderSlipsCollection()->getResults();

if (!$orderSlipCollection) {
return;
}

/** @var OrderSlip $orderSlip */
$orderSlip = end($orderSlipCollection);

if (!Validate::isLoadedObject($orderSlip)) {
return;
}

$customer = new Customer((int) $order->id_customer);
$useTax = Group::getPriceDisplayMethod((int) $customer->id_default_group);

if ($useTax) {
$amount = $orderSlip->total_products_tax_excl;
} else {
$amount = $orderSlip->total_products_tax_incl;
}

if ($orderSlip->shipping_cost) {
if ($useTax) {
$amount += $orderSlip->total_shipping_tax_excl;
} else {
$amount += $orderSlip->total_shipping_tax_incl;
}
}

$cartRuleTotal = 0;

// Refund based on product prices, but do not refund the voucher amount
if ($orderSlip->order_slip_type == 1 && is_array($cartRules = $order->getCartRules())) {
foreach ($cartRules as $cartRule) {
if ($useTax) {
$cartRuleTotal -= $cartRule['value_tax_excl'];
} else {
$cartRuleTotal -= $cartRule['value'];
}
}
}

$amount += $cartRuleTotal;

if ($amount <= 0) {
return;
}

$psCheckoutCartCollection = new PrestaShopCollection('PsCheckoutCart');
$psCheckoutCartCollection->where('id_cart', '=', (int) $order->id_cart);
$psCheckoutCartCollection->where('paypal_status', 'in', [PsCheckoutCart::STATUS_COMPLETED, PsCheckoutCart::STATUS_PARTIALLY_COMPLETED]);
$psCheckoutCartCollection->orderBy('date_upd', 'ASC');

if (!$psCheckoutCartCollection->count()) {
return;
}

/** @var PsCheckoutCart|bool $psCheckoutCart */
$psCheckoutCart = $psCheckoutCartCollection->getFirst();

if (!$psCheckoutCart) {
return;
}

/** @var \PrestaShop\Module\PrestashopCheckout\PayPal\PayPalOrderProvider $paypalOrderProvider */
$paypalOrderProvider = $this->getService('ps_checkout.paypal.provider.order');

try {
$paypalOrder = $paypalOrderProvider->getById($psCheckoutCart->paypal_order);
} catch (Exception $exception) {
return;
}

if (!isset($paypalOrder['purchase_units'][0]['payments']['captures'][0])) {
return;
}

$capture = $paypalOrder['purchase_units'][0]['payments']['captures'][0];

$totalCaptured = (float) $capture['amount']['value'];

$totalAlreadyRefund = 0;

if (isset($paypalOrder['purchase_units'][0]['payments']['refunds'])) {
$totalAlreadyRefund = array_reduce($paypalOrder['purchase_units'][0]['payments']['refunds'], function ($totalRefunded, $refund) {
return $totalRefunded + (float) $refund['amount']['value'];
});
throw new \PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException('Unable to get the Order from params');
}

if ($totalCaptured < $amount + $totalAlreadyRefund) {
throw new \PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException(sprintf('Refund amount %s is greater than captured amount %s', $totalCaptured, $amount));
}

$currency = new Currency($params['order']->id_currency);

/** @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(
$psCheckoutCart->getPaypalOrderId(),
$capture['id'],
$currency->iso_code,
sprintf('%01.2F', $amount)
$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
Expand All @@ -1814,64 +1722,23 @@ public function hookActionOrderSlipAdd(array $params)
public function hookDisplayPDFOrderSlip(array $params)
{
try {
/** @var OrderSlip $orderSlip */
$orderSlip = $params['object'];
$order = new Order($orderSlip->id_order);

if ($order->module !== $this->name) {
return '';
}

$psCheckoutCartCollection = new PrestaShopCollection('PsCheckoutCart');
$psCheckoutCartCollection->where('id_cart', '=', (int) $order->id_cart);
$psCheckoutCartCollection->where('paypal_status', 'in', [PsCheckoutCart::STATUS_COMPLETED, PsCheckoutCart::STATUS_PARTIALLY_COMPLETED]);
$psCheckoutCartCollection->orderBy('date_upd', 'ASC');

if (!$psCheckoutCartCollection->count()) {
return '';
}

/** @var PsCheckoutCart|bool $psCheckoutCart */
$psCheckoutCart = $psCheckoutCartCollection->getFirst();

if (!$psCheckoutCart) {
return '';
}

/** @var \PrestaShop\Module\PrestashopCheckout\PayPal\PayPalOrderProvider $paypalOrderProvider */
$paypalOrderProvider = $this->getService('ps_checkout.paypal.provider.order');

try {
$paypalOrder = $paypalOrderProvider->getById($psCheckoutCart->paypal_order);
} catch (Exception $exception) {
return '';
}

if (!isset($paypalOrder['purchase_units'][0]['payments']['refunds'][0])) {
return '';
}

foreach ($paypalOrder['purchase_units'][0]['payments']['refunds'] as $refund) {
if (number_format($refund['amount']['value'], 2) !== number_format($orderSlip->amount, 2)) {
continue;
}

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

if (!isset($paypalRefund)) {
return '';
}
/** @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' => $paypalRefund['id'],
'refund_amount' => $paypalRefund['amount']['value'],
'refund_currency' => $paypalRefund['amount']['currency_code'],
'refund_currency_id' => Currency::getIdByIsoCode($paypalRefund['amount']['currency_code'], $order->id_shop),
'refund_status' => $paypalRefund['status'],
'refund_note_to_payer' => isset($paypalRefund['note_to_payer']) ? $paypalRefund['note_to_payer'] : '',
'refund_create_time' => isset($paypalRefund['create_time']) ? $paypalRefund['create_time'] : '',
'refund_update_time' => isset($paypalRefund['update_time']) ? $paypalRefund['update_time'] : '',
'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');
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