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

[Refund] Do not expose exception messages to the customer #167

Merged
merged 2 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 25 additions & 3 deletions src/Action/Admin/RefundUnitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\RefundPlugin\Action\Admin;

use Psr\Log\LoggerInterface;
use Sylius\RefundPlugin\Creator\RefundUnitsCommandCreatorInterface;
use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -27,16 +29,21 @@ final class RefundUnitsAction
/** @var RefundUnitsCommandCreatorInterface */
private $commandCreator;

/** @var LoggerInterface */
private $logger;

public function __construct(
MessageBusInterface $commandBus,
Session $session,
UrlGeneratorInterface $router,
RefundUnitsCommandCreatorInterface $commandCreator
RefundUnitsCommandCreatorInterface $commandCreator,
LoggerInterface $logger
) {
$this->commandBus = $commandBus;
$this->session = $session;
$this->router = $router;
$this->commandCreator = $commandCreator;
$this->logger = $logger;
}

public function __invoke(Request $request): Response
Expand All @@ -45,17 +52,32 @@ public function __invoke(Request $request): Response
$this->commandBus->dispatch($this->commandCreator->fromRequest($request));

$this->session->getFlashBag()->add('success', 'sylius_refund.units_successfully_refunded');
} catch (\InvalidArgumentException $exception) {
} catch (InvalidRefundAmountException $exception) {
$this->session->getFlashBag()->add('error', $exception->getMessage());

$this->logger->error($exception->getMessage());
} catch (HandlerFailedException $exception) {
/** @var \Exception $previousException */
$previousException = $exception->getPrevious();

$this->session->getFlashBag()->add('error', $previousException->getMessage());
$this->provideErrorMessage($previousException);

$this->logger->error($previousException->getMessage());
}

return new RedirectResponse($this->router->generate(
'sylius_refund_order_refunds_list', ['orderNumber' => $request->attributes->get('orderNumber')]
));
}

private function provideErrorMessage(\Exception $previousException): void
{
if ($previousException instanceof InvalidRefundAmountException) {
$this->session->getFlashBag()->add('error', $previousException->getMessage());

return;
}

$this->session->getFlashBag()->add('error', 'sylius_refund.error_occurred');
}
}
8 changes: 4 additions & 4 deletions src/Creator/RefundUnitsCommandCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Sylius\RefundPlugin\Calculator\UnitRefundTotalCalculatorInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\ShipmentRefund;
Expand All @@ -30,10 +31,9 @@ public function fromRequest(Request $request): RefundUnits
$units = $this->filterEmptyRefundUnits($request->request->get('sylius_refund_units', []));
$shipments = $this->filterEmptyRefundUnits($request->request->get('sylius_refund_shipments', []));

Assert::false(
count($units) === 0 && count($shipments) === 0,
'sylius_refund.at_least_one_unit_should_be_selected_to_refund'
);
if (count($units) === 0 && count($shipments) === 0) {
throw InvalidRefundAmountException::withValidationConstraint('sylius_refund.at_least_one_unit_should_be_selected_to_refund');
}

return new RefundUnits(
$request->attributes->get('orderNumber'),
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<argument type="service" id="session" />
<argument type="service" id="router" />
<argument type="service" id="Sylius\RefundPlugin\Creator\RefundUnitsCommandCreator" />
<argument type="service" id="monolog.logger" />
</service>

<service id="Sylius\RefundPlugin\Action\CompleteRefundPaymentAction">
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/flashes.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ sylius_refund:
resend_credit_memo_success: 'Selected credit memo has been successfully resent'
unit_refund_exceeded: 'You cannot refund more money than the order unit total'
units_successfully_refunded: 'Selected order units have been successfully refunded'
error_occurred: 'Unexpected error occurred'