Skip to content

Commit

Permalink
Merge pull request #133 from synolia/fix/refund-plugin-1.4.0
Browse files Browse the repository at this point in the history
SYL-215 - Fix for refund plugin 1.4.0
  • Loading branch information
TheGrimmChester authored Sep 7, 2023
2 parents 85b332e + aa7bfa7 commit a80a8f1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 77 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ jobs:
strategy:
matrix:
php:
- 7.4
- 8.0
- 8.1
symfony:
- '4.4.*'
- '5.4.*'
- '6.1.*'
env:
Expand Down
24 changes: 0 additions & 24 deletions .github/workflows/sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ jobs:
fail-fast: false
matrix:
php:
- 7.4
- 8.0
- 8.1
sylius:
- 1.9.0
- 1.10.0
- 1.11.0
- 1.12.0
symfony:
Expand All @@ -32,27 +29,9 @@ jobs:
node:
- 14.x
exclude:
-
sylius: 1.9.0
php: 8.0
-
sylius: 1.9.0
php: 8.1
-
sylius: 1.11.0
php: 7.4
-
sylius: 1.9.0
symfony: 6.1
-
sylius: 1.10.0
symfony: 6.1
-
sylius: 1.11.0
symfony: 6.1
-
sylius: 1.12.0
php: 7.4
-
php: '8.0'
symfony: 6.1
Expand All @@ -65,9 +44,6 @@ jobs:
-
sylius: 1.11.0
symfony: 6.1
-
sylius: 1.12.0
php: 7.4
-
php: '8.0'
symfony: 6.1
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"description": "PayPlug payment plugin for Sylius applications.",
"license": "MIT",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"ext-json": "*",
"giggsey/libphonenumber-for-php": "^8.12",
"payplug/payplug-php": "^3.1",
"php-http/message-factory": "^1.1",
"sylius/refund-plugin": ">=1.0.0",
"sylius/refund-plugin": "^1.4",
"sylius/sylius": "^1.9.0 || ^1.10.0 || ^1.11.0",
"symfony/asset": "^4.4|^5.0|^6.0",
"symfony/lock": "^4.4|^5.0|^6.0",
Expand Down
16 changes: 7 additions & 9 deletions src/Action/Admin/RefundUnitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use Psr\Log\LoggerInterface;
use Sylius\RefundPlugin\Creator\RefundUnitsCommandCreatorInterface;
use Sylius\RefundPlugin\Creator\RequestCommandCreatorInterface;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -24,26 +25,23 @@ final class RefundUnitsAction
/** @var UrlGeneratorInterface */
private $router;

/** @var RefundUnitsCommandCreatorInterface */
private $commandCreator;

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

private RequestStack $requestStack;

public function __construct(
MessageBusInterface $commandBus,
RequestStack $requestStack,
private RequestStack $requestStack,
UrlGeneratorInterface $router,
RefundUnitsCommandCreatorInterface $commandCreator,
private RequestCommandCreatorInterface | RefundUnitsCommandCreatorInterface $commandCreator,
LoggerInterface $logger
) {
$this->commandBus = $commandBus;
$this->requestStack = $requestStack;
$this->router = $router;
$this->commandCreator = $commandCreator;
$this->logger = $logger;

if ($this->commandCreator instanceof RefundUnitsCommandCreatorInterface) {
trigger_deprecation('sylius/refund-plugin', '1.4', sprintf('Passing an instance of %s as constructor argument for %s is deprecated as of Sylius Refund Plugin 1.4 and will be removed in 2.0. Pass an instance of %s instead.', RefundUnitsCommandCreatorInterface::class, self::class, RequestCommandCreatorInterface::class));
}
}

public function __invoke(Request $request): Response
Expand Down
81 changes: 41 additions & 40 deletions src/Creator/RefundUnitsCommandCreatorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Payment\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Creator\RefundUnitsCommandCreatorInterface;
use Sylius\RefundPlugin\Converter\RefundUnitsConverterInterface;
use Sylius\RefundPlugin\Converter\Request\RequestToRefundUnitsConverterInterface;
use Sylius\RefundPlugin\Creator\RequestCommandCreatorInterface;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;

