Skip to content

Commit acdb9ab

Browse files
refactoring shipment api call (#87)
1 parent 8327839 commit acdb9ab

14 files changed

+84
-141
lines changed

src/Exception/MissingPriceLineItemException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(string $id)
1717

1818
public function getErrorCode(): string
1919
{
20-
return 'ORDER__LINE_ITEM_MISSING_PRICE_COLLECTION';
20+
return 'MOLLIE_PAYMENTS__LINE_ITEM_MISSING_PRICE_COLLECTION';
2121
}
2222

2323
public function getStatusCode(): int

src/Facade/MollieShipment.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ public function setShipment(string $orderDeliveryId, Context $context): bool
7070
}
7171

7272
$customFields = $order->getCustomFields();
73+
$mollieOrderId = $customFields[CustomFieldsInterface::MOLLIE_KEY][CustomFieldsInterface::ORDER_KEY] ?? null;
7374

74-
if (!isset($customFields[CustomFieldsInterface::MOLLIE_KEY][CustomFieldsInterface::ORDER_KEY])) {
75+
if (!$mollieOrderId) {
7576
$this->logger->warning(
7677
sprintf('Mollie orderId does not exist in shopware order (%s)', (string)$order->getOrderNumber())
7778
);
@@ -93,12 +94,10 @@ public function setShipment(string $orderDeliveryId, Context $context): bool
9394
return false;
9495
}
9596

96-
$mollieOrderId = $customFields[CustomFieldsInterface::MOLLIE_KEY][CustomFieldsInterface::ORDER_KEY];
97-
9897
$addedMollieShipment = $this->mollieApiOrderService->setShipment($mollieOrderId, $order->getSalesChannelId());
9998

10099
if ($addedMollieShipment) {
101-
$values[CustomFieldsInterface::DELIVERY_SHIPPED] = true;
100+
$values = [CustomFieldsInterface::DELIVERY_SHIPPED => true];
102101
$this->orderDeliveryService->updateCustomFields($delivery, $values, $context);
103102
}
104103

src/Resources/config/services.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@
240240

241241

242242
<!-- Validators -->
243-
<service id="Kiener\MolliePayments\Validator\OrderLineItemValidator"/>
243+
<service id="Kiener\MolliePayments\Validator\OrderLineItemValidator">
244+
<argument type="service" id="monolog.logger"/>
245+
</service>
244246

245247
</services>
246248

src/Resources/config/services/subscriber.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
<services>
88
<service id="Kiener\MolliePayments\Subscriber\OrderDeliverySubscriber" class="Kiener\MolliePayments\Subscriber\OrderDeliverySubscriber">
9-
<argument type="service" id="Mollie\Api\MollieApiClient"/>
109
<argument type="service" id="Kiener\MolliePayments\Facade\MollieShipment"/>
1110
<tag name="kernel.event_subscriber"/>
1211
</service>

src/Service/CustomFieldsInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
interface CustomFieldsInterface
1111
{
12-
1312
public const MOLLIE_KEY = 'mollie_payments';
1413

1514
public const ORDER_KEY = 'order_id';

src/Service/MollieApi/Order.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Kiener\MolliePayments\Factory\MollieApiFactory;
66
use Mollie\Api\Exceptions\ApiException;
7+
use Mollie\Api\Resources\OrderLine;
78
use Psr\Log\LoggerInterface;
89

910
class Order
@@ -21,7 +22,6 @@ class Order
2122

2223
public function __construct(MollieApiFactory $clientFactory, LoggerInterface $logger)
2324
{
24-
2525
$this->clientFactory = $clientFactory;
2626
$this->logger = $logger;
2727
}
@@ -38,28 +38,21 @@ public function setShipment(string $mollieOrderId, string $salesChannelId): bool
3838
'API error occured when fetching mollie order %s with message %s',
3939
$mollieOrderId,
4040
$e->getMessage()
41-
),
42-
$e->getTrace()
41+
)
4342
);
4443

45-
return false;
44+
throw $e;
4645
}
4746

48-
$shouldCreateShipment = false;
47+
/** @var OrderLine $orderLine */
48+
foreach ($mollieOrder->lines() as $orderLine) {
49+
if ($orderLine->shippableQuantity > 0) {
50+
$mollieOrder->shipAll();
4951

50-
foreach ($mollieOrder->lines as $line) {
51-
if ($line->shippableQuantity > 0) {
52-
$shouldCreateShipment = true;
53-
break;
52+
return true;
5453
}
5554
}
5655

57-
if ($shouldCreateShipment) {
58-
$mollieOrder->shipAll();
59-
60-
return true;
61-
}
62-
6356
return false;
6457
}
6558
}

