Skip to content

Commit

Permalink
Merge pull request #8 from Setono/set-tracking-number
Browse files Browse the repository at this point in the history
Take the 'ship' transition on the shipment instead of the order and also set the tracking number on the shipment
  • Loading branch information
loevgaard authored Sep 9, 2024
2 parents c34e895 + 1e03ddd commit ee478e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"sylius/payment": "^1.0",
"sylius/product": "^1.0",
"sylius/resource-bundle": "^1.0",
"sylius/shipping": "^1.0",
"sylius/ui-bundle": "^1.0",
"symfony/config": "^5.4 || ^6.4",
"symfony/console": "^5.4 || ^6.4",
Expand Down
30 changes: 22 additions & 8 deletions src/WebhookHandler/OrderPackedWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Setono\SyliusPeakPlugin\Model\OrderInterface;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\OrderShippingTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Order\OrderTransitions;
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Shipping\ShipmentTransitions;
use Webmozart\Assert\Assert;

final class OrderPackedWebhookHandler implements WebhookHandlerInterface, LoggerAwareInterface
Expand Down Expand Up @@ -65,13 +65,7 @@ public function handle(object $data): void
throw new \InvalidArgumentException(sprintf('Order lines on order %s are different', $data->orderId));
}

$orderShippingStateMachine = $this->stateMachineFactory->get($order, OrderShippingTransitions::GRAPH);

if ($orderShippingStateMachine->can(OrderShippingTransitions::TRANSITION_SHIP)) {
$this->logger->debug(sprintf('Shipment: Taking the "%s" transition', OrderShippingTransitions::TRANSITION_SHIP));

$orderShippingStateMachine->apply(OrderShippingTransitions::TRANSITION_SHIP);
}
$this->completeShipment($order, $data);

if ($data->paymentCaptured) {
$this->completePayment($order);
Expand Down Expand Up @@ -126,6 +120,26 @@ private static function assertSame(array $syliusOrderLines, array $peakOrderLine
Assert::count($peakOrderLines, 0);
}

private function completeShipment(OrderInterface $order, WebhookDataPickOrderPacked $data): void
{
$shipment = $order->getshipments()->last();
if (false === $shipment) {
$this->logger->debug('There is no shipment on the order');

return;
}

$shipment->setTracking($data->trackingNumber);

$shipmentStateMachine = $this->stateMachineFactory->get($shipment, ShipmentTransitions::GRAPH);

if ($shipmentStateMachine->can(ShipmentTransitions::TRANSITION_SHIP)) {
$this->logger->debug(sprintf('Shipment: Taking the "%s" transition', ShipmentTransitions::TRANSITION_SHIP));

$shipmentStateMachine->apply(ShipmentTransitions::TRANSITION_SHIP);
}
}

private function completePayment(OrderInterface $order): void
{
$this->logger->debug(sprintf('The payment is captured, so we will check if we can take the "%s" transition', PaymentTransitions::TRANSITION_COMPLETE));
Expand Down

0 comments on commit ee478e2

Please sign in to comment.