Skip to content

Commit

Permalink
NTR: PISHPS-393: send delivery on cancel item (#916)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Dec 16, 2024
1 parent 517bc02 commit d3fcc72
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 58 deletions.
7 changes: 6 additions & 1 deletion src/Components/CancelManager/CancelItemFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
namespace Kiener\MolliePayments\Components\CancelManager;

use Kiener\MolliePayments\Components\RefundManager\Integrators\StockManagerInterface;
use Kiener\MolliePayments\Event\OrderLinesUpdatedEvent;
use Kiener\MolliePayments\Factory\MollieApiFactory;
use Kiener\MolliePayments\Repository\OrderLineItem\OrderLineItemRepositoryInterface;
use Mollie\Api\MollieApiClient;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @final
Expand All @@ -23,13 +25,15 @@ class CancelItemFacade
private LoggerInterface $logger;
private OrderLineItemRepositoryInterface $orderLineItemRepository;
private StockManagerInterface $stockManager;
private EventDispatcherInterface $eventDispatcher;

public function __construct(MollieApiFactory $clientFactory, OrderLineItemRepositoryInterface $orderLineItemRepository, StockManagerInterface $stockManager, LoggerInterface $logger)
public function __construct(MollieApiFactory $clientFactory, OrderLineItemRepositoryInterface $orderLineItemRepository, StockManagerInterface $stockManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
{
$this->client = $clientFactory->getClient();
$this->logger = $logger;
$this->orderLineItemRepository = $orderLineItemRepository;
$this->stockManager = $stockManager;
$this->eventDispatcher = $eventDispatcher;
}

public function cancelItem(string $mollieOrderId, string $mollieLineId, string $shopwareLineId, int $quantity, bool $resetStock, Context $context): CancelItemResponse
Expand Down Expand Up @@ -86,6 +90,7 @@ public function cancelItem(string $mollieOrderId, string $mollieLineId, string $
$mollieOrder->cancelLines(['lines' => [$lines]]);
$this->logger->info('Item cancelled successful', ['orderId' => $mollieOrderId, 'mollieLineId' => $mollieLineId, 'quantity' => $quantity]);

$this->eventDispatcher->dispatch(new OrderLinesUpdatedEvent($mollieOrder));

$response = $response->withData($lines);
} catch (\Throwable $e) {
Expand Down
21 changes: 21 additions & 0 deletions src/Event/OrderLinesUpdatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Event;

use Mollie\Api\Resources\Order;

class OrderLinesUpdatedEvent
{
private Order $mollieOrder;

public function __construct(Order $mollieOrder)
{
$this->mollieOrder = $mollieOrder;
}

public function getMollieOrder(): Order
{
return $this->mollieOrder;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<service id="Kiener\MolliePayments\Service\MollieApi\Shipment">
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\Order"/>
<argument type="service" id="event_dispatcher"/>
</service>

<service id="Kiener\MolliePayments\Service\MollieApi\Payment">
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<argument type="service" id="Kiener\MolliePayments\Factory\MollieApiFactory"/>
<argument type="service" id="Kiener\MolliePayments\Repository\OrderLineItem\OrderLineItemRepository"/>
<argument type="service" id="Kiener\MolliePayments\Service\Stock\StockManager"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="mollie_payments.logger"/>
</service>

Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/services/subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="Kiener\MolliePayments\Subscriber\OrderLinesUpdatedSubscriber">
<argument type="service" id="mollie_payments.logger"/>
<tag name="kernel.event_subscriber"/>
</service>



</services>
</container>
68 changes: 15 additions & 53 deletions src/Service/MollieApi/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kiener\MolliePayments\Service\MollieApi;

use Kiener\MolliePayments\Event\OrderLinesUpdatedEvent;
use Kiener\MolliePayments\Exception\CouldNotFetchMollieOrderException;
use Kiener\MolliePayments\Exception\MollieOrderCouldNotBeShippedException;
use Kiener\MolliePayments\Service\MollieApi\Models\MollieShippingItem;
Expand All @@ -11,20 +12,23 @@
use Mollie\Api\Resources\Shipment as MollieShipment;
use Mollie\Api\Resources\ShipmentCollection;
use Mollie\Api\Types\OrderLineType;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Shipment implements ShipmentInterface
{
/**
* @var Order
*/
private $orderApiService;
private EventDispatcherInterface $eventDispatcher;

/**
* @param Order $orderApiService
*/
public function __construct(Order $orderApiService)
public function __construct(Order $orderApiService, EventDispatcherInterface $eventDispatcher)
{
$this->orderApiService = $orderApiService;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -78,8 +82,12 @@ public function shipOrder(string $mollieOrderId, string $salesChannelId, array $
];
}

$options = $this->addShippingCosts($mollieOrder, $options);
return $mollieOrder->createShipment($options);

$shipment = $mollieOrder->createShipment($options);

$this->eventDispatcher->dispatch(new OrderLinesUpdatedEvent($mollieOrder));

return $shipment;
} catch (ApiException $e) {
throw new MollieOrderCouldNotBeShippedException(
$mollieOrderId,
Expand Down Expand Up @@ -121,9 +129,11 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $
}

$mollieOrder = $this->orderApiService->getMollieOrder($mollieOrderId, $salesChannelId);
$options = $this->addShippingCosts($mollieOrder, $options);

return $mollieOrder->createShipment($options);

$shipment = $mollieOrder->createShipment($options);
$this->eventDispatcher->dispatch(new OrderLinesUpdatedEvent($mollieOrder));
return $shipment;
} catch (ApiException $e) {
throw new MollieOrderCouldNotBeShippedException(
$mollieOrderId,
Expand All @@ -136,54 +146,6 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $
}
}

/**
* @param \Mollie\Api\Resources\Order $mollieOrder
* @param array<mixed> $options
* @return array<mixed>
*/
private function addShippingCosts(\Mollie\Api\Resources\Order $mollieOrder, array $options): array
{
$shippingOptions = [];

$mollieLines = $mollieOrder->lines();

$shippableLines = [];

/**
* @var OrderLine $line
*/
foreach ($mollieLines as $line) {
if ($line->type === OrderLineType::TYPE_SHIPPING_FEE) {
$shippingOptions[] = [
'id' => $line->id,
'quantity' => $line->quantity,
];
continue;
}
if ($line->shippableQuantity > 0) {
$shippableLines[$line->id] = $line;
}
}


foreach ($options['lines'] as $line) {
$shippableLine = $shippableLines[$line['id']]??null;
if ($shippableLine === null) {
continue;
}
$shippableQuantity = $shippableLine->shippableQuantity - $line['quantity'];
if ($shippableQuantity === 0) {
unset($shippableLines[$line['id']]);
}
}
if (count($shippableLines) === 0) {
$options['lines'] = array_merge($options['lines'], $shippingOptions);
}


return $options;
}

/**
* @param string $mollieOrderId
* @param string $salesChannelId
Expand Down
57 changes: 57 additions & 0 deletions src/Subscriber/OrderLinesUpdatedSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Subscriber;

use Kiener\MolliePayments\Event\OrderLinesUpdatedEvent;
use Mollie\Api\Resources\OrderLine;
use Mollie\Api\Types\OrderLineType;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderLinesUpdatedSubscriber implements EventSubscriberInterface
{
private LoggerInterface $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

public static function getSubscribedEvents()
{
return [
OrderLinesUpdatedEvent::class => 'onOrderLinesUpdated',
];
}

public function onOrderLinesUpdated(OrderLinesUpdatedEvent $event): void
{
$mollieOrder = $event->getMollieOrder();

$shippingOptions = [];

$mollieLines = $mollieOrder->lines();

/**
* @var OrderLine $line
*/
foreach ($mollieLines as $line) {
if ($line->type === OrderLineType::TYPE_SHIPPING_FEE && $line->shippableQuantity > 0) {
$shippingOptions[] = [
'id' => $line->id,
'quantity' => $line->quantity,
];
}
}

if (count($shippingOptions) === 0) {
return;
}
try {
$mollieOrder->createShipment(['lines' => $shippingOptions]);
} catch (\Exception $exception) {
$this->logger->error("Failed to update shipping costs", ['message' => $exception->getMessage(), 'options' => $shippingOptions]);
}
}
}
5 changes: 3 additions & 2 deletions tests/PHPUnit/Fakes/CancelItemFacadeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Psr\Log\NullLogger;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @final
Expand Down Expand Up @@ -101,10 +102,10 @@ public function bild(): CancelItemFacade
/** @var MollieApiFactory $mollieFactory */
$mollieFactory = $this->testCase->getMockBuilder(MollieApiFactory::class)->disableOriginalConstructor()->getMock();
$mollieFactory->method('getClient')->willReturn($this->mollieClient);

$dispatcher = $this->testCase->getMockBuilder(EventDispatcherInterface::class)->getMock();
$orderLineRepository = new FakeOrderLineItemRepository($this->itemCollection);

return new CancelItemFacade($mollieFactory, $orderLineRepository, $this->stockManager, new NullLogger());
return new CancelItemFacade($mollieFactory, $orderLineRepository, $this->stockManager, $dispatcher, new NullLogger());
}

}
5 changes: 3 additions & 2 deletions tests/PHPUnit/Service/MollieApi/ShipmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Mollie\Api\Resources\Shipment as MollieShipment;
use Mollie\Api\Types\OrderLineType;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ShipmentTest extends TestCase
{
Expand All @@ -34,11 +35,11 @@ class ShipmentTest extends TestCase
protected function setUp(): void
{
$this->mollieOrder = $this->createMock(MollieOrder::class);

$dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->orderApiService = $this->createMock(Order::class);
$this->orderApiService->method('getMollieOrder')->willReturn($this->mollieOrder);

$this->shipmentApiService = new Shipment($this->orderApiService);
$this->shipmentApiService = new Shipment($this->orderApiService,$dispatcher);
}

protected function setUpOrderLines()
Expand Down

0 comments on commit d3fcc72

Please sign in to comment.