class RefundUnitsCommandCreatorDecorator implements RefundUnitsCommandCreatorInterface
class RefundUnitsCommandCreatorDecorator implements RequestCommandCreatorInterface
{
private const MINIMUM_REFUND_AMOUNT = 0.10;
private const MINIMUM_REFUND_AMOUNT = 10;

/** @var RefundUnitsCommandCreatorInterface */
/** @var RequestCommandCreatorInterface */
private $decorated;

/** @var PaymentMethodRepositoryInterface */
Expand All @@ -40,7 +45,8 @@ class RefundUnitsCommandCreatorDecorator implements RefundUnitsCommandCreatorInt
private $oneyClient;

public function __construct(
RefundUnitsCommandCreatorInterface $decorated,
RequestCommandCreatorInterface $decorated,
private RequestToRefundUnitsConverterInterface | RefundUnitsConverterInterface $requestToRefundUnitsConverter,
PaymentMethodRepositoryInterface $paymentMethodRepository,
OrderRepositoryInterface $orderRepository,
TranslatorInterface $translator,
Expand All @@ -57,15 +63,30 @@ public function fromRequest(Request $request): RefundUnits
{
Assert::true($request->attributes->has('orderNumber'), 'Refunded order number not provided');

$units = $this->filterEmptyRefundUnits(
$request->request->has('sylius_refund_units') ? $request->request->all()['sylius_refund_units'] : []
);
$shipments = $this->filterEmptyRefundUnits(
$request->request->has('sylius_refund_shipments') ? $request->request->all()['sylius_refund_shipments'] : []
);
if ($this->requestToRefundUnitsConverter instanceof RefundUnitsConverterInterface) {
/** @phpstan-ignore-next-line */
$units = $this->requestToRefundUnitsConverter->convert(
$request->request->has('sylius_refund_units') ? $request->request->all()['sylius_refund_units'] : [],
/* @phpstan-ignore-next-line */
RefundType::orderItemUnit(),
OrderItemUnitRefund::class,
);

/** @phpstan-ignore-next-line */
$shipments = $this->requestToRefundUnitsConverter->convert(
$request->request->has('sylius_refund_shipments') ? $request->request->all()['sylius_refund_shipments'] : [],
/* @phpstan-ignore-next-line */
RefundType::shipment(),
ShipmentRefund::class,
);

$units = array_merge($units, $shipments);
} else {
$units = $this->requestToRefundUnitsConverter->convert($request);
}

if (0 === count($units) && 0 === count($shipments)) {
throw InvalidRefundAmount::withValidationConstraint($this->translator->trans('sylius_refund.at_least_one_unit_should_be_selected_to_refund'));
if (0 === count($units)) {
throw InvalidRefundAmount::withValidationConstraint('sylius_refund.at_least_one_unit_should_be_selected_to_refund');
}

/** @var int $paymentMethodId */
Expand All @@ -79,7 +100,7 @@ public function fromRequest(Request $request): RefundUnits

if (PayPlugGatewayFactory::FACTORY_NAME !== $gateway->getFactoryName() &&
OneyGatewayFactory::FACTORY_NAME !== $gateway->getFactoryName()) {
return $this->decorated->fromRequest($request);
return $this->decorated->fromRequest($request); /** @phpstan-ignore-line */
}

if (OneyGatewayFactory::FACTORY_NAME === $gateway->getFactoryName()) {
Expand All @@ -90,49 +111,29 @@ public function fromRequest(Request $request): RefundUnits
$this->canOneyRefundBeMade($order);
}

$totalRefundRequest = $this->getTotalRefundAmount($units, $shipments);
$totalRefundRequest = $this->getTotalRefundAmount($units);

if ($totalRefundRequest < self::MINIMUM_REFUND_AMOUNT) {
throw InvalidRefundAmount::withValidationConstraint($this->translator->trans('payplug_sylius_payplug_plugin.ui.refund_minimum_amount_requirement_not_met'));
}

return $this->decorated->fromRequest($request);
return $this->decorated->fromRequest($request); /** @phpstan-ignore-line */
}

private function getTotalRefundAmount(array $units, array $shipments): float
private function getTotalRefundAmount(array $units): int
{
$total = 0;

foreach ($units as $unit) {
$total += $this->getAmount($unit) ?? 0;
}

foreach ($shipments as $unit) {
$total += $this->getAmount($unit) ?? 0;
$total += $this->getAmount($unit);
}

return $total;
}

private function filterEmptyRefundUnits(array $units): array
private function getAmount(OrderItemUnitRefund $unit): int
{
return array_filter($units, function (array $refundUnit): bool {
return
(isset($refundUnit['amount']) && '' !== $refundUnit['amount'])
|| isset($refundUnit['full'])
;
});
}

private function getAmount(array $unit): ?float
{
if (isset($unit['full'])) {
return null;
}

Assert::keyExists($unit, 'amount');

return (float) $unit['amount'];
return $unit->total();
}

private function canOneyRefundBeMade(OrderInterface $order): void
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
public="false"
>
<argument type="service" id="Sylius\RefundPlugin\Creator\RefundUnitsCommandCreator.inner"/>
<argument type="service" id="Sylius\RefundPlugin\Converter\Request\RequestToRefundUnitsConverterInterface"/>
</service>

<service
Expand Down

0 comments on commit a80a8f1

Please sign in to comment.