diff --git a/README.md b/README.md index 366bc2d5..79111461 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,23 @@ as shown below: Online refund logic should be implemented if you need it. As the first try for the possible customization, you can check out `Sylius\RefundPlugin\Event\UnitsRefunded` event. +## Post-refunding process + +After units are refunded, there are multiple other processes that should be triggered. By default, after units refund, there should be **CreditMemo** and +**RefundPayment** generated. As they're strictly coupled with each other, **RefundPayment** is always created after the **CreditMemo**. Moreover, if **RefundPayment** +fails, related **CreditMemo** should not be created as well. + +`\Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessManager` service facilitates the whole process. If you want to add one or more steps to it, you should create +a service implementing `\Sylius\RefundPlugin\ProcessManager\UnitsRefundedProcessStepInterface`, and register if with proper tag: + +```xml + + + +``` + +Tagged services would be executed according to their priority (descending). + ## Security issues If you think that you have found a security issue, please do not use the issue tracker and do not post it publicly. diff --git a/spec/ProcessManager/UnitsRefundedProcessManagerSpec.php b/spec/ProcessManager/UnitsRefundedProcessManagerSpec.php index 33d7e10c..8efc4ae3 100644 --- a/spec/ProcessManager/UnitsRefundedProcessManagerSpec.php +++ b/spec/ProcessManager/UnitsRefundedProcessManagerSpec.php @@ -17,7 +17,7 @@ function let( UnitsRefundedProcessStepInterface $creditMemoProcessManager, UnitsRefundedProcessStepInterface $refundPaymentProcessManager ): void { - $this->beConstructedWith($creditMemoProcessManager, $refundPaymentProcessManager); + $this->beConstructedWith([$creditMemoProcessManager, $refundPaymentProcessManager]); } function it_implements_units_refunded_process_manager_interface(): void diff --git a/src/ProcessManager/UnitsRefundedProcessManager.php b/src/ProcessManager/UnitsRefundedProcessManager.php index e6b4d95b..bdfe016b 100644 --- a/src/ProcessManager/UnitsRefundedProcessManager.php +++ b/src/ProcessManager/UnitsRefundedProcessManager.php @@ -8,23 +8,19 @@ final class UnitsRefundedProcessManager implements UnitsRefundedProcessManagerInterface { - /** @var UnitsRefundedProcessStepInterface */ - private $creditMemoProcessManager; + /** @var iterable|UnitsRefundedProcessStepInterface[] */ + private $steps; - /** @var UnitsRefundedProcessStepInterface */ - private $refundPaymentProcessManager; - - public function __construct( - UnitsRefundedProcessStepInterface $creditMemoProcessManager, - UnitsRefundedProcessStepInterface $refundPaymentProcessManager - ) { - $this->creditMemoProcessManager = $creditMemoProcessManager; - $this->refundPaymentProcessManager = $refundPaymentProcessManager; + public function __construct(iterable $steps) + { + $this->steps = $steps; } public function __invoke(UnitsRefunded $event): void { - $this->creditMemoProcessManager->next($event); - $this->refundPaymentProcessManager->next($event); + /** @var UnitsRefundedProcessStepInterface $step */ + foreach ($this->steps as $step) { + $step->next($event); + } } } diff --git a/src/Resources/config/services/event_bus.xml b/src/Resources/config/services/event_bus.xml index f20e06ff..2eff99da 100644 --- a/src/Resources/config/services/event_bus.xml +++ b/src/Resources/config/services/event_bus.xml @@ -9,15 +9,6 @@ - - - - - - @@ -28,8 +19,12 @@ - - + + + @@ -38,6 +33,12 @@ + + + + + + diff --git a/tests/Behat/Services/Factory/FailedRefundPaymentFactory.php b/tests/Behat/Services/Factory/FailedRefundPaymentFactory.php index 1c0b0530..d618fd1a 100644 --- a/tests/Behat/Services/Factory/FailedRefundPaymentFactory.php +++ b/tests/Behat/Services/Factory/FailedRefundPaymentFactory.php @@ -9,6 +9,8 @@ final class FailedRefundPaymentFactory implements RefundPaymentFactoryInterface { + private const FAILED_FILE = __DIR__.'/refund-payment-failed.json'; + /** @var RefundPaymentFactoryInterface */ private $baseRefundPaymentFactory; @@ -24,8 +26,8 @@ public function createWithData( string $state, int $paymentMethodId ): RefundPaymentInterface { - if (file_exists(__DIR__.'/refund-payment-failed.json')) { - unlink(__DIR__.'/refund-payment-failed.json'); + if (file_exists(self::FAILED_FILE)) { + unlink(self::FAILED_FILE); throw new \Exception('Refund payment creation failed'); } @@ -35,6 +37,6 @@ public function createWithData( public function failRefundPaymentCreation(): void { - touch(__DIR__.'/refund-payment-failed.json'); + touch(self::FAILED_FILE); } } diff --git a/tests/Behat/Services/Generator/FailedCreditMemoGenerator.php b/tests/Behat/Services/Generator/FailedCreditMemoGenerator.php index e130ae97..eeecc037 100644 --- a/tests/Behat/Services/Generator/FailedCreditMemoGenerator.php +++ b/tests/Behat/Services/Generator/FailedCreditMemoGenerator.php @@ -10,6 +10,8 @@ final class FailedCreditMemoGenerator implements CreditMemoGeneratorInterface { + private const FAILED_FILE = __DIR__.'/credit-memo-failed.json'; + /** @var CreditMemoGeneratorInterface */ private $baseCreditMemoGenerator; @@ -25,8 +27,8 @@ public function generate( array $shipments, string $comment ): CreditMemoInterface { - if (file_exists(__DIR__.'/credit-memo-failed.json')) { - unlink(__DIR__.'/credit-memo-failed.json'); + if (file_exists(self::FAILED_FILE)) { + unlink(self::FAILED_FILE); throw new \Exception('Credit memo generation failed'); } @@ -36,6 +38,6 @@ public function generate( public function failCreditMemoGeneration(): void { - touch(__DIR__.'/credit-memo-failed.json'); + touch(self::FAILED_FILE); } }