diff --git a/src/Service/MollieApi/Shipment.php b/src/Service/MollieApi/Shipment.php index a2daf793f..5b89ea282 100644 --- a/src/Service/MollieApi/Shipment.php +++ b/src/Service/MollieApi/Shipment.php @@ -78,6 +78,7 @@ public function shipOrder(string $mollieOrderId, string $salesChannelId, array $ ]; } + $options = $this->addShippingCosts($mollieOrder, $options); return $mollieOrder->createShipment($options); } catch (ApiException $e) { throw new MollieOrderCouldNotBeShippedException( @@ -120,6 +121,7 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $ } $mollieOrder = $this->orderApiService->getMollieOrder($mollieOrderId, $salesChannelId); + $options = $this->addShippingCosts($mollieOrder, $options); return $mollieOrder->createShipment($options); } catch (ApiException $e) { @@ -134,6 +136,54 @@ public function shipItem(string $mollieOrderId, string $salesChannelId, string $ } } + /** + * @param \Mollie\Api\Resources\Order $mollieOrder + * @param array $options + * @return array + */ + 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 diff --git a/tests/PHPUnit/Service/MollieApi/ShipmentTest.php b/tests/PHPUnit/Service/MollieApi/ShipmentTest.php index 4718ecb8f..acf0818ca 100644 --- a/tests/PHPUnit/Service/MollieApi/ShipmentTest.php +++ b/tests/PHPUnit/Service/MollieApi/ShipmentTest.php @@ -129,11 +129,16 @@ public function testShipOrderCannotBeShippedException() */ public function testShipItem() { + $this->mollieOrder ->expects($this->once()) ->method('createShipment') ->willReturn($this->createMock(MollieShipment::class)); + $this->mollieOrder->method('lines')->willReturn([]); + + + $this->shipmentApiService->shipItem('mollieOrderId', 'salesChannelId', 'mollieOrderLineId', 1, null); } @@ -146,6 +151,7 @@ public function testShipItemCannotBeShippedException() ->expects($this->once()) ->method('createShipment') ->willThrowException(new ApiException()); + $this->mollieOrder->method('lines')->willReturn([]); $this->expectException(MollieOrderCouldNotBeShippedException::class);