src/Service/MolliePaymentExtractor.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
class MolliePaymentExtractor
1010
{
11+
public const MOLLIE_PAYMENT_HANDLER_NAMESPACE = 'Kiener\MolliePayments\Handler\Method';
12+
1113
/**
1214
* method extracts last created transaction if it is a mollie payment transaction.
1315
*
14-
* @param OrderTransactionCollection $collection
16+
* @param OrderTransactionCollection|null $collection
1517
* @return OrderTransactionEntity|null
1618
*/
1719
public function extractLast(?OrderTransactionCollection $collection): ?OrderTransactionEntity
@@ -25,38 +27,31 @@ public function extractLast(?OrderTransactionCollection $collection): ?OrderTran
2527
}
2628

2729
// only transactions with a payment method
28-
$collection->filter(function (OrderTransactionEntity $transaction) {
30+
$collection->filter(static function (OrderTransactionEntity $transaction) {
2931
return ($transaction->getPaymentMethod() instanceof PaymentMethodEntity);
3032
});
3133

3234
// sort all transactions chronological
33-
$collection->sort(function (OrderTransactionEntity $a, OrderTransactionEntity $b) {
35+
$collection->sort(static function (OrderTransactionEntity $a, OrderTransactionEntity $b) {
3436
return $a->getCreatedAt() > $b->getCreatedAt();
3537
});
3638

3739
$lastTransaction = $collection->last();
3840

39-
if ($this->isMolliePayment($lastTransaction)) {
41+
if ($lastTransaction instanceof OrderTransactionEntity && $this->isMolliePayment($lastTransaction)) {
4042
return $lastTransaction;
4143
}
4244

4345
return null;
4446
}
4547

46-
private function isMolliePayment(?OrderTransactionEntity $transaction): bool
48+
private function isMolliePayment(OrderTransactionEntity $transaction): bool
4749
{
48-
if (!$transaction instanceof OrderTransactionEntity) {
49-
return false;
50-
}
51-
52-
$molliePaymentsNamespace = 'Kiener\MolliePayments\Handler\Method';
53-
54-
$handlerName = substr($transaction->getPaymentMethod()->getHandlerIdentifier(), 0, strlen($molliePaymentsNamespace));
55-
56-
if ($handlerName === $molliePaymentsNamespace) {
57-
return true;
58-
}
50+
$pattern = sprintf(
51+
'/^%s/',
52+
preg_quote(self::MOLLIE_PAYMENT_HANDLER_NAMESPACE)
53+
);
5954

60-
return false;
55+
return preg_match($pattern, $transaction->getPaymentMethod()->getHandlerIdentifier()) === 1;
6156
}
6257
}

src/Service/OrderDeliveryService.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class OrderDeliveryService
1717

1818
public function __construct(EntityRepositoryInterface $orderDeliveryRepository)
1919
{
20-
2120
$this->orderDeliveryRepository = $orderDeliveryRepository;
2221
}
2322

@@ -27,14 +26,7 @@ public function getDelivery(string $orderDeliveryId, Context $context): ?OrderDe
2726
$criteria->addAssociations(['order', 'order.transactions', 'order.transactions.paymentMethod']);
2827
$result = $this->orderDeliveryRepository->search($criteria, $context);
2928

30-
if ($result->count() === 0) {
31-
return null;
32-
}
33-
34-
/** @var OrderDeliveryEntity $delivery */
35-
$delivery = $result->first();
36-
37-
return $delivery;
29+
return $result->first();
3830
}
3931

4032
public function updateCustomFields(OrderDeliveryEntity $delivery, array $values, Context $context): void

src/Service/OrderService.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
use Mollie\Api\Types\OrderLineType;
99
use Psr\Log\LoggerInterface;
1010
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
11-
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
1211
use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
1312
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
1413
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
15-
use Shopware\Core\Checkout\Cart\Tax\TaxCalculator;
1614
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
1715
use Shopware\Core\Checkout\Order\OrderEntity;
1816
use Shopware\Core\Checkout\Promotion\Cart\PromotionProcessor;
1917
use Shopware\Core\Framework\Context;
20-
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
2118
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
2219
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
2320
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
@@ -146,19 +143,7 @@ public function getOrderLinesArray(OrderEntity $order): array
146143
$sku = $item->getProduct()->getProductNumber();
147144
}
148145

149-
try {
150-
$molliePreparedApiPrices = $this->calculateLineItemPriceData($item, $order->getTaxStatus(), $currencyCode);
151-
} catch (MissingPriceLineItemException $e) {
152-
$this->logger->critical(
153-
sprintf(
154-
'The order could not be prepared for mollie api. The LineItem with id (%s), sku (%s) has no prices',
155-
$item->getId(),
156-
(string)$sku
157-
)
158-
);
159-
160-
throw $e;
161-
}
146+
$molliePreparedApiPrices = $this->calculateLineItemPriceData($item, $order->getTaxStatus(), $currencyCode);
162147

163148
// Get the image
164149
$imageUrl = null;
@@ -298,6 +283,11 @@ public function calculateLineItemPriceData(OrderLineItemEntity $item, string $or
298283
$vatRate = $itemTax->getTaxRate();
299284
}
300285

286+
// Remove VAT if the order is tax free
287+
if ($orderTaxType === CartPrice::TAX_STATE_FREE) {
288+
$vatRate = 0.0;
289+
}
290+
301291
$unitPrice = $price->getUnitPrice();
302292
$lineItemTotalPrice = $item->getTotalPrice();
303293

@@ -312,10 +302,6 @@ public function calculateLineItemPriceData(OrderLineItemEntity $item, string $or
312302

313303
$unitPrice = round($unitPrice, self::MOLLIE_PRICE_PRECISION);
314304

315-
// Remove VAT if the order is tax free
316-
if ($orderTaxType === CartPrice::TAX_STATE_FREE) {
317-
$vatRate = 0.0;
318-
}
319305

320306
$roundedLineItemTotalPrice = round($lineItemTotalPrice, self::MOLLIE_PRICE_PRECISION);
321307
$roundedVatRate = round($vatRate, self::MOLLIE_PRICE_PRECISION);

src/Subscriber/OrderDeliverySubscriber.php

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,18 @@
1111

1212
class OrderDeliverySubscriber implements EventSubscriberInterface
1313
{
14-
15-
/** @var MollieApiClient */
16-
private $apiClient;
17-
18-
/** @var SettingsService */
19-
protected $settingsService;
20-
2114
/** @var MollieShipment */
2215
private $mollieShipment;
2316

17+
public function __construct(
18+
MollieShipment $mollieShipment
19+
)
20+
{
21+
$this->mollieShipment = $mollieShipment;
22+
}
23+
2424
/**
25-
* Returns an array of event names this subscriber wants to listen to.
26-
*
27-
* The array keys are event names and the value can be:
28-
*
29-
* * The method name to call (priority defaults to 0)
30-
* * An array composed of the method name to call and the priority
31-
* * An array of arrays composed of the method names to call and respective
32-
* priorities, or 0 if unset
33-
*
34-
* For instance:
35-
*
36-
* * array('eventName' => 'methodName')
37-
* * array('eventName' => array('methodName', $priority))
38-
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
39-
*
40-
* @return array The event names to listen to
25+
* @return array
4126
*/
4227
public static function getSubscribedEvents(): array
4328
{
@@ -46,21 +31,6 @@ public static function getSubscribedEvents(): array
4631
];
4732
}
4833

49-
/**
50-
* Creates a new instance of PaymentMethodSubscriber.
51-
*
52-
* @param MollieApiClient $apiClient
53-
* @param MollieShipment $mollieShipment
54-
*/
55-
public function __construct(
56-
MollieApiClient $apiClient,
57-
MollieShipment $mollieShipment
58-
)
59-
{
60-
$this->apiClient = $apiClient;
61-
$this->mollieShipment = $mollieShipment;
62-
}
63-
6434
public function onOrderDeliveryChanged(StateMachineStateChangeEvent $event): void
6535
{
6636
if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {

src/Validator/OrderLineItemValidator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@
55

66

77
use Kiener\MolliePayments\Exception\MissingPriceLineItemException;
8+
use Psr\Log\LoggerInterface;
89
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
910
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
1011

1112
class OrderLineItemValidator
1213
{
14+
/**
15+
* @var LoggerInterface
16+
*/
17+
private $logger;
18+
19+
public function __construct(LoggerInterface $logger)
20+
{
21+
$this->logger = $logger;
22+
}
23+
1324
/**
1425
* @param OrderLineItemEntity $lineItemEntity
1526
* @throws MissingPriceLineItemException
@@ -19,6 +30,13 @@ public function validate(OrderLineItemEntity $lineItemEntity): void
1930
$price = $lineItemEntity->getPrice();
2031

2132
if (!$price instanceof CalculatedPrice) {
33+
$this->logger->critical(
34+
sprintf(
35+
'The order could not be prepared for mollie api. The LineItem with id (%s) has no prices',
36+
$lineItemEntity->getId()
37+
)
38+
);
39+
2240
throw new MissingPriceLineItemException($lineItemEntity->getId());
2341
}
2442
}

0 commit comments

Comments
 (0)