Skip to content

Commit

Permalink
NTR: PISHPS-395: ship even without tracking data (#909)
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 9, 2024
1 parent 45de6f5 commit ccde869
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 41 deletions.
12 changes: 12 additions & 0 deletions src/Components/ShipmentManager/Models/TrackingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ public function getTrackingUrl(): string
{
return $this->trackingUrl;
}

/**
* @return array<string,string>
*/
public function toArray(): array
{
return [
'carrier' => $this->carrier,
'code' => $this->code,
'tracking_url' => $this->trackingUrl,
];
}
}
80 changes: 47 additions & 33 deletions src/Components/ShipmentManager/ShipmentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use Kiener\MolliePayments\Service\OrderService;
use Kiener\MolliePayments\Service\TrackingInfoStructFactory;
use Kiener\MolliePayments\Service\Transition\DeliveryTransitionServiceInterface;
use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct;
use Kiener\MolliePayments\Struct\Order\OrderAttributes;
use Kiener\MolliePayments\Struct\OrderLineItemEntity\OrderLineItemEntityAttributes;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
Expand Down Expand Up @@ -66,6 +68,11 @@ class ShipmentManager implements ShipmentManagerInterface
*/
private $trackingFactory;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param DeliveryTransitionServiceInterface $deliveryTransitionService
* @param Order $mollieApiOrderService
Expand All @@ -75,7 +82,7 @@ class ShipmentManager implements ShipmentManagerInterface
* @param OrderItemsExtractor $orderItemsExtractor
* @param TrackingInfoStructFactory $trackingFactory
*/
public function __construct(DeliveryTransitionServiceInterface $deliveryTransitionService, Order $mollieApiOrderService, ShipmentInterface $shipmentService, OrderService $orderService, OrderDeliveryExtractor $orderDataExtractor, OrderItemsExtractor $orderItemsExtractor, TrackingInfoStructFactory $trackingFactory)
public function __construct(DeliveryTransitionServiceInterface $deliveryTransitionService, Order $mollieApiOrderService, ShipmentInterface $shipmentService, OrderService $orderService, OrderDeliveryExtractor $orderDataExtractor, OrderItemsExtractor $orderItemsExtractor, TrackingInfoStructFactory $trackingFactory, LoggerInterface $logger)
{
$this->deliveryTransitionService = $deliveryTransitionService;
$this->mollieApiOrderService = $mollieApiOrderService;
Expand All @@ -84,6 +91,7 @@ public function __construct(DeliveryTransitionServiceInterface $deliveryTransiti
$this->orderDataExtractor = $orderDataExtractor;
$this->orderItemsExtractor = $orderItemsExtractor;
$this->trackingFactory = $trackingFactory;
$this->logger = $logger;
}


Expand Down Expand Up @@ -130,8 +138,8 @@ public function getTotals(string $orderId, Context $context): array
* @param null|TrackingData $tracking
* @param array<mixed> $shippingItems
* @param Context $context
* @throws NoDeliveriesFoundException
* @throws NoLineItemsProvidedException
* @throws NoDeliveriesFoundException
* @return \Mollie\Api\Resources\Shipment
*/
public function shipOrder(OrderEntity $order, ?TrackingData $tracking, array $shippingItems, Context $context): \Mollie\Api\Resources\Shipment
Expand All @@ -141,15 +149,7 @@ public function shipOrder(OrderEntity $order, ?TrackingData $tracking, array $sh
}


if ($tracking instanceof TrackingData) {
$trackingData = $this->trackingFactory->create(
$tracking->getCarrier(),
$tracking->getCode(),
$tracking->getTrackingUrl()
);
} else {
$trackingData = $this->trackingFactory->trackingFromOrder($order);
}
$trackingData = $this->getTrackingInfoStruct($tracking, $order);


$orderAttr = new OrderAttributes($order);
Expand Down Expand Up @@ -208,15 +208,7 @@ public function shipOrder(OrderEntity $order, ?TrackingData $tracking, array $sh
*/
public function shipOrderRest(OrderEntity $order, ?TrackingData $tracking, Context $context): \Mollie\Api\Resources\Shipment
{
if ($tracking instanceof TrackingData) {
$trackingData = $this->trackingFactory->create(
$tracking->getCarrier(),
$tracking->getCode(),
$tracking->getTrackingUrl()
);
} else {
$trackingData = $this->trackingFactory->trackingFromOrder($order);
}
$trackingData = $this->getTrackingInfoStruct($tracking, $order);

$orderAttr = new OrderAttributes($order);

Expand Down Expand Up @@ -262,20 +254,12 @@ public function shipItem(OrderEntity $order, string $itemIdentifier, int $quanti
$lineItem = $lineItems->first();
unset($lineItems);

if (!$lineItem instanceof OrderLineItemEntity) {
if (! $lineItem instanceof OrderLineItemEntity) {
throw new OrderLineItemNotFoundException($itemIdentifier);
}


if ($tracking instanceof TrackingData) {
$mollieTracking = $this->trackingFactory->create(
$tracking->getCarrier(),
$tracking->getCode(),
$tracking->getTrackingUrl()
);
} else {
$mollieTracking = $this->trackingFactory->trackingFromOrder($order);
}
$mollieTracking = $this->getTrackingInfoStruct($tracking, $order);

$mollieOrderLineId = $this->orderService->getMollieOrderLineId($lineItem);

Expand Down Expand Up @@ -352,7 +336,7 @@ private function findMatchingLineItems(OrderEntity $order, string $itemIdentifie

// If it's not a "product" type lineItem, for example if it's a completely custom lineItem type,
// check if the payload has a productNumber in it that matches the itemIdentifier.
if (!empty($lineItem->getPayload()) &&
if (! empty($lineItem->getPayload()) &&
array_key_exists('productNumber', $lineItem->getPayload()) &&
$lineItem->getPayload()['productNumber'] === $itemIdentifier) {
return true;
Expand All @@ -361,12 +345,12 @@ private function findMatchingLineItems(OrderEntity $order, string $itemIdentifie
// Check itemIdentifier against the mollie order_line_id custom field
$customFields = $lineItem->getCustomFields() ?? [];
$mollieOrderLineId = $customFields[CustomFieldsInterface::MOLLIE_KEY][CustomFieldsInterface::ORDER_LINE_KEY] ?? null;
if (!is_null($mollieOrderLineId) && $mollieOrderLineId === $itemIdentifier) {
if (! is_null($mollieOrderLineId) && $mollieOrderLineId === $itemIdentifier) {
return true;
}

// If it hasn't passed any of the above tests, check if the itemIdentifier is a valid Uuid...
if (!Uuid::isValid($itemIdentifier)) {
if (! Uuid::isValid($itemIdentifier)) {
return false;
}

Expand All @@ -380,4 +364,34 @@ private function findMatchingLineItems(OrderEntity $order, string $itemIdentifie
return false;
});
}

/**
* @param null|TrackingData $tracking
* @param OrderEntity $order
* @throws NoDeliveriesFoundException
* @return null|ShipmentTrackingInfoStruct
*/
private function getTrackingInfoStruct(?TrackingData $tracking, OrderEntity $order): ?ShipmentTrackingInfoStruct
{
try {
if ($tracking instanceof TrackingData) {
$trackingData = $this->trackingFactory->create(
$tracking->getCarrier(),
$tracking->getCode(),
$tracking->getTrackingUrl()
);
} else {
$trackingData = $this->trackingFactory->trackingFromOrder($order);
}
return $trackingData;
} catch (\InvalidArgumentException $exception) {
$loggerData = ['exception' => $exception->getMessage(), 'order' => $order->getOrderNumber()];
if ($tracking instanceof TrackingData) {
$loggerData['tracking'] = $tracking->toArray();
}

$this->logger->warning('Failed to get tracking information', $loggerData);
return null;
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/facades.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\OrderDeliveryExtractor"/>
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\OrderItemsExtractor"/>
<argument type="service" id="Kiener\MolliePayments\Service\TrackingInfoStructFactory"/>
<argument type="service" id="mollie_payments.logger"/>
</service>

<service id="Kiener\MolliePayments\Facade\MolliePaymentDoPay" public="true">
Expand Down
11 changes: 6 additions & 5 deletions src/Service/TrackingInfoStructFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public function create(string $trackingCarrier, string $trackingCode, string $tr
*/
private function createInfoStruct(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct
{
$this->logger->debug('Creating tracking information for shipment.', [
'trackingCarrier' => $trackingCarrier,
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);

if (empty($trackingCarrier) && empty($trackingCode)) {
$this->logger->debug('No tracking information provided for shipment.');
return null;
Expand All @@ -113,11 +119,6 @@ private function createInfoStruct(string $trackingCarrier, string $trackingCode,
throw new \InvalidArgumentException('Missing Argument for Tracking Code!');
}

$this->logger->debug('Creating tracking information for shipment.', [
'trackingCarrier' => $trackingCarrier,
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);

// determine if the provided tracking code is actually a tracking URL
if (empty($trackingUrl) === true && $this->urlParsingService->isUrl($trackingCode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class ShipmentManagerTest extends TestCase
public function setUp(): void
{
$this->fakeShipmentService = new FakeShipment();

$logger = new NullLogger();
$deliveryTransitionService = $this->createMock(DeliveryTransitionService::class);
$mollieApiOrderService = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
$orderService = $this->getMockBuilder(OrderService::class)->disableOriginalConstructor()->getMock();
$deliveryExtractor = new OrderDeliveryExtractor(new NullLogger());
$deliveryExtractor = new OrderDeliveryExtractor($logger);

$this->shipmentManager = new ShipmentManager(
$deliveryTransitionService,
Expand All @@ -64,7 +64,8 @@ public function setUp(): void
$orderService,
$deliveryExtractor,
new OrderItemsExtractor(),
new TrackingInfoStructFactory(new UrlParsingService(), new NullLogger())
new TrackingInfoStructFactory(new UrlParsingService(), $logger),
$logger
);

$this->context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
Expand Down

0 comments on commit ccde869

Please sign in to comment.