Skip to content

Commit

Permalink
Merge pull request #76 from BitBagCommerce/AD-79/Order_confirmation_e…
Browse files Browse the repository at this point in the history
…mails

Adyen plugin update (Order confirmation e-mails)
  • Loading branch information
PiotrSzymanski2000 authored Nov 30, 2022
2 parents 67cec2e + 248b026 commit 66b0b20
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
30 changes: 28 additions & 2 deletions src/Bus/Handler/PaymentFinalizationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;

final class PaymentFinalizationHandler implements MessageHandlerInterface
{
Expand All @@ -29,12 +30,16 @@ final class PaymentFinalizationHandler implements MessageHandlerInterface
/** @var RepositoryInterface */
private $orderRepository;

private MessageBusInterface $commandBus;

public function __construct(
FactoryInterface $stateMachineFactory,
RepositoryInterface $orderRepository
RepositoryInterface $orderRepository,
MessageBusInterface $commandBus
) {
$this->stateMachineFactory = $stateMachineFactory;
$this->orderRepository = $orderRepository;
$this->commandBus = $commandBus;
}

private function updatePaymentState(PaymentInterface $payment, string $transition): void
Expand All @@ -60,8 +65,29 @@ public function __invoke(PaymentFinalizationCommand $command): void
if (!$this->isAccepted($payment)) {
return;
}

$order = $payment->getOrder();
$this->updatePaymentState($payment, $command->getPaymentTransition());
if (null !== $order) {
$token = $order->getTokenValue();

// This is necessary because in Sylius 1.11 namespace of SendOrderConfirmation has been changed
if (null !== $token) {
/**
* @psalm-suppress MixedArgument
* @psalm-suppress UndefinedClass
*/
if (class_exists('\Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation')) {
$this->commandBus->dispatch(new \Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation($token));
} elseif (class_exists('\Sylius\Bundle\ApiBundle\Command\Checkout\SendOrderConfirmation')) {
/**
* @psalm-suppress MixedArgument
* @psalm-suppress UndefinedClass
*/
$this->commandBus->dispatch(new \Sylius\Bundle\ApiBundle\Command\Checkout\SendOrderConfirmation($token));
}
}
}

$this->updatePayment($payment);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services/bus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
>
<argument type="service" id="sm.factory"/>
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="sylius.command_bus"/>

<tag name="bitbag.sylius_adyen_plugin.command_bus" bus="sylius.command_bus" />
</service>
Expand Down Expand Up @@ -148,4 +149,4 @@
</service>

</services>
</container>
</container>
9 changes: 8 additions & 1 deletion tests/Unit/Bus/Handler/PaymentFinalizationHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\Payment;
use Sylius\Component\Core\OrderPaymentStates;
use Symfony\Component\Messenger\MessageBusInterface;

class PaymentFinalizationHandlerTest extends TestCase
{
Expand All @@ -30,15 +31,21 @@ class PaymentFinalizationHandlerTest extends TestCase
/** @var mixed|\PHPUnit\Framework\MockObject\MockObject|EntityRepository */
private $orderRepository;

/** @var mixed|\Symfony\Component\Messenger\MessageBusInterface */
private $commandBus;

protected function setUp(): void
{
$this->setupStateMachineMocks();

$this->orderRepository = $this->createMock(EntityRepository::class);

$this->commandBus = $this->createMock(MessageBusInterface::class);

$this->handler = new PaymentFinalizationHandler(
$this->stateMachineFactory,
$this->orderRepository
$this->orderRepository,
$this->commandBus,
);
}

Expand Down

0 comments on commit 66b0b20

Please sign in to comment.