Skip to content

Commit

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

Moved sending emails to order transition instead of on authorized payment
  • Loading branch information
pbalcerzak authored Dec 20, 2022
2 parents 66b0b20 + a4f374e commit 6ef3294
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.
30 changes: 2 additions & 28 deletions src/Bus/Handler/PaymentFinalizationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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 @@ -30,16 +29,12 @@ final class PaymentFinalizationHandler implements MessageHandlerInterface
/** @var RepositoryInterface */
private $orderRepository;

private MessageBusInterface $commandBus;

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

private function updatePaymentState(PaymentInterface $payment, string $transition): void
Expand All @@ -65,29 +60,8 @@ 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->updatePaymentState($payment, $command->getPaymentTransition());
$this->updatePayment($payment);
}

Expand Down
35 changes: 31 additions & 4 deletions src/Bus/Handler/PaymentStatusReceivedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;

final class PaymentStatusReceivedHandler implements MessageHandlerInterface
{
Expand All @@ -40,16 +41,21 @@ final class PaymentStatusReceivedHandler implements MessageHandlerInterface
/** @var RepositoryInterface */
private $orderRepository;

/** @var MessageBusInterface */
private $commandBus;

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

public function __invoke(PaymentStatusReceived $command): void
Expand Down Expand Up @@ -84,9 +90,30 @@ private function processCode(string $resultCode, PaymentStatusReceived $command)
private function updateOrderState(OrderInterface $order): void
{
$sm = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);
$sm->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE, true);

$this->orderRepository->add($order);
if ($sm->can(OrderCheckoutTransitions::TRANSITION_COMPLETE)) {
$sm->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE, true);

$this->orderRepository->add($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));
}
}
}
}

private function getResultCode(PaymentInterface $payment): string
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/bus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
>
<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 All @@ -29,6 +28,7 @@
<argument type="service" id="sylius.repository.payment"/>
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="bitbag.sylius_adyen_plugin.bus.dispatcher"/>
<argument type="service" id="sylius.command_bus"/>

<tag name="bitbag.sylius_adyen_plugin.command_bus" bus="sylius.command_bus" />
</service>
Expand Down
8 changes: 1 addition & 7 deletions tests/Unit/Bus/Handler/PaymentFinalizationHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,15 @@ 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->commandBus,
$this->orderRepository
);
}

Expand Down
10 changes: 8 additions & 2 deletions tests/Unit/Bus/Handler/PaymentStatusReceivedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\Payment;
use Symfony\Component\Messenger\MessageBusInterface;

class PaymentStatusReceivedHandlerTest extends TestCase
{
Expand All @@ -36,18 +37,23 @@ class PaymentStatusReceivedHandlerTest extends TestCase
/** @var \PHPUnit\Framework\MockObject\MockObject|EntityRepository */
private $orderRepository;

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

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

$this->paymentRepository = $this->createMock(EntityRepository::class);
$this->orderRepository = $this->createMock(EntityRepository::class);
$this->dispatcher = $this->createMock(DispatcherInterface::class);
$this->commandBus = $this->createMock(MessageBusInterface::class);
$this->handler = new PaymentStatusReceivedHandler(
$this->stateMachineFactory,
$this->paymentRepository,
$this->orderRepository,
$this->dispatcher
$this->dispatcher,
$this->commandBus,
);
}

Expand Down Expand Up @@ -85,7 +91,7 @@ public function testFlow(string $resultCode, bool $shouldPass): void
$invocation = $shouldPass ? $this->once() : $this->never();
$this->stateMachine
->expects($invocation)
->method('apply')
->method('can')
;

$this->dispatcher
Expand Down

0 comments on commit 6ef3294

Please sign in to comment.