From 85b332e74d4bf71498296deccc91c1d4474ebf11 Mon Sep 17 00:00:00 2001 From: Nicolas MELONI Date: Tue, 29 Aug 2023 14:36:20 +0200 Subject: [PATCH 1/3] remove refund plugin restriction --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 64c8b091..2f1dfb60 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "giggsey/libphonenumber-for-php": "^8.12", "payplug/payplug-php": "^3.1", "php-http/message-factory": "^1.1", - "sylius/refund-plugin": ">=1.0.0 <1.4", + "sylius/refund-plugin": ">=1.0.0", "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", From aa7bfa7e063ebed43e30df2a46c75d7d8b4f429e Mon Sep 17 00:00:00 2001 From: Nicolas MELONI Date: Tue, 29 Aug 2023 15:52:56 +0200 Subject: [PATCH 2/3] fix for refund plugin 1.4.0 --- .github/workflows/analysis.yaml | 2 - .github/workflows/sylius.yaml | 24 ------ composer.json | 4 +- src/Action/Admin/RefundUnitsAction.php | 16 ++-- .../RefundUnitsCommandCreatorDecorator.php | 81 ++++++++++--------- src/Resources/config/services.xml | 1 + 6 files changed, 51 insertions(+), 77 deletions(-) diff --git a/.github/workflows/analysis.yaml b/.github/workflows/analysis.yaml index 8ea66356..f872ebb6 100644 --- a/.github/workflows/analysis.yaml +++ b/.github/workflows/analysis.yaml @@ -17,11 +17,9 @@ jobs: strategy: matrix: php: - - 7.4 - 8.0 - 8.1 symfony: - - '4.4.*' - '5.4.*' - '6.1.*' env: diff --git a/.github/workflows/sylius.yaml b/.github/workflows/sylius.yaml index a94d47e2..ec633e4a 100644 --- a/.github/workflows/sylius.yaml +++ b/.github/workflows/sylius.yaml @@ -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: @@ -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 @@ -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 diff --git a/composer.json b/composer.json index 2f1dfb60..35d8dd72 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Action/Admin/RefundUnitsAction.php b/src/Action/Admin/RefundUnitsAction.php index 83767d56..75986900 100644 --- a/src/Action/Admin/RefundUnitsAction.php +++ b/src/Action/Admin/RefundUnitsAction.php @@ -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; @@ -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 diff --git a/src/Creator/RefundUnitsCommandCreatorDecorator.php b/src/Creator/RefundUnitsCommandCreatorDecorator.php index 849f4b1b..00a0578e 100644 --- a/src/Creator/RefundUnitsCommandCreatorDecorator.php +++ b/src/Creator/RefundUnitsCommandCreatorDecorator.php @@ -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 */ @@ -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, @@ -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 */ @@ -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()) { @@ -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 diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 851e15e4..975ace3f 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -246,6 +246,7 @@ public="false" > + Date: Thu, 7 Sep 2023 15:45:23 +0200 Subject: [PATCH 3/3] drop support for sylius < 1.11 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 35d8dd72..e7e66358 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "payplug/payplug-php": "^3.1", "php-http/message-factory": "^1.1", "sylius/refund-plugin": "^1.4", - "sylius/sylius": "^1.9.0 || ^1.10.0 || ^1.11.0", + "sylius/sylius": "^1.11.0", "symfony/asset": "^4.4|^5.0|^6.0", "symfony/lock": "^4.4|^5.0|^6.0", "symfony/validator": "^4.4|^5.0|^6.0"